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);
41 int luabind::detail::create_cpp_class_metatable(lua_State
* 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);
52 lua_pushstring(L
, "__gc");
53 lua_pushcclosure(L
, &garbage_collector_s
<detail::class_rep
>::apply
, 0);
56 lua_pushstring(L
, "__call");
57 lua_pushcclosure(L
, &class_rep::constructor_dispatcher
, 0);
60 lua_pushstring(L
, "__index");
61 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
64 return detail::ref(L
);
67 int luabind::detail::create_cpp_instance_metatable(lua_State
* 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);
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);
82 lua_pushstring(L
, "__newindex");
83 lua_pushcclosure(L
, &class_rep::settable_dispatcher
, 0);
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);
91 lua_pushstring(L
, "__gettable");
92 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
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
)
107 lua_pushstring(L
, "__luabind_classrep");
108 lua_pushboolean(L
, 1);
111 lua_pushstring(L
, "__gc");
112 lua_pushcclosure(L
, &detail::garbage_collector_s
<detail::class_rep
>::apply
, 0);
115 lua_pushstring(L
, "__newindex");
116 lua_pushcclosure(L
, &class_rep::lua_settable_dispatcher
, 0);
119 lua_pushstring(L
, "__call");
120 lua_pushcclosure(L
, &class_rep::construct_lua_class_callback
, 0);
123 lua_pushstring(L
, "__index");
124 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
127 return detail::ref(L
);
130 int luabind::detail::create_lua_instance_metatable(lua_State
* 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);
139 lua_pushstring(L
, "__index");
140 lua_pushcclosure(L
, &class_rep::lua_class_gettable
, 0);
143 lua_pushstring(L
, "__newindex");
144 lua_pushcclosure(L
, &class_rep::lua_class_settable
, 0);
147 lua_pushstring(L
, "__gc");
148 lua_pushcclosure(L
, detail::object_rep::garbage_collector
, 0);
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
)
161 lua_pushstring(L
, "__gc");
162 lua_pushcclosure(L
, detail::garbage_collector_s
<detail::free_functions::function_rep
>::apply
, 0);
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
)));
184 lua_pushstring(L
, "__gc");
185 lua_pushcclosure(L
, detail::garbage_collector_s
<detail::class_registry
>::apply
, 0);
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
);