Split `module.link(linker)` into two synchronous step `sourceTextModule.linkRequests()` and `sourceTextModule.instantiate()`. This allows creating vm modules and resolving the dependencies in a complete synchronous procedure. This also makes `syntheticModule.link()` redundant. The link step for a SyntheticModule is no-op and is already taken care in the constructor by initializing the binding slots with the given export names. PR-URL: https://github.com/nodejs/node/pull/59000 Backport-PR-URL: https://github.com/nodejs/node/pull/60152 Refs: https://github.com/nodejs/node/issues/37648 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
44 lines
1.4 KiB
JavaScript
44 lines
1.4 KiB
JavaScript
// Flags: --expose-internals
|
|
'use strict';
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
|
|
const { internalBinding } = require('internal/test/binding');
|
|
const { ModuleWrap } = internalBinding('module_wrap');
|
|
|
|
async function testModuleWrap() {
|
|
const unlinked = new ModuleWrap('unlinked', undefined, 'export * from "bar";', 0, 0);
|
|
assert.throws(() => {
|
|
unlinked.instantiate();
|
|
}, {
|
|
code: 'ERR_VM_MODULE_LINK_FAILURE',
|
|
});
|
|
|
|
const dependsOnUnlinked = new ModuleWrap('dependsOnUnlinked', undefined, 'export * from "unlinked";', 0, 0);
|
|
dependsOnUnlinked.link([unlinked]);
|
|
assert.throws(() => {
|
|
dependsOnUnlinked.instantiate();
|
|
}, {
|
|
code: 'ERR_VM_MODULE_LINK_FAILURE',
|
|
});
|
|
|
|
const foo = new ModuleWrap('foo', undefined, 'export * from "bar";', 0, 0);
|
|
const bar = new ModuleWrap('bar', undefined, 'export const five = 5', 0, 0);
|
|
|
|
const moduleRequests = foo.getModuleRequests();
|
|
assert.strictEqual(moduleRequests.length, 1);
|
|
assert.strictEqual(moduleRequests[0].specifier, 'bar');
|
|
|
|
foo.link([bar]);
|
|
foo.instantiate();
|
|
|
|
assert.strictEqual(await foo.evaluate(-1, false), undefined);
|
|
assert.strictEqual(foo.getNamespace().five, 5);
|
|
|
|
// Check that the module requests are the same after linking, instantiate, and evaluation.
|
|
assert.deepStrictEqual(moduleRequests, foo.getModuleRequests());
|
|
}
|
|
|
|
(async () => {
|
|
await testModuleWrap();
|
|
})().then(common.mustCall());
|