diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 5b5c97781499bb..0c65101e138abb 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1206,31 +1206,10 @@ void ContextifyContext::CompileFunction( data + cached_data_buf->ByteOffset(), cached_data_buf->ByteLength()); } - // Set host_defined_options - Local host_defined_options = - PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength); - host_defined_options->Set( - isolate, loader::HostDefinedOptions::kID, id_symbol); - - ScriptOrigin origin(isolate, - filename, - line_offset, // line offset - column_offset, // column offset - true, // is cross origin - -1, // script id - Local(), // source map URL - false, // is opaque (?) - false, // is WASM - false, // is ES Module - host_defined_options); - - ScriptCompiler::Source source = ScriptCompiler::Source(code, origin, cached_data); - ScriptCompiler::CompileOptions options; - if (source.GetCachedData() == nullptr) { - options = ScriptCompiler::kNoCompileOptions; - } else { - options = ScriptCompiler::kConsumeCodeCache; - } + Local host_defined_options = GetHostDefinedOptions(isolate, id_symbol); + ScriptCompiler::Source source = GetCommonJSSourceInstance( + isolate, code, filename, line_offset, column_offset, host_defined_options, cached_data); + ScriptCompiler::CompileOptions options = GetCompileOptions(source); Context::Scope scope(parsing_context); @@ -1279,6 +1258,49 @@ void ContextifyContext::CompileFunction( args.GetReturnValue().Set(result); } +Local ContextifyContext::GetHostDefinedOptions( + Isolate* isolate, + Local id_symbol) { + Local host_defined_options = + PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength); + host_defined_options->Set( + isolate, loader::HostDefinedOptions::kID, id_symbol); + return host_defined_options; +} + +ScriptCompiler::Source ContextifyContext::GetCommonJSSourceInstance( + Isolate* isolate, + Local code, + Local filename, + int line_offset, + int column_offset, + Local host_defined_options, + ScriptCompiler::CachedData* cached_data) { + ScriptOrigin origin(isolate, + filename, + line_offset, // line offset + column_offset, // column offset + true, // is cross origin + -1, // script id + Local(), // source map URL + false, // is opaque (?) + false, // is WASM + false, // is ES Module + host_defined_options); + return ScriptCompiler::Source(code, origin, cached_data); +} + +ScriptCompiler::CompileOptions ContextifyContext::GetCompileOptions( + ScriptCompiler::Source& source) { + ScriptCompiler::CompileOptions options; + if (source.GetCachedData() != nullptr) { + options = ScriptCompiler::kConsumeCodeCache; + } else { + options = ScriptCompiler::kNoCompileOptions; + } + return options; +} + Local ContextifyContext::CompileFunctionAndCacheResult( Environment* env, Local parsing_context, @@ -1366,38 +1388,15 @@ void ContextifyContext::ContainsModuleSyntax( Environment* env = Environment::GetCurrent(args); Isolate* isolate = env->isolate(); Local context = env->context(); + // TODO: Centralize this rather than matching the logic in cjs/loader.js and translators.js Local id_symbol = (String::Concat(isolate, String::NewFromUtf8(isolate, "cjs:").ToLocalChecked(), filename)).As(); - // TODO: Abstract this into a separate function - // Set host_defined_options - Local host_defined_options = - PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength); - host_defined_options->Set( - isolate, loader::HostDefinedOptions::kID, id_symbol); - - ScriptOrigin origin(isolate, - filename, - 0, // line offset - 0, // column offset - true, // is cross origin - -1, // script id - Local(), // source map URL - false, // is opaque (?) - false, // is WASM - false, // is ES Module - host_defined_options); - - ScriptCompiler::CachedData* cached_data = nullptr; - ScriptCompiler::Source source = ScriptCompiler::Source(code, origin, cached_data); - ScriptCompiler::CompileOptions options; - if (source.GetCachedData() == nullptr) { - options = ScriptCompiler::kNoCompileOptions; - } else { - options = ScriptCompiler::kConsumeCodeCache; - } - // End TODO + Local host_defined_options = GetHostDefinedOptions(isolate, id_symbol); + ScriptCompiler::Source source = GetCommonJSSourceInstance( + isolate, code, filename, 0, 0, host_defined_options, nullptr); + ScriptCompiler::CompileOptions options = GetCompileOptions(source); std::vector> params = { String::NewFromUtf8(isolate, "exports").ToLocalChecked(), diff --git a/src/node_contextify.h b/src/node_contextify.h index 9779711e33e440..d4323fef228363 100644 --- a/src/node_contextify.h +++ b/src/node_contextify.h @@ -93,6 +93,20 @@ class ContextifyContext : public BaseObject { bool produce_cached_data, v8::Local id_symbol, const errors::TryCatchScope& try_catch); + static v8::Local GetHostDefinedOptions( + v8::Isolate* isolate, + v8::Local id_symbol + ); + static v8::ScriptCompiler::Source GetCommonJSSourceInstance( + v8::Isolate* isolate, + v8::Local code, + v8::Local filename, + int line_offset, + int column_offset, + v8::Local host_defined_options, + v8::ScriptCompiler::CachedData* cached_data); + static v8::ScriptCompiler::CompileOptions GetCompileOptions( + v8::ScriptCompiler::Source& source); static void ContainsModuleSyntax( const v8::FunctionCallbackInfo& args); static void WeakCallback(