moste conversions lua->cpp seems to work.
[luabind.git] / src / open.cpp
blob3fb27262054915f137408e0bb149f17b10ec85e9
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 extern "C"
25 #include "lua.h"
28 #include <luabind/luabind.hpp>
30 using namespace luabind::detail;
32 void luabind::detail::add_operator_to_metatable(lua_State* L, int op_index)
34 lua_pushstring(L, get_operator_name(op_index));
35 lua_pushnumber(L, op_index);
36 lua_pushcclosure(L, &class_rep::operator_dispatcher, 1);
37 lua_settable(L, -3);
40 int luabind::detail::create_cpp_class_metatable(lua_State* L)
42 lua_newtable(L);
44 // mark the table with our (hopefully) unique tag
45 // that says that the user data that has this
46 // metatable is a class_rep
47 lua_pushstring(L, "__luabind_classrep");
48 lua_pushboolean(L, 1);
49 lua_rawset(L, -3);
51 lua_pushstring(L, "__gc");
52 lua_pushcclosure(L, &garbage_collector_s<detail::class_rep>::apply, 0);
53 lua_rawset(L, -3);
55 lua_pushstring(L, "__call");
56 lua_pushcclosure(L, &class_rep::constructor_dispatcher, 0);
57 lua_rawset(L, -3);
59 lua_pushstring(L, "__index");
60 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
61 lua_rawset(L, -3);
63 return detail::ref(L);
66 int luabind::detail::create_cpp_instance_metatable(lua_State* L)
68 lua_newtable(L);
70 // just indicate that this really is a class and not just any user data
71 lua_pushstring(L, "__luabind_class");
72 lua_pushboolean(L, 1);
73 lua_rawset(L, -3);
75 // __index and __newindex will simply be references to the class_rep
76 // wich in turn has it's own metamethods for __index and __newindex
77 lua_pushstring(L, "__index");
78 lua_pushcclosure(L, &class_rep::gettable_dispatcher, 0);
79 lua_rawset(L, -3);
81 lua_pushstring(L, "__newindex");
82 lua_pushcclosure(L, &class_rep::settable_dispatcher, 0);
83 lua_rawset(L, -3);
85 lua_pushstring(L, "__gc");
86 //lua_pushcclosure(L, &garbage_collector_s<detail::object_rep>::apply, 0);
87 lua_pushcclosure(L, detail::object_rep::garbage_collector, 0);
88 lua_rawset(L, -3);
90 lua_pushstring(L, "__gettable");
91 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
92 lua_rawset(L, -3);
94 for (int i = 0; i < number_of_operators; ++i) add_operator_to_metatable(L, i);
96 // store a reference to the instance-metatable in our class_rep
97 assert((lua_type(L, -1) == LUA_TTABLE) && "internal error, please report");
99 return detail::ref(L);
102 int luabind::detail::create_lua_class_metatable(lua_State* L)
104 lua_newtable(L);
106 lua_pushstring(L, "__luabind_classrep");
107 lua_pushboolean(L, 1);
108 lua_rawset(L, -3);
110 lua_pushstring(L, "__gc");
111 lua_pushcclosure(L, &detail::garbage_collector_s<detail::class_rep>::apply, 0);
112 lua_rawset(L, -3);
114 lua_pushstring(L, "__newindex");
115 lua_pushcclosure(L, &class_rep::lua_settable_dispatcher, 0);
116 lua_rawset(L, -3);
118 lua_pushstring(L, "__call");
119 lua_pushcclosure(L, &class_rep::construct_lua_class_callback, 0);
120 lua_rawset(L, -3);
122 lua_pushstring(L, "__index");
123 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
124 lua_rawset(L, -3);
126 return detail::ref(L);
129 int luabind::detail::create_lua_instance_metatable(lua_State* L)
131 lua_newtable(L);
133 // just indicate that this really is a class and not just any user data
134 lua_pushstring(L, "__luabind_class");
135 lua_pushboolean(L, 1);
136 lua_rawset(L, -3);
138 lua_pushstring(L, "__index");
139 lua_pushcclosure(L, &class_rep::lua_class_gettable, 0);
140 lua_rawset(L, -3);
142 lua_pushstring(L, "__newindex");
143 lua_pushcclosure(L, &class_rep::lua_class_settable, 0);
144 lua_rawset(L, -3);
146 lua_pushstring(L, "__gc");
147 //lua_pushcclosure(L, detail::garbage_collector_s<detail::object_rep>::apply, 0);
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 void luabind::open(lua_State* L)
159 // get the global class registry, or create one if it doesn't exist
160 // (it's global within a lua state)
161 detail::class_registry* r = 0;
163 // If you hit this assert it's because you have called luabind::open() twice on
164 // the same lua_State.
165 assert((detail::class_registry::get_registry(L) == 0) && "you cannot call luabind::open() twice");
167 lua_pushstring(L, "__luabind_classes");
168 r = static_cast<detail::class_registry*>(lua_newuserdata(L, sizeof(detail::class_registry)));
170 // set gc metatable
171 lua_newtable(L);
172 lua_pushstring(L, "__gc");
173 lua_pushcclosure(L, detail::garbage_collector_s<detail::class_registry>::apply, 0);
174 lua_settable(L, -3);
175 lua_setmetatable(L, -2);
177 new(r) detail::class_registry(L);
178 lua_settable(L, LUA_REGISTRYINDEX);
180 // add functions (class, cast etc...)
181 lua_pushstring(L, "class");
182 lua_pushcclosure(L, detail::create_class::stage1, 0);
183 lua_settable(L, LUA_GLOBALSINDEX);
185 detail::free_functions::function_registry* fun_registry = 0;
186 lua_pushstring(L, "__lua_free_functions");
187 fun_registry = static_cast<detail::free_functions::function_registry*>(lua_newuserdata(L, sizeof(detail::free_functions::function_registry)));
189 new(fun_registry) detail::free_functions::function_registry();
191 lua_newtable(L);
192 lua_pushstring(L, "__gc");
193 lua_pushcclosure(L, detail::garbage_collector_s<detail::free_functions::function_registry>::apply, 0);
194 lua_settable(L, -3);
195 lua_setmetatable(L, -2);
197 lua_settable(L, LUA_REGISTRYINDEX);