added makefiles for the library.
[luabind.git] / src / open.cpp
blobbbd9d1cde4985304569f754dd266a8109bf6b8c4
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"
26 #include "lauxlib.h"
27 #include "lualib.h"
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);
41 lua_settable(L, -3);
44 int luabind::detail::create_cpp_class_metatable(lua_State* L)
46 lua_newtable(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);
53 lua_rawset(L, -3);
55 lua_pushstring(L, "__gc");
56 lua_pushcclosure(L, &garbage_collector_s<detail::class_rep>::apply, 0);
57 lua_rawset(L, -3);
59 lua_pushstring(L, "__call");
60 lua_pushcclosure(L, &class_rep::constructor_dispatcher, 0);
61 lua_rawset(L, -3);
63 lua_pushstring(L, "__index");
64 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
65 lua_rawset(L, -3);
67 return detail::ref(L);
70 int luabind::detail::create_cpp_instance_metatable(lua_State* L)
72 lua_newtable(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);
77 lua_rawset(L, -3);
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);
83 lua_rawset(L, -3);
85 lua_pushstring(L, "__newindex");
86 lua_pushcclosure(L, &class_rep::settable_dispatcher, 0);
87 lua_rawset(L, -3);
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);
92 lua_rawset(L, -3);
94 lua_pushstring(L, "__gettable");
95 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
96 lua_rawset(L, -3);
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)
108 lua_newtable(L);
110 lua_pushstring(L, "__luabind_classrep");
111 lua_pushboolean(L, 1);
112 lua_rawset(L, -3);
114 lua_pushstring(L, "__gc");
115 lua_pushcclosure(L, &detail::garbage_collector_s<detail::class_rep>::apply, 0);
116 lua_rawset(L, -3);
118 lua_pushstring(L, "__newindex");
119 lua_pushcclosure(L, &class_rep::lua_settable_dispatcher, 0);
120 lua_rawset(L, -3);
122 lua_pushstring(L, "__call");
123 lua_pushcclosure(L, &class_rep::construct_lua_class_callback, 0);
124 lua_rawset(L, -3);
126 lua_pushstring(L, "__index");
127 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
128 lua_rawset(L, -3);
130 return detail::ref(L);
133 int luabind::detail::create_lua_instance_metatable(lua_State* L)
135 lua_newtable(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);
140 lua_rawset(L, -3);
142 lua_pushstring(L, "__index");
143 lua_pushcclosure(L, &class_rep::lua_class_gettable, 0);
144 lua_rawset(L, -3);
146 lua_pushstring(L, "__newindex");
147 lua_pushcclosure(L, &class_rep::lua_class_settable, 0);
148 lua_rawset(L, -3);
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);
153 lua_rawset(L, -3);
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)));
174 // set gc metatable
175 lua_newtable(L);
176 lua_pushstring(L, "__gc");
177 lua_pushcclosure(L, detail::garbage_collector_s<detail::class_registry>::apply, 0);
178 lua_settable(L, -3);
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();
195 lua_newtable(L);
196 lua_pushstring(L, "__gc");
197 lua_pushcclosure(L, detail::garbage_collector_s<detail::free_functions::function_registry>::apply, 0);
198 lua_settable(L, -3);
199 lua_setmetatable(L, -2);
201 lua_settable(L, LUA_REGISTRYINDEX);