|
| 1 | +#include <assert.h> |
| 2 | +#include <node.h> |
| 3 | + |
| 4 | +using node::Environment; |
| 5 | +using node::MultiIsolatePlatform; |
| 6 | +using v8::Context; |
| 7 | +using v8::FunctionCallbackInfo; |
| 8 | +using v8::HandleScope; |
| 9 | +using v8::Isolate; |
| 10 | +using v8::Local; |
| 11 | +using v8::Locker; |
| 12 | +using v8::MaybeLocal; |
| 13 | +using v8::Object; |
| 14 | +using v8::SharedArrayBuffer; |
| 15 | +using v8::String; |
| 16 | +using v8::Unlocker; |
| 17 | +using v8::Value; |
| 18 | + |
| 19 | +void RunInSeparateIsolate(const FunctionCallbackInfo<Value>& args) { |
| 20 | + Isolate* parent_isolate = args.GetIsolate(); |
| 21 | + |
| 22 | + assert(args[0]->IsString()); |
| 23 | + String::Utf8Value code(parent_isolate, args[0]); |
| 24 | + assert(*code != nullptr); |
| 25 | + assert(args[1]->IsSharedArrayBuffer()); |
| 26 | + auto arg_bs = args[1].As<SharedArrayBuffer>()->GetBackingStore(); |
| 27 | + |
| 28 | + Environment* parent_env = |
| 29 | + node::GetCurrentEnvironment(parent_isolate->GetCurrentContext()); |
| 30 | + assert(parent_env != nullptr); |
| 31 | + MultiIsolatePlatform* platform = node::GetMultiIsolatePlatform(parent_env); |
| 32 | + assert(parent_env != nullptr); |
| 33 | + |
| 34 | + { |
| 35 | + Unlocker unlocker(parent_isolate); |
| 36 | + |
| 37 | + std::vector<std::string> errors; |
| 38 | + const std::vector<std::string> empty_args; |
| 39 | + auto setup = |
| 40 | + node::CommonEnvironmentSetup::Create(platform, |
| 41 | + &errors, |
| 42 | + empty_args, |
| 43 | + empty_args, |
| 44 | + node::EnvironmentFlags::kNoFlags); |
| 45 | + assert(setup); |
| 46 | + |
| 47 | + { |
| 48 | + Locker locker(setup->isolate()); |
| 49 | + Isolate::Scope isolate_scope(setup->isolate()); |
| 50 | + HandleScope handle_scope(setup->isolate()); |
| 51 | + Context::Scope context_scope(setup->context()); |
| 52 | + auto arg = SharedArrayBuffer::New(setup->isolate(), arg_bs); |
| 53 | + auto result = setup->context()->Global()->Set( |
| 54 | + setup->context(), |
| 55 | + v8::String::NewFromUtf8Literal(setup->isolate(), "arg"), |
| 56 | + arg); |
| 57 | + assert(!result.IsNothing()); |
| 58 | + |
| 59 | + MaybeLocal<Value> loadenv_ret = |
| 60 | + node::LoadEnvironment(setup->env(), *code); |
| 61 | + assert(!loadenv_ret.IsEmpty()); |
| 62 | + |
| 63 | + (void)node::SpinEventLoop(setup->env()); |
| 64 | + |
| 65 | + node::Stop(setup->env()); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +void Initialize(Local<Object> exports, |
| 71 | + Local<Value> module, |
| 72 | + Local<Context> context) { |
| 73 | + NODE_SET_METHOD(exports, "runInSeparateIsolate", RunInSeparateIsolate); |
| 74 | +} |
| 75 | + |
| 76 | +NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize) |
0 commit comments