Skip to content

Commit 7521077

Browse files
legendecasaduh95
authored andcommitted
src: improve CompileFunctionAndCacheResult error handling
PR-URL: #58434 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent f6a46c8 commit 7521077

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
@@ -1501,24 +1501,22 @@ void ContextifyContext::CompileFunction(
15011501
}
15021502

15031503
TryCatchScope try_catch(env);
1504-
Local<Object> result = CompileFunctionAndCacheResult(env,
1505-
parsing_context,
1506-
&source,
1507-
params,
1508-
context_extensions,
1509-
options,
1510-
produce_cached_data,
1511-
id_symbol,
1512-
try_catch);
1513-
1514-
if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
1504+
MaybeLocal<Object> maybe_result =
1505+
CompileFunctionAndCacheResult(env,
1506+
parsing_context,
1507+
&source,
1508+
params,
1509+
context_extensions,
1510+
options,
1511+
produce_cached_data,
1512+
id_symbol,
1513+
try_catch);
1514+
Local<Object> result;
1515+
if (!maybe_result.ToLocal(&result)) {
1516+
CHECK(try_catch.HasCaught());
15151517
try_catch.ReThrow();
15161518
return;
15171519
}
1518-
1519-
if (result.IsEmpty()) {
1520-
return;
1521-
}
15221520
args.GetReturnValue().Set(result);
15231521
}
15241522

@@ -1534,7 +1532,7 @@ static LocalVector<String> GetCJSParameters(IsolateData* data) {
15341532
return result;
15351533
}
15361534

1537-
Local<Object> ContextifyContext::CompileFunctionAndCacheResult(
1535+
MaybeLocal<Object> ContextifyContext::CompileFunctionAndCacheResult(
15381536
Environment* env,
15391537
Local<Context> parsing_context,
15401538
ScriptCompiler::Source* source,
@@ -1556,28 +1554,29 @@ Local<Object> ContextifyContext::CompileFunctionAndCacheResult(
15561554

15571555
Local<Function> fn;
15581556
if (!maybe_fn.ToLocal(&fn)) {
1559-
if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
1557+
CHECK(try_catch.HasCaught());
1558+
if (!try_catch.HasTerminated()) {
15601559
errors::DecorateErrorStack(env, try_catch);
1561-
return Object::New(env->isolate());
15621560
}
1561+
return {};
15631562
}
15641563

15651564
Local<Context> context = env->context();
15661565
if (fn->SetPrivate(context, env->host_defined_option_symbol(), id_symbol)
15671566
.IsNothing()) {
1568-
return Object::New(env->isolate());
1567+
return {};
15691568
}
15701569

15711570
Isolate* isolate = env->isolate();
15721571
Local<Object> result = Object::New(isolate);
15731572
if (result->Set(parsing_context, env->function_string(), fn).IsNothing())
1574-
return Object::New(env->isolate());
1573+
return {};
15751574
if (result
15761575
->Set(parsing_context,
15771576
env->source_map_url_string(),
15781577
fn->GetScriptOrigin().SourceMapUrl())
15791578
.IsNothing())
1580-
return Object::New(env->isolate());
1579+
return {};
15811580

15821581
std::unique_ptr<ScriptCompiler::CachedData> new_cached_data;
15831582
if (produce_cached_data) {
@@ -1590,7 +1589,7 @@ Local<Object> ContextifyContext::CompileFunctionAndCacheResult(
15901589
produce_cached_data,
15911590
std::move(new_cached_data))
15921591
.IsNothing()) {
1593-
return Object::New(env->isolate());
1592+
return {};
15941593
}
15951594

15961595
return result;

src/node_contextify.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class ContextifyContext : public BaseObject {
8888
static void IsContext(const v8::FunctionCallbackInfo<v8::Value>& args);
8989
static void CompileFunction(
9090
const v8::FunctionCallbackInfo<v8::Value>& args);
91-
static v8::Local<v8::Object> CompileFunctionAndCacheResult(
91+
static v8::MaybeLocal<v8::Object> CompileFunctionAndCacheResult(
9292
Environment* env,
9393
v8::Local<v8::Context> parsing_context,
9494
v8::ScriptCompiler::Source* source,

0 commit comments

Comments
 (0)