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.
30 #define LUABIND_NO_HEADERS_ONLY
32 #include <luabind/luabind.hpp>
34 using namespace luabind::detail
;
36 void luabind::detail::add_operator_to_metatable(lua_State
* L
, int op_index
)
38 lua_pushstring(L
, get_operator_name(op_index
));
39 lua_pushnumber(L
, op_index
);
40 lua_pushcclosure(L
, &class_rep::operator_dispatcher
, 1);
44 int luabind::detail::create_cpp_class_metatable(lua_State
* L
)
48 // mark the table with our (hopefully) unique tag
49 // that says that the user data that has this
50 // metatable is a class_rep
51 lua_pushstring(L
, "__luabind_classrep");
52 lua_pushboolean(L
, 1);
55 lua_pushstring(L
, "__gc");
56 lua_pushcclosure(L
, &garbage_collector_s
<detail::class_rep
>::apply
, 0);
59 lua_pushstring(L
, "__call");
60 lua_pushcclosure(L
, &class_rep::constructor_dispatcher
, 0);
63 lua_pushstring(L
, "__index");
64 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
67 return detail::ref(L
);
70 int luabind::detail::create_cpp_instance_metatable(lua_State
* L
)
74 // just indicate that this really is a class and not just any user data
75 lua_pushstring(L
, "__luabind_class");
76 lua_pushboolean(L
, 1);
79 // __index and __newindex will simply be references to the class_rep
80 // wich in turn has it's own metamethods for __index and __newindex
81 lua_pushstring(L
, "__index");
82 lua_pushcclosure(L
, &class_rep::gettable_dispatcher
, 0);
85 lua_pushstring(L
, "__newindex");
86 lua_pushcclosure(L
, &class_rep::settable_dispatcher
, 0);
89 lua_pushstring(L
, "__gc");
90 //lua_pushcclosure(L, &garbage_collector_s<detail::object_rep>::apply, 0);
91 lua_pushcclosure(L
, detail::object_rep::garbage_collector
, 0);
94 lua_pushstring(L
, "__gettable");
95 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
98 for (int i
= 0; i
< number_of_operators
; ++i
) add_operator_to_metatable(L
, i
);
100 // store a reference to the instance-metatable in our class_rep
101 assert((lua_type(L
, -1) == LUA_TTABLE
) && "internal error, please report");
103 return detail::ref(L
);
106 int luabind::detail::create_lua_class_metatable(lua_State
* L
)
110 lua_pushstring(L
, "__luabind_classrep");
111 lua_pushboolean(L
, 1);
114 lua_pushstring(L
, "__gc");
115 lua_pushcclosure(L
, &detail::garbage_collector_s
<detail::class_rep
>::apply
, 0);
118 lua_pushstring(L
, "__newindex");
119 lua_pushcclosure(L
, &class_rep::lua_settable_dispatcher
, 0);
122 lua_pushstring(L
, "__call");
123 lua_pushcclosure(L
, &class_rep::construct_lua_class_callback
, 0);
126 lua_pushstring(L
, "__index");
127 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
130 return detail::ref(L
);
133 int luabind::detail::create_lua_instance_metatable(lua_State
* L
)
137 // just indicate that this really is a class and not just any user data
138 lua_pushstring(L
, "__luabind_class");
139 lua_pushboolean(L
, 1);
142 lua_pushstring(L
, "__index");
143 lua_pushcclosure(L
, &class_rep::lua_class_gettable
, 0);
146 lua_pushstring(L
, "__newindex");
147 lua_pushcclosure(L
, &class_rep::lua_class_settable
, 0);
150 lua_pushstring(L
, "__gc");
151 //lua_pushcclosure(L, detail::garbage_collector_s<detail::object_rep>::apply, 0);
152 lua_pushcclosure(L
, detail::object_rep::garbage_collector
, 0);
155 for (int i
= 0; i
< number_of_operators
; ++i
) add_operator_to_metatable(L
, i
);
157 // store a reference to the instance-metatable in our class_rep
158 return detail::ref(L
);
161 void luabind::open(lua_State
* L
)
163 // get the global class registry, or create one if it doesn't exist
164 // (it's global within a lua state)
165 detail::class_registry
* r
= 0;
167 // If you hit this assert it's because you have called luabind::open() twice on
168 // the same lua_State.
169 assert((detail::class_registry::get_registry(L
) == 0) && "you cannot call luabind::open() twice");
171 lua_pushstring(L
, "__luabind_classes");
172 r
= static_cast<detail::class_registry
*>(lua_newuserdata(L
, sizeof(detail::class_registry
)));
176 lua_pushstring(L
, "__gc");
177 lua_pushcclosure(L
, detail::garbage_collector_s
<detail::class_registry
>::apply
, 0);
179 lua_setmetatable(L
, -2);
181 new(r
) detail::class_registry(L
);
182 lua_settable(L
, LUA_REGISTRYINDEX
);
184 // add functions (class, cast etc...)
185 lua_pushstring(L
, "class");
186 lua_pushcclosure(L
, detail::create_class::stage1
, 0);
187 lua_settable(L
, LUA_GLOBALSINDEX
);
189 detail::free_functions::function_registry
* fun_registry
= 0;
190 lua_pushstring(L
, "__lua_free_functions");
191 fun_registry
= static_cast<detail::free_functions::function_registry
*>(lua_newuserdata(L
, sizeof(detail::free_functions::function_registry
)));
193 new(fun_registry
) detail::free_functions::function_registry();
196 lua_pushstring(L
, "__gc");
197 lua_pushcclosure(L
, detail::garbage_collector_s
<detail::free_functions::function_registry
>::apply
, 0);
199 lua_setmetatable(L
, -2);
201 lua_settable(L
, LUA_REGISTRYINDEX
);