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 #define LUABIND_BUILDING
25 #include <luabind/lua_include.hpp>
27 #include <luabind/luabind.hpp>
29 namespace luabind
{ namespace detail
33 // expects two tables on the lua stack:
36 void copy_member_table(lua_State
* L
)
40 while (lua_next(L
, -2))
42 lua_pushstring(L
, "__init");
43 if (lua_equal(L
, -1, -3))
48 else lua_pop(L
, 1); // __init string
50 lua_pushstring(L
, "__finalize");
51 if (lua_equal(L
, -1, -3))
56 else lua_pop(L
, 1); // __finalize string
58 lua_pushvalue(L
, -2); // copy key
66 int create_class::stage2(lua_State
* L
)
68 class_rep
* crep
= static_cast<class_rep
*>(lua_touserdata(L
, lua_upvalueindex(1)));
69 assert((crep
!= 0) && "internal error, please report");
70 assert((is_class_rep(L
, lua_upvalueindex(1))) && "internal error, please report");
72 #ifndef LUABIND_NO_ERROR_CHECKING
74 if (!is_class_rep(L
, 1))
76 lua_pushstring(L
, "expected class to derive from or a newline");
82 class_rep
* base
= static_cast<class_rep
*>(lua_touserdata(L
, 1));
83 class_rep::base_info binfo
;
85 binfo
.pointer_offset
= 0;
87 crep
->add_base_class(binfo
);
89 // copy base class members
95 crep
->get_default_table(L
);
96 base
->get_default_table(L
);
99 crep
->set_type(base
->type());
104 int create_class::stage1(lua_State
* L
)
107 #ifndef LUABIND_NO_ERROR_CHECKING
109 if (lua_gettop(L
) != 1 || lua_type(L
, 1) != LUA_TSTRING
|| lua_isnumber(L
, 1))
111 lua_pushstring(L
, "invalid construct, expected class name");
115 if (std::strlen(lua_tostring(L
, 1)) != lua_strlen(L
, 1))
117 lua_pushstring(L
, "luabind does not support class names with extra nulls");
123 const char* name
= lua_tostring(L
, 1);
125 void* c
= lua_newuserdata(L
, sizeof(class_rep
));
126 new(c
) class_rep(L
, name
);
128 // make the class globally available
129 lua_pushstring(L
, name
);
130 lua_pushvalue(L
, -2);
131 lua_settable(L
, LUA_GLOBALSINDEX
);
133 // also add it to the closure as return value
134 lua_pushcclosure(L
, &stage2
, 1);