*** empty log message ***
[luabind.git] / src / create_class.cpp
blobddf77c88cf32e47d952b35f4898d663db5d29852
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 #define LUABIND_BUILDING
27 #include <luabind/luabind.hpp>
29 using namespace luabind::detail;
31 int luabind::detail::create_class::stage2(lua_State* L)
33 class_rep* crep = static_cast<class_rep*>(lua_touserdata(L, lua_upvalueindex(1)));
34 assert((crep != 0) && "internal error, please report");
35 assert((is_class_rep(L, lua_upvalueindex(1))) && "internal error, please report");
37 #ifndef LUABIND_NO_ERROR_CHECKING
39 if (!is_class_rep(L, 1))
41 lua_pushstring(L, "expected class to derive from or a newline");
42 lua_error(L);
45 #endif
47 class_rep* base = static_cast<class_rep*>(lua_touserdata(L, 1));
48 class_rep::base_info binfo;
50 binfo.pointer_offset = 0;
51 binfo.base = base;
52 crep->add_base_class(binfo);
54 // set holder size and alignment so that we can class_rep::allocate
55 // can return the correctly sized buffers
56 crep->derived_from(base);
58 // this has changed, c++ classes now stores their
59 // methods in the table as well
60 // if (base->get_class_type() == class_rep::lua_class)
62 // copy base class members
64 detail::getref(L, crep->table_ref());
65 detail::getref(L, base->table_ref());
66 lua_pushnil(L);
68 while (lua_next(L, -2))
70 lua_pushstring(L, "__init");
71 if (lua_equal(L, -1, -3))
73 lua_pop(L, 2);
74 continue;
76 else lua_pop(L, 1); // string
78 lua_pushstring(L, "__finalize");
79 if (lua_equal(L, -1, -3))
81 lua_pop(L, 2);
82 continue;
84 else lua_pop(L, 1); // string
86 lua_pushvalue(L, -2); // copy key
87 lua_insert(L, -2);
88 lua_settable(L, -5);
92 crep->set_type(base->type());
94 return 0;
97 int luabind::detail::create_class::stage1(lua_State* L)
100 #ifndef LUABIND_NO_ERROR_CHECKING
102 if (lua_gettop(L) != 1 || lua_type(L, 1) != LUA_TSTRING || lua_isnumber(L, 1))
104 lua_pushstring(L, "invalid construct, expected class name");
105 lua_error(L);
108 #endif
110 const char* name = lua_tostring(L, 1);
112 void* c = lua_newuserdata(L, sizeof(class_rep));
113 new(c) class_rep(L, name);
115 // make the class globally available
116 lua_pushstring(L, name);
117 lua_pushvalue(L, -2);
118 lua_settable(L, LUA_GLOBALSINDEX);
120 // also add it to the closure as return value
121 lua_pushcclosure(L, &stage2, 1);
123 return 1;