ftk / quickjspp

QuickJS C++ wrapper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Returning Javascript exception from std::function wrapper terminates program.

Omnomios opened this issue · comments

When there's a Javascript exception thrown in a function, the program terminates when trying to create the qjs::Value.

Example:

    try
    {
        qjs::Value function = context.eval("() => { let a = b; };", "<test>");
        auto native = function.as<std::function<qjs::Value()>>();
        qjs::Value result = native();
    }
    catch(exception)
    {
        auto exc = context.getException();
        std::cerr << (exc.isError() ? "Error: " : "Throw: ") << (std::string)exc << std::endl;
        if((bool)exc["stack"])
            std::cerr << (std::string)exc["stack"] << std::endl;

        js_std_free_handlers(rt);
        return 1;
    }

Expected:

Error: ReferenceError: b is not defined
    at <anonymous> (<test>)

Actual:

terminate called after throwing an instance of 'qjs::exception'

Work around:
If you remove the JS_IsException check from the qjs::Value constructor (quickjspp.hpp:811) and instead handle it after it's constructed it works as expected.

    try
    {
        qjs::Value function = context.eval("() => { let a = b; };", "<test>");
        auto native = function.as<std::function<qjs::Value()>>();
        qjs::Value result = native();

        if(JS_IsException(result.v)) throw exception{};
    }
    catch(exception)