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