cstrings can now accept nil
[luabind.git] / src / function.cpp
blob3248a10431aa3cc6327903d872aacb19a8a25913
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())
38 *i = o;
40 else
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();
54 int match_index = -1;
55 bool ret;
57 #ifdef LUABIND_NO_ERROR_CHECKING
58 if (rep->overloads().size() == 1)
60 match_index = 0;
62 else
64 #endif
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)
71 , ambiguous
72 , min_match
73 , match_index
74 , num_params
76 #ifdef LUABIND_NO_ERROR_CHECKING
78 #else
79 if (!ret)
81 // this bock is needed to make sure the std::string is destructed
83 std::string msg = "no match for function call '";
84 msg += rep->name();
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()
93 , rep->name()
96 lua_pushstring(L, msg.c_str());
99 lua_error(L);
102 if (ambiguous)
104 // this bock is needed to make sure the std::string is destructed
106 std::string msg = "call of overloaded function '";
107 msg += rep->name();
108 msg += "(";
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;
114 find_exact_match(
116 , &rep->overloads().front()
117 , (int)rep->overloads().size()
118 , sizeof(overload_rep)
119 , min_match
120 , num_params
121 , candidates
124 msg += get_overload_signatures_candidates(
126 , candidates.begin()
127 , candidates.end()
128 , rep->name()
131 lua_pushstring(L, msg.c_str());
133 lua_error(L);
135 #endif
136 overload_rep const& ov_rep = rep->overloads()[match_index];
138 #ifndef LUABIND_NO_EXCEPTIONS
141 #endif
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);
156 catch(...)
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
163 lua_error(L);
164 return 0; // will never be reached
165 #endif
169 }}} // namespace luabind::detail::free_functions
171 #if 0
173 void luabind::detail::free_functions::function_rep::add_overload(
174 luabind::detail::free_functions::overload_rep const& o)
176 std::vector<luabind::detail::free_functions::overload_rep>::iterator i
177 = std::find(m_overloads.begin(), m_overloads.end(), o);
179 // if the overload already exists, overwrite the existing function
180 if (i != m_overloads.end())
182 *i = o;
184 else
186 m_overloads.push_back(o);
190 int luabind::detail::free_functions::function_dispatcher(lua_State* L)
192 function_rep* rep = static_cast<function_rep*>(lua_touserdata(L, lua_upvalueindex(1)));
194 bool ambiguous = false;
195 int min_match = std::numeric_limits<int>::max();
196 int match_index = -1;
197 bool ret;
199 #ifdef LUABIND_NO_ERROR_CHECKING
201 if (rep->overloads().size() == 1)
203 match_index = 0;
205 else
208 #endif
209 int num_params = lua_gettop(L);
210 ret = find_best_match(
211 L, &rep->overloads().front(), (int)rep->overloads().size()
212 , sizeof(free_functions::overload_rep), ambiguous, min_match
213 , match_index, num_params);
215 #ifdef LUABIND_NO_ERROR_CHECKING
218 #else
220 if (!ret)
222 // this bock is needed to make sure the std::string is destructed
224 std::string msg = "no match for function call '";
225 msg += rep->name();
226 msg += "' with the parameters (";
227 msg += stack_content_by_name(L, 1);
228 msg += ")\ncandidates are:\n";
230 msg += get_overload_signatures(L, rep->overloads().begin(), rep->overloads().end(), rep->name());
232 lua_pushstring(L, msg.c_str());
235 lua_error(L);
238 if (ambiguous)
240 // this bock is needed to make sure the std::string is destructed
242 std::string msg = "call of overloaded function '";
243 msg += rep->name();
244 msg += "(";
245 msg += stack_content_by_name(L, 1);
246 msg += ") is ambiguous\nnone of the overloads have a best conversion:";
248 std::vector<const overload_rep_base*> candidates;
249 find_exact_match(
250 L, &rep->overloads().front(), (int)rep->overloads().size()
251 , sizeof(free_functions::overload_rep), min_match
252 , num_params, candidates);
254 msg += get_overload_signatures_candidates(L, candidates.begin(), candidates.end(), rep->name());
256 lua_pushstring(L, msg.c_str());
258 lua_error(L);
260 #endif
261 const overload_rep& ov_rep = rep->overloads()[match_index];
263 #ifndef LUABIND_NO_EXCEPTIONS
266 #endif
267 return ov_rep.call(L, ov_rep.fun);
268 #ifndef LUABIND_NO_EXCEPTIONS
270 catch(const luabind::error& e)
273 catch(const std::exception& e)
275 lua_pushstring(L, e.what());
277 catch (const char* s)
279 lua_pushstring(L, s);
281 catch(...)
283 std::string msg = rep->name();
284 msg += "() threw an exception";
285 lua_pushstring(L, msg.c_str());
287 // we can only reach this line if an exception was thrown
288 lua_error(L);
289 return 0; // will never be reached
290 #endif
293 #endif