Use std::size_t instead of int for dependency count.
[luabind.git] / src / object_rep.cpp
blob19a78fb335f1d25901ebad43474b766ee14a27fa
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(0)
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 assert(m_dependency_cnt < sizeof(object_rep));
50 void* key = (char*)this + m_dependency_cnt;
52 lua_pushlightuserdata(L, key);
53 lua_pushvalue(L, index);
54 lua_rawset(L, LUA_REGISTRYINDEX);
56 ++m_dependency_cnt;
59 void object_rep::release_dependency_refs(lua_State* L)
61 for (std::size_t i = 0; i < m_dependency_cnt; ++i)
63 void* key = (char*)this + i;
64 lua_pushlightuserdata(L, key);
65 lua_pushnil(L);
66 lua_rawset(L, LUA_REGISTRYINDEX);
70 int destroy_instance(lua_State* L)
72 object_rep* instance = static_cast<object_rep*>(lua_touserdata(L, 1));
74 lua_pushstring(L, "__finalize");
75 lua_gettable(L, 1);
77 if (lua_isnil(L, -1))
79 lua_pop(L, 1);
81 else
83 lua_pushvalue(L, 1);
84 lua_call(L, 1, 0);
87 instance->release_dependency_refs(L);
88 instance->~object_rep();
89 return 0;
92 namespace
95 int set_instance_value(lua_State* L)
97 lua_getfenv(L, 1);
98 lua_pushvalue(L, 2);
99 lua_rawget(L, -2);
101 if (lua_isnil(L, -1) && lua_getmetatable(L, -2))
103 lua_pushvalue(L, 2);
104 lua_rawget(L, -2);
107 if (lua_tocfunction(L, -1) == &property_tag)
109 // this member is a property, extract the "set" function and call it.
110 lua_getupvalue(L, -1, 2);
112 if (lua_isnil(L, -1))
114 lua_pushfstring(L, "property '%s' is read only", lua_tostring(L, 2));
115 lua_error(L);
118 lua_pushvalue(L, 1);
119 lua_pushvalue(L, 3);
120 lua_call(L, 2, 0);
121 return 0;
124 lua_pop(L, 1);
126 if (!lua_getmetatable(L, 4))
128 lua_newtable(L);
129 lua_pushvalue(L, -1);
130 lua_setfenv(L, 1);
131 lua_pushvalue(L, 4);
132 lua_setmetatable(L, -2);
134 else
136 lua_pop(L, 1);
139 lua_pushvalue(L, 2);
140 lua_pushvalue(L, 3);
141 lua_rawset(L, -3);
143 return 0;
146 int get_instance_value(lua_State* L)
148 lua_getfenv(L, 1);
149 lua_pushvalue(L, 2);
150 lua_rawget(L, -2);
152 if (lua_isnil(L, -1) && lua_getmetatable(L, -2))
154 lua_pushvalue(L, 2);
155 lua_rawget(L, -2);
158 if (lua_tocfunction(L, -1) == &property_tag)
160 // this member is a property, extract the "get" function and call it.
161 lua_getupvalue(L, -1, 1);
162 lua_pushvalue(L, 1);
163 lua_call(L, 1, 1);
166 return 1;
169 int dispatch_operator(lua_State* L)
171 for (int i = 0; i < 2; ++i)
173 if (get_instance(L, 1 + i))
175 int nargs = lua_gettop(L);
177 lua_pushvalue(L, lua_upvalueindex(1));
178 lua_gettable(L, 1 + i);
180 if (lua_isnil(L, -1))
182 lua_pop(L, 1);
183 continue;
186 lua_insert(L, 1); // move the function to the bottom
188 nargs = lua_toboolean(L, lua_upvalueindex(2)) ? 1 : nargs;
190 if (lua_toboolean(L, lua_upvalueindex(2))) // remove trailing nil
191 lua_remove(L, 3);
193 lua_call(L, nargs, 1);
194 return 1;
198 lua_pop(L, lua_gettop(L));
199 lua_pushstring(L, "No such operator defined");
200 lua_error(L);
202 return 0;
205 } // namespace unnamed
207 LUABIND_API void push_instance_metatable(lua_State* L)
209 lua_newtable(L);
211 // just indicate that this really is a class and not just
212 // any user data
213 lua_pushboolean(L, 1);
214 lua_setfield(L, -2, "__luabind_class");
216 // This is used as a tag to determine if a userdata is a luabind
217 // instance. We use a numeric key and a cclosure for fast comparision.
218 lua_pushnumber(L, 1);
219 lua_pushcclosure(L, get_instance_value, 0);
220 lua_rawset(L, -3);
222 lua_pushcclosure(L, destroy_instance, 0);
223 lua_setfield(L, -2, "__gc");
225 lua_pushcclosure(L, get_instance_value, 0);
226 lua_setfield(L, -2, "__index");
228 lua_pushcclosure(L, set_instance_value, 0);
229 lua_setfield(L, -2, "__newindex");
231 for (int op = 0; op < number_of_operators; ++op)
233 lua_pushstring(L, get_operator_name(op));
234 lua_pushvalue(L, -1);
235 lua_pushboolean(L, op == op_unm || op == op_len);
236 lua_pushcclosure(L, &dispatch_operator, 2);
237 lua_settable(L, -3);
241 LUABIND_API object_rep* get_instance(lua_State* L, int index)
243 object_rep* result = static_cast<object_rep*>(lua_touserdata(L, index));
245 if (!result || !lua_getmetatable(L, index))
246 return 0;
248 lua_rawgeti(L, -1, 1);
250 if (lua_tocfunction(L, -1) != &get_instance_value)
251 result = 0;
253 lua_pop(L, 2);
255 return result;
258 LUABIND_API object_rep* push_new_instance(lua_State* L, class_rep* cls)
260 void* storage = lua_newuserdata(L, sizeof(object_rep));
261 object_rep* result = new (storage) object_rep(0, cls);
262 cls->get_table(L);
263 lua_setfenv(L, -2);
264 lua_rawgeti(L, LUA_REGISTRYINDEX, cls->metatable_ref());
265 lua_setmetatable(L, -2);
266 return result;