*** empty log message ***
[luabind.git] / src / create_class.cpp
blob7a55902844a819432c151579a18a5b47aa65ee98
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 extern "C"
25 #include "lua.h"
26 #include "lauxlib.h"
27 #include "lualib.h"
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");
47 lua_error(L);
50 #endif
52 class_rep* base = static_cast<class_rep*>(lua_touserdata(L, 1));
53 class_rep::base_info binfo;
55 binfo.pointer_offset = 0;
56 binfo.base = base;
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());
65 lua_pushnil(L);
67 while (lua_next(L, -2))
69 lua_pushstring(L, "__init");
70 if (lua_equal(L, -1, -3))
72 lua_pop(L, 2);
73 continue;
76 lua_pushstring(L, "__finalize");
77 if (lua_equal(L, -1, -3))
79 lua_pop(L, 2);
80 continue;
83 lua_pushvalue(L, -2); // copy key
84 lua_insert(L, -2);
85 lua_settable(L, -5);
89 crep->set_type(base->type());
91 return 0;
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");
102 lua_error(L);
105 #endif
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);
120 return 1;