From 9614907104aa2e34e3385398f6fa22d3bab74d87 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_contextify.cc | 27 ++++++++++++++------------- src/node_contextify.h | 4 ++-- src/node_native_module.cc | 22 +++++++++++----------- src/node_snapshotable.cc | 14 +++++++------- 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 969296656b1059..49a049e24abf2a 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; @@ -1130,11 +1129,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)) { @@ -1150,7 +1153,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); @@ -1196,16 +1199,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_native_module.cc b/src/node_native_module.cc index ffd406625db5c1..f046a75a3838f1 100644 --- a/src/node_native_module.cc +++ b/src/node_native_module.cc @@ -256,9 +256,9 @@ MaybeLocal NativeModuleLoader::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()) { @@ -280,20 +280,20 @@ MaybeLocal NativeModuleLoader::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 native 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_snapshotable.cc b/src/node_snapshotable.cc index f0c71042fd5e57..09b2efbf3b711c 100644 --- a/src/node_snapshotable.cc +++ b/src/node_snapshotable.cc @@ -484,13 +484,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); }