Skip to content

Commit 17581c2

Browse files
joyeecheungtargos
authored andcommitted
vm: use default HDO when importModuleDynamically is not set
This makes it possile to hit the in-isolate compilation cache when host-defined options are not necessary. PR-URL: #49950 Refs: #35375 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Stephen Belanger <[email protected]>
1 parent 47164e2 commit 17581c2

File tree

6 files changed

+81
-6
lines changed

6 files changed

+81
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
// This benchmarks compiling scripts that hit the in-isolate compilation
4+
// cache (by having the same source).
5+
const common = require('../common.js');
6+
const fs = require('fs');
7+
const vm = require('vm');
8+
const fixtures = require('../../test/common/fixtures.js');
9+
const scriptPath = fixtures.path('snapshot', 'typescript.js');
10+
11+
const bench = common.createBenchmark(main, {
12+
type: ['with-dynamic-import-callback', 'without-dynamic-import-callback'],
13+
n: [100],
14+
});
15+
16+
const scriptSource = fs.readFileSync(scriptPath, 'utf8');
17+
18+
function main({ n, type }) {
19+
let script;
20+
bench.start();
21+
const options = {};
22+
switch (type) {
23+
case 'with-dynamic-import-callback':
24+
// Use a dummy callback for now until we really need to benchmark it.
25+
options.importModuleDynamically = async () => {};
26+
break;
27+
case 'without-dynamic-import-callback':
28+
break;
29+
}
30+
for (let i = 0; i < n; i++) {
31+
script = new vm.Script(scriptSource, options);
32+
}
33+
bench.end(n);
34+
script.runInThisContext();
35+
}

lib/internal/modules/esm/utils.js

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ const {
1212
host_defined_option_symbol,
1313
},
1414
} = internalBinding('util');
15+
const {
16+
default_host_defined_options,
17+
} = internalBinding('symbols');
18+
1519
const {
1620
ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,
1721
ERR_INVALID_ARG_VALUE,
@@ -128,6 +132,13 @@ const moduleRegistries = new SafeWeakMap();
128132
*/
129133
function registerModule(referrer, registry) {
130134
const idSymbol = referrer[host_defined_option_symbol];
135+
if (idSymbol === default_host_defined_options) {
136+
// The referrer is compiled without custom callbacks, so there is
137+
// no registry to hold on to. We'll throw
138+
// ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING when a callback is
139+
// needed.
140+
return;
141+
}
131142
// To prevent it from being GC'ed.
132143
registry.callbackReferrer ??= referrer;
133144
moduleRegistries.set(idSymbol, registry);

lib/vm.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ class Script extends ContextifyScript {
8686
}
8787
validateBoolean(produceCachedData, 'options.produceCachedData');
8888

89+
if (importModuleDynamically !== undefined) {
90+
// Check that it's either undefined or a function before we pass
91+
// it into the native constructor.
92+
validateFunction(importModuleDynamically,
93+
'options.importModuleDynamically');
94+
}
8995
// Calling `ReThrow()` on a native TryCatch does not generate a new
9096
// abort-on-uncaught-exception check. A dummy try/catch in JS land
9197
// protects against that.
@@ -96,14 +102,13 @@ class Script extends ContextifyScript {
96102
columnOffset,
97103
cachedData,
98104
produceCachedData,
99-
parsingContext);
105+
parsingContext,
106+
importModuleDynamically !== undefined);
100107
} catch (e) {
101108
throw e; /* node-do-not-add-exception-line */
102109
}
103110

104111
if (importModuleDynamically !== undefined) {
105-
validateFunction(importModuleDynamically,
106-
'options.importModuleDynamically');
107112
const { importModuleDynamicallyWrap } = require('internal/vm/module');
108113
const { registerModule } = require('internal/modules/esm/utils');
109114
registerModule(this, {

src/env_properties.h

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
// Symbols are per-isolate primitives but Environment proxies them
3232
// for the sake of convenience.
3333
#define PER_ISOLATE_SYMBOL_PROPERTIES(V) \
34+
V(default_host_defined_options, "default_host_defined_options") \
3435
V(fs_use_promises_symbol, "fs_use_promises_symbol") \
3536
V(async_id_symbol, "async_id_symbol") \
3637
V(handle_onclose_symbol, "handle_onclose") \

src/node_contextify.cc

+13-3
Original file line numberDiff line numberDiff line change
@@ -771,10 +771,12 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
771771
bool produce_cached_data = false;
772772
Local<Context> parsing_context = context;
773773

774+
bool needs_custom_host_defined_options = false;
774775
if (argc > 2) {
775776
// new ContextifyScript(code, filename, lineOffset, columnOffset,
776-
// cachedData, produceCachedData, parsingContext)
777-
CHECK_EQ(argc, 7);
777+
// cachedData, produceCachedData, parsingContext,
778+
// needsCustomHostDefinedOptions)
779+
CHECK_EQ(argc, 8);
778780
CHECK(args[2]->IsNumber());
779781
line_offset = args[2].As<Int32>()->Value();
780782
CHECK(args[3]->IsNumber());
@@ -793,6 +795,9 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
793795
CHECK_NOT_NULL(sandbox);
794796
parsing_context = sandbox->context();
795797
}
798+
if (args[7]->IsTrue()) {
799+
needs_custom_host_defined_options = true;
800+
}
796801
}
797802

798803
ContextifyScript* contextify_script =
@@ -816,7 +821,12 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
816821

817822
Local<PrimitiveArray> host_defined_options =
818823
PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength);
819-
Local<Symbol> id_symbol = Symbol::New(isolate, filename);
824+
// We need a default host defined options that's the same for all scripts
825+
// not needing custom module callbacks for so that the isolate compilation
826+
// cache can be hit.
827+
Local<Symbol> id_symbol = needs_custom_host_defined_options
828+
? Symbol::New(isolate, filename)
829+
: env->default_host_defined_options();
820830
host_defined_options->Set(
821831
isolate, loader::HostDefinedOptions::kID, id_symbol);
822832

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
require('../common');
4+
const { Script } = require('vm');
5+
const assert = require('assert');
6+
7+
assert.rejects(async () => {
8+
const script = new Script('import("fs")');
9+
const imported = script.runInThisContext();
10+
await imported;
11+
}, {
12+
code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING'
13+
});

0 commit comments

Comments
 (0)