简体   繁体   中英

v8 extracting a global object from nodejs in C++

I have compiled nodejs using the "--shared" configure option. In my C++ code I have started a script in node (in its own thread) :

node::Start(argc, argv);

I have executed the following javascript to put an object into the global space :

global.someObject = new SomeObject;

I am now in C++ (on another thread) and I want to access the global "someObject". I have been thinking of using code along these lines, however the isolate vairable is NULL :

     v8::Isolate*  isolate = v8::Isolate::GetCurrent();
     v8::HandleScope scope(isolate);
     auto context = isolate->GetCurrentContext(); // no longer crashes
     auto global_obj = context->Global();
     v8::Local<v8::Value> objinfo = global_obj->GetHiddenValue(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "someObject"));

Any pointers or ideas ? How do you get a valid isolate variable from node in C++ ?

You need to run isolate->Exit() from the main thread and call isolate->Enter() from the other thread. You should also use v8::Locker and v8::Unlocker APIs. There are some examples here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM