Skip to content

Commit 651bfde

Browse files
committed
Abstract common code
1 parent 5c065f3 commit 651bfde

File tree

2 files changed

+67
-53
lines changed

2 files changed

+67
-53
lines changed

src/node_contextify.cc

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,31 +1206,12 @@ void ContextifyContext::CompileFunction(
12061206
data + cached_data_buf->ByteOffset(), cached_data_buf->ByteLength());
12071207
}
12081208

1209-
// Set host_defined_options
1210-
Local<PrimitiveArray> host_defined_options =
1211-
PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength);
1212-
host_defined_options->Set(
1213-
isolate, loader::HostDefinedOptions::kID, id_symbol);
1214-
1215-
ScriptOrigin origin(isolate,
1216-
filename,
1217-
line_offset, // line offset
1218-
column_offset, // column offset
1219-
true, // is cross origin
1220-
-1, // script id
1221-
Local<Value>(), // source map URL
1222-
false, // is opaque (?)
1223-
false, // is WASM
1224-
false, // is ES Module
1225-
host_defined_options);
1226-
1227-
ScriptCompiler::Source source = ScriptCompiler::Source(code, origin, cached_data);
1228-
ScriptCompiler::CompileOptions options;
1229-
if (source.GetCachedData() == nullptr) {
1230-
options = ScriptCompiler::kNoCompileOptions;
1231-
} else {
1232-
options = ScriptCompiler::kConsumeCodeCache;
1233-
}
1209+
Local<PrimitiveArray> host_defined_options = GetHostDefinedOptions(
1210+
isolate, id_symbol);
1211+
ScriptCompiler::Source source = GetCommonJSSourceInstance(
1212+
isolate, code, filename, line_offset, column_offset, host_defined_options,
1213+
cached_data);
1214+
ScriptCompiler::CompileOptions options = GetCompileOptions(source);
12341215

12351216
Context::Scope scope(parsing_context);
12361217

@@ -1279,6 +1260,49 @@ void ContextifyContext::CompileFunction(
12791260
args.GetReturnValue().Set(result);
12801261
}
12811262

1263+
Local<PrimitiveArray> ContextifyContext::GetHostDefinedOptions(
1264+
Isolate* isolate,
1265+
Local<Symbol> id_symbol) {
1266+
Local<PrimitiveArray> host_defined_options =
1267+
PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength);
1268+
host_defined_options->Set(
1269+
isolate, loader::HostDefinedOptions::kID, id_symbol);
1270+
return host_defined_options;
1271+
}
1272+
1273+
ScriptCompiler::Source ContextifyContext::GetCommonJSSourceInstance(
1274+
Isolate* isolate,
1275+
Local<String> code,
1276+
Local<String> filename,
1277+
int line_offset,
1278+
int column_offset,
1279+
Local<PrimitiveArray> host_defined_options,
1280+
ScriptCompiler::CachedData* cached_data) {
1281+
ScriptOrigin origin(isolate,
1282+
filename,
1283+
line_offset, // line offset
1284+
column_offset, // column offset
1285+
true, // is cross origin
1286+
-1, // script id
1287+
Local<Value>(), // source map URL
1288+
false, // is opaque (?)
1289+
false, // is WASM
1290+
false, // is ES Module
1291+
host_defined_options);
1292+
return ScriptCompiler::Source(code, origin, cached_data);
1293+
}
1294+
1295+
ScriptCompiler::CompileOptions ContextifyContext::GetCompileOptions(
1296+
const ScriptCompiler::Source& source) {
1297+
ScriptCompiler::CompileOptions options;
1298+
if (source.GetCachedData() != nullptr) {
1299+
options = ScriptCompiler::kConsumeCodeCache;
1300+
} else {
1301+
options = ScriptCompiler::kNoCompileOptions;
1302+
}
1303+
return options;
1304+
}
1305+
12821306
Local<Object> ContextifyContext::CompileFunctionAndCacheResult(
12831307
Environment* env,
12841308
Local<Context> parsing_context,
@@ -1373,34 +1397,11 @@ void ContextifyContext::ContainsModuleSyntax(
13731397
String::NewFromUtf8(isolate, "cjs:").ToLocalChecked(), filename))
13741398
.As<Symbol>();
13751399

1376-
// TODO: Abstract this into a separate function
1377-
// Set host_defined_options
1378-
Local<PrimitiveArray> host_defined_options =
1379-
PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength);
1380-
host_defined_options->Set(
1381-
isolate, loader::HostDefinedOptions::kID, id_symbol);
1382-
1383-
ScriptOrigin origin(isolate,
1384-
filename,
1385-
0, // line offset
1386-
0, // column offset
1387-
true, // is cross origin
1388-
-1, // script id
1389-
Local<Value>(), // source map URL
1390-
false, // is opaque (?)
1391-
false, // is WASM
1392-
false, // is ES Module
1393-
host_defined_options);
1394-
1395-
ScriptCompiler::CachedData* cached_data = nullptr;
1396-
ScriptCompiler::Source source = ScriptCompiler::Source(code, origin, cached_data);
1397-
ScriptCompiler::CompileOptions options;
1398-
if (source.GetCachedData() == nullptr) {
1399-
options = ScriptCompiler::kNoCompileOptions;
1400-
} else {
1401-
options = ScriptCompiler::kConsumeCodeCache;
1402-
}
1403-
// End TODO
1400+
Local<PrimitiveArray> host_defined_options = GetHostDefinedOptions(
1401+
isolate, id_symbol);
1402+
ScriptCompiler::Source source = GetCommonJSSourceInstance(
1403+
isolate, code, filename, 0, 0, host_defined_options, nullptr);
1404+
ScriptCompiler::CompileOptions options = GetCompileOptions(source);
14041405

14051406
std::vector<Local<String>> params = {
14061407
String::NewFromUtf8(isolate, "exports").ToLocalChecked(),

src/node_contextify.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ class ContextifyContext : public BaseObject {
9393
bool produce_cached_data,
9494
v8::Local<v8::Symbol> id_symbol,
9595
const errors::TryCatchScope& try_catch);
96+
static v8::Local<v8::PrimitiveArray> GetHostDefinedOptions(
97+
v8::Isolate* isolate,
98+
v8::Local<v8::Symbol> id_symbol);
99+
static v8::ScriptCompiler::Source GetCommonJSSourceInstance(
100+
v8::Isolate* isolate,
101+
v8::Local<v8::String> code,
102+
v8::Local<v8::String> filename,
103+
int line_offset,
104+
int column_offset,
105+
v8::Local<v8::PrimitiveArray> host_defined_options,
106+
v8::ScriptCompiler::CachedData* cached_data);
107+
static v8::ScriptCompiler::CompileOptions GetCompileOptions(
108+
const v8::ScriptCompiler::Source& source);
96109
static void ContainsModuleSyntax(
97110
const v8::FunctionCallbackInfo<v8::Value>& args);
98111
static void WeakCallback(

0 commit comments

Comments
 (0)