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))
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));
128 if (!lua_getmetatable(L
, 4))
131 lua_pushvalue(L
, -1);
134 lua_setmetatable(L
, -2);
148 int get_instance_value(lua_State
* L
)
154 if (lua_isnil(L
, -1) && lua_getmetatable(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);
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))
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
195 lua_call(L
, nargs
, 1);
200 lua_pop(L
, lua_gettop(L
));
201 lua_pushstring(L
, "No such operator defined");
207 } // namespace unnamed
209 LUABIND_API
void push_instance_metatable(lua_State
* 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);
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);
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
))
245 lua_rawgeti(L
, -1, 1);
247 if (lua_tocfunction(L
, -1) != &get_instance_value
)
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
);
261 lua_rawgeti(L
, LUA_REGISTRYINDEX
, cls
->metatable_ref());
262 lua_setmetatable(L
, -2);