mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
86afefafda
A referrer can be a Script Record, a Cyclic Module Record, or a Realm Record as defined in https://tc39.es/ecma262/#sec-HostLoadImportedModule. Add support for dynamic import calls with a realm as the referrer and allow specifying an `importModuleDynamically` callback in `vm.createContext`. PR-URL: https://github.com/nodejs/node/pull/50360 Refs: https://github.com/nodejs/node/issues/49726 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
71 lines
1.9 KiB
JavaScript
71 lines
1.9 KiB
JavaScript
// Flags: --experimental-vm-modules
|
|
import * as common from '../common/index.mjs';
|
|
import assert from 'node:assert';
|
|
import { Script, SourceTextModule, createContext } from 'node:vm';
|
|
|
|
async function test() {
|
|
const foo = new SourceTextModule('export const a = 1;');
|
|
await foo.link(common.mustNotCall());
|
|
await foo.evaluate();
|
|
|
|
const ctx = createContext({}, {
|
|
importModuleDynamically: common.mustCall((specifier, wrap) => {
|
|
assert.strictEqual(specifier, 'foo');
|
|
assert.strictEqual(wrap, ctx);
|
|
return foo;
|
|
}, 2),
|
|
});
|
|
{
|
|
const s = new Script('Promise.resolve("import(\'foo\')").then(eval)', {
|
|
importModuleDynamically: common.mustNotCall(),
|
|
});
|
|
|
|
const result = s.runInContext(ctx);
|
|
assert.strictEqual(await result, foo.namespace);
|
|
}
|
|
|
|
{
|
|
const m = new SourceTextModule('globalThis.fooResult = Promise.resolve("import(\'foo\')").then(eval)', {
|
|
context: ctx,
|
|
importModuleDynamically: common.mustNotCall(),
|
|
});
|
|
await m.link(common.mustNotCall());
|
|
await m.evaluate();
|
|
assert.strictEqual(await ctx.fooResult, foo.namespace);
|
|
delete ctx.fooResult;
|
|
}
|
|
}
|
|
|
|
async function testMissing() {
|
|
const ctx = createContext({});
|
|
{
|
|
const s = new Script('Promise.resolve("import(\'foo\')").then(eval)', {
|
|
importModuleDynamically: common.mustNotCall(),
|
|
});
|
|
|
|
const result = s.runInContext(ctx);
|
|
await assert.rejects(result, {
|
|
code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING',
|
|
});
|
|
}
|
|
|
|
{
|
|
const m = new SourceTextModule('globalThis.fooResult = Promise.resolve("import(\'foo\')").then(eval)', {
|
|
context: ctx,
|
|
importModuleDynamically: common.mustNotCall(),
|
|
});
|
|
await m.link(common.mustNotCall());
|
|
await m.evaluate();
|
|
|
|
await assert.rejects(ctx.fooResult, {
|
|
code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING',
|
|
});
|
|
delete ctx.fooResult;
|
|
}
|
|
}
|
|
|
|
await Promise.all([
|
|
test(),
|
|
testMissing(),
|
|
]).then(common.mustCall());
|