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.
28 #define LUABIND_BUILDING
30 #include <luabind/luabind.hpp>
32 using namespace luabind::detail
;
34 void luabind::detail::add_operator_to_metatable(lua_State
* L
, int op_index
)
36 lua_pushstring(L
, get_operator_name(op_index
));
37 lua_pushnumber(L
, op_index
);
38 lua_pushcclosure(L
, &class_rep::operator_dispatcher
, 1);
42 int luabind::detail::create_cpp_class_metatable(lua_State
* L
)
46 // mark the table with our (hopefully) unique tag
47 // that says that the user data that has this
48 // metatable is a class_rep
49 lua_pushstring(L
, "__luabind_classrep");
50 lua_pushboolean(L
, 1);
53 lua_pushstring(L
, "__gc");
54 lua_pushcclosure(L
, &garbage_collector_s
<detail::class_rep
>::apply
, 0);
57 lua_pushstring(L
, "__call");
58 lua_pushcclosure(L
, &class_rep::constructor_dispatcher
, 0);
61 lua_pushstring(L
, "__index");
62 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
65 return detail::ref(L
);
68 int luabind::detail::create_cpp_instance_metatable(lua_State
* L
)
72 // just indicate that this really is a class and not just any user data
73 lua_pushstring(L
, "__luabind_class");
74 lua_pushboolean(L
, 1);
77 // __index and __newindex will simply be references to the class_rep
78 // wich in turn has it's own metamethods for __index and __newindex
79 lua_pushstring(L
, "__index");
80 lua_pushcclosure(L
, &class_rep::gettable_dispatcher
, 0);
83 lua_pushstring(L
, "__newindex");
84 lua_pushcclosure(L
, &class_rep::settable_dispatcher
, 0);
87 lua_pushstring(L
, "__gc");
88 //lua_pushcclosure(L, &garbage_collector_s<detail::object_rep>::apply, 0);
89 lua_pushcclosure(L
, detail::object_rep::garbage_collector
, 0);
92 lua_pushstring(L
, "__gettable");
93 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
96 for (int i
= 0; i
< number_of_operators
; ++i
) add_operator_to_metatable(L
, i
);
98 // store a reference to the instance-metatable in our class_rep
99 assert((lua_type(L
, -1) == LUA_TTABLE
) && "internal error, please report");
101 return detail::ref(L
);
104 int luabind::detail::create_lua_class_metatable(lua_State
* L
)
108 lua_pushstring(L
, "__luabind_classrep");
109 lua_pushboolean(L
, 1);
112 lua_pushstring(L
, "__gc");
113 lua_pushcclosure(L
, &detail::garbage_collector_s
<detail::class_rep
>::apply
, 0);
116 lua_pushstring(L
, "__newindex");
117 lua_pushcclosure(L
, &class_rep::lua_settable_dispatcher
, 0);
120 lua_pushstring(L
, "__call");
121 lua_pushcclosure(L
, &class_rep::construct_lua_class_callback
, 0);
124 lua_pushstring(L
, "__index");
125 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
128 return detail::ref(L
);
131 int luabind::detail::create_lua_instance_metatable(lua_State
* L
)
135 // just indicate that this really is a class and not just any user data
136 lua_pushstring(L
, "__luabind_class");
137 lua_pushboolean(L
, 1);
140 lua_pushstring(L
, "__index");
141 lua_pushcclosure(L
, &class_rep::lua_class_gettable
, 0);
144 lua_pushstring(L
, "__newindex");
145 lua_pushcclosure(L
, &class_rep::lua_class_settable
, 0);
148 lua_pushstring(L
, "__gc");
149 //lua_pushcclosure(L, detail::garbage_collector_s<detail::object_rep>::apply, 0);
150 lua_pushcclosure(L
, detail::object_rep::garbage_collector
, 0);
153 for (int i
= 0; i
< number_of_operators
; ++i
) add_operator_to_metatable(L
, i
);
155 // store a reference to the instance-metatable in our class_rep
156 return detail::ref(L
);
159 void luabind::open(lua_State
* L
)
161 // get the global class registry, or create one if it doesn't exist
162 // (it's global within a lua state)
163 detail::class_registry
* r
= 0;
165 // If you hit this assert it's because you have called luabind::open() twice on
166 // the same lua_State.
167 assert((detail::class_registry::get_registry(L
) == 0) && "you cannot call luabind::open() twice");
169 lua_pushstring(L
, "__luabind_classes");
170 r
= static_cast<detail::class_registry
*>(lua_newuserdata(L
, sizeof(detail::class_registry
)));
174 lua_pushstring(L
, "__gc");
175 lua_pushcclosure(L
, detail::garbage_collector_s
<detail::class_registry
>::apply
, 0);
177 lua_setmetatable(L
, -2);
179 new(r
) detail::class_registry(L
);
180 lua_settable(L
, LUA_REGISTRYINDEX
);
182 // add functions (class, cast etc...)
183 lua_pushstring(L
, "class");
184 lua_pushcclosure(L
, detail::create_class::stage1
, 0);
185 lua_settable(L
, LUA_GLOBALSINDEX
);