Skip to content

Commit 975e2a5

Browse files
jasnelladuh95
authored andcommitted
src: improve error handling in module_wrap
Replacing ToLocalChecked() PR-URL: #57188 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 7f48811 commit 975e2a5

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

src/module_wrap.cc

+33-18
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,10 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
241241
uint32_t len = export_names_arr->Length();
242242
LocalVector<String> export_names(realm->isolate(), len);
243243
for (uint32_t i = 0; i < len; i++) {
244-
Local<Value> export_name_val =
245-
export_names_arr->Get(context, i).ToLocalChecked();
244+
Local<Value> export_name_val;
245+
if (!export_names_arr->Get(context, i).ToLocal(&export_name_val)) {
246+
return;
247+
}
246248
CHECK(export_name_val->IsString());
247249
export_names[i] = export_name_val.As<String>();
248250
}
@@ -612,7 +614,10 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {
612614
return;
613615
}
614616

615-
args.GetReturnValue().Set(result.ToLocalChecked());
617+
Local<Value> res;
618+
if (result.ToLocal(&res)) {
619+
args.GetReturnValue().Set(res);
620+
}
616621
}
617622

618623
void ModuleWrap::InstantiateSync(const FunctionCallbackInfo<Value>& args) {
@@ -862,10 +867,10 @@ static MaybeLocal<Promise> ImportModuleDynamically(
862867
// from the realm global object.
863868
if (options->Length() == HostDefinedOptions::kLength) {
864869
id = options->Get(context, HostDefinedOptions::kID).As<Symbol>();
865-
} else {
866-
id = context->Global()
867-
->GetPrivate(context, env->host_defined_option_symbol())
868-
.ToLocalChecked();
870+
} else if (!context->Global()
871+
->GetPrivate(context, env->host_defined_option_symbol())
872+
.ToLocal(&id)) {
873+
return MaybeLocal<Promise>();
869874
}
870875

871876
Local<Object> attributes =
@@ -985,7 +990,9 @@ MaybeLocal<Value> ModuleWrap::SyntheticModuleEvaluationStepsCallback(
985990
return MaybeLocal<Value>();
986991
}
987992

988-
resolver->Resolve(context, Undefined(isolate)).ToChecked();
993+
if (resolver->Resolve(context, Undefined(isolate)).IsNothing()) {
994+
return MaybeLocal<Value>();
995+
}
989996
return resolver->GetPromise();
990997
}
991998

@@ -1027,15 +1034,18 @@ void ModuleWrap::CreateCachedData(const FunctionCallbackInfo<Value>& args) {
10271034
std::unique_ptr<ScriptCompiler::CachedData> cached_data(
10281035
ScriptCompiler::CreateCodeCache(unbound_module_script));
10291036
Environment* env = Environment::GetCurrent(args);
1037+
Local<Object> result;
10301038
if (!cached_data) {
1031-
args.GetReturnValue().Set(Buffer::New(env, 0).ToLocalChecked());
1032-
} else {
1033-
MaybeLocal<Object> buf =
1034-
Buffer::Copy(env,
1035-
reinterpret_cast<const char*>(cached_data->data),
1036-
cached_data->length);
1037-
args.GetReturnValue().Set(buf.ToLocalChecked());
1039+
if (!Buffer::New(env, 0).ToLocal(&result)) {
1040+
return;
1041+
}
1042+
} else if (!Buffer::Copy(env,
1043+
reinterpret_cast<const char*>(cached_data->data),
1044+
cached_data->length)
1045+
.ToLocal(&result)) {
1046+
return;
10381047
}
1048+
args.GetReturnValue().Set(result);
10391049
}
10401050

10411051
// This v8::Module::ResolveModuleCallback simply links `import 'original'`
@@ -1082,8 +1092,10 @@ void ModuleWrap::CreateRequiredModuleFacade(
10821092

10831093
// The module facade instantiation simply links `import 'original'` in the
10841094
// facade with the original module and should never fail.
1085-
Local<Module> facade =
1086-
ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
1095+
Local<Module> facade;
1096+
if (!ScriptCompiler::CompileModule(isolate, &source).ToLocal(&facade)) {
1097+
return;
1098+
}
10871099
// Stash the original module in temporary_required_module_facade_original
10881100
// for the LinkRequireFacadeWithOriginal() callback to pick it up.
10891101
CHECK(env->temporary_required_module_facade_original.IsEmpty());
@@ -1094,7 +1106,10 @@ void ModuleWrap::CreateRequiredModuleFacade(
10941106
env->temporary_required_module_facade_original.Reset();
10951107

10961108
// The evaluation of the facade is synchronous.
1097-
Local<Value> evaluated = facade->Evaluate(context).ToLocalChecked();
1109+
Local<Value> evaluated;
1110+
if (!facade->Evaluate(context).ToLocal(&evaluated)) {
1111+
return;
1112+
}
10981113
CHECK(evaluated->IsPromise());
10991114
CHECK_EQ(evaluated.As<Promise>()->State(), Promise::PromiseState::kFulfilled);
11001115

0 commit comments

Comments
 (0)