mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
Revert "vm: add importModuleDynamically option to compileFunction"
This reverts commit 74c393dd93.
Fixes: https://github.com/nodejs/node/issues/33166
PR-URL: https://github.com/nodejs/node/pull/33364
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
This commit is contained in:
@@ -88,7 +88,7 @@ changes:
|
||||
This option is part of the experimental modules API, and should not be
|
||||
considered stable.
|
||||
* `specifier` {string} specifier passed to `import()`
|
||||
* `script` {vm.Script}
|
||||
* `module` {vm.Module}
|
||||
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
|
||||
recommended in order to take advantage of error tracking, and to avoid
|
||||
issues with namespaces that contain `then` function exports.
|
||||
@@ -807,6 +807,9 @@ changes:
|
||||
- v13.14.0
|
||||
pr-url: https://github.com/nodejs/node/pull/32985
|
||||
description: The `importModuleDynamically` option is now supported.
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/33364
|
||||
description: Removal of `importModuleDynamically` due to compatibility issues
|
||||
-->
|
||||
|
||||
* `code` {string} The body of the function to compile.
|
||||
@@ -829,16 +832,6 @@ changes:
|
||||
* `contextExtensions` {Object[]} An array containing a collection of context
|
||||
extensions (objects wrapping the current scope) to be applied while
|
||||
compiling. **Default:** `[]`.
|
||||
* `importModuleDynamically` {Function} Called during evaluation of this module
|
||||
when `import()` is called. If this option is not specified, calls to
|
||||
`import()` will reject with [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][].
|
||||
This option is part of the experimental modules API, and should not be
|
||||
considered stable.
|
||||
* `specifier` {string} specifier passed to `import()`
|
||||
* `function` {Function}
|
||||
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
|
||||
recommended in order to take advantage of error tracking, and to avoid
|
||||
issues with namespaces that contain `then` function exports.
|
||||
* Returns: {Function}
|
||||
|
||||
Compiles the given code into the provided context (if no context is
|
||||
|
||||
@@ -77,6 +77,7 @@ const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
|
||||
const manifest = getOptionValue('--experimental-policy') ?
|
||||
require('internal/process/policy').manifest :
|
||||
null;
|
||||
const { compileFunction } = internalBinding('contextify');
|
||||
|
||||
// Whether any user-provided CJS modules had been loaded (executed).
|
||||
// Used for internal assertions.
|
||||
@@ -1110,25 +1111,40 @@ function wrapSafe(filename, content, cjsModuleInstance) {
|
||||
},
|
||||
});
|
||||
}
|
||||
let compiled;
|
||||
try {
|
||||
return vm.compileFunction(content, [
|
||||
'exports',
|
||||
'require',
|
||||
'module',
|
||||
'__filename',
|
||||
'__dirname',
|
||||
], {
|
||||
compiled = compileFunction(
|
||||
content,
|
||||
filename,
|
||||
importModuleDynamically(specifier) {
|
||||
const loader = asyncESM.ESMLoader;
|
||||
return loader.import(specifier, normalizeReferrerURL(filename));
|
||||
},
|
||||
});
|
||||
0,
|
||||
0,
|
||||
undefined,
|
||||
false,
|
||||
undefined,
|
||||
[],
|
||||
[
|
||||
'exports',
|
||||
'require',
|
||||
'module',
|
||||
'__filename',
|
||||
'__dirname',
|
||||
]
|
||||
);
|
||||
} catch (err) {
|
||||
if (process.mainModule === cjsModuleInstance)
|
||||
enrichCJSError(err);
|
||||
throw err;
|
||||
}
|
||||
|
||||
const { callbackMap } = internalBinding('module_wrap');
|
||||
callbackMap.set(compiled.cacheKey, {
|
||||
importModuleDynamically: async (specifier) => {
|
||||
const loader = asyncESM.ESMLoader;
|
||||
return loader.import(specifier, normalizeReferrerURL(filename));
|
||||
}
|
||||
});
|
||||
|
||||
return compiled.function;
|
||||
}
|
||||
|
||||
// Run the file contents in the correct scope or sandbox. Expose
|
||||
|
||||
17
lib/vm.js
17
lib/vm.js
@@ -313,7 +313,6 @@ function compileFunction(code, params, options = {}) {
|
||||
produceCachedData = false,
|
||||
parsingContext = undefined,
|
||||
contextExtensions = [],
|
||||
importModuleDynamically,
|
||||
} = options;
|
||||
|
||||
validateString(filename, 'options.filename');
|
||||
@@ -361,22 +360,6 @@ function compileFunction(code, params, options = {}) {
|
||||
result.function.cachedData = result.cachedData;
|
||||
}
|
||||
|
||||
if (importModuleDynamically !== undefined) {
|
||||
if (typeof importModuleDynamically !== 'function') {
|
||||
throw new ERR_INVALID_ARG_TYPE('options.importModuleDynamically',
|
||||
'function',
|
||||
importModuleDynamically);
|
||||
}
|
||||
const { importModuleDynamicallyWrap } =
|
||||
require('internal/vm/module');
|
||||
const { callbackMap } = internalBinding('module_wrap');
|
||||
const wrapped = importModuleDynamicallyWrap(importModuleDynamically);
|
||||
const func = result.function;
|
||||
callbackMap.set(result.cacheKey, {
|
||||
importModuleDynamically: (s, _k) => wrapped(s, func),
|
||||
});
|
||||
}
|
||||
|
||||
return result.function;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,7 @@ const {
|
||||
Module,
|
||||
SourceTextModule,
|
||||
SyntheticModule,
|
||||
createContext,
|
||||
compileFunction,
|
||||
createContext
|
||||
} = require('vm');
|
||||
const util = require('util');
|
||||
|
||||
@@ -158,19 +157,3 @@ const util = require('util');
|
||||
name: 'TypeError'
|
||||
});
|
||||
}
|
||||
|
||||
// Test compileFunction importModuleDynamically
|
||||
{
|
||||
const module = new SyntheticModule([], () => {});
|
||||
module.link(() => {});
|
||||
const f = compileFunction('return import("x")', [], {
|
||||
importModuleDynamically(specifier, referrer) {
|
||||
assert.strictEqual(specifier, 'x');
|
||||
assert.strictEqual(referrer, f);
|
||||
return module;
|
||||
},
|
||||
});
|
||||
f().then((ns) => {
|
||||
assert.strictEqual(ns, module.namespace);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -154,7 +154,6 @@ const customTypesMap = {
|
||||
'URLSearchParams': 'url.html#url_class_urlsearchparams',
|
||||
|
||||
'vm.Module': 'vm.html#vm_class_vm_module',
|
||||
'vm.Script': 'vm.html#vm_class_vm_script',
|
||||
'vm.SourceTextModule': 'vm.html#vm_class_vm_sourcetextmodule',
|
||||
|
||||
'MessagePort': 'worker_threads.html#worker_threads_class_messageport',
|
||||
|
||||
Reference in New Issue
Block a user