diff --git a/include/chaiscript/dispatchkit/boxed_value.hpp b/include/chaiscript/dispatchkit/boxed_value.hpp index 94825a5..a4e60e7 100644 --- a/include/chaiscript/dispatchkit/boxed_value.hpp +++ b/include/chaiscript/dispatchkit/boxed_value.hpp @@ -38,9 +38,10 @@ namespace chaiscript Data(const Type_Info &ti, chaiscript::detail::Any to, bool tr, - const void *t_void_ptr) + const void *t_void_ptr, + bool t_return_value) : m_type_info(ti), m_obj(std::move(to)), m_data_ptr(ti.is_const()?nullptr:const_cast(t_void_ptr)), m_const_data_ptr(t_void_ptr), - m_is_ref(tr) + m_is_ref(tr), m_return_value(t_return_value) { } @@ -51,6 +52,7 @@ namespace chaiscript m_is_ref = rhs.m_is_ref; m_data_ptr = rhs.m_data_ptr; m_const_data_ptr = rhs.m_const_data_ptr; + m_return_value = rhs.m_return_value; if (rhs.m_attrs) { @@ -74,76 +76,81 @@ namespace chaiscript const void *m_const_data_ptr; std::unique_ptr> m_attrs; bool m_is_ref; + bool m_return_value; }; struct Object_Data { - static std::shared_ptr get(Boxed_Value::Void_Type) + static std::shared_ptr get(Boxed_Value::Void_Type, bool t_return_value) { return std::make_shared( detail::Get_Type_Info::get(), chaiscript::detail::Any(), false, - nullptr) + nullptr, + t_return_value) ; } template - static std::shared_ptr get(const std::shared_ptr *obj) + static std::shared_ptr get(const std::shared_ptr *obj, bool t_return_value) { - return get(*obj); + return get(*obj, t_return_value); } template - static std::shared_ptr get(const std::shared_ptr &obj) + static std::shared_ptr get(const std::shared_ptr &obj, bool t_return_value) { return std::make_shared( detail::Get_Type_Info::get(), chaiscript::detail::Any(obj), false, - obj.get() + obj.get(), + t_return_value ); } template - static std::shared_ptr get(std::shared_ptr &&obj) + static std::shared_ptr get(std::shared_ptr &&obj, bool t_return_value) { auto ptr = obj.get(); return std::make_shared( detail::Get_Type_Info::get(), chaiscript::detail::Any(std::move(obj)), false, - ptr + ptr, + t_return_value ); } template - static std::shared_ptr get(T *t) + static std::shared_ptr get(T *t, bool t_return_value) { - return get(std::ref(*t)); + return get(std::ref(*t), t_return_value); } template - static std::shared_ptr get(const T *t) + static std::shared_ptr get(const T *t, bool t_return_value) { - return get(std::cref(*t)); + return get(std::cref(*t), t_return_value); } template - static std::shared_ptr get(std::reference_wrapper obj) + static std::shared_ptr get(std::reference_wrapper obj, bool t_return_value) { auto p = &obj.get(); return std::make_shared( detail::Get_Type_Info::get(), chaiscript::detail::Any(std::move(obj)), true, - p + p, + t_return_value ); } template - static std::shared_ptr get(T t) + static std::shared_ptr get(T t, bool t_return_value) { auto p = std::make_shared(std::move(t)); auto ptr = p.get(); @@ -151,7 +158,8 @@ namespace chaiscript detail::Get_Type_Info::get(), chaiscript::detail::Any(std::move(p)), false, - ptr + ptr, + t_return_value ); } @@ -161,7 +169,8 @@ namespace chaiscript Type_Info(), chaiscript::detail::Any(), false, - nullptr + nullptr, + false ); } @@ -171,8 +180,8 @@ namespace chaiscript /// Basic Boxed_Value constructor template::type>::value>::type> - explicit Boxed_Value(T &&t) - : m_data(Object_Data::get(std::forward(t))) + explicit Boxed_Value(T &&t, bool t_return_value = false) + : m_data(Object_Data::get(std::forward(t), t_return_value)) { } @@ -239,6 +248,16 @@ namespace chaiscript return m_data->m_is_ref; } + bool is_return_value() const CHAISCRIPT_NOEXCEPT + { + return m_data->m_return_value; + } + + void reset_return_value() const CHAISCRIPT_NOEXCEPT + { + m_data->m_return_value = false; + } + bool is_pointer() const CHAISCRIPT_NOEXCEPT { return !is_ref(); diff --git a/include/chaiscript/dispatchkit/handle_return.hpp b/include/chaiscript/dispatchkit/handle_return.hpp index 9e7c0c2..b896689 100644 --- a/include/chaiscript/dispatchkit/handle_return.hpp +++ b/include/chaiscript/dispatchkit/handle_return.hpp @@ -38,14 +38,14 @@ namespace chaiscript typename = typename std::enable_if::type>::value>::type> static Boxed_Value handle(T r) { - return Boxed_Value(std::move(r)); + return Boxed_Value(std::move(r), true); } template::type>::value>::type> static Boxed_Value handle(T &&r) { - return Boxed_Value(std::make_shared(std::forward(r))); + return Boxed_Value(std::make_shared(std::forward(r)), true); } }; @@ -54,7 +54,7 @@ namespace chaiscript { static Boxed_Value handle(Ret *p) { - return Boxed_Value(p); + return Boxed_Value(p, true); } }; @@ -63,7 +63,7 @@ namespace chaiscript { static Boxed_Value handle(const Ret *p) { - return Boxed_Value(p); + return Boxed_Value(p, true); } }; @@ -72,7 +72,7 @@ namespace chaiscript { static Boxed_Value handle(const std::shared_ptr &r) { - return Boxed_Value(r); + return Boxed_Value(r, true); } }; @@ -81,7 +81,7 @@ namespace chaiscript { static Boxed_Value handle(const std::shared_ptr &r) { - return Boxed_Value(r); + return Boxed_Value(r, true); } }; @@ -90,7 +90,7 @@ namespace chaiscript { static Boxed_Value handle(const std::shared_ptr &r) { - return Boxed_Value(r); + return Boxed_Value(r, true); } }; @@ -99,7 +99,7 @@ namespace chaiscript { static Boxed_Value handle(const Ret &r) { - return Boxed_Value(std::cref(r)); + return Boxed_Value(std::cref(r), true); } }; diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 0b1c564..b7eb0a6 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -569,7 +569,12 @@ namespace chaiscript lhs.assign(rhs); return rhs; } else { - rhs = t_ss.call_function("clone", rhs); + if (!rhs.is_return_value()) + { + rhs = t_ss.call_function("clone", rhs); + } else { + rhs.reset_return_value(); + } } } diff --git a/samples/example.cpp b/samples/example.cpp index 6fc73db..51a8324 100644 --- a/samples/example.cpp +++ b/samples/example.cpp @@ -68,7 +68,8 @@ void take_shared_ptr(const std::shared_ptr &p) int main(int /*argc*/, char * /*argv*/[]) { using namespace chaiscript; - ChaiScript chai; + ChaiScript chai(chaiscript::Std_Lib::library()); + //Create a new system object and share it with the chaiscript engine System system;