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 if (!m_dependency_ref
.is_valid())
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
);
61 int destroy_instance(lua_State
* L
)
63 object_rep
* instance
= static_cast<object_rep
*>(lua_touserdata(L
, 1));
65 lua_pushstring(L
, "__finalize");
78 instance
->~object_rep();
85 int set_instance_value(lua_State
* L
)
91 if (lua_isnil(L
, -1) && lua_getmetatable(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));
116 if (!lua_getmetatable(L
, 4))
119 lua_pushvalue(L
, -1);
122 lua_setmetatable(L
, -2);
136 int get_instance_value(lua_State
* L
)
142 if (lua_isnil(L
, -1) && lua_getmetatable(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);
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))
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
183 lua_call(L
, nargs
, 1);
188 lua_pop(L
, lua_gettop(L
));
189 lua_pushstring(L
, "No such operator defined");
195 } // namespace unnamed
197 LUABIND_API
void push_instance_metatable(lua_State
* L
)
201 // just indicate that this really is a class and not just
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);
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);
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
))
238 lua_rawgeti(L
, -1, 1);
240 if (lua_tocfunction(L
, -1) != &get_instance_value
)
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
);
254 lua_rawgeti(L
, LUA_REGISTRYINDEX
, cls
->metatable_ref());
255 lua_setmetatable(L
, -2);