*** empty log message ***
[luabind.git] / src / create_class.cpp
blobc9bf3f89692177f36d36f103acccc20c97a9c230
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");
42 lua_error(L);
45 #endif
47 class_rep* base = static_cast<class_rep*>(lua_touserdata(L, 1));
48 class_rep::base_info binfo;
50 binfo.pointer_offset = 0;
51 binfo.base = base;
52 crep->add_base_class(binfo);
54 // this has changed, c++ classes now stores their
55 // methods in the table as well
56 // if (base->get_class_type() == class_rep::lua_class)
58 // copy base class members
60 detail::getref(L, crep->table_ref());
61 detail::getref(L, base->table_ref());
62 lua_pushnil(L);
64 while (lua_next(L, -2))
66 lua_pushstring(L, "__init");
67 if (lua_equal(L, -1, -3))
69 lua_pop(L, 2);
70 continue;
72 else lua_pop(L, 1); // string
74 lua_pushstring(L, "__finalize");
75 if (lua_equal(L, -1, -3))
77 lua_pop(L, 2);
78 continue;
80 else lua_pop(L, 1); // string
82 lua_pushvalue(L, -2); // copy key
83 lua_insert(L, -2);
84 lua_settable(L, -5);
88 crep->set_type(base->type());
90 return 0;
93 int luabind::detail::create_class::stage1(lua_State* L)
96 #ifndef LUABIND_NO_ERROR_CHECKING
98 if (lua_gettop(L) != 1 || lua_type(L, 1) != LUA_TSTRING || lua_isnumber(L, 1))
100 lua_pushstring(L, "invalid construct, expected class name");
101 lua_error(L);
104 #endif
106 const char* name = lua_tostring(L, 1);
108 void* c = lua_newuserdata(L, sizeof(class_rep));
109 new(c) class_rep(L, name);
111 // make the class globally available
112 lua_pushstring(L, name);
113 lua_pushvalue(L, -2);
114 lua_settable(L, LUA_GLOBALSINDEX);
116 // also add it to the closure as return value
117 lua_pushcclosure(L, &stage2, 1);
119 return 1;