diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index 38ff09c..d752856 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -397,6 +397,7 @@ namespace chaiscript { std::map > m_functions; std::map m_function_objects; + std::map m_boxed_functions; std::map m_global_objects; Type_Name_Map m_types; std::set m_reserved_words; @@ -684,13 +685,13 @@ namespace chaiscript // std::cout << "Getting function object: " << t_name << '\n'; chaiscript::detail::threading::shared_lock l(m_mutex); - const auto &funs = get_function_objects_int(); + const auto &funs = get_boxed_functions_int(); auto itr = funs.find(t_name); if (itr != funs.end()) { - return const_var(itr->second); + return itr->second; } else { throw std::range_error("Object not found: " + t_name); } @@ -1136,6 +1137,16 @@ namespace chaiscript return m_stack_holder->stacks.back(); } + const std::map &get_boxed_functions_int() const + { + return m_state.m_boxed_functions; + } + + std::map &get_boxed_functions_int() + { + return m_state.m_boxed_functions; + } + const std::map &get_function_objects_int() const { return m_state.m_function_objects; @@ -1276,34 +1287,39 @@ namespace chaiscript auto itr = funcs.find(t_name); auto &func_objs = get_function_objects_int(); + auto &boxed_funcs = get_boxed_functions_int(); - if (itr != funcs.end()) - { - auto &vec = itr->second; - for (const auto &func : vec) - { - if ((*t_f) == *(func)) + Proxy_Function new_func = + [&]() -> Proxy_Function { + if (itr != funcs.end()) { - throw chaiscript::exception::name_conflict_error(t_name); + auto &vec = itr->second; + for (const auto &func : vec) + { + if ((*t_f) == *(func)) + { + throw chaiscript::exception::name_conflict_error(t_name); + } + } + + vec.push_back(t_f); + std::stable_sort(vec.begin(), vec.end(), &function_less_than); + return std::make_shared(vec); + } else if (t_f->has_arithmetic_param()) { + // if the function is the only function but it also contains + // arithmetic operators, we must wrap it in a dispatch function + // to allow for automatic arithmetic type conversions + std::vector vec({t_f}); + funcs.insert(std::make_pair(t_name, vec)); + return std::make_shared(std::move(vec)); + } else { + funcs.insert(std::make_pair(t_name, std::vector{t_f})); + return t_f; } - } - - vec.push_back(t_f); - std::stable_sort(vec.begin(), vec.end(), &function_less_than); - func_objs[t_name] = std::make_shared(vec); - } else if (t_f->has_arithmetic_param()) { - // if the function is the only function but it also contains - // arithmetic operators, we must wrap it in a dispatch function - // to allow for automatic arithmetic type conversions - std::vector vec({t_f}); - funcs.insert(std::make_pair(t_name, vec)); - func_objs[t_name] = std::make_shared(std::move(vec)); - } else { - funcs.insert(std::make_pair(t_name, std::vector{t_f})); - func_objs[t_name] = t_f; - } - + }(); + boxed_funcs[t_name] = const_var(new_func); + func_objs[t_name] = std::move(new_func); } mutable chaiscript::detail::threading::shared_mutex m_mutex;