fixed tests for vc7
[luabind.git] / src / open.cpp
blobd575ca474dcb1f093ceb65fd3eed2d2c9ef0513c
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/class_info.hpp>
27 #include <luabind/function.hpp>
29 namespace luabind {
31 void open(lua_State* L)
33 // get the global class registry, or create one if it doesn't exist
34 // (it's global within a lua state)
35 detail::class_registry* r = 0;
37 // If you hit this assert it's because you have called luabind::open()
38 // twice on the same lua_State.
39 assert((detail::class_registry::get_registry(L) == 0)
40 && "you cannot call luabind::open() twice");
42 lua_pushstring(L, "__luabind_classes");
43 r = static_cast<detail::class_registry*>(
44 lua_newuserdata(L, sizeof(detail::class_registry)));
46 // set gc metatable
47 lua_newtable(L);
48 lua_pushstring(L, "__gc");
49 lua_pushcclosure(
51 , detail::garbage_collector_s<
52 detail::class_registry
53 >::apply
54 , 0);
56 lua_settable(L, -3);
57 lua_setmetatable(L, -2);
59 new(r) detail::class_registry(L);
60 lua_settable(L, LUA_REGISTRYINDEX);
62 // add functions (class, cast etc...)
63 lua_pushstring(L, "class");
64 lua_pushcclosure(L, detail::create_class::stage1, 0);
65 lua_settable(L, LUA_GLOBALSINDEX);
67 bind_class_info(L);
70 } // namespace luabind