-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: fix creating
Isolate
s from addons
daae938 broke addons which create their own `Isolate` instances, because enabling the shared-readonly-heap feature of V8 requires all snapshots used for different `Isolate`s to be identical. Usage of addons that do this has probably decreased quite a bit since Worker threads were introduced in Node.js, but it’s still a valid use case, and in any case the breakage was probably not intentional (although the referenced commit did require test changes because of this issue). This commit addresses this issue partially by caching the V8 snapshot parameters and ignoring ones passed in from users in `NewIsolate()` when this feature is enabled, and makes the `NodeMainInstance` snapshot-based isolate creation also re-use this code. PR-URL: #45885 Reviewed-By: James M Snell <[email protected]>
- Loading branch information
Showing
9 changed files
with
132 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
'targets': [ | ||
{ | ||
'target_name': 'binding', | ||
'sources': [ 'binding.cc' ], | ||
'includes': ['../common.gypi'], | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Flags: --no-node-snapshot | ||
'use strict'; | ||
require('../../common'); | ||
|
||
// Just re-execute the main test. | ||
require('./test'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
const common = require('../../common'); | ||
const binding = require(`./build/${common.buildType}/binding`); | ||
const assert = require('assert'); | ||
|
||
const arg = new SharedArrayBuffer(1); | ||
binding.runInSeparateIsolate('const arr = new Uint8Array(arg); arr[0] = 0x42;', arg); | ||
assert.deepStrictEqual(new Uint8Array(arg), new Uint8Array([0x42])); |