vm: unify host-defined option generation in vm.compileFunction · nodejs/node@9f7899e · GitHub
Skip to content

Commit 9f7899e

Browse files
joyeecheungrichardlau
authored andcommitted
vm: unify host-defined option generation in vm.compileFunction
Set a default host-defined option for vm.compileFunction so that it's consistent with vm.Script. PR-URL: #50137 Backport-PR-URL: #51004 Refs: #35375 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 6291c10 commit 9f7899e

4 files changed

Lines changed: 46 additions & 24 deletions

File tree

lib/internal/vm.js

Lines changed: 24 additions & 1 deletion

lib/vm.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ const {
4242
const {
4343
validateBoolean,
4444
validateBuffer,
45-
validateFunction,
4645
validateInt32,
4746
validateObject,
4847
validateOneOf,
@@ -55,6 +54,7 @@ const {
5554
kVmBreakFirstLineSymbol,
5655
} = require('internal/util');
5756
const {
57+
getHostDefinedOptionId,
5858
internalCompileFunction,
5959
isContext,
6060
} = require('internal/vm');
@@ -87,12 +87,8 @@ class Script extends ContextifyScript {
8787
}
8888
validateBoolean(produceCachedData, 'options.produceCachedData');
8989

90-
if (importModuleDynamically !== undefined) {
91-
// Check that it's either undefined or a function before we pass
92-
// it into the native constructor.
93-
validateFunction(importModuleDynamically,
94-
'options.importModuleDynamically');
95-
}
90+
const hostDefinedOptionId =
91+
getHostDefinedOptionId(importModuleDynamically, filename);
9692
// Calling `ReThrow()` on a native TryCatch does not generate a new
9793
// abort-on-uncaught-exception check. A dummy try/catch in JS land
9894
// protects against that.
@@ -104,7 +100,7 @@ class Script extends ContextifyScript {
104100
cachedData,
105101
produceCachedData,
106102
parsingContext,
107-
importModuleDynamically !== undefined);
103+
hostDefinedOptionId);
108104
} catch (e) {
109105
throw e; /* node-do-not-add-exception-line */
110106
}

src/node_contextify.cc

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -787,11 +787,11 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
787787
bool produce_cached_data = false;
788788
Local<Context> parsing_context = context;
789789

790-
bool needs_custom_host_defined_options = false;
790+
Local<Symbol> id_symbol;
791791
if (argc > 2) {
792792
// new ContextifyScript(code, filename, lineOffset, columnOffset,
793793
// cachedData, produceCachedData, parsingContext,
794-
// needsCustomHostDefinedOptions)
794+
// hostDefinedOptionId)
795795
CHECK_EQ(argc, 8);
796796
CHECK(args[2]->IsNumber());
797797
line_offset = args[2].As<Int32>()->Value();
@@ -811,9 +811,8 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
811811
CHECK_NOT_NULL(sandbox);
812812
parsing_context = sandbox->context();
813813
}
814-
if (args[7]->IsTrue()) {
815-
needs_custom_host_defined_options = true;
816-
}
814+
CHECK(args[7]->IsSymbol());
815+
id_symbol = args[7].As<Symbol>();
817816
}
818817

819818
ContextifyScript* contextify_script =
@@ -837,12 +836,6 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
837836

838837
Local<PrimitiveArray> host_defined_options =
839838
PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength);
840-
// We need a default host defined options that's the same for all scripts
841-
// not needing custom module callbacks for so that the isolate compilation
842-
// cache can be hit.
843-
Local<Symbol> id_symbol = needs_custom_host_defined_options
844-
? Symbol::New(isolate, filename)
845-
: env->default_host_defined_options();
846839
host_defined_options->Set(
847840
isolate, loader::HostDefinedOptions::kID, id_symbol);
848841

@@ -1201,6 +1194,10 @@ void ContextifyContext::CompileFunction(
12011194
params_buf = args[8].As<Array>();
12021195
}
12031196

1197+
// Argument 10: host-defined option symbol
1198+
CHECK(args[9]->IsSymbol());
1199+
Local<Symbol> id_symbol = args[9].As<Symbol>();
1200+
12041201
// Read cache from cached data buffer
12051202
ScriptCompiler::CachedData* cached_data = nullptr;
12061203
if (!cached_data_buf.IsEmpty()) {
@@ -1212,7 +1209,6 @@ void ContextifyContext::CompileFunction(
12121209
// Set host_defined_options
12131210
Local<PrimitiveArray> host_defined_options =
12141211
PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength);
1215-
Local<Symbol> id_symbol = Symbol::New(isolate, filename);
12161212
host_defined_options->Set(
12171213
isolate, loader::HostDefinedOptions::kID, id_symbol);
12181214

Lines changed: 10 additions & 3 deletions

0 commit comments

Comments
 (0)