Bad any cast after update

Updated to latest version (6ce716f5f2a83ec0a1badc97285681797b43ab8a),
now i have problems with script functions stored as std::function returning bool:

var testfun1 = fun(ScriptEvent ev){
    print("FLOOP")
    true // bad any cast
    // 1 // this works either way
}

var ev := ScriptEvent()
testfun1(ev)

// ev.func: std::function<bool (const ScriptEvent&)> func;
ev.func = testfun1
ev.func(ev)

Output:

FLOOP
FLOOP
terminate called after throwing an instance of ‘chaiscript::detail::exception::bad_any_cast’
 what():  bad any cast

as you can see - calling the function directly works returning bool or int. storing it as ev.func (which is bool returning std::function) and then calling it, works only if i return “1”, not “true”.
did something change about the way of returning bools since 6.1.0?

EDIT:
contained example to reproduce:

#include <chaiscript/chaiscript.hpp>
#include <functional>

struct Foo {
        std::function < bool (int) > func;
};

int main() {
    chaiscript::ChaiScript chai;

    chai.add(chaiscript::user_type < Foo > (),"Foo");
    chai.add(chaiscript::constructor < Foo() > (),"Foo");
    chai.add(chaiscript::constructor < Foo(const Foo &) > (),"Foo");
    chai.add(chaiscript::fun(&Foo::func),"func");

    chai.eval(R"(
            def test_bool (int n) {
                    true;
            }

            def test_int (int n) {
                    1;
            }

            var r = test_bool(123); // OK
            print("OK: "+r.to_string());

            var foo = Foo();
            foo.func = test_int;
            r = foo.func(123);      // OK
            print("OK: "+r.to_string());

            foo.func = test_bool;
            r = foo.func(123);      // FAIL:   what():  bad any cast
            print("OK: "+r.to_string());
    )");

}

g++ -I submodules/ChaiScript/include/ /tmp/test.cpp -ldl -o /tmp/test -std=c++17 -lpthread;

I think this looks like the same bug I reported about a month ago…

Can’t get ChaiScript lambda that returns bool into C++

Glen.

1 Like

Seems pretty similar - i’ve referenced this post in the issue.
Thanks for picking this up.