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

Here's a guess: the trigger function is not being called from the event loop thread, it's being called from another thread. In io.js, v8 calls must be in the event loop thread.

fivdi avatar Jan 25 '15 15:01 fivdi

Yeah, maybe, but I never used threads :/ Anyway, if you want, you can checkout the repo https://github.com/whitedrop/whitedrop/tree/feature/v8 Just ignore engine/ and parser/

vinz243 avatar Jan 25 '15 16:01 vinz243

It looks like it's an application that embeds v8 (like io.js does), but doesn't appear to have anything to do with io.js. If this is the case, this probably isn't the right issue tracker for help.

fivdi avatar Jan 25 '15 16:01 fivdi

In fact it embeds io.js which embeds v8 ;) if you look carefully, you'll see that in Interface.cpp we node::CreateEnvironnement in io.js. I use that rather than node::Start so I can expose my own API

vinz243 avatar Jan 25 '15 17:01 vinz243

About the thread id, this is what I get using some cout and GetCurrentThreadId

in main function, thread id is 29476
when inititiliasing v8, thread id is 29476
in trigger, thread id is 29476
#
# Fatal error in C:\OgreSDK\Projects\whitedrop\third_party\io.js\deps\v8\src/isolate.h, line 491
# CHECK(isolate != 0) failed
#

vinz243 avatar Jan 25 '15 18:01 vinz243

I suspect that you're missing a v8::Locker locker(isolate) or v8::Isolate::Scope isolate_scope(isolate) somewhere, or perhaps there's a call to v8::Unlocker that drops the lock again.

Rule of thumb: you need to hold the lock and have entered an isolate whenever you call v8::Isolate::GetCurrent().

It's even better to pass the isolate around explicitly instead of fetching it through v8::Isolate::GetCurrent(). The V8 team have stated their intent of dropping that function someday.

bnoordhuis avatar Jan 25 '15 19:01 bnoordhuis

Yeah, thank you. it seems someone was having the same issue. https://stackoverflow.com/questions/15596083/what-is-the-correct-way-to-use-v8locker-and-why-must-i-use-it

I need to learn more about this topic (and libuv as well)

vinz243 avatar Jan 25 '15 19:01 vinz243

Oh, also, how should I obtain the Isolate if I can't use GetCurrent?

vinz243 avatar Jan 25 '15 19:01 vinz243

Create it with Isolate::New(), then pass it around explicitly everywhere. Some classes have GetIsolate() methods that you can use (Object, FunctionCallbackInfo, etc.)

bnoordhuis avatar Jan 25 '15 20:01 bnoordhuis

Thank you very much. So a singleton pattern in my interface? And each time I use it I create a locker and it'll get destroyed at the end of the function, no? Le 25 janv. 2015 21:37, "Ben Noordhuis" [email protected] a écrit :

Create it with Isolate::New(), then pass it around explicitly everywhere. Some classes have GetIsolate() methods that you can use (Object, FunctionCallbackInfo, etc.)

— Reply to this email directly or view it on GitHub https://github.com/iojs/io.js/issues/602#issuecomment-71391941.

vinz243 avatar Jan 26 '15 07:01 vinz243

Correct. If your application is driven by a callback-style event loop, you can get away with creating a single Locker and Isolate::Scope before entering your main run function. io.js does that right before calling uv_run().

bnoordhuis avatar Jan 26 '15 10:01 bnoordhuis

Thank you I'll try soon :)

vinz243 avatar Jan 26 '15 11:01 vinz243

Thank you this is my new code:

        void trigger(std::string event)
        {

            Isolate* isolate = V8Interface::getCurrent()->getIsolate();

            Locker locker(isolate);
            HandleScope scope(isolate);

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

And now I get:

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

(i've pushed the new code to branch)

vinz243 avatar Jan 27 '15 17:01 vinz243

Oh wait http://grokbase.com/t/gg/nodejs/1375ppn1gx/looking-for-help-with-a-runtime-failure-in-a-native-extension-wrapping-a-c-library#20130705tlfa5676v4owa43sv4y4x2piwq

So callback is undefined?

vinz243 avatar Jan 27 '15 19:01 vinz243

It looks like it's because i'm calling it after. If I call it immediately then no problem

vinz243 avatar Jan 28 '15 11:01 vinz243

I don't know what type callback is but I infer it's Local<Function>. If that's the case, it's almost certainly wrong because a Local is only valid inside the HandleScope it was created in. If it needs to outlive the scope, use Persistent<Function>.

bnoordhuis avatar Jan 28 '15 14:01 bnoordhuis

Oh yes. I was just following io.js add-on tutorial. I saw it as Persistent on stackoverflow but the persistent object doesn't have a call function... So how do I call then?

The only problem with v8 is that their API change all the time so a lot of question and answers get outdated Le 28 janv. 2015 15:41, "Ben Noordhuis" [email protected] a écrit :

I don't know what type callback is but I infer it's Local. If that's the case, it's almost certainly wrong because a Local is only valid inside the HandleScope it was created in. If it needs to outlive the scope, use Persistent.

— Reply to this email directly or view it on GitHub https://github.com/iojs/io.js/issues/602#issuecomment-71844736.

vinz243 avatar Jan 28 '15 17:01 vinz243

I tried using an array of Persistent. Even with this commit I'm still getting that nasty error :( Any idea?

vinz243 avatar Jan 29 '15 14:01 vinz243

My brief understanding of V8 is that you need to create the persistent handle not cast to it, I don't see anywhere where you have a Persistent<T>::New.

This seems to have outgrown a iojs issue though to just a V8 issue, so perhaps stackoverflow would be a better medium to get help with this.

meandmycode avatar Jan 29 '15 19:01 meandmycode

I've already put it stackoverflow but no answer yet, even with the (new) bounty. I'll try tomorrow to see how can I use New

vinz243 avatar Jan 29 '15 19:01 vinz243