1'use strict';
2
3// Flags: --experimental-vm-modules
4
5const common = require('../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  {
30    const s = new SyntheticModule([], () => {
31      const p = Promise.reject();
32      p.catch(() => {});
33      return p;
34    });
35
36    await s.link(common.mustNotCall());
37    assert.strictEqual(await s.evaluate(), undefined);
38  }
39
40  for (const invalidName of [1, Symbol.iterator, {}, [], null, true, 0]) {
41    const s = new SyntheticModule([], () => {});
42    await s.link(() => {});
43    assert.throws(() => {
44      s.setExport(invalidName, undefined);
45    }, {
46      name: 'TypeError',
47    });
48  }
49
50  {
51    const s = new SyntheticModule([], () => {});
52    await s.link(() => {});
53    assert.throws(() => {
54      s.setExport('does not exist');
55    }, {
56      name: 'ReferenceError',
57    });
58  }
59
60  {
61    const s = new SyntheticModule([], () => {});
62    assert.throws(() => {
63      s.setExport('name', 'value');
64    }, {
65      code: 'ERR_VM_MODULE_STATUS',
66    });
67  }
68
69  {
70    assert.throws(() => {
71      SyntheticModule.prototype.setExport.call({}, 'foo');
72    }, {
73      code: 'ERR_VM_MODULE_NOT_MODULE',
74      message: /Provided module is not an instance of Module/
75    });
76  }
77
78})().then(common.mustCall());
79