mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 12:20:27 +01:00
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 Refs: https://github.com/nodejs/node/issues/37648 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
68 lines
1.7 KiB
JavaScript
68 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
// Flags: --experimental-vm-modules --js-source-phase-imports
|
|
|
|
require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const { SourceTextModule } = require('vm');
|
|
const test = require('node:test');
|
|
|
|
test('simple graph', async function simple() {
|
|
const foo = new SourceTextModule('export default 5;');
|
|
foo.linkRequests([]);
|
|
|
|
globalThis.fiveResult = undefined;
|
|
const bar = new SourceTextModule('import five from "foo"; fiveResult = five');
|
|
|
|
bar.linkRequests([foo]);
|
|
bar.instantiate();
|
|
|
|
await bar.evaluate();
|
|
assert.strictEqual(globalThis.fiveResult, 5);
|
|
delete globalThis.fiveResult;
|
|
});
|
|
|
|
test('invalid link values', () => {
|
|
const invalidValues = [
|
|
undefined,
|
|
null,
|
|
{},
|
|
SourceTextModule.prototype,
|
|
];
|
|
|
|
for (const value of invalidValues) {
|
|
const module = new SourceTextModule('import "foo"');
|
|
assert.throws(() => module.linkRequests([value]), {
|
|
code: 'ERR_VM_MODULE_NOT_MODULE',
|
|
});
|
|
}
|
|
});
|
|
|
|
test('mismatch linkage', () => {
|
|
const foo = new SourceTextModule('export default 5;');
|
|
foo.linkRequests([]);
|
|
|
|
// Link with more modules than requested.
|
|
const bar = new SourceTextModule('import foo from "foo";');
|
|
assert.throws(() => bar.linkRequests([foo, foo]), {
|
|
code: 'ERR_MODULE_LINK_MISMATCH',
|
|
});
|
|
|
|
// Link with fewer modules than requested.
|
|
const baz = new SourceTextModule('import foo from "foo"; import bar from "bar";');
|
|
assert.throws(() => baz.linkRequests([foo]), {
|
|
code: 'ERR_MODULE_LINK_MISMATCH',
|
|
});
|
|
|
|
// Link a same module cache key with different instances.
|
|
const qux = new SourceTextModule(`
|
|
import foo from "foo";
|
|
import source Foo from "foo";
|
|
`);
|
|
assert.throws(() => qux.linkRequests([foo, bar]), {
|
|
code: 'ERR_MODULE_LINK_MISMATCH',
|
|
});
|
|
});
|