Remove old unused files accidentally left around.
[luabind.git] / src / open.cpp
blob051c8bc17f8222999420cd6ab51cc744f0e897d1
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 #define LUABIND_BUILDING
25 #include <luabind/lua_include.hpp>
27 #include <luabind/luabind.hpp>
28 #include <luabind/function.hpp>
29 #include <luabind/get_main_thread.hpp>
31 namespace luabind {
33 namespace
36 int make_property(lua_State* L)
38 int args = lua_gettop(L);
40 if (args == 0 || args > 2)
42 lua_pushstring(L, "make_property() called with wrong number of arguments.");
43 lua_error(L);
46 if (args == 1)
47 lua_pushnil(L);
49 lua_pushcclosure(L, &detail::property_tag, 2);
50 return 1;
53 int main_thread_tag;
55 int deprecated_super(lua_State* L)
57 lua_pushstring(L,
58 "DEPRECATION: 'super' has been deprecated in favor of "
59 "directly calling the base class __init() function. "
60 "This error can be disabled by calling 'luabind::disable_super_deprecation()'."
62 lua_error(L);
64 return 0;
67 int destroy_class_id_map(lua_State* L)
69 detail::class_id_map* m =
70 (detail::class_id_map*)lua_touserdata(L, 1);
71 m->~class_id_map();
72 return 0;
75 int destroy_cast_graph(lua_State* L)
77 detail::cast_graph* g =
78 (detail::cast_graph*)lua_touserdata(L, 1);
79 g->~cast_graph();
80 return 0;
83 int destroy_class_map(lua_State* L)
85 detail::class_map* m =
86 (detail::class_map*)lua_touserdata(L, 1);
87 m->~class_map();
88 return 0;
91 } // namespace unnamed
93 LUABIND_API lua_State* get_main_thread(lua_State* L)
95 lua_pushlightuserdata(L, &main_thread_tag);
96 lua_rawget(L, LUA_REGISTRYINDEX);
97 lua_State* result = static_cast<lua_State*>(lua_touserdata(L, -1));
98 lua_pop(L, 1);
100 if (!result)
101 throw std::runtime_error("Unable to get main thread, luabind::open() not called?");
103 return result;
106 LUABIND_API void open(lua_State* L)
108 bool is_main_thread = lua_pushthread(L) == 1;
109 lua_pop(L, 1);
111 if (!is_main_thread)
113 throw std::runtime_error(
114 "luabind::open() must be called with the main thread "
115 "lua_State*"
119 // get the global class registry, or create one if it doesn't exist
120 // (it's global within a lua state)
121 detail::class_registry* r = 0;
123 // If you hit this assert it's because you have called luabind::open()
124 // twice on the same lua_State.
125 assert((detail::class_registry::get_registry(L) == 0)
126 && "you cannot call luabind::open() twice");
128 lua_pushstring(L, "__luabind_classes");
129 r = static_cast<detail::class_registry*>(
130 lua_newuserdata(L, sizeof(detail::class_registry)));
132 // set gc metatable
133 lua_newtable(L);
134 lua_pushstring(L, "__gc");
135 lua_pushcclosure(
137 , detail::garbage_collector_s<
138 detail::class_registry
139 >::apply
140 , 0);
142 lua_settable(L, -3);
143 lua_setmetatable(L, -2);
145 new(r) detail::class_registry(L);
146 lua_settable(L, LUA_REGISTRYINDEX);
148 lua_pushstring(L, "__luabind_class_id_map");
149 void* classes_storage = lua_newuserdata(L, sizeof(detail::class_id_map));
150 detail::class_id_map* class_ids = new (classes_storage) detail::class_id_map;
151 (void)class_ids;
153 lua_newtable(L);
154 lua_pushcclosure(L, &destroy_class_id_map, 0);
155 lua_setfield(L, -2, "__gc");
156 lua_setmetatable(L, -2);
158 lua_settable(L, LUA_REGISTRYINDEX);
160 lua_pushstring(L, "__luabind_cast_graph");
161 void* cast_graph_storage = lua_newuserdata(
162 L, sizeof(detail::cast_graph));
163 detail::cast_graph* graph = new (cast_graph_storage) detail::cast_graph;
164 (void)graph;
166 lua_newtable(L);
167 lua_pushcclosure(L, &destroy_cast_graph, 0);
168 lua_setfield(L, -2, "__gc");
169 lua_setmetatable(L, -2);
171 lua_settable(L, LUA_REGISTRYINDEX);
173 lua_pushstring(L, "__luabind_class_map");
174 void* class_map_storage = lua_newuserdata(
175 L, sizeof(detail::class_map));
176 detail::class_map* classes = new (class_map_storage) detail::class_map;
177 (void)classes;
179 lua_newtable(L);
180 lua_pushcclosure(L, &destroy_class_map, 0);
181 lua_setfield(L, -2, "__gc");
182 lua_setmetatable(L, -2);
184 lua_settable(L, LUA_REGISTRYINDEX);
186 // add functions (class, cast etc...)
187 lua_pushstring(L, "class");
188 lua_pushcclosure(L, detail::create_class::stage1, 0);
189 lua_settable(L, LUA_GLOBALSINDEX);
191 lua_pushstring(L, "property");
192 lua_pushcclosure(L, &make_property, 0);
193 lua_settable(L, LUA_GLOBALSINDEX);
195 lua_pushlightuserdata(L, &main_thread_tag);
196 lua_pushlightuserdata(L, L);
197 lua_rawset(L, LUA_REGISTRYINDEX);
199 lua_pushstring(L, "super");
200 lua_pushcclosure(L, &deprecated_super, 0);
201 lua_settable(L, LUA_GLOBALSINDEX);
204 } // namespace luabind