*** empty log message ***
[luabind.git] / src / create_class.cpp
blob5eecc568b2061cc1f5a3f8bb2601916e9231b962
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>
27 using namespace luabind::detail;
29 int luabind::detail::create_class::stage2(lua_State* L)
31 class_rep* crep = static_cast<class_rep*>(lua_touserdata(L, lua_upvalueindex(1)));
32 assert((crep != 0) && "internal error, please report");
33 assert((is_class_rep(L, lua_upvalueindex(1))) && "internal error, please report");
35 #ifndef LUABIND_NO_ERROR_CHECKING
37 if (!is_class_rep(L, 1))
39 lua_pushstring(L, "expected class to derive from or a newline");
40 lua_error(L);
43 #endif
45 class_rep* base = static_cast<class_rep*>(lua_touserdata(L, 1));
46 class_rep::base_info binfo;
48 binfo.pointer_offset = 0;
49 binfo.base = base;
50 crep->add_base_class(binfo);
52 // set holder size and alignment so that we can class_rep::allocate
53 // can return the correctly sized buffers
54 crep->derived_from(base);
56 // this has changed, c++ classes now stores their
57 // methods in the table as well
58 // if (base->get_class_type() == class_rep::lua_class)
60 // copy base class members
62 detail::getref(L, crep->table_ref());
63 detail::getref(L, base->table_ref());
64 lua_pushnil(L);
66 while (lua_next(L, -2))
68 lua_pushstring(L, "__init");
69 if (lua_equal(L, -1, -3))
71 lua_pop(L, 2);
72 continue;
74 else lua_pop(L, 1); // string
76 lua_pushstring(L, "__finalize");
77 if (lua_equal(L, -1, -3))
79 lua_pop(L, 2);
80 continue;
82 else lua_pop(L, 1); // string
84 lua_pushvalue(L, -2); // copy key
85 lua_insert(L, -2);
86 lua_settable(L, -5);
90 crep->set_type(base->type());
92 return 0;
95 int luabind::detail::create_class::stage1(lua_State* L)
98 #ifndef LUABIND_NO_ERROR_CHECKING
100 if (lua_gettop(L) != 1 || lua_type(L, 1) != LUA_TSTRING || lua_isnumber(L, 1))
102 lua_pushstring(L, "invalid construct, expected class name");
103 lua_error(L);
106 if (std::strlen(lua_tostring(L, 1)) != lua_strlen(L, 1))
108 lua_pushstring(L, "luabind does not support class names with extra nulls");
109 lua_error(L);
112 #endif
114 const char* name = lua_tostring(L, 1);
116 void* c = lua_newuserdata(L, sizeof(class_rep));
117 new(c) class_rep(L, name);
119 // make the class globally available
120 lua_pushstring(L, name);
121 lua_pushvalue(L, -2);
122 lua_settable(L, LUA_GLOBALSINDEX);
124 // also add it to the closure as return value
125 lua_pushcclosure(L, &stage2, 1);
127 return 1;