Search code examples
c++castingv8

cast from void * to Local<Function>


I'm integrating with some code that uses a void * for a context object (in this case a v8 Function) which I wish to cast back to a Function, but this is causing a compile error. How would you case this?

void callback(void* context) {
    Local<Function> func = static_cast<Local<Function> *>(context);
    func->Call(Context::GetCurrent()->Global(), 1, 0);
}

Here is the code that register's the callback. Note that the fn gets passed into "callback" as a void *.

Persistent<Function> fn = Persistent<Function>::New(Handle<Function>::Cast(args[0]));
registerEvent(&callback, /* context*/ &fn);

Solution

  • In order for this to work you will need to cast context and assign it to a pointer value as such:

    Local<Function> *func = static_cast<Local<Function> *>(context);