Skip to content

Commit c2cf978

Browse files
mcollinacodebytere
authored andcommitted
Revert "vm: add importModuleDynamically option to compileFunction"
This reverts commit 74c393d. Fixes: #33166 PR-URL: #33364 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Beth Griggs <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent c2d2dfc commit c2cf978

File tree

5 files changed

+33
-59
lines changed

5 files changed

+33
-59
lines changed

doc/api/vm.md

+4-11
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ changes:
8888
This option is part of the experimental modules API, and should not be
8989
considered stable.
9090
* `specifier` {string} specifier passed to `import()`
91-
* `script` {vm.Script}
91+
* `module` {vm.Module}
9292
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
9393
recommended in order to take advantage of error tracking, and to avoid
9494
issues with namespaces that contain `then` function exports.
@@ -805,6 +805,9 @@ changes:
805805
- version: v14.1.0
806806
pr-url: https://github.com/nodejs/node/pull/32985
807807
description: The `importModuleDynamically` option is now supported.
808+
- version: REPLACEME
809+
pr-url: https://github.com/nodejs/node/pull/33364
810+
description: Removal of `importModuleDynamically` due to compatibility issues
808811
-->
809812

810813
* `code` {string} The body of the function to compile.
@@ -827,16 +830,6 @@ changes:
827830
* `contextExtensions` {Object[]} An array containing a collection of context
828831
extensions (objects wrapping the current scope) to be applied while
829832
compiling. **Default:** `[]`.
830-
* `importModuleDynamically` {Function} Called during evaluation of this module
831-
when `import()` is called. If this option is not specified, calls to
832-
`import()` will reject with [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][].
833-
This option is part of the experimental modules API, and should not be
834-
considered stable.
835-
* `specifier` {string} specifier passed to `import()`
836-
* `function` {Function}
837-
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
838-
recommended in order to take advantage of error tracking, and to avoid
839-
issues with namespaces that contain `then` function exports.
840833
* Returns: {Function}
841834

842835
Compiles the given code into the provided context (if no context is

lib/internal/modules/cjs/loader.js

+28-12
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
7777
const manifest = getOptionValue('--experimental-policy') ?
7878
require('internal/process/policy').manifest :
7979
null;
80+
const { compileFunction } = internalBinding('contextify');
8081

8182
// Whether any user-provided CJS modules had been loaded (executed).
8283
// Used for internal assertions.
@@ -1110,25 +1111,40 @@ function wrapSafe(filename, content, cjsModuleInstance) {
11101111
},
11111112
});
11121113
}
1114+
let compiled;
11131115
try {
1114-
return vm.compileFunction(content, [
1115-
'exports',
1116-
'require',
1117-
'module',
1118-
'__filename',
1119-
'__dirname',
1120-
], {
1116+
compiled = compileFunction(
1117+
content,
11211118
filename,
1122-
importModuleDynamically(specifier) {
1123-
const loader = asyncESM.ESMLoader;
1124-
return loader.import(specifier, normalizeReferrerURL(filename));
1125-
},
1126-
});
1119+
0,
1120+
0,
1121+
undefined,
1122+
false,
1123+
undefined,
1124+
[],
1125+
[
1126+
'exports',
1127+
'require',
1128+
'module',
1129+
'__filename',
1130+
'__dirname',
1131+
]
1132+
);
11271133
} catch (err) {
11281134
if (process.mainModule === cjsModuleInstance)
11291135
enrichCJSError(err);
11301136
throw err;
11311137
}
1138+
1139+
const { callbackMap } = internalBinding('module_wrap');
1140+
callbackMap.set(compiled.cacheKey, {
1141+
importModuleDynamically: async (specifier) => {
1142+
const loader = asyncESM.ESMLoader;
1143+
return loader.import(specifier, normalizeReferrerURL(filename));
1144+
}
1145+
});
1146+
1147+
return compiled.function;
11321148
}
11331149

11341150
// Run the file contents in the correct scope or sandbox. Expose

lib/vm.js

-17
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,6 @@ function compileFunction(code, params, options = {}) {
313313
produceCachedData = false,
314314
parsingContext = undefined,
315315
contextExtensions = [],
316-
importModuleDynamically,
317316
} = options;
318317

319318
validateString(filename, 'options.filename');
@@ -361,22 +360,6 @@ function compileFunction(code, params, options = {}) {
361360
result.function.cachedData = result.cachedData;
362361
}
363362

364-
if (importModuleDynamically !== undefined) {
365-
if (typeof importModuleDynamically !== 'function') {
366-
throw new ERR_INVALID_ARG_TYPE('options.importModuleDynamically',
367-
'function',
368-
importModuleDynamically);
369-
}
370-
const { importModuleDynamicallyWrap } =
371-
require('internal/vm/module');
372-
const { callbackMap } = internalBinding('module_wrap');
373-
const wrapped = importModuleDynamicallyWrap(importModuleDynamically);
374-
const func = result.function;
375-
callbackMap.set(result.cacheKey, {
376-
importModuleDynamically: (s, _k) => wrapped(s, func),
377-
});
378-
}
379-
380363
return result.function;
381364
}
382365

test/parallel/test-vm-module-basic.js

+1-18
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ const {
88
Module,
99
SourceTextModule,
1010
SyntheticModule,
11-
createContext,
12-
compileFunction,
11+
createContext
1312
} = require('vm');
1413
const util = require('util');
1514

@@ -158,19 +157,3 @@ const util = require('util');
158157
name: 'TypeError'
159158
});
160159
}
161-
162-
// Test compileFunction importModuleDynamically
163-
{
164-
const module = new SyntheticModule([], () => {});
165-
module.link(() => {});
166-
const f = compileFunction('return import("x")', [], {
167-
importModuleDynamically(specifier, referrer) {
168-
assert.strictEqual(specifier, 'x');
169-
assert.strictEqual(referrer, f);
170-
return module;
171-
},
172-
});
173-
f().then((ns) => {
174-
assert.strictEqual(ns, module.namespace);
175-
});
176-
}

tools/doc/type-parser.js

-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ const customTypesMap = {
154154
'URLSearchParams': 'url.html#url_class_urlsearchparams',
155155

156156
'vm.Module': 'vm.html#vm_class_vm_module',
157-
'vm.Script': 'vm.html#vm_class_vm_script',
158157
'vm.SourceTextModule': 'vm.html#vm_class_vm_sourcetextmodule',
159158

160159
'MessagePort': 'worker_threads.html#worker_threads_class_messageport',

0 commit comments

Comments
 (0)