diff --git a/shell/app/node_main.cc b/shell/app/node_main.cc index c9dcd64cdb68..861f899729ed 100644 --- a/shell/app/node_main.cc +++ b/shell/app/node_main.cc @@ -164,7 +164,7 @@ int NodeMain(int argc, char* argv[]) { exec_argc, exec_argv); CHECK_NE(nullptr, env); - // TODO(codebytere): we shouldn't have to call this - upstream? + // This needs to be called before the inspector is initialized. env->InitializeDiagnostics(); // This is needed in order to enable v8 host weakref hooks. diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc index 5c6765df348a..7987194cea49 100644 --- a/shell/common/node_bindings.cc +++ b/shell/common/node_bindings.cc @@ -219,6 +219,23 @@ void SetNodeOptions(base::Environment* env) { } } +void HostCleanupFinalizationGroupCallback( + v8::Local context, + v8::Local group) { + node::Environment* env = node::Environment::GetCurrent(context); + if (env == nullptr) { + return; + } + env->RegisterFinalizationGroupForCleanup(group); +} + +bool AllowWasmCodeGenerationCallback(v8::Local context, + v8::Local) { + v8::Local wasm_code_gen = context->GetEmbedderData( + node::ContextEmbedderIndex::kAllowWasmCodeGeneration); + return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue(); +} + } // namespace namespace electron { @@ -394,15 +411,34 @@ node::Environment* NodeBindings::CreateEnvironment( } if (browser_env_ == BrowserEnvironment::BROWSER) { - // SetAutorunMicrotasks is no longer called in node::CreateEnvironment - // so instead call it here to match expected node behavior + // This policy requires that microtask checkpoints be explicitly invoked. + // Node.js requires this. context->GetIsolate()->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit); } else { - // Node uses the deprecated SetAutorunMicrotasks(false) mode, we should - // switch to use the scoped policy to match blink's behavior. + // Match Blink's behavior by allowing microtasks invocation to be controlled + // by MicrotasksScope objects. context->GetIsolate()->SetMicrotasksPolicy(v8::MicrotasksPolicy::kScoped); } + // This needs to be called before the inspector is initialized. + env->InitializeDiagnostics(); + + // Ensure that WeakRefs work properly by specifying the callback to be called + // when FinalizationRegistries are ready to be cleaned up and require + // FinalizationGroup::Cleanup() to be called in a future task. + context->GetIsolate()->SetHostCleanupFinalizationGroupCallback( + HostCleanupFinalizationGroupCallback); + + // Set the callback to invoke to check if wasm code generation should be + // allowed. + context->GetIsolate()->SetAllowWasmCodeGenerationCallback( + AllowWasmCodeGenerationCallback); + + // Generate more detailed source positions to code objects. This results in + // better results when mapping profiling samples to script source. + v8::CpuProfiler::UseDetailedSourcePositionsForProfiling( + context->GetIsolate()); + gin_helper::Dictionary process(context->GetIsolate(), env->process_object()); process.SetReadOnly("type", process_type); process.Set("resourcesPath", resources_path);