Initial commit, includes Lua with broken Luabind as a backup for branching purposes
[terrastrategy.git] / src / luabind / open.cpp
blobd77bd0507187661077c2285a7201b4bdbd4eedcb
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 #include <luabind/lua_include.hpp>
25 #include <luabind/luabind.hpp>
26 #include <luabind/function.hpp>
28 namespace luabind {
30 namespace
33 int make_property(lua_State* L)
35 int args = lua_gettop(L);
37 if (args == 0 || args > 2)
39 lua_pushstring(L, "make_property() called with wrong number of arguments.");
40 lua_error(L);
43 if (args == 1)
44 lua_pushnil(L);
46 lua_pushcclosure(L, &detail::property_tag, 2);
47 return 1;
50 } // namespace unnamed
52 void open(lua_State* L)
54 // get the global class registry, or create one if it doesn't exist
55 // (it's global within a lua state)
56 detail::class_registry* r = 0;
58 // If you hit this assert it's because you have called luabind::open()
59 // twice on the same lua_State.
60 assert((detail::class_registry::get_registry(L) == 0)
61 && "you cannot call luabind::open() twice");
63 lua_pushstring(L, "__luabind_classes");
64 r = static_cast<detail::class_registry*>(
65 lua_newuserdata(L, sizeof(detail::class_registry)));
67 // set gc metatable
68 lua_newtable(L);
69 lua_pushstring(L, "__gc");
70 lua_pushcclosure(
72 , detail::garbage_collector_s<
73 detail::class_registry
74 >::apply
75 , 0);
77 lua_settable(L, -3);
78 lua_setmetatable(L, -2);
80 new(r) detail::class_registry(L);
81 lua_settable(L, LUA_REGISTRYINDEX);
83 // add functions (class, cast etc...)
84 lua_pushstring(L, "class");
85 lua_pushcclosure(L, detail::create_class::stage1, 0);
86 lua_settable(L, LUA_GLOBALSINDEX);
88 lua_pushstring(L, "property");
89 lua_pushcclosure(L, &make_property, 0);
90 lua_settable(L, LUA_GLOBALSINDEX);
93 } // namespace luabind