diff --git a/src/node_contextify.cc b/src/node_contextify.cc index bbc3d8b8ea66a3..8f255c87b8e74c 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1206,31 +1206,12 @@ 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 +1260,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( + const 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, @@ -1373,34 +1397,11 @@ void ContextifyContext::ContainsModuleSyntax( 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..a49c052a285dcf 100644 --- a/src/node_contextify.h +++ b/src/node_contextify.h @@ -93,6 +93,19 @@ 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( + const v8::ScriptCompiler::Source& source); static void ContainsModuleSyntax( const v8::FunctionCallbackInfo& args); static void WeakCallback(