diff --git a/releasenotes.md b/releasenotes.md index 4300bd1..5f4f3eb 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -31,6 +31,7 @@ Current Version: 6.0.0 * Support for passing r-value references to functions * Support for containing unique_ptr * Add helpers for exposing enum classes to ChaiScript +* Allow typed ChaiScript defined functions to perform conversions on call #303 #### Improvements diff --git a/unittests/compiled_tests.cpp b/unittests/compiled_tests.cpp index 39ee06e..86227db 100644 --- a/unittests/compiled_tests.cpp +++ b/unittests/compiled_tests.cpp @@ -1129,3 +1129,60 @@ TEST_CASE("Use unique_ptr") } +class A +{ + public: + A() = default; + A(const A&) = default; + A(A &&) = default; + A &operator=(const A&) = default; + A &operator=(A&&) = default; + virtual ~A() = default; +}; + +class B : public A +{ + public: + B() = default; +}; + +TEST_CASE("Test typed chaiscript functions to perform conversions") +{ + chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser()); + + //------------------------------------------------------------------------- + + chai.add(chaiscript::user_type(), "A"); + + chai.add(chaiscript::user_type(), "B"); + chai.add(chaiscript::base_class()); + + chai.add(chaiscript::fun([](const B &) + { + }), "CppFunctWithBArg"); + + chai.add(chaiscript::fun([]() -> std::shared_ptr + { + return (std::shared_ptr(new B())); + }), "Create"); + + chai.eval(R"( + var inst = Create() // A* + + // it prints "A" + inst.type_name().print() + + // Ok it is casted using conversion + CppFunctWithBArg(inst) + + // Define a function with B as argument + def ChaiFuncWithBArg(B inst) + { + print("ok") + } + + // don't work + ChaiFuncWithBArg(inst) + )"); +} +