Skip to content

Commit 12aff38

Browse files
committed
Revert "Revert "vm: add importModuleDynamically option to compileFunction""
This reverts commit 2d5d773. See: #32985 See: #33364 See: #33166 Fixes: #31860
1 parent 109a296 commit 12aff38

File tree

5 files changed

+62
-30
lines changed

5 files changed

+62
-30
lines changed

doc/api/vm.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ changes:
9090
This option is part of the experimental modules API, and should not be
9191
considered stable.
9292
* `specifier` {string} specifier passed to `import()`
93-
* `module` {vm.Module}
93+
* `script` {vm.Script}
9494
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
9595
recommended in order to take advantage of error tracking, and to avoid
9696
issues with namespaces that contain `then` function exports.
@@ -821,6 +821,9 @@ changes:
821821
- version: v14.3.0
822822
pr-url: https://github.com/nodejs/node/pull/33364
823823
description: Removal of `importModuleDynamically` due to compatibility issues
824+
- version: REPLACEME
825+
pr-url: REPLACEME
826+
description: Added `importModuleDynamically` option again.
824827
-->
825828

826829
* `code` {string} The body of the function to compile.
@@ -843,6 +846,16 @@ changes:
843846
* `contextExtensions` {Object[]} An array containing a collection of context
844847
extensions (objects wrapping the current scope) to be applied while
845848
compiling. **Default:** `[]`.
849+
* `importModuleDynamically` {Function} Called during evaluation of this module
850+
when `import()` is called. If this option is not specified, calls to
851+
`import()` will reject with [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][].
852+
This option is part of the experimental modules API, and should not be
853+
considered stable.
854+
* `specifier` {string} specifier passed to `import()`
855+
* `function` {Function}
856+
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
857+
recommended in order to take advantage of error tracking, and to avoid
858+
issues with namespaces that contain `then` function exports.
846859
* Returns: {Function}
847860

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

lib/internal/modules/cjs/loader.js

+12-28
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
8686
const policy = getOptionValue('--experimental-policy') ?
8787
require('internal/process/policy') :
8888
null;
89-
const { compileFunction } = internalBinding('contextify');
9089

9190
// Whether any user-provided CJS modules had been loaded (executed).
9291
// Used for internal assertions.
@@ -1000,40 +999,25 @@ function wrapSafe(filename, content, cjsModuleInstance) {
1000999
},
10011000
});
10021001
}
1003-
let compiled;
10041002
try {
1005-
compiled = compileFunction(
1006-
content,
1003+
return vm.compileFunction(content, [
1004+
'exports',
1005+
'require',
1006+
'module',
1007+
'__filename',
1008+
'__dirname',
1009+
], {
10071010
filename,
1008-
0,
1009-
0,
1010-
undefined,
1011-
false,
1012-
undefined,
1013-
[],
1014-
[
1015-
'exports',
1016-
'require',
1017-
'module',
1018-
'__filename',
1019-
'__dirname',
1020-
]
1021-
);
1011+
importModuleDynamically(specifier) {
1012+
const loader = asyncESM.ESMLoader;
1013+
return loader.import(specifier, normalizeReferrerURL(filename));
1014+
},
1015+
});
10221016
} catch (err) {
10231017
if (process.mainModule === cjsModuleInstance)
10241018
enrichCJSError(err);
10251019
throw err;
10261020
}
1027-
1028-
const { callbackMap } = internalBinding('module_wrap');
1029-
callbackMap.set(compiled.cacheKey, {
1030-
importModuleDynamically: async (specifier) => {
1031-
const loader = asyncESM.ESMLoader;
1032-
return loader.import(specifier, normalizeReferrerURL(filename));
1033-
}
1034-
});
1035-
1036-
return compiled.function;
10371021
}
10381022

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

lib/vm.js

+17
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ function compileFunction(code, params, options = {}) {
325325
produceCachedData = false,
326326
parsingContext = undefined,
327327
contextExtensions = [],
328+
importModuleDynamically,
328329
} = options;
329330

330331
validateString(filename, 'options.filename');
@@ -372,6 +373,22 @@ function compileFunction(code, params, options = {}) {
372373
result.function.cachedData = result.cachedData;
373374
}
374375

376+
if (importModuleDynamically !== undefined) {
377+
if (typeof importModuleDynamically !== 'function') {
378+
throw new ERR_INVALID_ARG_TYPE('options.importModuleDynamically',
379+
'function',
380+
importModuleDynamically);
381+
}
382+
const { importModuleDynamicallyWrap } =
383+
require('internal/vm/module');
384+
const { callbackMap } = internalBinding('module_wrap');
385+
const wrapped = importModuleDynamicallyWrap(importModuleDynamically);
386+
const func = result.function;
387+
callbackMap.set(result.cacheKey, {
388+
importModuleDynamically: (s, _k) => wrapped(s, func),
389+
});
390+
}
391+
375392
return result.function;
376393
}
377394

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

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

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

tools/doc/type-parser.js

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

170170
'vm.Module': 'vm.html#vm_class_vm_module',
171+
'vm.Script': 'vm.html#vm_class_vm_script',
171172
'vm.SourceTextModule': 'vm.html#vm_class_vm_sourcetextmodule',
172173

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

0 commit comments

Comments
 (0)