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 #include <luabind/lua_include.hpp>
25 #include <luabind/luabind.hpp>
28 using namespace luabind::detail
;
31 #ifndef LUABIND_NO_ERROR_CHECKING
33 std::string
luabind::detail::get_overload_signatures_candidates(
35 , std::vector
<const overload_rep_base
*>::iterator start
36 , std::vector
<const overload_rep_base
*>::iterator end
40 for (; start
!= end
; ++start
)
43 (*start
)->get_signature(L
, s
);
52 luabind::detail::class_rep::class_rep(LUABIND_TYPE_INFO type
55 , void(*destructor
)(void*)
56 , void(*const_holder_destructor
)(void*)
57 , LUABIND_TYPE_INFO holder_type
58 , LUABIND_TYPE_INFO const_holder_type
59 , void*(*extractor
)(void*)
60 , const void*(*const_extractor
)(void*)
61 , void(*const_converter
)(void*,void*)
62 , void(*construct_holder
)(void*,void*)
63 , void(*construct_const_holder
)(void*,void*)
65 , int holder_alignment
)
68 , m_holder_type(holder_type
)
69 , m_const_holder_type(const_holder_type
)
70 , m_extractor(extractor
)
71 , m_const_extractor(const_extractor
)
72 , m_const_converter(const_converter
)
73 , m_construct_holder(construct_holder
)
74 , m_construct_const_holder(construct_const_holder
)
75 , m_holder_size(holder_size
)
76 , m_holder_alignment(holder_alignment
)
78 , m_table_ref(LUA_NOREF
)
79 , m_class_type(cpp_class
)
80 , m_destructor(destructor
)
81 , m_const_holder_destructor(const_holder_destructor
)
84 assert(m_holder_alignment
>= 1 && "internal error");
87 m_table_ref
= detail::ref(L
);
89 class_registry
* r
= class_registry::get_registry(L
);
90 assert((r
->cpp_class() != LUA_NOREF
) && "you must call luabind::open()");
92 detail::getref(L
, r
->cpp_class());
93 lua_setmetatable(L
, -2);
95 lua_pushvalue(L
, -1); // duplicate our user data
96 m_self_ref
= detail::ref(L
); // pop one of them
98 m_instance_metatable
= r
->cpp_instance();
101 luabind::detail::class_rep::class_rep(lua_State
* L
, const char* name
)
102 : m_type(LUABIND_INVALID_TYPE_INFO
)
103 , m_holder_type(LUABIND_INVALID_TYPE_INFO
)
104 , m_const_holder_type(LUABIND_INVALID_TYPE_INFO
)
106 , m_const_extractor(0)
107 , m_const_converter(0)
108 , m_construct_holder(0)
109 , m_construct_const_holder(0)
111 , m_holder_alignment(1)
112 , m_class_type(lua_class
)
114 , m_const_holder_destructor(0)
115 , m_operator_cache(0)
117 #ifndef LUABIND_DONT_COPY_STRINGS
118 m_strings
.push_back(detail::dup_string(name
));
119 m_name
= m_strings
.back();
124 m_table_ref
= detail::ref(L
);
126 class_registry
* r
= class_registry::get_registry(L
);
127 assert((r
->cpp_class() != LUA_NOREF
) && "you must call luabind::open()");
129 detail::getref(L
, r
->lua_class());
130 lua_setmetatable(L
, -2);
131 lua_pushvalue(L
, -1); // duplicate our user data
132 m_self_ref
= detail::ref(L
); // pop one of them
134 m_instance_metatable
= r
->lua_instance();
137 luabind::detail::class_rep::~class_rep()
139 #ifndef LUABIND_DONT_COPY_STRINGS
140 for (std::vector
<char*>::iterator i
= m_strings
.begin();
141 i
!= m_strings
.end(); ++i
)
148 // leaves object on lua stack
149 std::pair
<void*,void*>
150 luabind::detail::class_rep::allocate(lua_State
* L
) const
152 const int overlap
= sizeof(object_rep
)&(m_holder_alignment
-1);
153 const int padding
= overlap
==0?0:m_holder_alignment
-overlap
;
154 const int size
= sizeof(object_rep
) + padding
+ m_holder_size
;
156 char* mem
= static_cast<char*>(lua_newuserdata(L
, size
));
157 char* ptr
= mem
+ sizeof(object_rep
) + padding
;
159 return std::pair
<void*,void*>(mem
,ptr
);
162 //#include <iostream>
163 // lua stack: userdata, key
164 int luabind::detail::class_rep::gettable(lua_State
* L
)
166 // if key is nil, return nil
173 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, 1));
175 // we have to ignore the first argument since this may point to
176 // a method that is not present in this class (but in a subclass)
177 const char* key
= lua_tostring(L
, 2);
179 #ifndef LUABIND_NO_ERROR_CHECKING
181 if (std::strlen(key
) != lua_strlen(L
, 2))
184 std::string
msg("luabind does not support "
185 "member names with extra nulls:\n");
186 msg
+= std::string(lua_tostring(L
, 2), lua_strlen(L
, 2));
187 lua_pushstring(L
, msg
.c_str());
194 // special case to see if this is a null-pointer
195 if (key
&& !std::strcmp(key
, "__ok"))
197 class_rep
* crep
= obj
->crep();
199 void* p
= crep
->extractor() ? crep
->extractor()(obj
->ptr())
202 lua_pushboolean(L
, p
!= 0);
206 detail::getref(L
, obj
->crep()->table_ref());
210 if (!lua_isnil(L
, -1))
212 // std::cout << "method found in lua table: " << key << "\n";
213 lua_remove(L
, -2); // more table
219 std::map<const char*, method_rep, ltstr>::iterator i = m_methods.find(key);
221 if (i != m_methods.end())
223 // the name is a method, return it
224 lua_pushlightuserdata(L, &i->second);
225 lua_pushcclosure(L, function_dispatcher, 1);
229 std::map
<const char*, callback
, ltstr
>::iterator j
= m_getters
.find(key
);
230 if (j
!= m_getters
.end())
232 // the name is a data member
233 return j
->second
.func(L
, j
->second
.pointer_offset
);
240 // called from the metamethod for __newindex
241 // the object pointer is passed on the lua stack
242 // lua stack: userdata, key, value
243 bool luabind::detail::class_rep::settable(lua_State
* L
)
245 // if the key is 'nil' fail
246 if (lua_isnil(L
, 2)) return false;
248 // we have to ignore the first argument since this may point to
249 // a method that is not present in this class (but in a subclass)
251 const char* key
= lua_tostring(L
, 2);
253 if (std::strlen(key
) == lua_strlen(L
, 2))
255 std::map
<const char*, callback
, ltstr
>::iterator j
= m_setters
.find(key
);
256 if (j
!= m_setters
.end())
258 // the name is a data member
259 #ifndef LUABIND_NO_ERROR_CHECKING
260 if (j
->second
.match(L
, 3) < 0)
262 std::string
msg("the attribute '");
266 msg
+= "' is of type: ";
267 j
->second
.sig(L
, msg
);
268 msg
+= "\nand does not match: (";
269 msg
+= stack_content_by_name(L
, 3);
271 lua_pushstring(L
, msg
.c_str());
275 j
->second
.func(L
, j
->second
.pointer_offset
);
279 if (m_getters
.find(key
) != m_getters
.end())
281 // this means that we have a getter but no
282 // setter for an attribute. We will then fail
283 // because that attribute is read-only
284 std::string
msg("the attribute '");
288 msg
+= "' is read only";
289 lua_pushstring(L
, msg
.c_str());
294 // set the attribute to the object's table
295 getref(L
, table_ref());
303 int class_rep::gettable_dispatcher(lua_State
* L
)
305 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, 1));
306 return obj
->crep()->gettable(L
);
309 // this is called as __newindex metamethod on every instance of this class
310 int luabind::detail::class_rep::settable_dispatcher(lua_State
* L
)
312 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, 1));
314 bool success
= obj
->crep()->settable(L
);
316 #ifndef LUABIND_NO_ERROR_CHECKING
320 // class_rep::settable() will leave
321 // error message on the stack in case
332 int luabind::detail::class_rep::operator_dispatcher(lua_State
* L
)
334 int id
= static_cast<int>(lua_tonumber(L
, lua_upvalueindex(1)));
338 object_rep
* operand
[2];
339 for (int i
= 0; i
< 2; ++i
)
340 operand
[i
] = detail::is_class_object(L
, i
+ 1);
342 // we cannot compare the types here, we have to compare the pointers of the class_reps
343 // since all lua-classes have the same type (LUABIND_INVALID_TYPE_INFO)
344 if (operand
[0] && operand
[1])
345 if (operand
[0]->crep() == operand
[1]->crep()) operand
[1] = 0;
347 std::vector
<operator_callback
>* overloads
[2];
348 for (int i
= 0; i
< 2; ++i
)
349 if (operand
[i
]) overloads
[i
] = &operand
[i
]->crep()->m_operators
[id
]; else overloads
[i
] = 0;
351 std::size_t num_overloads
[2];
352 for (int i
= 0; i
< 2; ++i
)
353 if (overloads
[i
]) num_overloads
[i
] = overloads
[i
]->size(); else num_overloads
[i
] = 0;
355 for (int i
= 0; i
< 2; ++i
)
356 if (operand
[i
] && operand
[i
]->crep()->get_class_type() == class_rep::lua_class
)
358 /* // if this is a lua class we have to
359 // look in its table to see if there's
360 // any overload of this operator
361 detail::getref(L, operand[i]->crep()->table_ref());
362 lua_pushstring(L, get_operator_name(id));
364 // if we have tha operator, set num_overloads to 1
365 if (lua_isfunction(L, -1)) num_overloads[i] = 1;
368 if (operand
[i
]->crep()->has_operator_in_lua(L
, id
))
369 num_overloads
[i
] = 1;
372 bool ambiguous
= false;
373 int match_index
= -1;
374 int min_match
= std::numeric_limits
<int>::max();
376 #ifdef LUABIND_NO_ERROR_CHECKING
378 if (num_overloads
[0] == 1 && num_overloads
[1] == 0)
383 else if (num_overloads
[0] == 0 && num_overloads
[1] == 1)
393 int num_params
= lua_gettop(L
);
395 // have look at the right operand.
396 // if the right operand is a class and
397 // not the same class as this, we have to
398 // try to match it's operators too
399 for (int i
= 0; i
< 2; ++i
)
401 if (num_overloads
[i
])
403 if (operand
[i
]->crep()->get_class_type() == class_rep::lua_class
)
405 // if this is a lua class
406 // and num_overloads is > 0
407 // it means that it has implemented
408 // this operator. Set match_index to
409 // 0 to signal that this operand has
410 // an overload, but leave the min_match
411 // at int-max to mark it as a last fallback
413 if (match_index
== -1) match_index
= 0;
415 else if (find_best_match(
417 , &overloads
[i
]->front()
418 , overloads
[i
]->size()
419 , sizeof(operator_callback
)
431 #ifdef LUABIND_NO_ERROR_CHECKING
437 if (match_index
== -1)
440 std::string
msg("no operator ");
441 msg
+= get_operator_symbol(id
);
442 msg
+= " matched the arguments (";
443 msg
+= stack_content_by_name(L
, 1);
444 msg
+= ")\ncandidates are:\n";
446 for (int i
= 0; i
< 2; ++i
)
450 msg
+= get_overload_signatures(
452 , overloads
[i
]->begin()
453 , overloads
[i
]->end()
454 , get_operator_symbol(id
));
458 // if num_overloads is > 0 it would mean that this is
459 // a lua class with this operator overloaded. And if
460 // that's the case, it should always match (and if an
461 // operator matches, we never come here).
462 assert(num_overloads
[i
] == 0 && "internal error");
465 lua_pushstring(L
, msg
.c_str());
472 std::string
msg("call of overloaded operator ");
473 msg
+= get_operator_symbol(id
);
475 msg
+= stack_content_by_name(L
, 1);
476 msg
+= ")' is ambiguous\nnone of the overloads have a best conversion:\n";
478 std::vector
<const overload_rep_base
*> candidates
;
480 find_exact_match(L
, &overloads
[0]->front(), overloads
[0]->size(), sizeof(operator_callback
), min_match
, num_params
, candidates
);
483 find_exact_match(L
, &overloads
[1]->front(), overloads
[1]->size(), sizeof(operator_callback
), min_match
, num_params
, candidates
);
485 msg
+= get_overload_signatures_candidates(L
, candidates
.begin(), candidates
.end(), get_operator_symbol(id
));
487 lua_pushstring(L
, msg
.c_str());
494 if (operand
[operand_id
]->crep()->get_class_type() == class_rep::lua_class
)
496 detail::getref(L
, operand
[operand_id
]->crep()->table_ref());
497 lua_pushstring(L
, get_operator_name(id
));
499 lua_insert(L
, -4); // move the function to the bottom
500 lua_pop(L
, 1); // remove the table
506 return (*overloads
[operand_id
])[match_index
].call(L
);
510 // this is called as metamethod __call on the class_rep.
511 int luabind::detail::class_rep::constructor_dispatcher(lua_State
* L
)
513 class_rep
* crep
= static_cast<class_rep
*>(lua_touserdata(L
, 1));
514 construct_rep
* rep
= &crep
->m_constructor
;
516 bool ambiguous
= false;
517 int match_index
= -1;
518 int min_match
= std::numeric_limits
<int>::max();
521 #ifdef LUABIND_NO_ERROR_CHECKING
523 if (rep
->overloads
.size() == 1)
532 int num_params
= lua_gettop(L
) - 1;
533 found
= find_best_match(L
, &rep
->overloads
.front(), rep
->overloads
.size(), sizeof(construct_rep::overload_t
), ambiguous
, min_match
, match_index
, num_params
);
535 #ifdef LUABIND_NO_ERROR_CHECKING
543 std::string
msg("no constructor of '");
545 msg
+= "' matched the arguments (";
546 msg
+= stack_content_by_name(L
, 2);
547 msg
+= ")\n candidates are:\n";
549 msg
+= get_overload_signatures(L
, rep
->overloads
.begin(), rep
->overloads
.end(), crep
->name());
551 lua_pushstring(L
, msg
.c_str());
558 std::string
msg("call of overloaded constructor '");
561 msg
+= stack_content_by_name(L
, 2);
562 msg
+= ")' is ambiguous\nnone of the overloads have a best conversion:\n";
564 std::vector
<const overload_rep_base
*> candidates
;
565 find_exact_match(L
, &rep
->overloads
.front(), rep
->overloads
.size(), sizeof(construct_rep::overload_t
), min_match
, num_params
, candidates
);
566 msg
+= get_overload_signatures_candidates(L
, candidates
.begin(), candidates
.end(), crep
->name());
568 lua_pushstring(L
, msg
.c_str());
575 #ifndef LUABIND_NO_EXCEPTIONS
582 void* object_ptr
= rep
->overloads
[match_index
].construct(L
);
587 boost::tie(obj_rep
,held
) = crep
->allocate(L
);
589 if (crep
->has_holder())
591 crep
->m_construct_holder(held
, object_ptr
);
594 new(obj_rep
) object_rep(object_ptr
, crep
, object_rep::owner
, crep
->destructor());
596 detail::getref(L
, crep
->m_instance_metatable
);
597 lua_setmetatable(L
, -2);
600 #ifndef LUABIND_NO_EXCEPTIONS
604 catch(const std::exception
& e
)
606 lua_pushstring(L
, e
.what());
610 lua_pushstring(L
, s
);
615 std::string msg
= crep
->name();
616 msg
+= "() threw an exception";
617 lua_pushstring(L
, msg
.c_str());
621 // we can only reach this line if an exception was thrown
623 return 0; // will never be reached
629 // the functions dispatcher assumes the following:
630 // there is one upvalue that points to the method_rep that this dispatcher is to call
631 // the first parameter on the lua stack is an object_rep that points to the object the
632 // call is being made on
633 int luabind::detail::class_rep::function_dispatcher(lua_State
* L
)
635 method_rep
* rep
= static_cast<method_rep
*>(lua_touserdata(L
, lua_upvalueindex(1)));
636 object_rep
* obj
= reinterpret_cast<object_rep
*>(lua_touserdata(L
, 1));
638 #ifndef LUABIND_NO_ERROR_CHECKING
640 if (is_class_object(L
, 1) == 0)
643 std::string msg
= "No self reference given as first parameter to member function '";
644 msg
+= rep
->crep
->name();
647 msg
+= "'. Have you used '.' instead of ':'?";
649 lua_pushstring(L
, msg
.c_str());
655 if (implicit_cast(obj
->crep(), rep
->crep
->type(), p
) < 0)
658 std::string msg
= "invalid self reference given to '";
659 msg
+= rep
->crep
->name();
663 lua_pushstring(L
, msg
.c_str());
670 bool ambiguous
= false;
671 int match_index
= -1;
672 int min_match
= std::numeric_limits
<int>::max();
675 #ifdef LUABIND_NO_ERROR_CHECKING
676 if (rep
->overloads().size() == 1)
684 int num_params
= lua_gettop(L
) - 1;
685 found
= find_best_match(L
, &rep
->overloads().front(), rep
->overloads().size(), sizeof(overload_rep
), ambiguous
, min_match
, match_index
, num_params
);
687 #ifdef LUABIND_NO_ERROR_CHECKING
696 std::string msg
= "no overload of '";
697 msg
+= rep
->crep
->name();
700 msg
+= "' matched the arguments (";
701 msg
+= stack_content_by_name(L
, 2);
702 msg
+= ")\ncandidates are:\n";
704 std::string function_name
;
705 function_name
+= rep
->crep
->name();
706 function_name
+= ":";
707 function_name
+= rep
->name
;
709 msg
+= get_overload_signatures(L
, rep
->overloads().begin(), rep
->overloads().end(), function_name
);
711 lua_pushstring(L
, msg
.c_str());
718 std::string msg
= "call of overloaded '";
719 msg
+= rep
->crep
->name();
723 msg
+= stack_content_by_name(L
, 2);
724 msg
+= ")' is ambiguous\nnone of the overloads have a best conversion:\n";
726 std::vector
<const overload_rep_base
*> candidates
;
727 find_exact_match(L
, &rep
->overloads().front(), rep
->overloads().size(), sizeof(overload_rep
), min_match
, num_params
, candidates
);
729 std::string function_name
;
730 function_name
+= rep
->crep
->name();
731 function_name
+= ":";
732 function_name
+= rep
->name
;
734 msg
+= get_overload_signatures_candidates(L
, candidates
.begin(), candidates
.end(), function_name
);
736 lua_pushstring(L
, msg
.c_str());
743 #ifndef LUABIND_NO_EXCEPTIONS
750 const overload_rep
& o
= rep
->overloads()[match_index
];
751 return o
.call(L
, *obj
);
753 #ifndef LUABIND_NO_EXCEPTIONS
756 catch(const std::exception
& e
)
758 lua_pushstring(L
, e
.what());
760 catch (const char* s
)
762 lua_pushstring(L
, s
);
766 std::string msg
= rep
->crep
->name();
769 msg
+= "() threw an exception";
770 lua_pushstring(L
, msg
.c_str());
772 // we can only reach this line if an exception was thrown
774 return 0; // will never be reached
780 void luabind::detail::class_rep::add_base_class(const luabind::detail::class_rep::base_info
& binfo
)
782 // If you hit this assert you are deriving from a type that is not registered
783 // in lua. That is, in the class_<> you are giving a baseclass that isn't registered.
784 // Please note that if you don't need to have access to the base class or the
785 // conversion from the derived class to the base class, you don't need
786 // to tell luabind that it derives.
787 assert(binfo
.base
&& "You cannot derive from an unregistered type");
789 class_rep
* bcrep
= binfo
.base
;
791 // import all functions from the base
792 for (std::map
<const char*, method_rep
, ltstr
>::const_iterator i
= bcrep
->m_methods
.begin();
793 i
!= bcrep
->m_methods
.end(); ++i
)
795 // If we would assume that our base class will not be garbage collected until
796 // this class is collected, we wouldn't had to copy these strings.
797 #ifndef LUABIND_DONT_COPY_STRINGS
798 m_strings
.push_back(dup_string(i
->first
));
799 method_rep
& m
= m_methods
[m_strings
.back()];
801 method_rep
& m
= m_methods
[i
->first
];
806 for (std::vector
<overload_rep
>::const_iterator j
= i
->second
.overloads().begin();
807 j
!= i
->second
.overloads().end(); ++j
)
810 o
.add_offset(binfo
.pointer_offset
);
815 // import all getters from the base
816 for (std::map
<const char*, callback
, ltstr
>::const_iterator i
= bcrep
->m_getters
.begin();
817 i
!= bcrep
->m_getters
.end(); ++i
)
819 #ifndef LUABIND_DONT_COPY_STRINGS
820 m_strings
.push_back(dup_string(i
->first
));
821 callback
& m
= m_getters
[m_strings
.back()];
823 callback
& m
= m_getters
[i
->first
];
825 m
.pointer_offset
= i
->second
.pointer_offset
+ binfo
.pointer_offset
;
826 m
.func
= i
->second
.func
;
829 // import all setters from the base
830 for (std::map
<const char*, callback
, ltstr
>::const_iterator i
= bcrep
->m_setters
.begin();
831 i
!= bcrep
->m_setters
.end(); ++i
)
833 #ifndef LUABIND_DONT_COPY_STRINGS
834 // TODO: optimize this by not copying the string if it already exists in m_setters.
835 // This goes for m_getters, m_static_constants and m_functions too. Both here
836 // in add_base() and in the add_function(), add_getter() ... functions.
837 m_strings
.push_back(dup_string(i
->first
));
838 callback
& m
= m_setters
[m_strings
.back()];
840 callback
& m
= m_setters
[i
->first
];
842 m
.pointer_offset
= i
->second
.pointer_offset
+ binfo
.pointer_offset
;
843 m
.func
= i
->second
.func
;
846 // import all static constants
847 for (std::map
<const char*, int, ltstr
>::const_iterator i
= bcrep
->m_static_constants
.begin();
848 i
!= bcrep
->m_static_constants
.end(); ++i
)
850 #ifndef LUABIND_DONT_COPY_STRINGS
851 m_strings
.push_back(dup_string(i
->first
));
852 int& v
= m_static_constants
[m_strings
.back()];
854 int& v
= m_static_constants
[i
->first
];
859 // import all operators
860 for (int i
= 0; i
< number_of_operators
; ++i
)
862 for (std::vector
<operator_callback
>::const_iterator j
= bcrep
->m_operators
[i
].begin();
863 j
!= bcrep
->m_operators
[i
].end(); ++j
)
864 m_operators
[i
].push_back(*j
);
867 // also, save the baseclass info to be used for typecasts
868 m_bases
.push_back(binfo
);
871 void luabind::detail::class_rep::add_function(const char* name, const overload_rep& o)
874 #ifdef LUABIND_DONT_COPY_STRINGS
875 detail::method_rep& method = m_methods[name];
878 m_strings.push_back(dup_string(name));
879 detail::method_rep& method = m_methods[m_strings.back()];
880 method.name = m_strings.back();
883 method.add_overload(o);
887 void luabind::detail::class_rep::add_getter(const char* name, const boost::function2<int, lua_State*, int>& g)
891 c.pointer_offset = 0;
892 #ifndef LUABIND_DONT_COPY_STRINGS
893 m_strings.push_back(dup_string(name));
894 m_getters[m_strings.back()] = c;
900 void luabind::detail::class_rep::add_setter(const char* name, const boost::function2<int, lua_State*, int>& s)
904 c.pointer_offset = 0;
905 #ifndef LUABIND_DONT_COPY_STRINGS
906 m_strings.push_back(dup_string(name));
907 m_setters[m_strings.back()] = c;
913 #ifndef LUABIND_NO_ERROR_CHECKING
914 void luabind::detail::class_rep::add_operator(lua_State*, int op_id, int(*func)(lua_State*), int(*matcher)(lua_State*), void(*sig)(lua_State*, std::string&), int arity)
916 void luabind::detail::class_rep::add_operator(lua_State*, int op_id, int(*func)(lua_State*), int(*matcher)(lua_State*), int arity)
921 o.set_match_fun(matcher);
924 #ifndef LUABIND_NO_ERROR_CHECKING
929 m_operators[op_id].push_back(o);
932 void luabind::detail::class_rep::add_static_constant(const char* name, int val)
934 #ifndef LUABIND_DONT_COPY_STRINGS
935 m_strings.push_back(dup_string(name));
936 m_static_constants[m_strings.back()] = val;
938 m_static_constants[name] = val;
942 int luabind::detail::class_rep::super_callback(lua_State
* L
)
944 int args
= lua_gettop(L
);
946 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, lua_upvalueindex(2)));
947 class_rep
* crep
= static_cast<class_rep
*>(lua_touserdata(L
, lua_upvalueindex(1)));
948 class_rep
* base
= crep
->bases()[0].base
;
950 if (base
->get_class_type() == class_rep::lua_class
)
952 if (base
->bases().empty())
954 obj
->set_flags(obj
->flags() & ~object_rep::call_super
);
956 lua_pushstring(L
, "super");
958 lua_settable(L
, LUA_GLOBALSINDEX
);
962 lua_pushstring(L
, "super");
963 lua_pushlightuserdata(L
, base
);
964 lua_pushvalue(L
, lua_upvalueindex(2));
965 lua_pushcclosure(L
, super_callback
, 2);
966 lua_settable(L
, LUA_GLOBALSINDEX
);
969 detail::getref(L
, base
->table_ref());
970 lua_pushstring(L
, "__init");
975 lua_pushvalue(L
, lua_upvalueindex(2));
978 lua_call(L
, args
+ 1, 0);
980 // TODO: instead of clearing the global variable "super"
981 // store it temporarily in the registry. maybe we should
982 // have some kind of warning if the super global is used?
983 lua_pushstring(L
, "super");
985 lua_settable(L
, LUA_GLOBALSINDEX
);
989 obj
->set_flags(obj
->flags() & ~object_rep::call_super
);
991 // we need to push some garbage at index 1 to make the construction work
992 lua_pushboolean(L
, 1);
995 construct_rep
* rep
= &base
->m_constructor
;
997 bool ambiguous
= false;
998 int match_index
= -1;
999 int min_match
= std::numeric_limits
<int>::max();
1002 #ifdef LUABIND_NO_ERROR_CHECKING
1004 if (rep
->overloads
.size() == 1)
1013 int num_params
= lua_gettop(L
) - 1;
1014 found
= find_best_match(L
, &rep
->overloads
.front(), rep
->overloads
.size(), sizeof(construct_rep::overload_t
), ambiguous
, min_match
, match_index
, num_params
);
1016 #ifdef LUABIND_NO_ERROR_CHECKING
1025 std::string msg
= "no constructor of '";
1026 msg
+= base
->m_name
;
1027 msg
+= "' matched the arguments (";
1028 msg
+= stack_content_by_name(L
, 2);
1030 lua_pushstring(L
, msg
.c_str());
1037 std::string msg
= "call of overloaded constructor '";
1038 msg
+= base
->m_name
;
1040 msg
+= stack_content_by_name(L
, 2);
1041 msg
+= ")' is ambiguous";
1042 lua_pushstring(L
, msg
.c_str());
1047 // TODO: should this be a warning or something?
1049 // since the derived class is a lua class
1050 // it may have reimplemented virtual functions
1051 // therefore, we have to instantiate the Basewrapper
1052 // if there is no basewrapper, throw a run-time error
1053 if (!rep->overloads[match_index].has_wrapped_construct())
1056 std::string msg = "Cannot derive from C++ class '";
1057 msg += base->name();
1058 msg += "'. It does not have a wrapped type";
1059 lua_pushstring(L, msg.c_str());
1066 #ifndef LUABIND_NO_EXCEPTIONS
1072 void* storage_ptr
= obj
->ptr();
1074 if (!rep
->overloads
[match_index
].has_wrapped_construct())
1076 // if the type doesn't have a wrapped type, use the ordinary constructor
1077 void* instance
= rep
->overloads
[match_index
].construct(L
);
1079 if (crep
->has_holder())
1081 crep
->m_construct_holder(storage_ptr
, instance
);
1085 obj
->set_object(instance
);
1090 // get reference to lua object
1091 lua_pushvalue(L
, lua_upvalueindex(2));
1092 detail::lua_reference ref
;
1094 void* instance
= rep
->overloads
[match_index
].construct_wrapped(L
, ref
);
1096 if (crep
->has_holder())
1098 crep
->m_construct_holder(storage_ptr
, instance
);
1102 obj
->set_object(instance
);
1105 // TODO: is the wrapped type destructed correctly?
1106 // it should, since the destructor is either the wrapped type's
1107 // destructor or the base type's destructor, depending on wether
1108 // the type has a wrapped type or not.
1109 obj
->set_destructor(base
->destructor());
1112 #ifndef LUABIND_NO_EXCEPTIONS
1115 catch(const std::exception
& e
)
1117 lua_pushstring(L
, e
.what());
1119 catch(const char* s
)
1121 lua_pushstring(L
, s
);
1125 std::string msg
= base
->m_name
;
1126 msg
+= "() threw an exception";
1127 lua_pushstring(L
, msg
.c_str());
1129 // can only be reached if an exception was thrown
1140 int luabind::detail::class_rep::lua_settable_dispatcher(lua_State
* L
)
1142 class_rep
* crep
= static_cast<class_rep
*>(lua_touserdata(L
, 1));
1143 detail::getref(L
, crep
->m_table_ref
);
1147 crep
->m_operator_cache
= 0; // invalidate cache
1152 int luabind::detail::class_rep::construct_lua_class_callback(lua_State
* L
)
1154 class_rep
* crep
= static_cast<class_rep
*>(lua_touserdata(L
, 1));
1156 int args
= lua_gettop(L
);
1158 // lua stack: crep <arguments>
1161 detail::lua_reference ref
;
1164 bool has_bases
= !crep
->bases().empty();
1168 lua_pushstring(L
, "super");
1169 lua_pushvalue(L
, 1); // crep
1172 // lua stack: crep <arguments> "super" crep
1174 // lua stack: crep <arguments>
1176 // if we have a baseclass we set the flag to say that the super has not yet been called
1177 // we will use this flag later to check that it actually was called from __init()
1178 int flags
= object_rep::lua_class
| object_rep::owner
| (has_bases
? object_rep::call_super
: 0);
1180 // void* obj_ptr = lua_newuserdata(L, sizeof(object_rep));
1184 boost::tie(obj_ptr
, held_storage
) = crep
->allocate(L
);
1185 (new(obj_ptr
) object_rep(crep
, flags
, ref
))->set_object(held_storage
);
1187 detail::getref(L
, crep
->metatable_ref());
1188 lua_setmetatable(L
, -2);
1190 // lua stack: crep <arguments> "super" crep obj_ptr
1192 // lua stack: crep <arguments> obj_ptr
1194 if (has_bases
) lua_pushvalue(L
, -1); // obj_ptr
1195 lua_replace(L
, 1); // obj_ptr
1197 // lua stack: obj_ptr <arguments> "super" crep obj_ptr
1199 // lua stack: obj_ptr <arguments>
1203 lua_pushcclosure(L
, super_callback
, 2);
1204 // lua stack: crep <arguments> "super" function
1205 lua_settable(L
, LUA_GLOBALSINDEX
);
1208 // lua stack: crep <arguments>
1210 lua_pushvalue(L
, 1);
1213 detail::getref(L
, crep
->table_ref());
1214 lua_pushstring(L
, "__init");
1215 lua_gettable(L
, -2);
1217 #ifndef LUABIND_NO_ERROR_CHECKING
1219 // TODO: should this be a run-time error?
1220 // maybe the default behavior should be to just call
1221 // the base calss' constructor. We should register
1222 // the super callback funktion as __init
1223 if (!lua_isfunction(L
, -1))
1226 std::string msg
= crep
->name();
1227 msg
+= ":__init is not defined";
1228 lua_pushstring(L
, msg
.c_str());
1235 lua_insert(L
, 2); // function first on stack
1237 // TODO: lua_call may invoke longjump! make sure we don't have any memory leaks!
1238 // we don't have any stack objects here
1239 lua_call(L
, args
, 0);
1241 #ifndef LUABIND_NO_ERROR_CHECKING
1243 object_rep
* obj
= static_cast<object_rep
*>(obj_ptr
);
1244 if (obj
->flags() & object_rep::call_super
)
1246 lua_pushstring(L
, "derived class must call super on base");
1255 // called from the metamethod for __index
1256 // obj is the object pointer
1257 int luabind::detail::class_rep::lua_class_gettable(lua_State
* L
)
1259 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, 1));
1260 class_rep
* crep
= obj
->crep();
1262 #ifndef LUABIND_NO_ERROR_CHECKING
1264 if (obj
->flags() & object_rep::call_super
)
1266 lua_pushstring(L
, "derived class must call super on base");
1272 // we have to ignore the first argument since this may point to
1273 // a method that is not present in this class (but in a subclass)
1275 // BUG: This might catch members called "__ok\0foobar"
1276 const char* key
= lua_tostring(L
, 2);
1278 if (key
&& !std::strcmp(key
, "__ok"))
1280 class_rep
* crep
= obj
->crep();
1282 void* p
= crep
->extractor() ? crep
->extractor()(obj
->ptr())
1285 lua_pushboolean(L
, p
!= 0);
1289 obj
->get_lua_table(L
);
1290 lua_pushvalue(L
, 2);
1291 lua_gettable(L
, -2);
1293 if (!lua_isnil(L
, -1))
1295 lua_remove(L
, -2); // remove table
1301 detail::getref(L
, crep
->table_ref());
1302 lua_pushvalue(L
, 2);
1303 lua_gettable(L
, -2);
1305 if (!lua_isnil(L
, -1))
1307 lua_remove(L
, -2); // more table
1313 if (lua_isnil(L
, 2))
1319 std::map
<const char*, method_rep
, ltstr
>::iterator i
= crep
->m_methods
.find(key
);
1320 if (i
!= crep
->m_methods
.end())
1322 // the name is a method, return it
1323 lua_pushlightuserdata(L
, &i
->second
);
1324 lua_pushcclosure(L
, class_rep::function_dispatcher
, 1);
1328 std::map
<const char*, class_rep::callback
, ltstr
>::iterator j
= crep
->m_getters
.find(key
);
1329 if (j
!= crep
->m_getters
.end())
1331 // the name is a data member
1332 return j
->second
.func(L
, j
->second
.pointer_offset
);
1339 // called from the metamethod for __newindex
1340 // obj is the object pointer
1341 int luabind::detail::class_rep::lua_class_settable(lua_State
* L
)
1343 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, 1));
1344 class_rep
* crep
= obj
->crep();
1346 #ifndef LUABIND_NO_ERROR_CHECKING
1348 if (obj
->flags() & object_rep::call_super
)
1350 // this block makes sure the std::string is destructed
1351 // before lua_error is called
1353 std::string msg
= "derived class '";
1354 msg
+= crep
->name();
1355 msg
+= "'must call super on base";
1356 lua_pushstring(L
, msg
.c_str());
1363 // we have to ignore the first argument since this may point to
1364 // a method that is not present in this class (but in a subclass)
1365 // BUG: This will not work with keys with extra nulls in them
1366 const char* key
= lua_tostring(L
, 2);
1369 std::map
<const char*, class_rep::callback
, ltstr
>::iterator j
= crep
->m_setters
.find(key
);
1371 // if the strlen(key) is not the true length,
1372 // it means that the member-name contains
1373 // extra nulls. luabind does not support such
1374 // names as member names. So, use the lua
1375 // table as fall-back
1376 if (j
== crep
->m_setters
.end()
1377 || std::strlen(key
) != lua_strlen(L
, 2))
1379 std::map
<const char*, class_rep::callback
, ltstr
>::iterator k
= crep
->m_getters
.find(key
);
1381 #ifndef LUABIND_NO_ERROR_CHECKING
1383 if (k
!= crep
->m_getters
.end())
1386 std::string msg
= "cannot set property '";
1387 msg
+= crep
->name();
1390 msg
+= "', because it's read only";
1391 lua_pushstring(L
, msg
.c_str());
1398 obj
->get_lua_table(L
);
1404 // the name is a data member
1405 j
->second
.func(L
, j
->second
.pointer_offset
);
1411 int luabind::detail::class_rep::static_class_gettable(lua_State
* L
)
1413 class_rep
* crep
= static_cast<class_rep
*>(lua_touserdata(L
, 1));
1415 detail::getref(L
, crep
->m_table_ref
);
1416 lua_pushvalue(L
, 2);
1417 lua_gettable(L
, -2);
1418 if (!lua_isnil(L
, -1)) return 1;
1421 const char* key
= lua_tostring(L
, 2);
1423 if (std::strlen(key
) != lua_strlen(L
, 2))
1429 std::map
<const char*, method_rep
, ltstr
>::iterator i
= crep
->m_methods
.find(key
);
1430 if (i
!= crep
->m_methods
.end())
1432 // the name is a method, return it
1433 lua_pushlightuserdata(L
, &i
->second
);
1434 lua_pushcclosure(L
, class_rep::function_dispatcher
, 1);
1438 std::map
<const char*, int, ltstr
>::const_iterator j
= crep
->m_static_constants
.find(key
);
1440 if (j
!= crep
->m_static_constants
.end())
1442 lua_pushnumber(L
, j
->second
);
1446 #ifndef LUABIND_NO_ERROR_CHECKING
1449 std::string msg
= "no static '";
1451 msg
+= "' in class '";
1452 msg
+= crep
->name();
1454 lua_pushstring(L
, msg
.c_str());
1465 bool luabind::detail::is_class_rep(lua_State
* L
, int index
)
1467 if (lua_getmetatable(L
, index
) == 0) return false;
1469 lua_pushstring(L
, "__luabind_classrep");
1470 lua_gettable(L
, -2);
1471 if (lua_toboolean(L
, -1))
1481 void luabind::detail::finalize(lua_State
* L
, class_rep
* crep
)
1483 if (crep
->get_class_type() != class_rep::lua_class
) return;
1485 // lua_pushvalue(L, -1); // copy the object ref
1486 detail::getref(L
, crep
->table_ref());
1487 lua_pushstring(L
, "__finalize");
1488 lua_gettable(L
, -2);
1491 if (lua_isnil(L
, -1))
1497 lua_pushvalue(L
, -2);
1501 for (std::vector
<class_rep::base_info
>::const_iterator
1502 i
= crep
->bases().begin(); i
!= crep
->bases().end(); ++i
)
1504 if (i
->base
) finalize(L
, i
->base
);
1508 void* luabind::detail::class_rep::convert_to(LUABIND_TYPE_INFO target_type
, const object_rep
* obj
, void* target_memory
) const
1510 // TODO: since this is a member function, we don't have to use the accesor functions for
1511 // the types and the extractor
1515 if (!(LUABIND_TYPE_INFO_EQUAL(obj
->crep()->holder_type(), target_type
))
1516 && !(LUABIND_TYPE_INFO_EQUAL(obj
->crep()->const_holder_type(), target_type
)))
1518 steps
= implicit_cast(this, target_type
, offset
);
1521 // should never be called with a type that can't be cast
1522 assert((steps
>= 0) && "internal error, please report");
1524 if (LUABIND_TYPE_INFO_EQUAL(target_type
, holder_type()))
1526 // if the type we are trying to convert to is the holder_type
1527 // it means that his crep has a holder_type (since it would have
1528 // been invalid otherwise, and T cannot be invalid). It also means
1529 // that we need no conversion, since the holder_type is what the
1530 // object points to.
1534 if (LUABIND_TYPE_INFO_EQUAL(target_type
, const_holder_type()))
1536 if (obj
->flags() & object_rep::constant
)
1538 // we are holding a constant
1543 // we are holding a non-constant, we need to convert it
1544 // to a const_holder.
1545 m_const_converter(obj
->ptr(), target_memory
);
1546 return target_memory
;
1554 // this means that we have a holder type where the
1555 // raw-pointer needs to be extracted
1556 raw_pointer
= extractor()(obj
->ptr());
1560 raw_pointer
= obj
->ptr();
1563 return static_cast<char*>(raw_pointer
) + offset
;
1566 void luabind::detail::class_rep::cache_operators(lua_State
* L
)
1568 m_operator_cache
= 0x1;
1570 for (int i
= 0; i
< number_of_operators
; ++i
)
1572 getref(L
, table_ref());
1573 lua_pushstring(L
, get_operator_name(i
));
1576 if (lua_isfunction(L
, -1)) m_operator_cache
|= 1 << (i
+ 1);
1582 bool luabind::detail::class_rep::has_operator_in_lua(lua_State
* L
, int id
)
1584 if ((m_operator_cache
& 0x1) == 0)
1587 const int mask
= 1 << (id
+ 1);
1589 return (m_operator_cache
& mask
) != 0;
1592 void luabind::detail::class_rep::add_method(lua_State
* L
, const char* name
, detail::method_rep
& m
)
1594 detail::getref(L
, table_ref());
1595 lua_pushstring(L
, name
);
1596 lua_gettable(L
, -2);
1598 if (lua_isnil(L
, -1) || !lua_iscfunction(L
, -1))
1601 lua_pushstring(L
, name
);
1602 lua_pushlightuserdata(L
, const_cast<void*>((const void*)&m
));
1603 lua_pushcclosure(L
, function_dispatcher
, 1);
1604 lua_settable(L
, -3);
1609 lua_getupvalue(L
, -1, 1);
1610 method_rep
* inherited
= static_cast<method_rep
*>(lua_touserdata(L
, -1));
1612 for (std::vector
<overload_rep
>::const_iterator i
= inherited
->overloads().begin();
1613 i
!= inherited
->overloads().end(); ++i
)
1618 detail::getref(L
, table_ref());
1619 lua_pushstring(L
, name
);
1620 lua_pushlightuserdata(L
, const_cast<void*>((const void*)&m
));
1621 lua_pushcclosure(L
, function_dispatcher
, 1);
1622 lua_settable(L
, -3);
1627 const class_rep::property_map
& luabind::detail::class_rep::properties() const