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 namespace luabind
{ namespace detail
31 // expects two tables on the lua stack:
34 void copy_member_table(lua_State
* L
)
38 while (lua_next(L
, -2))
40 lua_pushstring(L
, "__init");
41 if (lua_equal(L
, -1, -3))
46 else lua_pop(L
, 1); // __init string
48 lua_pushstring(L
, "__finalize");
49 if (lua_equal(L
, -1, -3))
54 else lua_pop(L
, 1); // __finalize string
56 lua_pushvalue(L
, -2); // copy key
64 int create_class::stage2(lua_State
* L
)
66 class_rep
* crep
= static_cast<class_rep
*>(lua_touserdata(L
, lua_upvalueindex(1)));
67 assert((crep
!= 0) && "internal error, please report");
68 assert((is_class_rep(L
, lua_upvalueindex(1))) && "internal error, please report");
70 #ifndef LUABIND_NO_ERROR_CHECKING
72 if (!is_class_rep(L
, 1))
74 lua_pushstring(L
, "expected class to derive from or a newline");
80 class_rep
* base
= static_cast<class_rep
*>(lua_touserdata(L
, 1));
81 class_rep::base_info binfo
;
83 binfo
.pointer_offset
= 0;
85 crep
->add_base_class(binfo
);
87 // set holder size and alignment so that we can class_rep::allocate
88 // can return the correctly sized buffers
89 crep
->derived_from(base
);
91 // copy base class members
97 crep
->get_default_table(L
);
98 base
->get_default_table(L
);
101 crep
->set_type(base
->type());
106 int create_class::stage1(lua_State
* L
)
109 #ifndef LUABIND_NO_ERROR_CHECKING
111 if (lua_gettop(L
) != 1 || lua_type(L
, 1) != LUA_TSTRING
|| lua_isnumber(L
, 1))
113 lua_pushstring(L
, "invalid construct, expected class name");
117 if (std::strlen(lua_tostring(L
, 1)) != lua_strlen(L
, 1))
119 lua_pushstring(L
, "luabind does not support class names with extra nulls");
125 const char* name
= lua_tostring(L
, 1);
127 void* c
= lua_newuserdata(L
, sizeof(class_rep
));
128 new(c
) class_rep(L
, name
);
130 // make the class globally available
131 lua_pushstring(L
, name
);
132 lua_pushvalue(L
, -2);
133 lua_settable(L
, LUA_GLOBALSINDEX
);
135 // also add it to the closure as return value
136 lua_pushcclosure(L
, &stage2
, 1);