Skip to content

Commit 3b6895a

Browse files
legendecastargos
authored andcommitted
src: improve CompileFunctionAndCacheResult error handling
PR-URL: #58434 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent 27ada1f commit 3b6895a

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

src/node_contextify.cc

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,24 +1511,22 @@ void ContextifyContext::CompileFunction(
15111511
}
15121512

15131513
TryCatchScope try_catch(env);
1514-
Local<Object> result = CompileFunctionAndCacheResult(env,
1515-
parsing_context,
1516-
&source,
1517-
params,
1518-
context_extensions,
1519-
options,
1520-
produce_cached_data,
1521-
id_symbol,
1522-
try_catch);
1523-
1524-
if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
1514+
MaybeLocal<Object> maybe_result =
1515+
CompileFunctionAndCacheResult(env,
1516+
parsing_context,
1517+
&source,
1518+
params,
1519+
context_extensions,
1520+
options,
1521+
produce_cached_data,
1522+
id_symbol,
1523+
try_catch);
1524+
Local<Object> result;
1525+
if (!maybe_result.ToLocal(&result)) {
1526+
CHECK(try_catch.HasCaught());
15251527
try_catch.ReThrow();
15261528
return;
15271529
}
1528-
1529-
if (result.IsEmpty()) {
1530-
return;
1531-
}
15321530
args.GetReturnValue().Set(result);
15331531
}
15341532

@@ -1544,7 +1542,7 @@ static LocalVector<String> GetCJSParameters(IsolateData* data) {
15441542
return result;
15451543
}
15461544

1547-
Local<Object> ContextifyContext::CompileFunctionAndCacheResult(
1545+
MaybeLocal<Object> ContextifyContext::CompileFunctionAndCacheResult(
15481546
Environment* env,
15491547
Local<Context> parsing_context,
15501548
ScriptCompiler::Source* source,
@@ -1566,28 +1564,29 @@ Local<Object> ContextifyContext::CompileFunctionAndCacheResult(
15661564

15671565
Local<Function> fn;
15681566
if (!maybe_fn.ToLocal(&fn)) {
1569-
if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
1567+
CHECK(try_catch.HasCaught());
1568+
if (!try_catch.HasTerminated()) {
15701569
errors::DecorateErrorStack(env, try_catch);
1571-
return Object::New(env->isolate());
15721570
}
1571+
return {};
15731572
}
15741573

15751574
Local<Context> context = env->context();
15761575
if (fn->SetPrivate(context, env->host_defined_option_symbol(), id_symbol)
15771576
.IsNothing()) {
1578-
return Object::New(env->isolate());
1577+
return {};
15791578
}
15801579

15811580
Isolate* isolate = env->isolate();
15821581
Local<Object> result = Object::New(isolate);
15831582
if (result->Set(parsing_context, env->function_string(), fn).IsNothing())
1584-
return Object::New(env->isolate());
1583+
return {};
15851584
if (result
15861585
->Set(parsing_context,
15871586
env->source_map_url_string(),
15881587
fn->GetScriptOrigin().SourceMapUrl())
15891588
.IsNothing())
1590-
return Object::New(env->isolate());
1589+
return {};
15911590

15921591
std::unique_ptr<ScriptCompiler::CachedData> new_cached_data;
15931592
if (produce_cached_data) {
@@ -1600,7 +1599,7 @@ Local<Object> ContextifyContext::CompileFunctionAndCacheResult(
16001599
produce_cached_data,
16011600
std::move(new_cached_data))
16021601
.IsNothing()) {
1603-
return Object::New(env->isolate());
1602+
return {};
16041603
}
16051604

16061605
return result;

src/node_contextify.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class ContextifyContext final : CPPGC_MIXIN(ContextifyContext) {
146146
static void IsContext(const v8::FunctionCallbackInfo<v8::Value>& args);
147147
static void CompileFunction(
148148
const v8::FunctionCallbackInfo<v8::Value>& args);
149-
static v8::Local<v8::Object> CompileFunctionAndCacheResult(
149+
static v8::MaybeLocal<v8::Object> CompileFunctionAndCacheResult(
150150
Environment* env,
151151
v8::Local<v8::Context> parsing_context,
152152
v8::ScriptCompiler::Source* source,

0 commit comments

Comments
 (0)