From ceb62e5be828ef08db9aecfda2100b94ea452ba9 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Wed, 10 Aug 2022 09:35:43 +0000 Subject: [PATCH] src: remove usage on ScriptCompiler::CompileFunctionInContext MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit V8 APIs like HostImportModuleDynamicallyCallback and ScriptCompiler::CompileFunction is moving away from ScriptOrModule. Replaces ScriptCompiler::CompileFunctionInContext with ScriptCompiler::CompileFunction to remove the usages on the optional out param ScriptOrModule. PR-URL: https://github.com/nodejs/node/pull/44198 Fixes: https://github.com/nodejs/node-v8/issues/214 Reviewed-By: Michaƫl Zasso Reviewed-By: Joyee Cheung --- src/node_builtins.cc | 22 +++++++++++----------- src/node_contextify.cc | 27 ++++++++++++++------------- src/node_contextify.h | 4 ++-- src/node_snapshotable.cc | 14 +++++++------- 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/node_builtins.cc b/src/node_builtins.cc index 150fb0064851c8..93ae8d13a336a7 100644 --- a/src/node_builtins.cc +++ b/src/node_builtins.cc @@ -243,9 +243,9 @@ MaybeLocal BuiltinLoader::LookupAndCompileInternal( ScriptCompiler::CachedData* cached_data = nullptr; { // Note: The lock here should not extend into the - // `CompileFunctionInContext()` call below, because this function may - // recurse if there is a syntax error during bootstrap (because the fatal - // exception handler is invoked, which may load built-in modules). + // `CompileFunction()` call below, because this function may recurse if + // there is a syntax error during bootstrap (because the fatal exception + // handler is invoked, which may load built-in modules). Mutex::ScopedLock lock(code_cache_mutex_); auto cache_it = code_cache_.find(id); if (cache_it != code_cache_.end()) { @@ -267,20 +267,20 @@ MaybeLocal BuiltinLoader::LookupAndCompileInternal( has_cache ? "with" : "without"); MaybeLocal maybe_fun = - ScriptCompiler::CompileFunctionInContext(context, - &script_source, - parameters->size(), - parameters->data(), - 0, - nullptr, - options); + ScriptCompiler::CompileFunction(context, + &script_source, + parameters->size(), + parameters->data(), + 0, + nullptr, + options); // This could fail when there are early errors in the built-in modules, // e.g. the syntax errors Local fun; if (!maybe_fun.ToLocal(&fun)) { // In the case of early errors, v8 is already capable of - // decorating the stack for us - note that we use CompileFunctionInContext + // decorating the stack for us - note that we use CompileFunction // so there is no need to worry about wrappers. return MaybeLocal(); } diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 75c0d45b4c8aeb..8d3a580bf0ddb1 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -70,7 +70,6 @@ using v8::PropertyHandlerFlags; using v8::Script; using v8::ScriptCompiler; using v8::ScriptOrigin; -using v8::ScriptOrModule; using v8::String; using v8::Uint32; using v8::UnboundScript; @@ -1126,11 +1125,15 @@ void ContextifyContext::CompileFunction( } } - Local script; - MaybeLocal maybe_fn = ScriptCompiler::CompileFunctionInContext( - parsing_context, &source, params.size(), params.data(), - context_extensions.size(), context_extensions.data(), options, - v8::ScriptCompiler::NoCacheReason::kNoCacheNoReason, &script); + MaybeLocal maybe_fn = ScriptCompiler::CompileFunction( + parsing_context, + &source, + params.size(), + params.data(), + context_extensions.size(), + context_extensions.data(), + options, + v8::ScriptCompiler::NoCacheReason::kNoCacheNoReason); Local fn; if (!maybe_fn.ToLocal(&fn)) { @@ -1146,7 +1149,7 @@ void ContextifyContext::CompileFunction( context).ToLocal(&cache_key)) { return; } - CompiledFnEntry* entry = new CompiledFnEntry(env, cache_key, id, script); + CompiledFnEntry* entry = new CompiledFnEntry(env, cache_key, id, fn); env->id_to_function_map.emplace(id, entry); Local result = Object::New(isolate); @@ -1192,16 +1195,14 @@ void CompiledFnEntry::WeakCallback( CompiledFnEntry::CompiledFnEntry(Environment* env, Local object, uint32_t id, - Local script) - : BaseObject(env, object), - id_(id), - script_(env->isolate(), script) { - script_.SetWeak(this, WeakCallback, v8::WeakCallbackType::kParameter); + Local fn) + : BaseObject(env, object), id_(id), fn_(env->isolate(), fn) { + fn_.SetWeak(this, WeakCallback, v8::WeakCallbackType::kParameter); } CompiledFnEntry::~CompiledFnEntry() { env()->id_to_function_map.erase(id_); - script_.ClearWeak(); + fn_.ClearWeak(); } static void StartSigintWatchdog(const FunctionCallbackInfo& args) { diff --git a/src/node_contextify.h b/src/node_contextify.h index d45b73b36e295d..c9b5fc0f62dcfc 100644 --- a/src/node_contextify.h +++ b/src/node_contextify.h @@ -174,14 +174,14 @@ class CompiledFnEntry final : public BaseObject { CompiledFnEntry(Environment* env, v8::Local object, uint32_t id, - v8::Local script); + v8::Local fn); ~CompiledFnEntry(); bool IsNotIndicativeOfMemoryLeakAtExit() const override { return true; } private: uint32_t id_; - v8::Global script_; + v8::Global fn_; static void WeakCallback(const v8::WeakCallbackInfo& data); }; diff --git a/src/node_snapshotable.cc b/src/node_snapshotable.cc index ecc5f99304ea8d..7c047bb251a993 100644 --- a/src/node_snapshotable.cc +++ b/src/node_snapshotable.cc @@ -1335,13 +1335,13 @@ void CompileSerializeMain(const FunctionCallbackInfo& args) { }; ScriptCompiler::Source script_source(source, origin); Local fn; - if (ScriptCompiler::CompileFunctionInContext(context, - &script_source, - parameters.size(), - parameters.data(), - 0, - nullptr, - ScriptCompiler::kEagerCompile) + if (ScriptCompiler::CompileFunction(context, + &script_source, + parameters.size(), + parameters.data(), + 0, + nullptr, + ScriptCompiler::kEagerCompile) .ToLocal(&fn)) { args.GetReturnValue().Set(fn); }