Skip to content

Commit c4a8c7d

Browse files
committed
Abstract common code
1 parent 76ceb58 commit c4a8c7d

File tree

2 files changed

+66
-53
lines changed

2 files changed

+66
-53
lines changed

src/node_contextify.cc

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,31 +1206,10 @@ 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(isolate, id_symbol);
1210+
ScriptCompiler::Source source = GetCommonJSSourceInstance(
1211+
isolate, code, filename, line_offset, column_offset, host_defined_options, cached_data);
1212+
ScriptCompiler::CompileOptions options = GetCompileOptions(source);
12341213

12351214
Context::Scope scope(parsing_context);
12361215

@@ -1279,6 +1258,49 @@ void ContextifyContext::CompileFunction(
12791258
args.GetReturnValue().Set(result);
12801259
}
12811260

1261+
Local<PrimitiveArray> ContextifyContext::GetHostDefinedOptions(
1262+
Isolate* isolate,
1263+
Local<Symbol> id_symbol) {
1264+
Local<PrimitiveArray> host_defined_options =
1265+
PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength);
1266+
host_defined_options->Set(
1267+
isolate, loader::HostDefinedOptions::kID, id_symbol);
1268+
return host_defined_options;
1269+
}
1270+
1271+
ScriptCompiler::Source ContextifyContext::GetCommonJSSourceInstance(
1272+
Isolate* isolate,
1273+
Local<String> code,
1274+
Local<String> filename,
1275+
int line_offset,
1276+
int column_offset,
1277+
Local<PrimitiveArray> host_defined_options,
1278+
ScriptCompiler::CachedData* cached_data) {
1279+
ScriptOrigin origin(isolate,
1280+
filename,
1281+
line_offset, // line offset
1282+
column_offset, // column offset
1283+
true, // is cross origin
1284+
-1, // script id
1285+
Local<Value>(), // source map URL
1286+
false, // is opaque (?)
1287+
false, // is WASM
1288+
false, // is ES Module
1289+
host_defined_options);
1290+
return ScriptCompiler::Source(code, origin, cached_data);
1291+
}
1292+
1293+
ScriptCompiler::CompileOptions ContextifyContext::GetCompileOptions(
1294+
ScriptCompiler::Source& source) {
1295+
ScriptCompiler::CompileOptions options;
1296+
if (source.GetCachedData() != nullptr) {
1297+
options = ScriptCompiler::kConsumeCodeCache;
1298+
} else {
1299+
options = ScriptCompiler::kNoCompileOptions;
1300+
}
1301+
return options;
1302+
}
1303+
12821304
Local<Object> ContextifyContext::CompileFunctionAndCacheResult(
12831305
Environment* env,
12841306
Local<Context> parsing_context,
@@ -1366,38 +1388,15 @@ void ContextifyContext::ContainsModuleSyntax(
13661388
Environment* env = Environment::GetCurrent(args);
13671389
Isolate* isolate = env->isolate();
13681390
Local<Context> context = env->context();
1391+
13691392
// TODO: Centralize this rather than matching the logic in cjs/loader.js and translators.js
13701393
Local<Symbol> id_symbol = (String::Concat(isolate,
13711394
String::NewFromUtf8(isolate, "cjs:").ToLocalChecked(), filename)).As<Symbol>();
13721395

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

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

src/node_contextify.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ 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+
);
100+
static v8::ScriptCompiler::Source GetCommonJSSourceInstance(
101+
v8::Isolate* isolate,
102+
v8::Local<v8::String> code,
103+
v8::Local<v8::String> filename,
104+
int line_offset,
105+
int column_offset,
106+
v8::Local<v8::PrimitiveArray> host_defined_options,
107+
v8::ScriptCompiler::CachedData* cached_data);
108+
static v8::ScriptCompiler::CompileOptions GetCompileOptions(
109+
v8::ScriptCompiler::Source& source);
96110
static void ContainsModuleSyntax(
97111
const v8::FunctionCallbackInfo<v8::Value>& args);
98112
static void WeakCallback(

0 commit comments

Comments
 (0)