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>
28 namespace luabind
{ namespace detail
{ namespace free_functions
{
30 void function_rep::add_overload(overload_rep
const& o
)
32 std::vector
<overload_rep
>::iterator i
= std::find(
33 m_overloads
.begin(), m_overloads
.end(), o
);
35 // if the overload already exists, overwrite the existing function
36 if (i
!= m_overloads
.end())
42 m_overloads
.push_back(o
);
46 int function_dispatcher(lua_State
* L
)
48 function_rep
* rep
= static_cast<function_rep
*>(
49 lua_touserdata(L
, lua_upvalueindex(1))
52 bool ambiguous
= false;
53 int min_match
= std::numeric_limits
<int>::max();
57 #ifdef LUABIND_NO_ERROR_CHECKING
58 if (rep
->overloads().size() == 1)
65 int num_params
= lua_gettop(L
);
66 ret
= find_best_match(
68 , &rep
->overloads().front()
69 , (int)rep
->overloads().size()
70 , sizeof(overload_rep
)
76 #ifdef LUABIND_NO_ERROR_CHECKING
81 // this bock is needed to make sure the std::string is destructed
83 std::string msg
= "no match for function call '";
85 msg
+= "' with the parameters (";
86 msg
+= stack_content_by_name(L
, 1);
87 msg
+= ")\ncandidates are:\n";
89 msg
+= get_overload_signatures(
91 , rep
->overloads().begin()
92 , rep
->overloads().end()
96 lua_pushstring(L
, msg
.c_str());
104 // this bock is needed to make sure the std::string is destructed
106 std::string msg
= "call of overloaded function '";
109 msg
+= stack_content_by_name(L
, 1);
110 msg
+= ") is ambiguous\nnone of the overloads "
111 "have a best conversion:";
113 std::vector
<overload_rep_base
const*> candidates
;
116 , &rep
->overloads().front()
117 , (int)rep
->overloads().size()
118 , sizeof(overload_rep
)
124 msg
+= get_overload_signatures_candidates(
131 lua_pushstring(L
, msg
.c_str());
136 overload_rep
const& ov_rep
= rep
->overloads()[match_index
];
138 #ifndef LUABIND_NO_EXCEPTIONS
142 return ov_rep
.call(L
, ov_rep
.fun
);
143 #ifndef LUABIND_NO_EXCEPTIONS
145 catch(const luabind::error
&)
148 catch(const std::exception
& e
)
150 lua_pushstring(L
, e
.what());
152 catch (const char* s
)
154 lua_pushstring(L
, s
);
158 std::string msg
= rep
->name();
159 msg
+= "() threw an exception";
160 lua_pushstring(L
, msg
.c_str());
162 // we can only reach this line if an exception was thrown
164 return 0; // will never be reached
168 }}} // namespace luabind::detail::free_functions