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/config.hpp>
26 #include <luabind/luabind.hpp>
27 #include <luabind/exception_handler.hpp>
29 namespace luabind
{ namespace detail
{ namespace free_functions
{
31 void function_rep::add_overload(overload_rep
const& o
)
33 m_overloads
.push_back(o
);
36 int function_dispatcher(lua_State
* L
)
38 function_rep
* rep
= static_cast<function_rep
*>(
39 lua_touserdata(L
, lua_upvalueindex(1))
42 bool ambiguous
= false;
43 int min_match
= std::numeric_limits
<int>::max();
47 #ifdef LUABIND_NO_ERROR_CHECKING
48 if (rep
->overloads().size() == 1)
55 int num_params
= lua_gettop(L
);
56 ret
= find_best_match(
58 , &rep
->overloads().front()
59 , (int)rep
->overloads().size()
60 , sizeof(overload_rep
)
66 #ifdef LUABIND_NO_ERROR_CHECKING
71 // this bock is needed to make sure the std::string is destructed
73 std::string msg
= "no match for function call '";
75 msg
+= "' with the parameters (";
76 msg
+= stack_content_by_name(L
, 1);
77 msg
+= ")\ncandidates are:\n";
79 msg
+= get_overload_signatures(
81 , rep
->overloads().begin()
82 , rep
->overloads().end()
86 lua_pushstring(L
, msg
.c_str());
94 // this bock is needed to make sure the std::string is destructed
96 std::string msg
= "call of overloaded function '";
99 msg
+= stack_content_by_name(L
, 1);
100 msg
+= ") is ambiguous\nnone of the overloads "
101 "have a best conversion:";
103 std::vector
<overload_rep_base
const*> candidates
;
106 , &rep
->overloads().front()
107 , (int)rep
->overloads().size()
108 , sizeof(overload_rep
)
114 msg
+= get_overload_signatures_candidates(
121 lua_pushstring(L
, msg
.c_str());
126 overload_rep
const& ov_rep
= rep
->overloads()[match_index
];
128 #ifndef LUABIND_NO_EXCEPTIONS
132 return ov_rep
.call(L
, ov_rep
.fun
);
133 #ifndef LUABIND_NO_EXCEPTIONS
137 detail::handle_exception_aux(L
);
139 // we can only reach this line if an exception was thrown
141 return 0; // will never be reached
145 }}} // namespace luabind::detail::free_functions