Skip to content

Commit

Permalink
Abstract common code
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyBooth committed Oct 14, 2023
1 parent 5c065f3 commit 651bfde
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 53 deletions.
107 changes: 54 additions & 53 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1206,31 +1206,12 @@ void ContextifyContext::CompileFunction(
data + cached_data_buf->ByteOffset(), cached_data_buf->ByteLength());
}

// Set host_defined_options
Local<PrimitiveArray> 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<Value>(), // 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<PrimitiveArray> 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);

Expand Down Expand Up @@ -1279,6 +1260,49 @@ void ContextifyContext::CompileFunction(
args.GetReturnValue().Set(result);
}

Local<PrimitiveArray> ContextifyContext::GetHostDefinedOptions(
Isolate* isolate,
Local<Symbol> id_symbol) {
Local<PrimitiveArray> 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<String> code,
Local<String> filename,
int line_offset,
int column_offset,
Local<PrimitiveArray> 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<Value>(), // 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<Object> ContextifyContext::CompileFunctionAndCacheResult(
Environment* env,
Local<Context> parsing_context,
Expand Down Expand Up @@ -1373,34 +1397,11 @@ void ContextifyContext::ContainsModuleSyntax(
String::NewFromUtf8(isolate, "cjs:").ToLocalChecked(), filename))
.As<Symbol>();

// TODO: Abstract this into a separate function
// Set host_defined_options
Local<PrimitiveArray> 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<Value>(), // 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<PrimitiveArray> 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<Local<String>> params = {
String::NewFromUtf8(isolate, "exports").ToLocalChecked(),
Expand Down
13 changes: 13 additions & 0 deletions src/node_contextify.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ class ContextifyContext : public BaseObject {
bool produce_cached_data,
v8::Local<v8::Symbol> id_symbol,
const errors::TryCatchScope& try_catch);
static v8::Local<v8::PrimitiveArray> GetHostDefinedOptions(
v8::Isolate* isolate,
v8::Local<v8::Symbol> id_symbol);
static v8::ScriptCompiler::Source GetCommonJSSourceInstance(
v8::Isolate* isolate,
v8::Local<v8::String> code,
v8::Local<v8::String> filename,
int line_offset,
int column_offset,
v8::Local<v8::PrimitiveArray> host_defined_options,
v8::ScriptCompiler::CachedData* cached_data);
static v8::ScriptCompiler::CompileOptions GetCompileOptions(
const v8::ScriptCompiler::Source& source);
static void ContainsModuleSyntax(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void WeakCallback(
Expand Down

0 comments on commit 651bfde

Please sign in to comment.