vm: use internal versions of compileFunction and Script · nodejs/node@052e095 · GitHub
Skip to content

Commit 052e095

Browse files
joyeecheungrichardlau
authored andcommitted
vm: use internal versions of compileFunction and Script
Instead of using the public versions of the vm APIs internally, use the internal versions so that we can skip unnecessary argument validation. The public versions would need special care to the generation of host-defined options to hit the isolate compilation cache when imporModuleDynamically isn't used, while internally it's almost always used, so this allows us to handle the host-defined options separately. 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 9f7899e commit 052e095

7 files changed

Lines changed: 245 additions & 173 deletions

File tree

lib/internal/modules/cjs/loader.js

Lines changed: 40 additions & 32 deletions

lib/internal/process/execution.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
Symbol,
45
RegExpPrototypeExec,
56
globalThis,
67
} = primordials;
@@ -24,7 +25,9 @@ const {
2425
emitAfter,
2526
popAsyncContext,
2627
} = require('internal/async_hooks');
27-
28+
const {
29+
makeContextifyScript, runScriptInThisContext,
30+
} = require('internal/vm');
2831
// shouldAbortOnUncaughtToggle is a typed array for faster
2932
// communication with JS.
3033
const { shouldAbortOnUncaughtToggle } = internalBinding('util');
@@ -52,8 +55,7 @@ function evalModule(source, print) {
5255

5356
function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) {
5457
const CJSModule = require('internal/modules/cjs/loader').Module;
55-
const { kVmBreakFirstLineSymbol } = require('internal/util');
56-
const { pathToFileURL } = require('url');
58+
const { pathToFileURL } = require('internal/url');
5759

5860
const cwd = tryGetCwd();
5961
const origModule = globalThis.module; // Set e.g. when called from the REPL.
@@ -78,16 +80,25 @@ function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) {
7880
`;
7981
globalThis.__filename = name;
8082
RegExpPrototypeExec(/^/, ''); // Necessary to reset RegExp statics before user code runs.
81-
const result = module._compile(script, `${name}-wrapper`)(() =>
82-
require('vm').runInThisContext(body, {
83-
filename: name,
84-
displayErrors: true,
85-
[kVmBreakFirstLineSymbol]: !!breakFirstLine,
86-
importModuleDynamically(specifier, _, importAttributes) {
87-
const loader = asyncESM.esmLoader;
88-
return loader.import(specifier, baseUrl, importAttributes);
89-
},
90-
}));
83+
const result = module._compile(script, `${name}-wrapper`)(() => {
84+
const hostDefinedOptionId = Symbol(name);
85+
async function importModuleDynamically(specifier, _, importAttributes) {
86+
const loader = asyncESM.esmLoader;
87+
return loader.import(specifier, baseUrl, importAttributes);
88+
}
89+
const script = makeContextifyScript(
90+
body, // code
91+
name, // filename,
92+
0, // lineOffset
93+
0, // columnOffset,
94+
undefined, // cachedData
95+
false, // produceCachedData
96+
undefined, // parsingContext
97+
hostDefinedOptionId, // hostDefinedOptionId
98+
importModuleDynamically, // importModuleDynamically
99+
);
100+
return runScriptInThisContext(script, true, !!breakFirstLine);
101+
});
91102
if (print) {
92103
const { log } = require('internal/console/global');
93104
log(result);

lib/internal/vm.js

Lines changed: 69 additions & 61 deletions

0 commit comments

Comments
 (0)