Skip to content

Commit 508941f

Browse files
gahaastargos
authored andcommitted
src: initialize PerIsolateData eagerly
1 parent f5ba27a commit 508941f

6 files changed

Lines changed: 23 additions & 15 deletions

File tree

src/env.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ IsolateData::IsolateData(Isolate* isolate,
3737
zero_fill_field_(zero_fill_field),
3838
platform_(platform) {
3939
if (platform_ != nullptr)
40-
platform_->RegisterIsolate(this, event_loop);
40+
platform_->RegisterIsolate(isolate_, event_loop);
4141

4242
// Create string and private symbol properties as internalized one byte
4343
// strings after the platform is properly initialized.
@@ -88,7 +88,7 @@ IsolateData::IsolateData(Isolate* isolate,
8888

8989
IsolateData::~IsolateData() {
9090
if (platform_ != nullptr)
91-
platform_->UnregisterIsolate(this);
91+
platform_->UnregisterIsolate(isolate_);
9292
}
9393

9494

src/node.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4199,17 +4199,22 @@ bool AllowWasmCodeGenerationCallback(
41994199
return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue();
42004200
}
42014201

4202-
Isolate* NewIsolate(ArrayBufferAllocator* allocator) {
4202+
Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
42034203
Isolate::CreateParams params;
42044204
params.array_buffer_allocator = allocator;
42054205
#ifdef NODE_ENABLE_VTUNE_PROFILING
42064206
params.code_event_handler = vTune::GetVtuneCodeEventHandler();
42074207
#endif
42084208

4209-
Isolate* isolate = Isolate::New(params);
4209+
Isolate* isolate = Isolate::Allocate();
42104210
if (isolate == nullptr)
42114211
return nullptr;
42124212

4213+
// Register the isolate on the platform before the isolate gets initialized,
4214+
// so that the isolate can access the platform during initialization.
4215+
v8_platform.Platform()->RegisterIsolate(isolate, event_loop);
4216+
Isolate::Initialize(isolate, params);
4217+
42134218
isolate->AddMessageListener(OnMessage);
42144219
isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
42154220
isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
@@ -4224,7 +4229,7 @@ inline int Start(uv_loop_t* event_loop,
42244229
int exec_argc, const char* const* exec_argv) {
42254230
std::unique_ptr<ArrayBufferAllocator, decltype(&FreeArrayBufferAllocator)>
42264231
allocator(CreateArrayBufferAllocator(), &FreeArrayBufferAllocator);
4227-
Isolate* const isolate = NewIsolate(allocator.get());
4232+
Isolate* const isolate = NewIsolate(allocator.get(), event_loop);
42284233
if (isolate == nullptr)
42294234
return 12; // Signal internal error.
42304235

@@ -4260,6 +4265,7 @@ inline int Start(uv_loop_t* event_loop,
42604265
}
42614266

42624267
isolate->Dispose();
4268+
v8_platform.Platform()->UnregisterIsolate(isolate);
42634269

42644270
return exit_code;
42654271
}

src/node.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,14 @@ class MultiIsolatePlatform : public v8::Platform {
229229
virtual void CancelPendingDelayedTasks(v8::Isolate* isolate) = 0;
230230

231231
// These will be called by the `IsolateData` creation/destruction functions.
232-
virtual void RegisterIsolate(IsolateData* isolate_data,
232+
virtual void RegisterIsolate(v8::Isolate* isolate,
233233
struct uv_loop_s* loop) = 0;
234-
virtual void UnregisterIsolate(IsolateData* isolate_data) = 0;
234+
virtual void UnregisterIsolate(v8::Isolate* isolate) = 0;
235235
};
236236

237237
// Creates a new isolate with Node.js-specific settings.
238-
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator);
238+
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator,
239+
struct uv_loop_s* event_loop);
239240

240241
// Creates a new context with Node.js-specific tweaks. Currently, it removes
241242
// the `v8BreakIterator` property from the global `Intl` object if present.

src/node_platform.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ NodePlatform::NodePlatform(int thread_pool_size,
138138
std::make_shared<WorkerThreadsTaskRunner>(thread_pool_size);
139139
}
140140

141-
void NodePlatform::RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) {
142-
Isolate* isolate = isolate_data->isolate();
141+
void NodePlatform::RegisterIsolate(Isolate* isolate, uv_loop_t* loop) {
143142
Mutex::ScopedLock lock(per_isolate_mutex_);
144143
std::shared_ptr<PerIsolatePlatformData> existing = per_isolate_[isolate];
145144
if (existing) {
@@ -151,8 +150,7 @@ void NodePlatform::RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) {
151150
}
152151
}
153152

154-
void NodePlatform::UnregisterIsolate(IsolateData* isolate_data) {
155-
Isolate* isolate = isolate_data->isolate();
153+
void NodePlatform::UnregisterIsolate(Isolate* isolate) {
156154
Mutex::ScopedLock lock(per_isolate_mutex_);
157155
std::shared_ptr<PerIsolatePlatformData> existing = per_isolate_[isolate];
158156
CHECK(existing);

src/node_platform.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ class NodePlatform : public MultiIsolatePlatform {
139139
// flushing.
140140
bool FlushForegroundTasks(v8::Isolate* isolate);
141141

142-
void RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) override;
143-
void UnregisterIsolate(IsolateData* isolate_data) override;
142+
void RegisterIsolate(v8::Isolate* isolate, uv_loop_t* loop) override;
143+
void UnregisterIsolate(v8::Isolate* isolate) override;
144144

145145
std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
146146
v8::Isolate* isolate) override;

test/cctest/node_test_fixture.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,15 @@ class NodeTestFixture : public ::testing::Test {
8585
}
8686

8787
virtual void SetUp() {
88-
isolate_ = v8::Isolate::New(params);
88+
isolate_ = v8::Isolate::Allocate();
8989
CHECK_NE(isolate_, nullptr);
90+
platform->RegisterIsolate(isolate_, &current_loop);
91+
v8::Isolate::Initialize(isolate_, params);
9092
}
9193

9294
virtual void TearDown() {
9395
isolate_->Dispose();
96+
platform->UnregisterIsolate(isolate_);
9497
isolate_ = nullptr;
9598
}
9699
};

0 commit comments

Comments
 (0)