Only apply divide by zero protection to integers

Also allow arithmetic error to bubble up to the caller.
This commit is contained in:
Jason Turner
2015-01-09 20:06:04 -07:00
parent 8746a9eea5
commit 25b15a3449
3 changed files with 12 additions and 4 deletions

View File

@@ -397,7 +397,6 @@ namespace chaiscript
m->add(user_type<std::runtime_error>(), "runtime_error");
m->add(chaiscript::base_class<std::exception, std::runtime_error>());
m->add(constructor<std::runtime_error (const std::string &)>(), "runtime_error");
m->add(fun(std::function<std::string (const std::runtime_error &)>(&what)), "what");
@@ -487,7 +486,12 @@ namespace chaiscript
m->add(chaiscript::fun(&has_parse_tree), "has_parse_tree");
m->add(chaiscript::fun(&get_parse_tree), "get_parse_tree");
m->add(chaiscript::base_class<std::exception, chaiscript::exception::eval_error>());
m->add(chaiscript::user_type<chaiscript::exception::eval_error>("eval_error"));
m->add(chaiscript::base_class<std::runtime_error, chaiscript::exception::eval_error>());
m->add(chaiscript::user_type<chaiscript::exception::arithmetic_error>("arithmetic_error"));
m->add(chaiscript::base_class<std::runtime_error, chaiscript::exception::arithmetic_error>());
// chaiscript::bootstrap::standard_library::vector_type<std::vector<std::shared_ptr<chaiscript::AST_Node> > >("AST_NodeVector", m);

View File

@@ -53,7 +53,9 @@ namespace chaiscript
template<typename T>
static void check_divide_by_zero(T t)
{
if(std::is_arithmetic<T>::value) if(t==0) throw chaiscript::exception::arithmetic_error("divide by zero");
if(std::is_integral<T>::value && std::is_arithmetic<T>::value && t==0) {
throw chaiscript::exception::arithmetic_error("divide by zero");
}
}
#else
template<typename T>

View File

@@ -93,6 +93,8 @@ namespace chaiscript
// If it's an arithmetic operation we want to short circuit dispatch
try{
return Boxed_Number::do_oper(t_oper, t_lhs, t_rhs);
} catch (const chaiscript::exception::arithmetic_error &) {
throw;
} catch (...) {
throw exception::eval_error("Error with numeric operator calling: " + t_oper_string);
}