Suppress unused variable warning.
[luabind.git] / src / object_rep.cpp
bloba4419fb7f500f25b89e020ec5db3b09cd597e998
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/detail/object_rep.hpp>
26 #include <luabind/detail/class_rep.hpp>
28 namespace luabind { namespace detail
31 // dest is a function that is called to delete the c++ object this struct holds
32 object_rep::object_rep(instance_holder* instance, class_rep* crep)
33 : m_instance(instance)
34 , m_classrep(crep)
35 , m_dependency_cnt(1)
38 object_rep::~object_rep()
40 if (!m_instance)
41 return;
42 m_instance->~instance_holder();
43 deallocate(m_instance);
46 void object_rep::add_dependency(lua_State* L, int index)
48 if (!m_dependency_ref.is_valid())
50 lua_newtable(L);
51 m_dependency_ref.set(L);
54 m_dependency_ref.get(L);
55 lua_pushvalue(L, index);
56 lua_rawseti(L, -2, m_dependency_cnt);
57 lua_pop(L, 1);
58 ++m_dependency_cnt;
61 int destroy_instance(lua_State* L)
63 object_rep* instance = static_cast<object_rep*>(lua_touserdata(L, 1));
65 lua_pushstring(L, "__finalize");
66 lua_gettable(L, 1);
68 if (lua_isnil(L, -1))
70 lua_pop(L, 1);
72 else
74 lua_pushvalue(L, 1);
75 lua_call(L, 1, 0);
78 instance->~object_rep();
79 return 0;
82 namespace
85 int set_instance_value(lua_State* L)
87 lua_getfenv(L, 1);
88 lua_pushvalue(L, 2);
89 lua_rawget(L, -2);
91 if (lua_isnil(L, -1) && lua_getmetatable(L, -2))
93 lua_pushvalue(L, 2);
94 lua_rawget(L, -2);
97 if (lua_tocfunction(L, -1) == &property_tag)
99 // this member is a property, extract the "set" function and call it.
100 lua_getupvalue(L, -1, 2);
102 if (lua_isnil(L, -1))
104 lua_pushfstring(L, "property '%s' is read only", lua_tostring(L, 2));
105 lua_error(L);
108 lua_pushvalue(L, 1);
109 lua_pushvalue(L, 3);
110 lua_call(L, 2, 0);
111 return 0;
114 lua_pop(L, 1);
116 if (!lua_getmetatable(L, 4))
118 lua_newtable(L);
119 lua_pushvalue(L, -1);
120 lua_setfenv(L, 1);
121 lua_pushvalue(L, 4);
122 lua_setmetatable(L, -2);
124 else
126 lua_pop(L, 1);
129 lua_pushvalue(L, 2);
130 lua_pushvalue(L, 3);
131 lua_rawset(L, -3);
133 return 0;
136 int get_instance_value(lua_State* L)
138 lua_getfenv(L, 1);
139 lua_pushvalue(L, 2);
140 lua_rawget(L, -2);
142 if (lua_isnil(L, -1) && lua_getmetatable(L, -2))
144 lua_pushvalue(L, 2);
145 lua_rawget(L, -2);
148 if (lua_tocfunction(L, -1) == &property_tag)
150 // this member is a property, extract the "get" function and call it.
151 lua_getupvalue(L, -1, 1);
152 lua_pushvalue(L, 1);
153 lua_call(L, 1, 1);
156 return 1;
159 int dispatch_operator(lua_State* L)
161 for (int i = 0; i < 2; ++i)
163 if (get_instance(L, 1 + i))
165 int nargs = lua_gettop(L);
167 lua_pushvalue(L, lua_upvalueindex(1));
168 lua_gettable(L, 1 + i);
170 if (lua_isnil(L, -1))
172 lua_pop(L, 1);
173 continue;
176 lua_insert(L, 1); // move the function to the bottom
178 nargs = lua_toboolean(L, lua_upvalueindex(2)) ? 1 : nargs;
180 if (lua_toboolean(L, lua_upvalueindex(2))) // remove trailing nil
181 lua_remove(L, 3);
183 lua_call(L, nargs, 1);
184 return 1;
188 lua_pop(L, lua_gettop(L));
189 lua_pushstring(L, "No such operator defined");
190 lua_error(L);
192 return 0;
195 } // namespace unnamed
197 LUABIND_API void push_instance_metatable(lua_State* L)
199 lua_newtable(L);
201 // just indicate that this really is a class and not just
202 // any user data
203 lua_pushboolean(L, 1);
204 lua_setfield(L, -2, "__luabind_class");
206 // This is used as a tag to determine if a userdata is a luabind
207 // instance. We use a numeric key and a cclosure for fast comparision.
208 lua_pushnumber(L, 1);
209 lua_pushcclosure(L, get_instance_value, 0);
210 lua_rawset(L, -3);
212 lua_pushcclosure(L, destroy_instance, 0);
213 lua_setfield(L, -2, "__gc");
215 lua_pushcclosure(L, get_instance_value, 0);
216 lua_setfield(L, -2, "__index");
218 lua_pushcclosure(L, set_instance_value, 0);
219 lua_setfield(L, -2, "__newindex");
221 for (int op = 0; op < number_of_operators; ++op)
223 lua_pushstring(L, get_operator_name(op));
224 lua_pushvalue(L, -1);
225 lua_pushboolean(L, op == op_unm || op == op_len);
226 lua_pushcclosure(L, &dispatch_operator, 2);
227 lua_settable(L, -3);
231 LUABIND_API object_rep* get_instance(lua_State* L, int index)
233 object_rep* result = static_cast<object_rep*>(lua_touserdata(L, index));
235 if (!result || !lua_getmetatable(L, index))
236 return 0;
238 lua_rawgeti(L, -1, 1);
240 if (lua_tocfunction(L, -1) != &get_instance_value)
241 result = 0;
243 lua_pop(L, 2);
245 return result;
248 LUABIND_API object_rep* push_new_instance(lua_State* L, class_rep* cls)
250 void* storage = lua_newuserdata(L, sizeof(object_rep));
251 object_rep* result = new (storage) object_rep(0, cls);
252 cls->get_table(L);
253 lua_setfenv(L, -2);
254 lua_rawgeti(L, LUA_REGISTRYINDEX, cls->metatable_ref());
255 lua_setmetatable(L, -2);
256 return result;