diff --git a/src/env.cc b/src/env.cc index 8fe6711632389a..a12502773a83f6 100644 --- a/src/env.cc +++ b/src/env.cc @@ -46,7 +46,7 @@ IsolateData::IsolateData(Isolate* isolate, zero_fill_field_(zero_fill_field), platform_(platform) { if (platform_ != nullptr) - platform_->RegisterIsolate(this, event_loop); + platform_->RegisterIsolate(isolate_, event_loop); options_.reset(new PerIsolateOptions(*per_process_opts->per_isolate)); @@ -99,7 +99,7 @@ IsolateData::IsolateData(Isolate* isolate, IsolateData::~IsolateData() { if (platform_ != nullptr) - platform_->UnregisterIsolate(this); + platform_->UnregisterIsolate(isolate_); } diff --git a/src/node.cc b/src/node.cc index 3597571cdffd4c..2711378d7b9918 100644 --- a/src/node.cc +++ b/src/node.cc @@ -3095,17 +3095,22 @@ bool AllowWasmCodeGenerationCallback( return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue(); } -Isolate* NewIsolate(ArrayBufferAllocator* allocator) { +Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) { Isolate::CreateParams params; params.array_buffer_allocator = allocator; #ifdef NODE_ENABLE_VTUNE_PROFILING params.code_event_handler = vTune::GetVtuneCodeEventHandler(); #endif - Isolate* isolate = Isolate::New(params); + Isolate* isolate = Isolate::Allocate(); if (isolate == nullptr) return nullptr; + // Register the isolate on the platform before the isolate gets initialized, + // so that the isolate can access the platform during initialization. + v8_platform.Platform()->RegisterIsolate(isolate, event_loop); + Isolate::Initialize(isolate, params); + isolate->AddMessageListener(OnMessage); isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException); isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit); @@ -3120,7 +3125,7 @@ inline int Start(uv_loop_t* event_loop, const std::vector& exec_args) { std::unique_ptr allocator(CreateArrayBufferAllocator(), &FreeArrayBufferAllocator); - Isolate* const isolate = NewIsolate(allocator.get()); + Isolate* const isolate = NewIsolate(allocator.get(), event_loop); if (isolate == nullptr) return 12; // Signal internal error. @@ -3158,6 +3163,7 @@ inline int Start(uv_loop_t* event_loop, } isolate->Dispose(); + v8_platform.Platform()->UnregisterIsolate(isolate); return exit_code; } diff --git a/src/node.h b/src/node.h index 90e764f386a8a9..580fb89ee08eee 100644 --- a/src/node.h +++ b/src/node.h @@ -243,13 +243,14 @@ class NODE_EXTERN MultiIsolatePlatform : public v8::Platform { virtual void CancelPendingDelayedTasks(v8::Isolate* isolate) = 0; // These will be called by the `IsolateData` creation/destruction functions. - virtual void RegisterIsolate(IsolateData* isolate_data, + virtual void RegisterIsolate(v8::Isolate* isolate, struct uv_loop_s* loop) = 0; - virtual void UnregisterIsolate(IsolateData* isolate_data) = 0; + virtual void UnregisterIsolate(v8::Isolate* isolate) = 0; }; // Creates a new isolate with Node.js-specific settings. -NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator); +NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator, + struct uv_loop_s* event_loop); // Creates a new context with Node.js-specific tweaks. NODE_EXTERN v8::Local NewContext( diff --git a/src/node_platform.cc b/src/node_platform.cc index 92e9b371c5be6f..1c237159f2d2e9 100644 --- a/src/node_platform.cc +++ b/src/node_platform.cc @@ -259,8 +259,7 @@ NodePlatform::NodePlatform(int thread_pool_size, std::make_shared(thread_pool_size); } -void NodePlatform::RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) { - Isolate* isolate = isolate_data->isolate(); +void NodePlatform::RegisterIsolate(Isolate* isolate, uv_loop_t* loop) { Mutex::ScopedLock lock(per_isolate_mutex_); std::shared_ptr existing = per_isolate_[isolate]; if (existing) { @@ -272,8 +271,7 @@ void NodePlatform::RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) { } } -void NodePlatform::UnregisterIsolate(IsolateData* isolate_data) { - Isolate* isolate = isolate_data->isolate(); +void NodePlatform::UnregisterIsolate(Isolate* isolate) { Mutex::ScopedLock lock(per_isolate_mutex_); std::shared_ptr existing = per_isolate_[isolate]; CHECK(existing); diff --git a/src/node_platform.h b/src/node_platform.h index 7297df7142242b..9b9720f63804d5 100644 --- a/src/node_platform.h +++ b/src/node_platform.h @@ -141,8 +141,8 @@ class NodePlatform : public MultiIsolatePlatform { v8::TracingController* GetTracingController() override; bool FlushForegroundTasks(v8::Isolate* isolate) override; - void RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) override; - void UnregisterIsolate(IsolateData* isolate_data) override; + void RegisterIsolate(v8::Isolate* isolate, uv_loop_t* loop) override; + void UnregisterIsolate(v8::Isolate* isolate) override; std::shared_ptr GetForegroundTaskRunner( v8::Isolate* isolate) override; diff --git a/src/node_worker.cc b/src/node_worker.cc index 80deddedde6ff4..609ae2f34795c0 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -67,9 +67,9 @@ Worker::Worker(Environment* env, Local wrap) array_buffer_allocator_.reset(CreateArrayBufferAllocator()); - isolate_ = NewIsolate(array_buffer_allocator_.get()); - CHECK_NE(isolate_, nullptr); CHECK_EQ(uv_loop_init(&loop_), 0); + isolate_ = NewIsolate(array_buffer_allocator_.get(), &loop_); + CHECK_NE(isolate_, nullptr); thread_exit_async_.reset(new uv_async_t); thread_exit_async_->data = this; @@ -265,6 +265,7 @@ void Worker::DisposeIsolate() { platform->CancelPendingDelayedTasks(isolate_); isolate_data_.reset(); + platform->UnregisterIsolate(isolate_); isolate_->Dispose(); isolate_ = nullptr; diff --git a/test/cctest/node_test_fixture.h b/test/cctest/node_test_fixture.h index 8cba5d99ba3c64..4a729be09c2d0b 100644 --- a/test/cctest/node_test_fixture.h +++ b/test/cctest/node_test_fixture.h @@ -90,10 +90,13 @@ class NodeTestFixture : public ::testing::Test { &node::FreeArrayBufferAllocator); isolate_ = NewIsolate(allocator.get()); CHECK_NE(isolate_, nullptr); + platform->RegisterIsolate(isolate_, ¤t_loop); + v8::Isolate::Initialize(isolate_, params); } virtual void TearDown() { isolate_->Dispose(); + platform->UnregisterIsolate(isolate_); isolate_ = nullptr; } };