chaiscript returning user defined type in script function

181 views Asked by At

For the following code:

#include <chaiscript/chaiscript.hpp>

struct Str {};
Str cppfun() { return Str{}; }

int main()
{
    chaiscript::ChaiScript chai;
    chai.add(chaiscript::fun(&cppfun), "cppfun");
    chai.eval(R"(
        def MyFun() { return cppfun() }
        var x = MyFun()
    )");
}

It works well. However, if I modify the code as following:

#include <chaiscript/chaiscript.hpp>

struct Str {};
Str cppfun() { return Str{}; }

int main()
{
    chaiscript::ChaiScript chai;
    chai.add(chaiscript::fun(&cppfun), "cppfun");
    chai.eval(R"(
        def MyFun() { var res = cppfun(); return res }
        var x = MyFun()
    )");
}

There will be runtime error raised during it runs. And I also tried to modify the code as follows:

#include <chaiscript/chaiscript.hpp>

struct Str {};
Str cppfun() { return Str{}; }

int main()
{
    chaiscript::ChaiScript chai;
    chai.add(chaiscript::fun(&cppfun), "cppfun");
    chai.eval(R"(
        def MyFun() { var x = cppfun(); var y = x }
        MyFun()
    )");
}

And there is another type of error during running.

So I guess this is caused by the miss of assignment operation instruction for user defined structure. Are there anyone that can tell me how to fix the code and make it works? Thanks a lot!

1

There are 1 answers

0
Wei Li On

Solved this issue. Basically I need a function to implement the clone behavior. Thanks guys!