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.
30 #define LUABIND_NO_HEADERS_ONLY
32 #include <luabind/luabind.hpp>
34 using namespace luabind::detail
;
36 int luabind::detail::create_class::stage2(lua_State
* L
)
38 class_rep
* crep
= static_cast<class_rep
*>(lua_touserdata(L
, lua_upvalueindex(1)));
39 assert((crep
!= 0) && "internal error, please report");
40 assert((is_class_rep(L
, lua_upvalueindex(1))) && "internal error, please report");
42 #ifndef LUABIND_NO_ERROR_CHECKING
44 if (!is_class_rep(L
, 1))
46 lua_pushstring(L
, "expected class to derive from or a newline");
52 class_rep
* base
= static_cast<class_rep
*>(lua_touserdata(L
, 1));
53 class_rep::base_info binfo
;
55 binfo
.pointer_offset
= 0;
57 crep
->add_base_class(binfo
);
59 if (base
->get_class_type() == class_rep::lua_class
)
61 // copy base class members
63 detail::getref(L
, crep
->table_ref());
64 detail::getref(L
, base
->table_ref());
67 while (lua_next(L
, -2))
69 lua_pushstring(L
, "__init");
70 if (lua_equal(L
, -1, -3))
76 lua_pushstring(L
, "__finalize");
77 if (lua_equal(L
, -1, -3))
83 lua_pushvalue(L
, -2); // copy key
89 crep
->set_type(base
->type());
94 int luabind::detail::create_class::stage1(lua_State
* L
)
97 #ifndef LUABIND_NO_ERROR_CHECKING
99 if (lua_gettop(L
) != 1 || lua_type(L
, 1) != LUA_TSTRING
|| lua_isnumber(L
, 1))
101 lua_pushstring(L
, "invalid construct, expected class name");
107 const char* name
= lua_tostring(L
, 1);
109 void* c
= lua_newuserdata(L
, sizeof(class_rep
));
110 new(c
) class_rep(L
, name
);
112 // make the class globally available
113 lua_pushstring(L
, name
);
114 lua_pushvalue(L
, -2);
115 lua_settable(L
, LUA_GLOBALSINDEX
);
117 // also add it to the closure as return value
118 lua_pushcclosure(L
, &stage2
, 1);