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");
47 class_rep
* base
= static_cast<class_rep
*>(lua_touserdata(L
, 1));
48 class_rep::base_info binfo
;
50 binfo
.pointer_offset
= 0;
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());
68 while (lua_next(L
, -2))
70 lua_pushstring(L
, "__init");
71 if (lua_equal(L
, -1, -3))
76 else lua_pop(L
, 1); // string
78 lua_pushstring(L
, "__finalize");
79 if (lua_equal(L
, -1, -3))
84 else lua_pop(L
, 1); // string
86 lua_pushvalue(L
, -2); // copy key
92 crep
->set_type(base
->type());
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");
108 if (std::strlen(lua_tostring(L
, 1)) != lua_strlen(L
, 1))
110 lua_pushstring(L
, "luabind does not support class names with extra nulls");
116 const char* name
= lua_tostring(L
, 1);
118 void* c
= lua_newuserdata(L
, sizeof(class_rep
));
119 new(c
) class_rep(L
, name
);
121 // make the class globally available
122 lua_pushstring(L
, name
);
123 lua_pushvalue(L
, -2);
124 lua_settable(L
, LUA_GLOBALSINDEX
);
126 // also add it to the closure as return value
127 lua_pushcclosure(L
, &stage2
, 1);