Fix broken type conversion call implementation
- We need to properly order the function so that the one with the least number if type differences is the one that is tried first.
This commit is contained in:
@@ -314,7 +314,7 @@ namespace chaiscript
|
|||||||
protected:
|
protected:
|
||||||
virtual Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
|
virtual Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
|
||||||
{
|
{
|
||||||
return dispatch::dispatch(m_funcs.cbegin(), m_funcs.cend(), params, t_conversions);
|
return dispatch::dispatch(m_funcs, params, t_conversions);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -775,9 +775,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
Boxed_Value call_function(const std::string &t_name, const std::vector<Boxed_Value> ¶ms) const
|
Boxed_Value call_function(const std::string &t_name, const std::vector<Boxed_Value> ¶ms) const
|
||||||
{
|
{
|
||||||
std::vector<Proxy_Function> functions = get_function(t_name);
|
return dispatch::dispatch(get_function(t_name), params, m_conversions);
|
||||||
|
|
||||||
return dispatch::dispatch(functions.begin(), functions.end(), params, m_conversions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Boxed_Value call_function(const std::string &t_name) const
|
Boxed_Value call_function(const std::string &t_name) const
|
||||||
|
@@ -712,17 +712,42 @@ namespace chaiscript
|
|||||||
* each function against the set of parameters, in order, until a matching
|
* each function against the set of parameters, in order, until a matching
|
||||||
* function is found or throw dispatch_error if no matching function is found
|
* function is found or throw dispatch_error if no matching function is found
|
||||||
*/
|
*/
|
||||||
template<typename InItr>
|
template<typename Funcs>
|
||||||
Boxed_Value dispatch(InItr begin, const InItr &end,
|
Boxed_Value dispatch(const Funcs &funcs,
|
||||||
const std::vector<Boxed_Value> &plist, const Type_Conversions &t_conversions)
|
const std::vector<Boxed_Value> &plist, const Type_Conversions &t_conversions)
|
||||||
{
|
{
|
||||||
InItr orig(begin);
|
|
||||||
while (begin != end)
|
std::multimap<int, const Proxy_Function_Base *> ordered_funcs;
|
||||||
|
|
||||||
|
for (const auto &func : funcs)
|
||||||
|
{
|
||||||
|
int numdiffs = 0;
|
||||||
|
const auto arity = func->get_arity();
|
||||||
|
|
||||||
|
if (arity == -1)
|
||||||
|
{
|
||||||
|
numdiffs = plist.size();
|
||||||
|
} else if (arity == plist.size()) {
|
||||||
|
for (size_t i = 0; i < plist.size(); ++i)
|
||||||
|
{
|
||||||
|
if (!func->get_param_types()[i+1].bare_equal(plist[i].get_type_info()))
|
||||||
|
{
|
||||||
|
++numdiffs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ordered_funcs.insert(std::make_pair(numdiffs, func.get()));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto &func : ordered_funcs )
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if ((*begin)->filter(plist, t_conversions))
|
if (func.second->filter(plist, t_conversions))
|
||||||
{
|
{
|
||||||
return (*(*begin))(plist, t_conversions);
|
return (*(func.second))(plist, t_conversions);
|
||||||
}
|
}
|
||||||
} catch (const exception::bad_boxed_cast &) {
|
} catch (const exception::bad_boxed_cast &) {
|
||||||
//parameter failed to cast, try again
|
//parameter failed to cast, try again
|
||||||
@@ -732,22 +757,9 @@ namespace chaiscript
|
|||||||
//guard failed to allow the function to execute,
|
//guard failed to allow the function to execute,
|
||||||
//try again
|
//try again
|
||||||
}
|
}
|
||||||
++begin;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return detail::dispatch_with_conversions(orig, end, plist, t_conversions);
|
return detail::dispatch_with_conversions(funcs.cbegin(), funcs.cend(), plist, t_conversions);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Take a vector of functions and a vector of parameters. Attempt to execute
|
|
||||||
* each function against the set of parameters, in order, until a matching
|
|
||||||
* function is found or throw dispatch_error if no matching function is found
|
|
||||||
*/
|
|
||||||
template<typename Funcs>
|
|
||||||
Boxed_Value dispatch(const Funcs &funcs,
|
|
||||||
const std::vector<Boxed_Value> &plist, const Type_Conversions &t_conversions)
|
|
||||||
{
|
|
||||||
return dispatch::dispatch(funcs.begin(), funcs.end(), plist, t_conversions);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user