v8 extracting a global object from nodejs in C++

旧时模样 提交于 2019-12-11 07:34:20

问题


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++ ?


回答1:


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.



来源:https://stackoverflow.com/questions/46559457/v8-extracting-a-global-object-from-nodejs-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!