Skip to content

Commit

Permalink
fixup! vm: support using the default loader to handle dynamic import()
Browse files Browse the repository at this point in the history
Co-authored-by: Brian Muenzenmeyer <[email protected]>
Co-authored-by: Geoffrey Booth <[email protected]>
Co-authored-by: Michaël Zasso <[email protected]>
Co-authored-by: Antoine du Hamel <[email protected]>
  • Loading branch information
5 people authored Dec 21, 2023
1 parent 5b2655d commit f99b2d0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 35 deletions.
8 changes: 4 additions & 4 deletions doc/api/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ changes:
**Default:** `false`.
* `importModuleDynamically`
{Function|vm.constants.USE\_MAIN\_CONTEXT\_DEFAULT\_LOADER}
Used to specify the how the modules should be loaded during the evaluation
Used to specify how the modules should be loaded during the evaluation
of this script when `import()` is called. This option is part of the
experimental modules API. We do not recommend using it in a production
environment. For detailed information, see
Expand Down Expand Up @@ -1044,15 +1044,15 @@ added: REPLACEME

* {Object}

Returns an object containing commonly used constants for vm operations.
Returns an object containing commonly used constants for VM operations.

### `vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental
> Stability: 1.1 - Active development

A constant that can be used as the `importModuleDynamically` option to
`vm.Script` and `vm.compileFunction()` so that Node.js uses the default
Expand Down Expand Up @@ -1632,7 +1632,7 @@ using it in a production environment.

### When the `importModuleDynamically` option is not specified or undefined

If this option is not specified, or if it's undefined, code containing
If this option is not specified, or if it's `undefined`, code containing
`import()` can still be compiled by the vm APIs, but when the compiled code is
executed and it actually calls `import()`, the result will reject with
[`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][].
Expand Down
35 changes: 4 additions & 31 deletions test/es-module/test-vm-main-context-default-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,18 @@ assert(
!process.execArgv.includes('--experimental-vm-modules'),
'This test must be run without --experimental-vm-modules');

assert(typeof USE_MAIN_CONTEXT_DEFAULT_LOADER, 'symbol');
assert.strictEqual(typeof USE_MAIN_CONTEXT_DEFAULT_LOADER, 'symbol');

async function testNotFoundErrors(options) {
// Import user modules.
const script = new Script('import("./message.mjs")', options);
// Use try-catch for better async stack traces in the logs.
let result = 'uncaught-fallback';
try {
result = await script.runInThisContext();
} catch (error) {
assert.strictEqual(error.code, 'ERR_MODULE_NOT_FOUND');
} finally {
assert.strictEqual(
result, 'uncaught-fallback',
`import from vm.Script with ${inspect(options)} should throw ERR_MODULE_NOT_FOUND`);
}
await assert.rejects(script.runInThisContext(), { code: 'ERR_MODULE_NOT_FOUND' });

result = 'uncaught-fallback';

Check failure on line 32 in test/es-module/test-vm-main-context-default-loader.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'result' is not defined
const imported = compileFunction('return import("./message.mjs")', [], options)();
// Use try-catch for better async stack traces in the logs.
try {
result = await imported;
} catch (error) {
assert.strictEqual(error.code, 'ERR_MODULE_NOT_FOUND');
} finally {
assert.strictEqual(
result, 'uncaught-fallback',
`import from vm.compileFunction with ${inspect(options)} should throw ERR_MODULE_NOT_FOUND`);
}
await assert.rejects(imported, { code: 'ERR_MODULE_NOT_FOUND' });
}

async function testLoader(options) {
Expand Down Expand Up @@ -135,17 +118,7 @@ async function main() {
const s = new Script('Promise.resolve("import(\'./message.mjs\')").then(eval)', {
importModuleDynamically: common.mustNotCall(),
});
// Use try-catch for better async stack traces in the logs.
let result = 'uncaught-fallback';
try {
result = await s.runInContext(ctx);
} catch (error) {
assert.strictEqual(error.code, 'ERR_MODULE_NOT_FOUND');
} finally {
assert.strictEqual(
result, 'uncaught-fallback',
`import from vm.createContext() with ${inspect(options)} should throw ERR_MODULE_NOT_FOUND`);
}
await assert.rejects(s.runInContext(ctx), { code: 'ERR_MODULE_NOT_FOUND' });
}

await testLoader(undefinedOptions);
Expand Down

0 comments on commit f99b2d0

Please sign in to comment.