11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Flags: --experimental-vm-modules
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst { Script, SourceTextModule } = require('vm');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciasync function testNoCallback() {
111cb0ef41Sopenharmony_ci  const m = new SourceTextModule(`
121cb0ef41Sopenharmony_ci    globalThis.importResult = import("foo");
131cb0ef41Sopenharmony_ci    globalThis.importResult.catch(() => {});
141cb0ef41Sopenharmony_ci  `);
151cb0ef41Sopenharmony_ci  await m.link(common.mustNotCall());
161cb0ef41Sopenharmony_ci  await m.evaluate();
171cb0ef41Sopenharmony_ci  let threw = false;
181cb0ef41Sopenharmony_ci  try {
191cb0ef41Sopenharmony_ci    await globalThis.importResult;
201cb0ef41Sopenharmony_ci  } catch (err) {
211cb0ef41Sopenharmony_ci    threw = true;
221cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING');
231cb0ef41Sopenharmony_ci  }
241cb0ef41Sopenharmony_ci  delete globalThis.importResult;
251cb0ef41Sopenharmony_ci  assert(threw);
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciasync function test() {
291cb0ef41Sopenharmony_ci  const foo = new SourceTextModule('export const a = 1;');
301cb0ef41Sopenharmony_ci  await foo.link(common.mustNotCall());
311cb0ef41Sopenharmony_ci  await foo.evaluate();
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  {
341cb0ef41Sopenharmony_ci    const s = new Script('import("foo")', {
351cb0ef41Sopenharmony_ci      importModuleDynamically: common.mustCall((specifier, wrap) => {
361cb0ef41Sopenharmony_ci        assert.strictEqual(specifier, 'foo');
371cb0ef41Sopenharmony_ci        assert.strictEqual(wrap, s);
381cb0ef41Sopenharmony_ci        return foo;
391cb0ef41Sopenharmony_ci      }),
401cb0ef41Sopenharmony_ci    });
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci    const result = s.runInThisContext();
431cb0ef41Sopenharmony_ci    assert.strictEqual(foo.namespace, await result);
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  {
471cb0ef41Sopenharmony_ci    const m = new SourceTextModule('globalThis.fooResult = import("foo")', {
481cb0ef41Sopenharmony_ci      importModuleDynamically: common.mustCall((specifier, wrap) => {
491cb0ef41Sopenharmony_ci        assert.strictEqual(specifier, 'foo');
501cb0ef41Sopenharmony_ci        assert.strictEqual(wrap, m);
511cb0ef41Sopenharmony_ci        return foo;
521cb0ef41Sopenharmony_ci      }),
531cb0ef41Sopenharmony_ci    });
541cb0ef41Sopenharmony_ci    await m.link(common.mustNotCall());
551cb0ef41Sopenharmony_ci    await m.evaluate();
561cb0ef41Sopenharmony_ci    assert.strictEqual(foo.namespace, await globalThis.fooResult);
571cb0ef41Sopenharmony_ci    delete globalThis.fooResult;
581cb0ef41Sopenharmony_ci  }
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  {
611cb0ef41Sopenharmony_ci    const s = new Script('import("foo", { with: { key: "value" } })', {
621cb0ef41Sopenharmony_ci      importModuleDynamically: common.mustCall((specifier, wrap, attributes) => {
631cb0ef41Sopenharmony_ci        assert.strictEqual(specifier, 'foo');
641cb0ef41Sopenharmony_ci        assert.strictEqual(wrap, s);
651cb0ef41Sopenharmony_ci        assert.deepStrictEqual(attributes, { __proto__: null, key: 'value' });
661cb0ef41Sopenharmony_ci        return foo;
671cb0ef41Sopenharmony_ci      }),
681cb0ef41Sopenharmony_ci    });
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci    const result = s.runInThisContext();
711cb0ef41Sopenharmony_ci    assert.strictEqual(foo.namespace, await result);
721cb0ef41Sopenharmony_ci  }
731cb0ef41Sopenharmony_ci}
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ciasync function testInvalid() {
761cb0ef41Sopenharmony_ci  const m = new SourceTextModule('globalThis.fooResult = import("foo")', {
771cb0ef41Sopenharmony_ci    importModuleDynamically: common.mustCall((specifier, wrap) => {
781cb0ef41Sopenharmony_ci      return 5;
791cb0ef41Sopenharmony_ci    }),
801cb0ef41Sopenharmony_ci  });
811cb0ef41Sopenharmony_ci  await m.link(common.mustNotCall());
821cb0ef41Sopenharmony_ci  await m.evaluate();
831cb0ef41Sopenharmony_ci  await globalThis.fooResult.catch(common.mustCall((e) => {
841cb0ef41Sopenharmony_ci    assert.strictEqual(e.code, 'ERR_VM_MODULE_NOT_MODULE');
851cb0ef41Sopenharmony_ci  }));
861cb0ef41Sopenharmony_ci  delete globalThis.fooResult;
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  const s = new Script('import("bar")', {
891cb0ef41Sopenharmony_ci    importModuleDynamically: common.mustCall((specifier, wrap) => {
901cb0ef41Sopenharmony_ci      return undefined;
911cb0ef41Sopenharmony_ci    }),
921cb0ef41Sopenharmony_ci  });
931cb0ef41Sopenharmony_ci  let threw = false;
941cb0ef41Sopenharmony_ci  try {
951cb0ef41Sopenharmony_ci    await s.runInThisContext();
961cb0ef41Sopenharmony_ci  } catch (e) {
971cb0ef41Sopenharmony_ci    threw = true;
981cb0ef41Sopenharmony_ci    assert.strictEqual(e.code, 'ERR_VM_MODULE_NOT_MODULE');
991cb0ef41Sopenharmony_ci  }
1001cb0ef41Sopenharmony_ci  assert(threw);
1011cb0ef41Sopenharmony_ci}
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ciasync function testInvalidimportModuleDynamically() {
1041cb0ef41Sopenharmony_ci  assert.throws(
1051cb0ef41Sopenharmony_ci    () => new Script(
1061cb0ef41Sopenharmony_ci      'import("foo")',
1071cb0ef41Sopenharmony_ci      { importModuleDynamically: false }),
1081cb0ef41Sopenharmony_ci    { code: 'ERR_INVALID_ARG_TYPE' }
1091cb0ef41Sopenharmony_ci  );
1101cb0ef41Sopenharmony_ci}
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci(async function() {
1131cb0ef41Sopenharmony_ci  await testNoCallback();
1141cb0ef41Sopenharmony_ci  await test();
1151cb0ef41Sopenharmony_ci  await testInvalid();
1161cb0ef41Sopenharmony_ci  await testInvalidimportModuleDynamically();
1171cb0ef41Sopenharmony_ci}()).then(common.mustCall());
118