v8. save & call JavaScript function from c++

0

I am trying to save JavaScript function in my c++ app and call it from another thread.

But I got "Unhandled exception at 0x0101B5D5 in Console.exe: 0xC0000005: Access violation reading location 0x00000017."

Saving the JavaScript function (in requestCallbacks vector):

Handle<Value> HttpEngine::addListener(const Arguments& args)
{
    Locker locker;
    HandleScope scope;

    HttpEngine* pThis = UnwrapHttpEngine( args.This() );

    Persistent<Function> callback = Persistent<Function>::New(Handle<Function>::Cast(args[0]));
    pThis->requestCallbacks.push_back(*callback);

    return Boolean::New(true);
}

An attempt to call it from another thread:

void HttpEngine::emit()
{
    Locker locker;
    HandleScope scope;

    for (size_t i = 0; i < requestCallbacks.size(); i++)
    {
        Persistent<Function> func = static_cast<Function*>(requestCallbacks[i]);

        Handle<Value> args[1];
        args[0] = v8::String::New("http://google.com");
        func->Call(Context::GetCurrent()->Global(), 1, args);
    }
}

JavaScript code (where the httpEngine is my c++ object in global scope):

httpEngine.addListener(function (url) {
    print('on request: ' + url);
});
c++
v8
embedded-v8
asked on Stack Overflow May 28, 2017 by Khachatur

1 Answer

0

What's the type of requestCallbacks? Try putting the Persistent<Function> in there, not a raw Function* (using the * operator on a Persistent is both unsupported and most certainly not what you want to do).

answered on Stack Overflow May 29, 2017 by jmrk

User contributions licensed under CC BY-SA 3.0