Add missing index_tuple.hpp header.
[luabind.git] / src / object_rep.cpp
blob2f342a866deb5e537384c3ffd9756377a2bc2774
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);
105 lua_replace(L, -3);
106 lua_pop(L, 1);
109 if (lua_tocfunction(L, -1) == &property_tag)
111 // this member is a property, extract the "set" function and call it.
112 lua_getupvalue(L, -1, 2);
114 if (lua_isnil(L, -1))
116 lua_pushfstring(L, "property '%s' is read only", lua_tostring(L, 2));
117 lua_error(L);
120 lua_pushvalue(L, 1);
121 lua_pushvalue(L, 3);
122 lua_call(L, 2, 0);
123 return 0;
126 lua_pop(L, 1);
128 if (!lua_getmetatable(L, 4))
130 lua_newtable(L);
131 lua_pushvalue(L, -1);
132 lua_setfenv(L, 1);
133 lua_pushvalue(L, 4);
134 lua_setmetatable(L, -2);
136 else
138 lua_pop(L, 1);
141 lua_pushvalue(L, 2);
142 lua_pushvalue(L, 3);
143 lua_rawset(L, -3);
145 return 0;
148 int get_instance_value(lua_State* L)
150 lua_getfenv(L, 1);
151 lua_pushvalue(L, 2);
152 lua_rawget(L, -2);
154 if (lua_isnil(L, -1) && lua_getmetatable(L, -2))
156 lua_pushvalue(L, 2);
157 lua_rawget(L, -2);
160 if (lua_tocfunction(L, -1) == &property_tag)
162 // this member is a property, extract the "get" function and call it.
163 lua_getupvalue(L, -1, 1);
164 lua_pushvalue(L, 1);
165 lua_call(L, 1, 1);
168 return 1;
171 int dispatch_operator(lua_State* L)
173 for (int i = 0; i < 2; ++i)
175 if (get_instance(L, 1 + i))
177 int nargs = lua_gettop(L);
179 lua_pushvalue(L, lua_upvalueindex(1));
180 lua_gettable(L, 1 + i);
182 if (lua_isnil(L, -1))
184 lua_pop(L, 1);
185 continue;
188 lua_insert(L, 1); // move the function to the bottom
190 nargs = lua_toboolean(L, lua_upvalueindex(2)) ? 1 : nargs;
192 if (lua_toboolean(L, lua_upvalueindex(2))) // remove trailing nil
193 lua_remove(L, 3);
195 lua_call(L, nargs, 1);
196 return 1;
200 lua_pop(L, lua_gettop(L));
201 lua_pushstring(L, "No such operator defined");
202 lua_error(L);
204 return 0;
207 } // namespace unnamed
209 LUABIND_API void push_instance_metatable(lua_State* L)
211 lua_newtable(L);
213 // This is used as a tag to determine if a userdata is a luabind
214 // instance. We use a numeric key and a cclosure for fast comparision.
215 lua_pushnumber(L, 1);
216 lua_pushcclosure(L, get_instance_value, 0);
217 lua_rawset(L, -3);
219 lua_pushcclosure(L, destroy_instance, 0);
220 lua_setfield(L, -2, "__gc");
222 lua_pushcclosure(L, get_instance_value, 0);
223 lua_setfield(L, -2, "__index");
225 lua_pushcclosure(L, set_instance_value, 0);
226 lua_setfield(L, -2, "__newindex");
228 for (int op = 0; op < number_of_operators; ++op)
230 lua_pushstring(L, get_operator_name(op));
231 lua_pushvalue(L, -1);
232 lua_pushboolean(L, op == op_unm || op == op_len);
233 lua_pushcclosure(L, &dispatch_operator, 2);
234 lua_settable(L, -3);
238 LUABIND_API object_rep* get_instance(lua_State* L, int index)
240 object_rep* result = static_cast<object_rep*>(lua_touserdata(L, index));
242 if (!result || !lua_getmetatable(L, index))
243 return 0;
245 lua_rawgeti(L, -1, 1);
247 if (lua_tocfunction(L, -1) != &get_instance_value)
248 result = 0;
250 lua_pop(L, 2);
252 return result;
255 LUABIND_API object_rep* push_new_instance(lua_State* L, class_rep* cls)
257 void* storage = lua_newuserdata(L, sizeof(object_rep));
258 object_rep* result = new (storage) object_rep(0, cls);
259 cls->get_table(L);
260 lua_setfenv(L, -2);
261 lua_rawgeti(L, LUA_REGISTRYINDEX, cls->metatable_ref());
262 lua_setmetatable(L, -2);
263 return result;