Skip to content

Commit 8dae59b

Browse files
committed
split CompileFunction
1 parent e41aff5 commit 8dae59b

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

src/node_contextify.cc

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,15 +1222,14 @@ void ContextifyContext::CompileFunction(
12221222
false, // is ES Module
12231223
host_defined_options);
12241224

1225-
ScriptCompiler::Source source(code, origin, cached_data);
1225+
ScriptCompiler::Source source = ScriptCompiler::Source(code, origin, cached_data);
12261226
ScriptCompiler::CompileOptions options;
12271227
if (source.GetCachedData() == nullptr) {
12281228
options = ScriptCompiler::kNoCompileOptions;
12291229
} else {
12301230
options = ScriptCompiler::kConsumeCodeCache;
12311231
}
12321232

1233-
TryCatchScope try_catch(env);
12341233
Context::Scope scope(parsing_context);
12351234

12361235
// Read context extensions from buffer
@@ -1255,9 +1254,42 @@ void ContextifyContext::CompileFunction(
12551254
}
12561255
}
12571256

1257+
TryCatchScope try_catch(env);
1258+
Local<Object> result = CompileFunctionAndCacheResult(
1259+
env,
1260+
parsing_context,
1261+
source,
1262+
params,
1263+
context_extensions,
1264+
options,
1265+
produce_cached_data,
1266+
id_symbol,
1267+
try_catch);
1268+
1269+
if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
1270+
try_catch.ReThrow();
1271+
return;
1272+
}
1273+
1274+
if (result.IsEmpty()) {
1275+
return;
1276+
}
1277+
args.GetReturnValue().Set(result);
1278+
}
1279+
1280+
Local<Object> ContextifyContext::CompileFunctionAndCacheResult(
1281+
Environment* env,
1282+
Local<Context> parsing_context,
1283+
const ScriptCompiler::Source& source,
1284+
std::vector<Local<String>> params,
1285+
std::vector<Local<Object>> context_extensions,
1286+
ScriptCompiler::CompileOptions options,
1287+
bool produce_cached_data,
1288+
Local<Symbol> id_symbol,
1289+
const TryCatchScope& try_catch) {
12581290
MaybeLocal<Function> maybe_fn = ScriptCompiler::CompileFunction(
12591291
parsing_context,
1260-
&source,
1292+
const_cast<ScriptCompiler::Source*>(&source),
12611293
params.size(),
12621294
params.data(),
12631295
context_extensions.size(),
@@ -1269,24 +1301,26 @@ void ContextifyContext::CompileFunction(
12691301
if (!maybe_fn.ToLocal(&fn)) {
12701302
if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
12711303
errors::DecorateErrorStack(env, try_catch);
1272-
try_catch.ReThrow();
1304+
return Object::New(env->isolate());
12731305
}
1274-
return;
12751306
}
1307+
1308+
Local<Context> context = env->context();
12761309
if (fn->SetPrivate(context, env->host_defined_option_symbol(), id_symbol)
12771310
.IsNothing()) {
1278-
return;
1311+
return Object::New(env->isolate());
12791312
}
12801313

1314+
Isolate* isolate = env->isolate();
12811315
Local<Object> result = Object::New(isolate);
12821316
if (result->Set(parsing_context, env->function_string(), fn).IsNothing())
1283-
return;
1317+
return Object::New(env->isolate());
12841318
if (result
12851319
->Set(parsing_context,
12861320
env->source_map_url_string(),
12871321
fn->GetScriptOrigin().SourceMapUrl())
12881322
.IsNothing())
1289-
return;
1323+
return Object::New(env->isolate());
12901324

12911325
std::unique_ptr<ScriptCompiler::CachedData> new_cached_data;
12921326
if (produce_cached_data) {
@@ -1299,10 +1333,10 @@ void ContextifyContext::CompileFunction(
12991333
produce_cached_data,
13001334
std::move(new_cached_data))
13011335
.IsNothing()) {
1302-
return;
1336+
return Object::New(env->isolate());
13031337
}
13041338

1305-
args.GetReturnValue().Set(result);
1339+
return result;
13061340
}
13071341

13081342
static void StartSigintWatchdog(const FunctionCallbackInfo<Value>& args) {

src/node_contextify.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ class ContextifyContext : public BaseObject {
8383
static void IsContext(const v8::FunctionCallbackInfo<v8::Value>& args);
8484
static void CompileFunction(
8585
const v8::FunctionCallbackInfo<v8::Value>& args);
86+
static v8::Local<v8::Object> CompileFunctionAndCacheResult(
87+
Environment* env,
88+
v8::Local<v8::Context> parsing_context,
89+
const v8::ScriptCompiler::Source& source,
90+
std::vector<v8::Local<v8::String>> params,
91+
std::vector<v8::Local<v8::Object>> context_extensions,
92+
v8::ScriptCompiler::CompileOptions options,
93+
bool produce_cached_data,
94+
v8::Local<v8::Symbol> id_symbol,
95+
const errors::TryCatchScope& try_catch);
8696
static void WeakCallback(
8797
const v8::WeakCallbackInfo<ContextifyContext>& data);
8898
static void PropertyGetterCallback(

0 commit comments

Comments
 (0)