Use compute_score() when invoking constructors.
[luabind.git] / src / function.cpp
blob9379ef1e3eaca6489874bc9905de7561383f1f37
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();
44 int match_index = -1;
45 bool ret;
47 #ifdef LUABIND_NO_ERROR_CHECKING
48 if (rep->overloads().size() == 1)
50 match_index = 0;
52 else
54 #endif
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)
61 , ambiguous
62 , min_match
63 , match_index
64 , num_params
66 #ifdef LUABIND_NO_ERROR_CHECKING
68 #else
69 if (!ret)
71 // this bock is needed to make sure the std::string is destructed
73 std::string msg = "no match for function call '";
74 msg += rep->name();
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()
83 , rep->name()
86 lua_pushstring(L, msg.c_str());
89 lua_error(L);
92 if (ambiguous)
94 // this bock is needed to make sure the std::string is destructed
96 std::string msg = "call of overloaded function '";
97 msg += rep->name();
98 msg += "(";
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;
104 find_exact_match(
106 , &rep->overloads().front()
107 , (int)rep->overloads().size()
108 , sizeof(overload_rep)
109 , min_match
110 , num_params
111 , candidates
114 msg += get_overload_signatures_candidates(
116 , candidates.begin()
117 , candidates.end()
118 , rep->name()
121 lua_pushstring(L, msg.c_str());
123 lua_error(L);
125 #endif
126 overload_rep const& ov_rep = rep->overloads()[match_index];
128 #ifndef LUABIND_NO_EXCEPTIONS
131 #endif
132 return ov_rep.call(L, ov_rep.fun);
133 #ifndef LUABIND_NO_EXCEPTIONS
135 catch (...)
137 detail::handle_exception_aux(L);
139 // we can only reach this line if an exception was thrown
140 lua_error(L);
141 return 0; // will never be reached
142 #endif
145 }}} // namespace luabind::detail::free_functions