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>
74 lines
1.7 KiB
JavaScript
74 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
// Flags: --experimental-vm-modules
|
|
|
|
const common = require('../common');
|
|
const { SyntheticModule, SourceTextModule } = require('vm');
|
|
const assert = require('assert');
|
|
|
|
(async () => {
|
|
{
|
|
const s = new SyntheticModule(['x'], () => {
|
|
s.setExport('x', 1);
|
|
});
|
|
|
|
const m = new SourceTextModule(`
|
|
import { x } from 'synthetic';
|
|
|
|
export const getX = () => x;
|
|
`);
|
|
|
|
await m.link(() => s);
|
|
await m.evaluate();
|
|
|
|
assert.strictEqual(m.namespace.getX(), 1);
|
|
s.setExport('x', 42);
|
|
assert.strictEqual(m.namespace.getX(), 42);
|
|
}
|
|
|
|
{
|
|
const s = new SyntheticModule([], () => {
|
|
const p = Promise.reject();
|
|
p.catch(() => {});
|
|
return p;
|
|
});
|
|
|
|
await s.link(common.mustNotCall());
|
|
assert.strictEqual(await s.evaluate(), undefined);
|
|
}
|
|
|
|
for (const invalidName of [1, Symbol.iterator, {}, [], null, true, 0]) {
|
|
const s = new SyntheticModule([], () => {});
|
|
await s.link(() => {});
|
|
assert.throws(() => {
|
|
s.setExport(invalidName, undefined);
|
|
}, {
|
|
name: 'TypeError',
|
|
});
|
|
}
|
|
|
|
{
|
|
const s = new SyntheticModule([], () => {});
|
|
await s.link(() => {});
|
|
assert.throws(() => {
|
|
s.setExport('does not exist');
|
|
}, {
|
|
name: 'ReferenceError',
|
|
});
|
|
}
|
|
|
|
{
|
|
const s = new SyntheticModule(['name'], () => {});
|
|
// Exports of SyntheticModule can be immediately set after creation.
|
|
// No link is required.
|
|
s.setExport('name', 'value');
|
|
}
|
|
|
|
for (const value of [null, {}, SyntheticModule.prototype]) {
|
|
assert.throws(() => {
|
|
SyntheticModule.prototype.setExport.call(value, 'foo');
|
|
}, { code: 'ERR_INVALID_THIS' });
|
|
}
|
|
|
|
})().then(common.mustCall());
|