From 43def57852ece0a09a1f88721a63e0e6c2a8d6c9 Mon Sep 17 00:00:00 2001 From: ktm Date: Sun, 17 Jan 2016 00:01:51 -0500 Subject: [PATCH] add set_global, update unit test --- .../chaiscript/dispatchkit/dispatchkit.hpp | 37 +++++++++++++------ .../chaiscript/language/chaiscript_engine.hpp | 14 +++++-- unittests/global_in_script.chai | 14 +++++++ 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index aa09486..b962df9 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -535,9 +535,9 @@ namespace chaiscript chaiscript::detail::threading::unique_lock l(m_mutex); - const auto itr = m_state.m_global_objects.find(name); - if (itr == m_state.m_global_objects.end()) - { + const auto itr = m_state.m_global_objects.find(name); + if (itr == m_state.m_global_objects.end()) + { m_state.m_global_objects.insert(std::make_pair(name, obj)); return obj; } else { @@ -549,18 +549,33 @@ namespace chaiscript /// Adds a new global (non-const) shared object, between all the threads void add_global(const Boxed_Value &obj, const std::string &name) { - validate_object_name(name); + validate_object_name(name); - chaiscript::detail::threading::unique_lock l(m_mutex); + chaiscript::detail::threading::unique_lock l(m_mutex); - if (m_state.m_global_objects.find(name) != m_state.m_global_objects.end()) - { - throw chaiscript::exception::name_conflict_error(name); - } else { - m_state.m_global_objects.insert(std::make_pair(name, obj)); - } + if (m_state.m_global_objects.find(name) != m_state.m_global_objects.end()) + { + throw chaiscript::exception::name_conflict_error(name); + } else { + m_state.m_global_objects.insert(std::make_pair(name, obj)); + } } + /// Adds a new global (non-const) shared object, between all the threads + void set_global(const Boxed_Value &obj, const std::string &name) + { + validate_object_name(name); + + chaiscript::detail::threading::unique_lock l(m_mutex); + + const auto itr = m_state.m_global_objects.find(name); + if (itr != m_state.m_global_objects.end()) + { + itr->second.assign(obj); + } else { + m_state.m_global_objects.insert(std::make_pair(name, obj)); + } + } /// Adds a new scope to the stack void new_scope() diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 4764c67..09039fc 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -401,7 +401,7 @@ namespace chaiscript m_engine.add(fun([this](const Boxed_Value &t_bv, const std::string &t_name){ add_global_const(t_bv, t_name); }), "add_global_const"); m_engine.add(fun([this](const Boxed_Value &t_bv, const std::string &t_name){ add_global(t_bv, t_name); }), "add_global"); - + m_engine.add(fun([this](const Boxed_Value &t_bv, const std::string &t_name){ set_global(t_bv, t_name); }), "set_global"); } @@ -622,11 +622,17 @@ namespace chaiscript /// ChaiScript is thread-safe but provides no threading locking mechanism to the script ChaiScript &add_global(const Boxed_Value &t_bv, const std::string &t_name) { - m_engine.add_global(t_bv, t_name); - return *this; + m_engine.add_global(t_bv, t_name); + return *this; } - /// \brief Represents the current state of the ChaiScript system. State and be saved and restored + ChaiScript &set_global(const Boxed_Value &t_bv, const std::string &t_name) + { + m_engine.set_global(t_bv, t_name); + return *this; + } + + /// \brief Represents the current state of the ChaiScript system. State and be saved and restored /// \warning State object does not contain the user defined type conversions of the engine. They /// are left out due to performance considerations involved in tracking the state /// \sa ChaiScript::get_state diff --git a/unittests/global_in_script.chai b/unittests/global_in_script.chai index 951a910..ad0f7e6 100644 --- a/unittests/global_in_script.chai +++ b/unittests/global_in_script.chai @@ -8,3 +8,17 @@ def myFun() } myFun(); + + +def myFun2() +{ + assert_equal(j, 7) +} + +set_global(7, "j") + +myFun2(); + +set_global(5, "j") + +myFun(); \ No newline at end of file