Update method_missing support to reduce exceptions

This commit is contained in:
Jason Turner
2015-04-22 12:15:15 -06:00
parent f3943f215f
commit a542ec01f6

View File

@@ -831,31 +831,46 @@ namespace chaiscript
if (is_attribute_call(funs, params, t_has_params)) { if (is_attribute_call(funs, params, t_has_params)) {
return do_attribute_call(1, params, funs, m_conversions); return do_attribute_call(1, params, funs, m_conversions);
} else { } else {
try { std::exception_ptr except;
return dispatch::dispatch(funs, params, m_conversions);
} catch(chaiscript::exception::dispatch_error&) {
const auto functions = get_function("method_missing");
const bool is_no_param = [&]()->bool{ if (!funs.empty()) {
for (const auto &f : functions) { try {
if (f->get_arity() != 2) { return dispatch::dispatch(funs, params, m_conversions);
return false; } catch(chaiscript::exception::dispatch_error&) {
} except = std::current_exception();
} }
return true; }
}();
if (!functions.empty()) { // If we get here we know that either there was no method with that name,
std::vector<Boxed_Value> tmp_params(params); // or there was no matching method
tmp_params.insert(tmp_params.begin() + 1, var(t_name));
if (is_no_param) { const auto functions = get_function("method_missing");
return do_attribute_call(2, tmp_params, functions, m_conversions);
} else { const bool is_no_param = [&]()->bool{
return dispatch::dispatch(functions, tmp_params, m_conversions); for (const auto &f : functions) {
if (f->get_arity() != 2) {
return false;
} }
} }
return true;
}();
throw; if (!functions.empty()) {
std::vector<Boxed_Value> tmp_params(params);
tmp_params.insert(tmp_params.begin() + 1, var(t_name));
if (is_no_param) {
return do_attribute_call(2, tmp_params, functions, m_conversions);
} else {
return dispatch::dispatch(functions, tmp_params, m_conversions);
}
}
// If we get all the way down here we know there was no "method_missing"
// method at all.
if (except) {
std::rethrow_exception(except);
} else {
throw chaiscript::exception::dispatch_error(params, std::vector<Const_Proxy_Function>(funs.begin(), funs.end()));
} }
} }
} }