Revert 5134297. This caused overload ambiguities.
[luabind.git] / src / open.cpp
blobe39d53de8ab593afc7d26e68c1fb2eaea61fded3
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 } // namespace unnamed
57 LUABIND_API lua_State* get_main_thread(lua_State* L)
59 lua_pushlightuserdata(L, &main_thread_tag);
60 lua_rawget(L, LUA_REGISTRYINDEX);
61 lua_State* result = static_cast<lua_State*>(lua_touserdata(L, -1));
62 lua_pop(L, 1);
64 if (!result)
65 throw std::runtime_error("Unable to get main thread, luabind::open() not called?");
67 return result;
70 LUABIND_API void open(lua_State* L)
72 bool is_main_thread = lua_pushthread(L) == 1;
73 lua_pop(L, 1);
75 if (!is_main_thread)
77 throw std::runtime_error(
78 "luabind::open() must be called with the main thread "
79 "lua_State*"
83 // get the global class registry, or create one if it doesn't exist
84 // (it's global within a lua state)
85 detail::class_registry* r = 0;
87 // If you hit this assert it's because you have called luabind::open()
88 // twice on the same lua_State.
89 assert((detail::class_registry::get_registry(L) == 0)
90 && "you cannot call luabind::open() twice");
92 lua_pushstring(L, "__luabind_classes");
93 r = static_cast<detail::class_registry*>(
94 lua_newuserdata(L, sizeof(detail::class_registry)));
96 // set gc metatable
97 lua_newtable(L);
98 lua_pushstring(L, "__gc");
99 lua_pushcclosure(
101 , detail::garbage_collector_s<
102 detail::class_registry
103 >::apply
104 , 0);
106 lua_settable(L, -3);
107 lua_setmetatable(L, -2);
109 new(r) detail::class_registry(L);
110 lua_settable(L, LUA_REGISTRYINDEX);
112 // TODO this leaks.
113 lua_pushstring(L, "__luabind_class_id_map");
114 void* classes_storage = lua_newuserdata(L, sizeof(detail::class_id_map));
115 detail::class_id_map* class_ids = new (classes_storage) detail::class_id_map;
116 (void)class_ids;
117 lua_settable(L, LUA_REGISTRYINDEX);
119 lua_pushstring(L, "__luabind_cast_graph");
120 void* cast_graph_storage = lua_newuserdata(
121 L, sizeof(detail::cast_graph));
122 detail::cast_graph* graph = new (cast_graph_storage) detail::cast_graph;
123 (void)graph;
124 lua_settable(L, LUA_REGISTRYINDEX);
126 lua_pushstring(L, "__luabind_class_map");
127 void* class_map_storage = lua_newuserdata(
128 L, sizeof(detail::class_map));
129 detail::class_map* classes = new (class_map_storage) detail::class_map;
130 (void)classes;
131 lua_settable(L, LUA_REGISTRYINDEX);
133 // add functions (class, cast etc...)
134 lua_pushstring(L, "class");
135 lua_pushcclosure(L, detail::create_class::stage1, 0);
136 lua_settable(L, LUA_GLOBALSINDEX);
138 lua_pushstring(L, "property");
139 lua_pushcclosure(L, &make_property, 0);
140 lua_settable(L, LUA_GLOBALSINDEX);
142 lua_pushlightuserdata(L, &main_thread_tag);
143 lua_pushlightuserdata(L, L);
144 lua_rawset(L, LUA_REGISTRYINDEX);
147 } // namespace luabind