Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
bec1b91b7b | ||
![]() |
4b81a24a0a | ||
![]() |
cefb4d3c78 | ||
![]() |
41c1c490c8 | ||
![]() |
1e62eb4e12 | ||
![]() |
c07c2a9cc2 | ||
![]() |
46c45e8fc7 | ||
![]() |
91a3ae1f14 | ||
![]() |
328aef10d7 |
@@ -103,7 +103,7 @@ set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/description.txt"
|
||||
|
||||
set(CPACK_PACKAGE_VERSION_MAJOR 5)
|
||||
set(CPACK_PACKAGE_VERSION_MINOR 8)
|
||||
set(CPACK_PACKAGE_VERSION_PATCH 1)
|
||||
set(CPACK_PACKAGE_VERSION_PATCH 4)
|
||||
|
||||
set(CPACK_PACKAGE_EXECUTABLES "chai;ChaiScript Eval")
|
||||
set(CPACK_PACKAGE_VENDOR "ChaiScript.com")
|
||||
|
@@ -163,6 +163,28 @@ chaiscript::Boxed_Number(chai.eval("5.3 + 2.1")).get_as<int>(); // works with an
|
||||
static_cast<int>(chai.eval<double>("5.3+2.1")); // this version only works if we know that it's a double
|
||||
```
|
||||
|
||||
### Conversion Caveats
|
||||
|
||||
Conversion to `std::shared_ptr<T> &` is supported for function calls, but if you attempt to keep a reference to a `shared_ptr<>` you might invoke undefined behavior
|
||||
|
||||
```cpp
|
||||
// ok this is supported, you can register it with chaiscript engine
|
||||
void nullify_shared_ptr(std::shared_ptr<int> &t) {
|
||||
t == nullptr
|
||||
}
|
||||
```
|
||||
|
||||
```cpp
|
||||
int main()
|
||||
{
|
||||
// do some stuff and create a chaiscript instance
|
||||
std::shared_ptr<int> &ptr = chai.eval<std::shared_ptr<int> &>(somevalue);
|
||||
// DO NOT do this. Taking a non-const reference to a shared_ptr is not
|
||||
// supported and causes undefined behavior in the chaiscript engine
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Sharing Values
|
||||
|
||||
```
|
||||
|
@@ -99,7 +99,7 @@
|
||||
namespace chaiscript {
|
||||
static const int version_major = 5;
|
||||
static const int version_minor = 8;
|
||||
static const int version_patch = 1;
|
||||
static const int version_patch = 4;
|
||||
|
||||
static const char *compiler_version = CHAISCRIPT_COMPILER_VERSION;
|
||||
static const char *compiler_name = CHAISCRIPT_COMPILER_NAME;
|
||||
|
@@ -173,6 +173,21 @@ namespace chaiscript
|
||||
{
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper_Inner<std::shared_ptr<Result> &>
|
||||
{
|
||||
static_assert(!std::is_const<Result>::value, "Non-const reference to std::shared_ptr<const T> is not supported");
|
||||
|
||||
typedef Boxed_Value::Sentinel<Result> Result_Type;
|
||||
|
||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||
{
|
||||
std::shared_ptr<Result> &res = ob.get().cast<std::shared_ptr<Result> >();
|
||||
return ob.pointer_sentinel(res);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/// Cast_Helper_Inner for casting to a const std::shared_ptr<const> & type
|
||||
template<typename Result>
|
||||
struct Cast_Helper_Inner<const std::shared_ptr<const Result> > : Cast_Helper_Inner<std::shared_ptr<const Result> >
|
||||
|
@@ -231,6 +231,50 @@ namespace chaiscript
|
||||
return m_data->m_type_info.bare_equal(ti);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
struct Sentinel {
|
||||
Sentinel(std::shared_ptr<T> &ptr, Data &data)
|
||||
: m_ptr(ptr), m_data(data)
|
||||
{
|
||||
}
|
||||
|
||||
~Sentinel()
|
||||
{
|
||||
// save new pointer data
|
||||
m_data.get().m_data_ptr = m_ptr.get().get();
|
||||
m_data.get().m_const_data_ptr = m_ptr.get().get();
|
||||
}
|
||||
|
||||
Sentinel& operator=(Sentinel&&s) {
|
||||
m_ptr = std::move(s.m_ptr);
|
||||
m_data = std::move(s.m_data);
|
||||
}
|
||||
|
||||
Sentinel(Sentinel &&s)
|
||||
: m_ptr(std::move(s.m_ptr)),
|
||||
m_data(std::move(s.m_data))
|
||||
{
|
||||
}
|
||||
|
||||
operator std::shared_ptr<T>&() const
|
||||
{
|
||||
return m_ptr.get();
|
||||
}
|
||||
|
||||
Sentinel &operator=(const Sentinel &) = delete;
|
||||
Sentinel(Sentinel&) = delete;
|
||||
|
||||
std::reference_wrapper<std::shared_ptr<T>> m_ptr;
|
||||
std::reference_wrapper<Data> m_data;
|
||||
};
|
||||
|
||||
|
||||
template<typename T>
|
||||
Sentinel<T> pointer_sentinel(std::shared_ptr<T> &ptr) const
|
||||
{
|
||||
return Sentinel<T>(ptr, *(m_data.get()));
|
||||
}
|
||||
|
||||
bool is_null() const CHAISCRIPT_NOEXCEPT
|
||||
{
|
||||
return (m_data->m_data_ptr == nullptr && m_data->m_const_data_ptr == nullptr);
|
||||
|
@@ -115,6 +115,24 @@ namespace chaiscript
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
struct Handle_Return<Ret *&>
|
||||
{
|
||||
static Boxed_Value handle(Ret *p)
|
||||
{
|
||||
return Boxed_Value(p, true);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
struct Handle_Return<const Ret *&>
|
||||
{
|
||||
static Boxed_Value handle(const Ret *p)
|
||||
{
|
||||
return Boxed_Value(p, true);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
struct Handle_Return<Ret *>
|
||||
{
|
||||
|
@@ -846,7 +846,8 @@ namespace chaiscript
|
||||
plist.begin(),
|
||||
std::back_inserter(newplist),
|
||||
[](const Type_Info &ti, const Boxed_Value ¶m) -> Boxed_Value {
|
||||
if (ti.is_arithmetic() && param.get_type_info().is_arithmetic()) {
|
||||
if (ti.is_arithmetic() && param.get_type_info().is_arithmetic()
|
||||
&& param.get_type_info() != ti) {
|
||||
return Boxed_Number(param).get_as(ti).bv;
|
||||
} else {
|
||||
return param;
|
||||
@@ -854,8 +855,6 @@ namespace chaiscript
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
try {
|
||||
return (*(matching_func->second))(newplist, t_conversions);
|
||||
} catch (const exception::bad_boxed_cast &) {
|
||||
|
@@ -60,6 +60,16 @@ namespace chaiscript
|
||||
return m_type_info < ti.m_type_info;
|
||||
}
|
||||
|
||||
CHAISCRIPT_CONSTEXPR bool operator!=(const Type_Info &ti) const CHAISCRIPT_NOEXCEPT
|
||||
{
|
||||
return !(operator==(ti));
|
||||
}
|
||||
|
||||
CHAISCRIPT_CONSTEXPR bool operator!=(const std::type_info &ti) const CHAISCRIPT_NOEXCEPT
|
||||
{
|
||||
return !(operator==(ti));
|
||||
}
|
||||
|
||||
CHAISCRIPT_CONSTEXPR bool operator==(const Type_Info &ti) const CHAISCRIPT_NOEXCEPT
|
||||
{
|
||||
return ti.m_type_info == m_type_info
|
||||
@@ -162,6 +172,11 @@ namespace chaiscript
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Get_Type_Info<std::shared_ptr<T> &> : Get_Type_Info<std::shared_ptr<T>>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Get_Type_Info<const std::shared_ptr<T> &>
|
||||
{
|
||||
|
@@ -1,6 +1,16 @@
|
||||
Notes:
|
||||
=======
|
||||
Current Version: 5.8.1
|
||||
Current Version: 5.8.4
|
||||
|
||||
### Changes since 5.8.3
|
||||
* Fix case with some numeric conversions mixed with numerics that do not need conversion
|
||||
|
||||
### Changes since 5.8.2
|
||||
* Add support for reference of pointer return types
|
||||
|
||||
### Changes since 5.8.1
|
||||
* Allow casting to non-const & std::shared_ptr<T>
|
||||
|
||||
|
||||
### Changes since 5.8.0
|
||||
* Fix parsing of floats to be locale independent #250
|
||||
|
@@ -114,6 +114,16 @@ std::shared_ptr<TestBaseType> null_factory()
|
||||
return std::shared_ptr<TestBaseType>();
|
||||
}
|
||||
|
||||
void update_shared_ptr(std::shared_ptr<TestBaseType> &ptr)
|
||||
{
|
||||
ptr = std::make_shared<TestDerivedType>();
|
||||
}
|
||||
|
||||
void nullify_shared_ptr(std::shared_ptr<TestBaseType> &ptr)
|
||||
{
|
||||
ptr = nullptr;
|
||||
}
|
||||
|
||||
std::string hello_world()
|
||||
{
|
||||
return "Hello World";
|
||||
@@ -209,6 +219,10 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_mo
|
||||
m->add(chaiscript::type_conversion<const char *, std::string>());
|
||||
m->add(chaiscript::constructor<Type2 (const TestBaseType &)>(), "Type2");
|
||||
|
||||
m->add(chaiscript::fun(&update_shared_ptr), "update_shared_ptr");
|
||||
m->add(chaiscript::fun(&nullify_shared_ptr), "nullify_shared_ptr");
|
||||
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
|
@@ -54,12 +54,13 @@ bool test_type_conversion(const Boxed_Value &bv, bool expectedpass)
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
bool do_test(const Boxed_Value &bv, bool T, bool ConstT, bool TRef, bool ConstTRef, bool TPtr, bool ConstTPtr, bool TPtrConst,
|
||||
bool ConstTPtrConst, bool SharedPtrT, bool SharedConstPtrT,
|
||||
bool ConstSharedPtrT, bool ConstSharedConstPtrT, bool ConstSharedPtrTRef, bool ConstSharedPtrTConstRef,
|
||||
bool WrappedRef, bool WrappedConstRef, bool ConstWrappedRef, bool ConstWrappedConstRef,
|
||||
bool ConstWrappedRefRef, bool ConstWrappedConstRefRef, bool Number,
|
||||
bool ConstNumber, bool ConstNumberRef, bool TPtrConstRef, bool ConstTPtrConstRef)
|
||||
bool do_test(const Boxed_Value &bv,
|
||||
bool T, bool ConstT, bool TRef, bool ConstTRef, bool TPtr,
|
||||
bool ConstTPtr, bool TPtrConst, bool ConstTPtrConst, bool SharedPtrT, bool SharedConstPtrT,
|
||||
bool SharedPtrTRef, bool ConstSharedPtrT, bool ConstSharedConstPtrT, bool ConstSharedPtrTRef, bool ConstSharedPtrTConstRef,
|
||||
bool WrappedRef, bool WrappedConstRef, bool ConstWrappedRef, bool ConstWrappedConstRef, bool ConstWrappedRefRef,
|
||||
bool ConstWrappedConstRefRef, bool Number, bool ConstNumber, bool ConstNumberRef, bool TPtrConstRef,
|
||||
bool ConstTPtrConstRef)
|
||||
{
|
||||
bool passed = true;
|
||||
passed &= test_type_conversion<Type>(bv, T);
|
||||
@@ -72,8 +73,8 @@ bool do_test(const Boxed_Value &bv, bool T, bool ConstT, bool TRef, bool ConstTR
|
||||
passed &= test_type_conversion<const Type * const>(bv, ConstTPtrConst);
|
||||
passed &= test_type_conversion<std::shared_ptr<Type> >(bv, SharedPtrT);
|
||||
passed &= test_type_conversion<std::shared_ptr<const Type> >(bv, SharedConstPtrT);
|
||||
passed &= test_type_conversion<std::shared_ptr<Type> &>(bv, false);
|
||||
passed &= test_type_conversion<std::shared_ptr<const Type> &>(bv, false);
|
||||
passed &= test_type_conversion<std::shared_ptr<Type> &>(bv, SharedPtrTRef);
|
||||
//passed &= test_type_conversion<std::shared_ptr<const Type> &>(bv, false);
|
||||
passed &= test_type_conversion<const std::shared_ptr<Type> >(bv, ConstSharedPtrT);
|
||||
passed &= test_type_conversion<const std::shared_ptr<const Type> >(bv, ConstSharedConstPtrT);
|
||||
passed &= test_type_conversion<const std::shared_ptr<Type> &>(bv, ConstSharedPtrTRef);
|
||||
@@ -115,37 +116,37 @@ bool built_in_type_test(const T &initial, bool ispod)
|
||||
T i = T(initial);
|
||||
passed &= do_test<T>(var(i), true, true, true, true, true,
|
||||
true, true, true, true, true,
|
||||
true, true, true, true, true,
|
||||
true, true, true, true, true, true,
|
||||
true, true, true, true, true,
|
||||
ispod, ispod, ispod, true, true);
|
||||
|
||||
passed &= do_test<T>(const_var(i), true, true, false, true, false,
|
||||
true, false, true, false, true,
|
||||
false, true, false, true, false,
|
||||
false, false, true, false, true, false,
|
||||
true, false, true, false, true,
|
||||
ispod, ispod, ispod, false, true);
|
||||
|
||||
passed &= do_test<T>(var(&i), true, true, true, true, true,
|
||||
true, true, true, false, false,
|
||||
false, false, false, false, true,
|
||||
false, false, false, false, false, true,
|
||||
true, true, true, true, true,
|
||||
ispod, ispod, ispod, true, true);
|
||||
|
||||
passed &= do_test<T>(const_var(&i), true, true, false, true, false,
|
||||
true, false, true, false, false,
|
||||
false, false, false, false, false,
|
||||
false, false, false, false, false, false,
|
||||
true, false, true, false, true,
|
||||
ispod, ispod, ispod, false, true);
|
||||
|
||||
passed &= do_test<T>(var(std::ref(i)), true, true, true, true, true,
|
||||
true, true, true, false, false,
|
||||
false, false, false, false, true,
|
||||
false, false, false, false, false, true,
|
||||
true, true, true, true, true,
|
||||
ispod, ispod, ispod, true, true);
|
||||
|
||||
passed &= do_test<T>(var(std::cref(i)), true, true, false, true, false,
|
||||
true, false, true, false, false,
|
||||
false, false, false, false, false,
|
||||
false, false, false, false, false, false,
|
||||
true, false, true, false, true,
|
||||
ispod, ispod, ispod, false, true);
|
||||
|
||||
@@ -156,33 +157,33 @@ bool built_in_type_test(const T &initial, bool ispod)
|
||||
|
||||
passed &= do_test<T>(var(i), true, true, true, true, true,
|
||||
true, true, true, true, true,
|
||||
true, true, true, true, true,
|
||||
true, true, true, true, true, true,
|
||||
true, true, true, true, true,
|
||||
ispod, ispod, ispod, true, true);
|
||||
|
||||
// But a pointer or reference to it should be necessarily const
|
||||
passed &= do_test<T>(var(&ir), true, true, false, true, false,
|
||||
true, false, true, false, false,
|
||||
false, false, false, false, false,
|
||||
false, false, false, false, false, false,
|
||||
true, false, true, false, true,
|
||||
ispod, ispod, ispod, false, true);
|
||||
|
||||
passed &= do_test<T>(var(std::ref(ir)), true, true, false, true, false,
|
||||
true, false, true, false, false,
|
||||
false, false, false, false, false,
|
||||
false, false, false, false, false, false,
|
||||
true, false, true, false, true,
|
||||
ispod, ispod, ispod, false, true);
|
||||
|
||||
// Make sure const of const works too
|
||||
passed &= do_test<T>(const_var(&ir), true, true, false, true, false,
|
||||
true, false, true, false, false,
|
||||
false, false, false, false, false,
|
||||
false, false, false, false, false, false,
|
||||
true, false, true, false, true,
|
||||
ispod, ispod, ispod, false, true);
|
||||
|
||||
passed &= do_test<T>(const_var(std::ref(ir)), true, true, false, true, false,
|
||||
true, false, true, false, false,
|
||||
false, false, false, false, false,
|
||||
false, false, false, false, false, false,
|
||||
true, false, true, false, true,
|
||||
ispod, ispod, ispod, false, true);
|
||||
|
||||
@@ -192,14 +193,14 @@ bool built_in_type_test(const T &initial, bool ispod)
|
||||
const T*cip = &i;
|
||||
passed &= do_test<T>(var(cip), true, true, false, true, false,
|
||||
true, false, true, false, false,
|
||||
false, false, false, false, false,
|
||||
false, false, false, false, false, false,
|
||||
true, false, true, false, true,
|
||||
ispod, ispod, ispod, false, true);
|
||||
|
||||
// make sure const of const works
|
||||
passed &= do_test<T>(const_var(cip), true, true, false, true, false,
|
||||
true, false, true, false, false,
|
||||
false, false, false, false, false,
|
||||
false, false, false, false, false, false,
|
||||
true, false, true, false, true,
|
||||
ispod, ispod, ispod, false, true);
|
||||
|
||||
@@ -209,13 +210,13 @@ bool built_in_type_test(const T &initial, bool ispod)
|
||||
|
||||
passed &= do_test<T>(var(ip), true, true, true, true, true,
|
||||
true, true, true, true, true,
|
||||
true, true, true, true, true,
|
||||
true, true, true, true, true, true,
|
||||
true, true, true, true, true,
|
||||
ispod, ispod, ispod, true, true);
|
||||
|
||||
passed &= do_test<T>(const_var(ip), true, true, false, true, false,
|
||||
true, false, true, false, true,
|
||||
false, true, false, true, false,
|
||||
false, false, true, false, true, false,
|
||||
true, false, true, false, true,
|
||||
ispod, ispod, ispod, false, true);
|
||||
|
||||
@@ -224,14 +225,14 @@ bool built_in_type_test(const T &initial, bool ispod)
|
||||
|
||||
passed &= do_test<T>(var(ipc), true, true, false, true, false,
|
||||
true, false, true, false, true,
|
||||
false, true, false, true, false,
|
||||
false, false, true, false, true, false,
|
||||
true, false, true, false, true,
|
||||
ispod, ispod, ispod, false, true);
|
||||
|
||||
// const of this should be the same, making sure it compiles
|
||||
passed &= do_test<T>(const_var(ipc), true, true, false, true, false,
|
||||
true, false, true, false, true,
|
||||
false, true, false, true, false,
|
||||
false, false, true, false, true, false,
|
||||
true, false, true, false, true,
|
||||
ispod, ispod, ispod, false, true);
|
||||
|
||||
|
@@ -950,4 +950,24 @@ TEST_CASE("Parse floats with non-posix locale")
|
||||
|
||||
|
||||
|
||||
bool FindBitmap(int &ox, int &oy, long) {
|
||||
ox = 1;
|
||||
oy = 2;
|
||||
return true;
|
||||
}
|
||||
|
||||
TEST_CASE("Mismatched numeric types only convert necessary params")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
chai.add(chaiscript::fun(&FindBitmap), "FindBitmap");
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
chai.add(chaiscript::var(&x), "x");
|
||||
chai.add(chaiscript::var(&y), "y");
|
||||
chai.eval( "if ( FindBitmap ( x, y, 0) ) { print(\"found at \" + to_string(x) + \", \" + to_string(y))}" );
|
||||
CHECK(x == 1);
|
||||
CHECK(y == 2);
|
||||
|
||||
}
|
||||
|
||||
|
25
unittests/shared_ptr_update.chai
Normal file
25
unittests/shared_ptr_update.chai
Normal file
@@ -0,0 +1,25 @@
|
||||
load_module("test_module")
|
||||
|
||||
auto o := null_factory();
|
||||
|
||||
assert_true(o.is_var_null());
|
||||
|
||||
update_shared_ptr(o);
|
||||
|
||||
assert_false(o.is_var_null());
|
||||
assert_true(o.base_only_func() == -9);
|
||||
|
||||
nullify_shared_ptr(o);
|
||||
o.nullify_shared_ptr();
|
||||
|
||||
assert_true(o.is_var_null());
|
||||
|
||||
try {
|
||||
o.func();
|
||||
} catch (e) {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
assert_true(false);
|
||||
|
||||
|
Reference in New Issue
Block a user