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
)
38 object_rep::~object_rep()
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
);
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
);
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");
87 instance
->release_dependency_refs(L
);
88 instance
->~object_rep();
95 int set_instance_value(lua_State
* L
)
101 if (lua_isnil(L
, -1) && lua_getmetatable(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));
126 if (!lua_getmetatable(L
, 4))
129 lua_pushvalue(L
, -1);
132 lua_setmetatable(L
, -2);
146 int get_instance_value(lua_State
* L
)
152 if (lua_isnil(L
, -1) && lua_getmetatable(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);
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))
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
193 lua_call(L
, nargs
, 1);
198 lua_pop(L
, lua_gettop(L
));
199 lua_pushstring(L
, "No such operator defined");
205 } // namespace unnamed
207 LUABIND_API
void push_instance_metatable(lua_State
* L
)
211 // just indicate that this really is a class and not just
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);
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);
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
))
248 lua_rawgeti(L
, -1, 1);
250 if (lua_tocfunction(L
, -1) != &get_instance_value
)
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
);
264 lua_rawgeti(L
, LUA_REGISTRYINDEX
, cls
->metatable_ref());
265 lua_setmetatable(L
, -2);