Skip to content

Commit 6f20d29

Browse files
committed
Revert "vm: add importModuleDynamically option to compileFunction"
This reverts commit 74c393d. Fixes: #33166
1 parent cfec30f commit 6f20d29

5 files changed

Lines changed: 33 additions & 59 deletions

File tree

doc/api/vm.md

Lines changed: 4 additions & 11 deletions
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.
@@ -811,6 +811,9 @@ changes:
811811
- v13.14.0
812812
pr-url: https://github.com/nodejs/node/pull/32985
813813
description: The `importModuleDynamically` option is now supported.
814+
- version: REPLACEME
815+
pr-url: https://github.com/nodejs/node/pull/33364
816+
description: Removal of `importModuleDynamically` due to compatibility issues
814817
-->
815818

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

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

lib/internal/modules/cjs/loader.js

Lines changed: 28 additions & 12 deletions
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.
@@ -1102,25 +1103,40 @@ function wrapSafe(filename, content, cjsModuleInstance) {
11021103
},
11031104
});
11041105
}
1106+
let compiled;
11051107
try {
1106-
return vm.compileFunction(content, [
1107-
'exports',
1108-
'require',
1109-
'module',
1110-
'__filename',
1111-
'__dirname',
1112-
], {
1108+
compiled = compileFunction(
1109+
content,
11131110
filename,
1114-
importModuleDynamically(specifier) {
1115-
const loader = asyncESM.ESMLoader;
1116-
return loader.import(specifier, normalizeReferrerURL(filename));
1117-
},
1118-
});
1111+
0,
1112+
0,
1113+
undefined,
1114+
false,
1115+
undefined,
1116+
[],
1117+
[
1118+
'exports',
1119+
'require',
1120+
'module',
1121+
'__filename',
1122+
'__dirname',
1123+
]
1124+
);
11191125
} catch (err) {
11201126
if (process.mainModule === cjsModuleInstance)
11211127
enrichCJSError(err);
11221128
throw err;
11231129
}
1130+
1131+
const { callbackMap } = internalBinding('module_wrap');
1132+
callbackMap.set(compiled.cacheKey, {
1133+
importModuleDynamically: async (specifier) => {
1134+
const loader = asyncESM.ESMLoader;
1135+
return loader.import(specifier, normalizeReferrerURL(filename));
1136+
}
1137+
});
1138+
1139+
return compiled.function;
11241140
}
11251141

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

lib/vm.js

Lines changed: 0 additions & 17 deletions
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

Lines changed: 1 addition & 18 deletions
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

Lines changed: 0 additions & 1 deletion
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)