• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Flags: --experimental-vm-modules
4
5const common = require('../common');
6
7const assert = require('assert');
8const { Script, SourceTextModule } = require('vm');
9
10async function testNoCallback() {
11  const m = new SourceTextModule(`
12    globalThis.importResult = import("foo");
13    globalThis.importResult.catch(() => {});
14  `);
15  await m.link(common.mustNotCall());
16  await m.evaluate();
17  let threw = false;
18  try {
19    await globalThis.importResult;
20  } catch (err) {
21    threw = true;
22    assert.strictEqual(err.code, 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING');
23  }
24  delete globalThis.importResult;
25  assert(threw);
26}
27
28async function test() {
29  const foo = new SourceTextModule('export const a = 1;');
30  await foo.link(common.mustNotCall());
31  await foo.evaluate();
32
33  {
34    const s = new Script('import("foo")', {
35      importModuleDynamically: common.mustCall((specifier, wrap) => {
36        assert.strictEqual(specifier, 'foo');
37        assert.strictEqual(wrap, s);
38        return foo;
39      }),
40    });
41
42    const result = s.runInThisContext();
43    assert.strictEqual(foo.namespace, await result);
44  }
45
46  {
47    const m = new SourceTextModule('globalThis.fooResult = import("foo")', {
48      importModuleDynamically: common.mustCall((specifier, wrap) => {
49        assert.strictEqual(specifier, 'foo');
50        assert.strictEqual(wrap, m);
51        return foo;
52      }),
53    });
54    await m.link(common.mustNotCall());
55    await m.evaluate();
56    assert.strictEqual(foo.namespace, await globalThis.fooResult);
57    delete globalThis.fooResult;
58  }
59
60  {
61    const s = new Script('import("foo", { with: { key: "value" } })', {
62      importModuleDynamically: common.mustCall((specifier, wrap, attributes) => {
63        assert.strictEqual(specifier, 'foo');
64        assert.strictEqual(wrap, s);
65        assert.deepStrictEqual(attributes, { __proto__: null, key: 'value' });
66        return foo;
67      }),
68    });
69
70    const result = s.runInThisContext();
71    assert.strictEqual(foo.namespace, await result);
72  }
73}
74
75async function testInvalid() {
76  const m = new SourceTextModule('globalThis.fooResult = import("foo")', {
77    importModuleDynamically: common.mustCall((specifier, wrap) => {
78      return 5;
79    }),
80  });
81  await m.link(common.mustNotCall());
82  await m.evaluate();
83  await globalThis.fooResult.catch(common.mustCall((e) => {
84    assert.strictEqual(e.code, 'ERR_VM_MODULE_NOT_MODULE');
85  }));
86  delete globalThis.fooResult;
87
88  const s = new Script('import("bar")', {
89    importModuleDynamically: common.mustCall((specifier, wrap) => {
90      return undefined;
91    }),
92  });
93  let threw = false;
94  try {
95    await s.runInThisContext();
96  } catch (e) {
97    threw = true;
98    assert.strictEqual(e.code, 'ERR_VM_MODULE_NOT_MODULE');
99  }
100  assert(threw);
101}
102
103async function testInvalidimportModuleDynamically() {
104  assert.throws(
105    () => new Script(
106      'import("foo")',
107      { importModuleDynamically: false }),
108    { code: 'ERR_INVALID_ARG_TYPE' }
109  );
110}
111
112(async function() {
113  await testNoCallback();
114  await test();
115  await testInvalid();
116  await testInvalidimportModuleDynamically();
117}()).then(common.mustCall());
118