From 0ff09adb47bb96c6c50e52f7193fa3bb1345985f Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 23 Feb 2025 15:27:19 -0800 Subject: [PATCH] src: improve error handling in module_wrap Replacing ToLocalChecked() --- src/module_wrap.cc | 51 ++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 8ba5dc01228fef..2f601fdd029296 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -241,8 +241,10 @@ void ModuleWrap::New(const FunctionCallbackInfo& args) { uint32_t len = export_names_arr->Length(); LocalVector export_names(realm->isolate(), len); for (uint32_t i = 0; i < len; i++) { - Local export_name_val = - export_names_arr->Get(context, i).ToLocalChecked(); + Local export_name_val; + if (!export_names_arr->Get(context, i).ToLocal(&export_name_val)) { + return; + } CHECK(export_name_val->IsString()); export_names[i] = export_name_val.As(); } @@ -612,7 +614,10 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo& args) { return; } - args.GetReturnValue().Set(result.ToLocalChecked()); + Local res; + if (result.ToLocal(&res)) { + args.GetReturnValue().Set(res); + } } void ModuleWrap::InstantiateSync(const FunctionCallbackInfo& args) { @@ -862,10 +867,10 @@ static MaybeLocal ImportModuleDynamically( // from the realm global object. if (options->Length() == HostDefinedOptions::kLength) { id = options->Get(context, HostDefinedOptions::kID).As(); - } else { - id = context->Global() - ->GetPrivate(context, env->host_defined_option_symbol()) - .ToLocalChecked(); + } else if (!context->Global() + ->GetPrivate(context, env->host_defined_option_symbol()) + .ToLocal(&id)) { + return MaybeLocal(); } Local attributes = @@ -985,7 +990,9 @@ MaybeLocal ModuleWrap::SyntheticModuleEvaluationStepsCallback( return MaybeLocal(); } - resolver->Resolve(context, Undefined(isolate)).ToChecked(); + if (resolver->Resolve(context, Undefined(isolate)).IsNothing()) { + return MaybeLocal(); + } return resolver->GetPromise(); } @@ -1027,15 +1034,18 @@ void ModuleWrap::CreateCachedData(const FunctionCallbackInfo& args) { std::unique_ptr cached_data( ScriptCompiler::CreateCodeCache(unbound_module_script)); Environment* env = Environment::GetCurrent(args); + Local result; if (!cached_data) { - args.GetReturnValue().Set(Buffer::New(env, 0).ToLocalChecked()); - } else { - MaybeLocal buf = - Buffer::Copy(env, - reinterpret_cast(cached_data->data), - cached_data->length); - args.GetReturnValue().Set(buf.ToLocalChecked()); + if (!Buffer::New(env, 0).ToLocal(&result)) { + return; + } + } else if (!Buffer::Copy(env, + reinterpret_cast(cached_data->data), + cached_data->length) + .ToLocal(&result)) { + return; } + args.GetReturnValue().Set(result); } // This v8::Module::ResolveModuleCallback simply links `import 'original'` @@ -1082,8 +1092,10 @@ void ModuleWrap::CreateRequiredModuleFacade( // The module facade instantiation simply links `import 'original'` in the // facade with the original module and should never fail. - Local facade = - ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked(); + Local facade; + if (!ScriptCompiler::CompileModule(isolate, &source).ToLocal(&facade)) { + return; + } // Stash the original module in temporary_required_module_facade_original // for the LinkRequireFacadeWithOriginal() callback to pick it up. CHECK(env->temporary_required_module_facade_original.IsEmpty()); @@ -1094,7 +1106,10 @@ void ModuleWrap::CreateRequiredModuleFacade( env->temporary_required_module_facade_original.Reset(); // The evaluation of the facade is synchronous. - Local evaluated = facade->Evaluate(context).ToLocalChecked(); + Local evaluated; + if (!facade->Evaluate(context).ToLocal(&evaluated)) { + return; + } CHECK(evaluated->IsPromise()); CHECK_EQ(evaluated.As()->State(), Promise::PromiseState::kFulfilled);