@@ -1206,32 +1206,17 @@ void ContextifyContext::CompileFunction(
1206
1206
data + cached_data_buf->ByteOffset (), cached_data_buf->ByteLength ());
1207
1207
}
1208
1208
1209
- // Set host_defined_options
1210
1209
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
-
1210
+ GetHostDefinedOptions (isolate, id_symbol);
1227
1211
ScriptCompiler::Source source =
1228
- ScriptCompiler::Source (code, origin, cached_data);
1229
- ScriptCompiler::CompileOptions options;
1230
- if (source.GetCachedData () == nullptr ) {
1231
- options = ScriptCompiler::kNoCompileOptions ;
1232
- } else {
1233
- options = ScriptCompiler::kConsumeCodeCache ;
1234
- }
1212
+ GetCommonJSSourceInstance (isolate,
1213
+ code,
1214
+ filename,
1215
+ line_offset,
1216
+ column_offset,
1217
+ host_defined_options,
1218
+ cached_data);
1219
+ ScriptCompiler::CompileOptions options = GetCompileOptions (source);
1235
1220
1236
1221
Context::Scope scope (parsing_context);
1237
1222
@@ -1279,6 +1264,48 @@ void ContextifyContext::CompileFunction(
1279
1264
args.GetReturnValue ().Set (result);
1280
1265
}
1281
1266
1267
+ Local<PrimitiveArray> ContextifyContext::GetHostDefinedOptions (
1268
+ Isolate* isolate, Local<Symbol> id_symbol) {
1269
+ Local<PrimitiveArray> host_defined_options =
1270
+ PrimitiveArray::New (isolate, loader::HostDefinedOptions::kLength );
1271
+ host_defined_options->Set (
1272
+ isolate, loader::HostDefinedOptions::kID , id_symbol);
1273
+ return host_defined_options;
1274
+ }
1275
+
1276
+ ScriptCompiler::Source ContextifyContext::GetCommonJSSourceInstance (
1277
+ Isolate* isolate,
1278
+ Local<String> code,
1279
+ Local<String> filename,
1280
+ int line_offset,
1281
+ int column_offset,
1282
+ Local<PrimitiveArray> host_defined_options,
1283
+ ScriptCompiler::CachedData* cached_data) {
1284
+ ScriptOrigin origin (isolate,
1285
+ filename,
1286
+ line_offset, // line offset
1287
+ column_offset, // column offset
1288
+ true , // is cross origin
1289
+ -1 , // script id
1290
+ Local<Value>(), // source map URL
1291
+ false , // is opaque (?)
1292
+ false , // is WASM
1293
+ false , // is ES Module
1294
+ host_defined_options);
1295
+ return ScriptCompiler::Source (code, origin, cached_data);
1296
+ }
1297
+
1298
+ ScriptCompiler::CompileOptions ContextifyContext::GetCompileOptions (
1299
+ const ScriptCompiler::Source& source) {
1300
+ ScriptCompiler::CompileOptions options;
1301
+ if (source.GetCachedData () != nullptr ) {
1302
+ options = ScriptCompiler::kConsumeCodeCache ;
1303
+ } else {
1304
+ options = ScriptCompiler::kNoCompileOptions ;
1305
+ }
1306
+ return options;
1307
+ }
1308
+
1282
1309
Local<Object> ContextifyContext::CompileFunctionAndCacheResult (
1283
1310
Environment* env,
1284
1311
Local<Context> parsing_context,
@@ -1375,35 +1402,11 @@ void ContextifyContext::ContainsModuleSyntax(
1375
1402
filename))
1376
1403
.As <Symbol>();
1377
1404
1378
- // TODO: Abstract this into a separate function
1379
- // Set host_defined_options
1380
1405
Local<PrimitiveArray> host_defined_options =
1381
- PrimitiveArray::New (isolate, loader::HostDefinedOptions::kLength );
1382
- host_defined_options->Set (
1383
- isolate, loader::HostDefinedOptions::kID , id_symbol);
1384
-
1385
- ScriptOrigin origin (isolate,
1386
- filename,
1387
- 0 , // line offset
1388
- 0 , // column offset
1389
- true , // is cross origin
1390
- -1 , // script id
1391
- Local<Value>(), // source map URL
1392
- false , // is opaque (?)
1393
- false , // is WASM
1394
- false , // is ES Module
1395
- host_defined_options);
1396
-
1397
- ScriptCompiler::CachedData* cached_data = nullptr ;
1398
- ScriptCompiler::Source source =
1399
- ScriptCompiler::Source (code, origin, cached_data);
1400
- ScriptCompiler::CompileOptions options;
1401
- if (source.GetCachedData () == nullptr ) {
1402
- options = ScriptCompiler::kNoCompileOptions ;
1403
- } else {
1404
- options = ScriptCompiler::kConsumeCodeCache ;
1405
- }
1406
- // End TODO
1406
+ GetHostDefinedOptions (isolate, id_symbol);
1407
+ ScriptCompiler::Source source = GetCommonJSSourceInstance (
1408
+ isolate, code, filename, 0 , 0 , host_defined_options, nullptr );
1409
+ ScriptCompiler::CompileOptions options = GetCompileOptions (source);
1407
1410
1408
1411
std::vector<Local<String>> params = {
1409
1412
String::NewFromUtf8 (isolate, " exports" ).ToLocalChecked (),
0 commit comments