node icon indicating copy to clipboard operation
node copied to clipboard

Calling a javascript callback at anytime?

Open vinz243 opened this issue 11 years ago • 26 comments

I'm trying to call a callback in V8 from another point of my function. So:

void trigger(std::string event)
    {

        Isolate* isolate = Isolate::GetCurrent();

        if(events.count(event))
        {
            for(callback cb : events.find(event)->second)
                cb->Call(isolate->GetCurrentContext()->Global(), 0, {});
        }
    }

The trigger function can be called anywhere at any time in my C++ code. I tried a simple example (init v8 then call trigger), and I get:

#
# Fatal error in C:\OgreSDK\Projects\whitedrop\third_party\io.js\deps\v8\src/isolate.h, line 491
# CHECK(isolate != 0) failed
#

This is my main:

Scribe::V8Interface v8Interface = Scribe::V8Interface();
v8Interface.initialize();

for(Whitedrop::WorldEvent* event : Whitedrop::worldEvents)
{
    event->onStart();
}

vinz243 avatar Jan 25 '15 14:01 vinz243

With this:


void addWorldEvent(const FunctionCallbackInfo<Value>& args)
        {

            if (args.Length())
            {
                String::Utf8Value event(args[0]->ToString());
                if (event.length())
                {  
                    Isolate* isolate = V8Interface::getCurrent()->getIsolate();

                    Locker locker(isolate);
                    HandleScope scope(isolate);
                    Local<Function> loc = Local<Function>::Cast(args[1]);
                    Function* func = *loc;
                    callback cb = callback::New(isolate, *func);

                     if(!events.count(*event))
                     {
                    events[*event] = callbacks({ cb });
                     } 
                     else 
                    {
                        events.find(*event)->second.push_back(cb);
                     }
                }
            }

        }

I'm getting this:

  C:\OgreSDK\Projects\whitedrop\scripting\v8\src\Event.cpp(25): error C2248: 'v8::PersistentBase<T>::New' : can't access private member declared in 'v8::PersistentBase<T>' [C:\OgreSDK\Projects\whitedrop\build_win32\Whitedrop.vcxproj]
  C:\OgreSDK\Projects\whitedrop\scripting\v8\src\Event.cpp(25): error C2248: 'v8::Persistent<v8::Function,v8::NonCopyablePersistentTraits<T>>::Persistent' : can't access private member declared in class 'v8::Persistent<v8::Function,v8::NonCopyablePersistentTraits<T>>' [C:\OgreSDK\Projects\whitedrop\build_win32\Whitedrop.vcxproj]

So New is private? But what then?

vinz243 avatar Jan 29 '15 20:01 vinz243

You initialize the Persistent with:

persistent_function.Reset(isolate, callback);

When you want to retrieve it again, you call:

Local<Function> callback = Local<Function>::New(isolate, persistent_function);

bnoordhuis avatar Jan 29 '15 21:01 bnoordhuis

What is persistent_function? If I try with an empty constructor I get error C2440: '=' : can't convert 'v8::Primitive *' to 'v8::Object *volatile '

vinz243 avatar Jan 30 '15 11:01 vinz243

OK I understood. I tried with Reset, and I'm still getting that error. See commit https://github.com/whitedrop/whitedrop/commit/b212757552ec7c32a29187dc44a667a06c6e8655

vinz243 avatar Jan 30 '15 20:01 vinz243

I don't want to be that guy but I think this conversation should continue on stack overflow.

piscisaureus avatar Jan 31 '15 01:01 piscisaureus

You can check it out here. Please guys help me :)

vinz243 avatar Jan 31 '15 08:01 vinz243