pmed / v8pp

Bind C++ functions and classes into V8 JavaScript engine
http://pmed.github.io/v8pp/
Other
901 stars 121 forks source link

If I want to export Multiple Obj Class, how to deal with v8::Isolate* #98

Closed zxbzswxr closed 5 years ago

zxbzswxr commented 5 years ago

I see the tutorial and have some puzzles. If I want to wrap many many C++ classes, when I want to export them all the classes must use the same one v8::Isolate pointer? the function like this form v8::Local init(v8::Isolate isolate){ v8::EscapableHandleScope scope(isolate); .....many many classes to be exported.......... v8pp::module m(isolate);........ } It seem All these class exporting have to bind with the same v8::Isolate pointer . what 's the exact role does the v8::Isolate pointer play during exporting these wrappered C++ classes?? If I want to use V8PP_PLUGIN_INIT(v8::Isolate isolate) { return file::init(isolate); } and I want to write a single init function for every object . It may like this V8PP_PLUGIN_INIT(v8::Isolate isolate) { initClassA(isolate); initClassB(isolate); initClassB(isolate); ........... ?? what to return ??? } how to deal with this ???

pmed commented 5 years ago

Hi,

According to V8 documentation

Isolate represents an isolated instance of the V8 engine. V8 isolates have completely separate states. Objects from one isolate must not be used in other isolates. The embedder can create multiple isolates and use them in parallel in multiple threads. An isolate can be entered by at most one thread at any given time. The Locker/Unlocker API must be used to synchronize.

So the V8 API forces to use an Isolate with each V8 value.

The result type of V8PP_PLUGIN_INIT function is v8::Local<v8::Value>, typically it's a v8::Object, that could be created from a v8::ObjectTemplate or with its wrapper v8pp::module, see file.cpp plugin example

zxbzswxr commented 5 years ago

@pmed so,Isolate is a thread associated conception? If I bind many classes to unique Isolate pointer,all these class can only be accessed in one thread at any given time in node.js? If these classes work on a server . Can it be fine if many people access these classes using distributed client terminals?