scope update
[luabind.git] / src / open.cpp
blob602d7bd398aefcb0b07296f1c9586f52647bbfc8
1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
23 #include <luabind/lua_include.hpp>
25 #define LUABIND_BUILDING
27 #include <luabind/luabind.hpp>
28 #include <luabind/class_info.hpp>
29 #include <luabind/function.hpp>
31 using namespace luabind::detail;
33 void luabind::detail::add_operator_to_metatable(lua_State* L, int op_index)
35 lua_pushstring(L, get_operator_name(op_index));
36 lua_pushnumber(L, op_index);
37 lua_pushcclosure(L, &class_rep::operator_dispatcher, 1);
38 lua_settable(L, -3);
41 int luabind::detail::create_cpp_class_metatable(lua_State* L)
43 lua_newtable(L);
45 // mark the table with our (hopefully) unique tag
46 // that says that the user data that has this
47 // metatable is a class_rep
48 lua_pushstring(L, "__luabind_classrep");
49 lua_pushboolean(L, 1);
50 lua_rawset(L, -3);
52 lua_pushstring(L, "__gc");
53 lua_pushcclosure(L, &garbage_collector_s<detail::class_rep>::apply, 0);
54 lua_rawset(L, -3);
56 lua_pushstring(L, "__call");
57 lua_pushcclosure(L, &class_rep::constructor_dispatcher, 0);
58 lua_rawset(L, -3);
60 lua_pushstring(L, "__index");
61 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
62 lua_rawset(L, -3);
64 return detail::ref(L);
67 int luabind::detail::create_cpp_instance_metatable(lua_State* L)
69 lua_newtable(L);
71 // just indicate that this really is a class and not just any user data
72 lua_pushstring(L, "__luabind_class");
73 lua_pushboolean(L, 1);
74 lua_rawset(L, -3);
76 // __index and __newindex will simply be references to the class_rep
77 // wich in turn has it's own metamethods for __index and __newindex
78 lua_pushstring(L, "__index");
79 lua_pushcclosure(L, &class_rep::gettable_dispatcher, 0);
80 lua_rawset(L, -3);
82 lua_pushstring(L, "__newindex");
83 lua_pushcclosure(L, &class_rep::settable_dispatcher, 0);
84 lua_rawset(L, -3);
86 lua_pushstring(L, "__gc");
87 //lua_pushcclosure(L, &garbage_collector_s<detail::object_rep>::apply, 0);
88 lua_pushcclosure(L, detail::object_rep::garbage_collector, 0);
89 lua_rawset(L, -3);
91 lua_pushstring(L, "__gettable");
92 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
93 lua_rawset(L, -3);
95 for (int i = 0; i < number_of_operators; ++i) add_operator_to_metatable(L, i);
97 // store a reference to the instance-metatable in our class_rep
98 assert((lua_type(L, -1) == LUA_TTABLE) && "internal error, please report");
100 return detail::ref(L);
103 int luabind::detail::create_lua_class_metatable(lua_State* L)
105 lua_newtable(L);
107 lua_pushstring(L, "__luabind_classrep");
108 lua_pushboolean(L, 1);
109 lua_rawset(L, -3);
111 lua_pushstring(L, "__gc");
112 lua_pushcclosure(L, &detail::garbage_collector_s<detail::class_rep>::apply, 0);
113 lua_rawset(L, -3);
115 lua_pushstring(L, "__newindex");
116 lua_pushcclosure(L, &class_rep::lua_settable_dispatcher, 0);
117 lua_rawset(L, -3);
119 lua_pushstring(L, "__call");
120 lua_pushcclosure(L, &class_rep::construct_lua_class_callback, 0);
121 lua_rawset(L, -3);
123 lua_pushstring(L, "__index");
124 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
125 lua_rawset(L, -3);
127 return detail::ref(L);
130 int luabind::detail::create_lua_instance_metatable(lua_State* L)
132 lua_newtable(L);
134 // just indicate that this really is a class and not just any user data
135 lua_pushstring(L, "__luabind_class");
136 lua_pushboolean(L, 1);
137 lua_rawset(L, -3);
139 lua_pushstring(L, "__index");
140 lua_pushcclosure(L, &class_rep::lua_class_gettable, 0);
141 lua_rawset(L, -3);
143 lua_pushstring(L, "__newindex");
144 lua_pushcclosure(L, &class_rep::lua_class_settable, 0);
145 lua_rawset(L, -3);
147 lua_pushstring(L, "__gc");
148 lua_pushcclosure(L, detail::object_rep::garbage_collector, 0);
149 lua_rawset(L, -3);
151 for (int i = 0; i < number_of_operators; ++i) add_operator_to_metatable(L, i);
153 // store a reference to the instance-metatable in our class_rep
154 return detail::ref(L);
157 int luabind::detail::create_lua_function_metatable(lua_State* L)
159 lua_newtable(L);
161 lua_pushstring(L, "__gc");
162 lua_pushcclosure(L, detail::garbage_collector_s<detail::free_functions::function_rep>::apply, 0);
164 lua_rawset(L, -3);
166 return detail::ref(L);
169 void luabind::open(lua_State* L)
171 // get the global class registry, or create one if it doesn't exist
172 // (it's global within a lua state)
173 detail::class_registry* r = 0;
175 // If you hit this assert it's because you have called luabind::open() twice on
176 // the same lua_State.
177 assert((detail::class_registry::get_registry(L) == 0) && "you cannot call luabind::open() twice");
179 lua_pushstring(L, "__luabind_classes");
180 r = static_cast<detail::class_registry*>(lua_newuserdata(L, sizeof(detail::class_registry)));
182 // set gc metatable
183 lua_newtable(L);
184 lua_pushstring(L, "__gc");
185 lua_pushcclosure(L, detail::garbage_collector_s<detail::class_registry>::apply, 0);
186 lua_settable(L, -3);
187 lua_setmetatable(L, -2);
189 new(r) detail::class_registry(L);
190 lua_settable(L, LUA_REGISTRYINDEX);
192 // add functions (class, cast etc...)
193 lua_pushstring(L, "class");
194 lua_pushcclosure(L, detail::create_class::stage1, 0);
195 lua_settable(L, LUA_GLOBALSINDEX);
197 bind_class_info(L);