2025-08-29 13:17:18 +01:00
|
|
|
// Flags: --expose-internals --js-source-phase-imports
|
2020-07-14 17:45:39 +02:00
|
|
|
'use strict';
|
|
|
|
|
const common = require('../common');
|
2018-02-01 09:44:27 -06:00
|
|
|
const assert = require('assert');
|
|
|
|
|
|
2018-07-30 01:34:51 -07:00
|
|
|
const { internalBinding } = require('internal/test/binding');
|
|
|
|
|
const { ModuleWrap } = internalBinding('module_wrap');
|
2018-02-01 09:44:27 -06:00
|
|
|
|
2025-08-29 13:17:18 +01:00
|
|
|
async function testModuleWrap() {
|
|
|
|
|
const unlinked = new ModuleWrap('unlinked', undefined, 'export * from "bar";', 0, 0);
|
2025-09-11 13:54:24 +01:00
|
|
|
// `moduleWrap.hasAsyncGraph` is only available after been instantiated.
|
|
|
|
|
assert.throws(() => unlinked.hasAsyncGraph, {
|
|
|
|
|
code: 'ERR_MODULE_NOT_INSTANTIATED',
|
|
|
|
|
});
|
2025-08-29 13:17:18 +01:00
|
|
|
assert.throws(() => {
|
|
|
|
|
unlinked.instantiate();
|
|
|
|
|
}, {
|
|
|
|
|
code: 'ERR_VM_MODULE_LINK_FAILURE',
|
|
|
|
|
});
|
2025-07-29 13:17:52 +01:00
|
|
|
|
2025-08-29 13:17:18 +01:00
|
|
|
const dependsOnUnlinked = new ModuleWrap('dependsOnUnlinked', undefined, 'export * from "unlinked";', 0, 0);
|
|
|
|
|
dependsOnUnlinked.link([unlinked]);
|
|
|
|
|
assert.throws(() => {
|
|
|
|
|
dependsOnUnlinked.instantiate();
|
|
|
|
|
}, {
|
|
|
|
|
code: 'ERR_VM_MODULE_LINK_FAILURE',
|
|
|
|
|
});
|
2025-07-29 13:17:52 +01:00
|
|
|
|
2025-08-29 13:17:18 +01:00
|
|
|
const foo = new ModuleWrap('foo', undefined, 'export * from "bar";', 0, 0);
|
|
|
|
|
const bar = new ModuleWrap('bar', undefined, 'export const five = 5', 0, 0);
|
2018-02-01 09:44:27 -06:00
|
|
|
|
2024-04-25 11:33:15 +01:00
|
|
|
const moduleRequests = foo.getModuleRequests();
|
|
|
|
|
assert.strictEqual(moduleRequests.length, 1);
|
|
|
|
|
assert.strictEqual(moduleRequests[0].specifier, 'bar');
|
2018-02-01 09:44:27 -06:00
|
|
|
|
2025-09-11 13:54:24 +01:00
|
|
|
// `moduleWrap.hasAsyncGraph` is only available after been instantiated.
|
|
|
|
|
assert.throws(() => foo.hasAsyncGraph, {
|
|
|
|
|
code: 'ERR_MODULE_NOT_INSTANTIATED',
|
|
|
|
|
});
|
2025-07-04 20:56:08 +01:00
|
|
|
foo.link([bar]);
|
2018-02-01 09:44:27 -06:00
|
|
|
foo.instantiate();
|
2025-09-11 13:54:24 +01:00
|
|
|
// `moduleWrap.hasAsyncGraph` is accessible after been instantiated.
|
|
|
|
|
assert.strictEqual(bar.hasAsyncGraph, false);
|
|
|
|
|
assert.strictEqual(foo.hasAsyncGraph, false);
|
2018-02-01 09:44:27 -06:00
|
|
|
|
2019-11-11 22:30:02 -08:00
|
|
|
assert.strictEqual(await foo.evaluate(-1, false), undefined);
|
2019-09-09 12:20:16 -05:00
|
|
|
assert.strictEqual(foo.getNamespace().five, 5);
|
2024-04-25 11:33:15 +01:00
|
|
|
|
|
|
|
|
// Check that the module requests are the same after linking, instantiate, and evaluation.
|
|
|
|
|
assert.deepStrictEqual(moduleRequests, foo.getModuleRequests());
|
2025-08-29 13:17:18 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-11 13:54:24 +01:00
|
|
|
async function testAsyncGraph() {
|
|
|
|
|
const foo = new ModuleWrap('foo', undefined, 'export * from "bar";', 0, 0);
|
|
|
|
|
const bar = new ModuleWrap('bar', undefined, 'await undefined; export const five = 5', 0, 0);
|
|
|
|
|
|
|
|
|
|
const moduleRequests = foo.getModuleRequests();
|
|
|
|
|
assert.strictEqual(moduleRequests.length, 1);
|
|
|
|
|
assert.strictEqual(moduleRequests[0].specifier, 'bar');
|
|
|
|
|
|
|
|
|
|
// `moduleWrap.hasAsyncGraph` is only available after been instantiated.
|
|
|
|
|
assert.throws(() => foo.hasAsyncGraph, {
|
|
|
|
|
code: 'ERR_MODULE_NOT_INSTANTIATED',
|
|
|
|
|
});
|
|
|
|
|
foo.link([bar]);
|
|
|
|
|
foo.instantiate();
|
|
|
|
|
// `moduleWrap.hasAsyncGraph` is accessible after been instantiated.
|
|
|
|
|
assert.strictEqual(bar.hasAsyncGraph, true);
|
|
|
|
|
assert.strictEqual(foo.hasAsyncGraph, true);
|
|
|
|
|
|
|
|
|
|
const evalPromise = foo.evaluate(-1, false);
|
|
|
|
|
assert.throws(() => foo.getNamespace().five, ReferenceError);
|
|
|
|
|
assert.strictEqual(await evalPromise, undefined);
|
|
|
|
|
assert.strictEqual(foo.getNamespace().five, 5);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-29 13:17:18 +01:00
|
|
|
// Verify that linking two module with a same ModuleCacheKey throws an error.
|
|
|
|
|
function testLinkMismatch() {
|
|
|
|
|
const foo = new ModuleWrap('foo', undefined, `
|
|
|
|
|
import source BarSource from 'bar';
|
|
|
|
|
import bar from 'bar';
|
|
|
|
|
`, 0, 0);
|
|
|
|
|
const bar1 = new ModuleWrap('bar', undefined, 'export const five = 5', 0, 0);
|
|
|
|
|
const bar2 = new ModuleWrap('bar', undefined, 'export const six = 6', 0, 0);
|
2025-07-29 13:17:52 +01:00
|
|
|
|
2025-08-29 13:17:18 +01:00
|
|
|
const moduleRequests = foo.getModuleRequests();
|
|
|
|
|
assert.strictEqual(moduleRequests.length, 2);
|
|
|
|
|
assert.strictEqual(moduleRequests[0].specifier, moduleRequests[1].specifier);
|
|
|
|
|
assert.throws(() => {
|
|
|
|
|
foo.link([bar1, bar2]);
|
|
|
|
|
}, {
|
|
|
|
|
code: 'ERR_MODULE_LINK_MISMATCH',
|
|
|
|
|
// Test that ModuleCacheKey::ToString() is used in the error message.
|
2025-08-07 17:48:32 +02:00
|
|
|
message: `Module request 'ModuleCacheKey("bar")' at index 1 must be linked to the same module requested at index 0`
|
2025-08-29 13:17:18 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
|
await testModuleWrap();
|
2025-09-11 13:54:24 +01:00
|
|
|
await testAsyncGraph();
|
2025-08-29 13:17:18 +01:00
|
|
|
testLinkMismatch();
|
2020-07-14 17:45:39 +02:00
|
|
|
})().then(common.mustCall());
|