• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Flags: --experimental-vm-modules
4
5require('../common');
6const { SyntheticModule, SourceTextModule } = require('vm');
7const assert = require('assert');
8
9(async () => {
10  {
11    const s = new SyntheticModule(['x'], () => {
12      s.setExport('x', 1);
13    });
14
15    const m = new SourceTextModule(`
16    import { x } from 'synthetic';
17
18    export const getX = () => x;
19    `);
20
21    await m.link(() => s);
22    await m.evaluate();
23
24    assert.strictEqual(m.namespace.getX(), 1);
25    s.setExport('x', 42);
26    assert.strictEqual(m.namespace.getX(), 42);
27  }
28
29  for (const invalidName of [1, Symbol.iterator, {}, [], null, true, 0]) {
30    const s = new SyntheticModule([], () => {});
31    await s.link(() => {});
32    assert.throws(() => {
33      s.setExport(invalidName, undefined);
34    }, {
35      name: 'TypeError',
36    });
37  }
38
39  {
40    const s = new SyntheticModule([], () => {});
41    await s.link(() => {});
42    assert.throws(() => {
43      s.setExport('does not exist');
44    }, {
45      name: 'ReferenceError',
46    });
47  }
48
49  {
50    const s = new SyntheticModule([], () => {});
51    assert.throws(() => {
52      s.setExport('name', 'value');
53    }, {
54      code: 'ERR_VM_MODULE_STATUS',
55    });
56  }
57})();
58