11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Flags: --experimental-vm-modules
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciconst { SyntheticModule, SourceTextModule } = require('vm');
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci(async () => {
101cb0ef41Sopenharmony_ci  {
111cb0ef41Sopenharmony_ci    const s = new SyntheticModule(['x'], () => {
121cb0ef41Sopenharmony_ci      s.setExport('x', 1);
131cb0ef41Sopenharmony_ci    });
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci    const m = new SourceTextModule(`
161cb0ef41Sopenharmony_ci    import { x } from 'synthetic';
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci    export const getX = () => x;
191cb0ef41Sopenharmony_ci    `);
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci    await m.link(() => s);
221cb0ef41Sopenharmony_ci    await m.evaluate();
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci    assert.strictEqual(m.namespace.getX(), 1);
251cb0ef41Sopenharmony_ci    s.setExport('x', 42);
261cb0ef41Sopenharmony_ci    assert.strictEqual(m.namespace.getX(), 42);
271cb0ef41Sopenharmony_ci  }
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  {
301cb0ef41Sopenharmony_ci    const s = new SyntheticModule([], () => {
311cb0ef41Sopenharmony_ci      const p = Promise.reject();
321cb0ef41Sopenharmony_ci      p.catch(() => {});
331cb0ef41Sopenharmony_ci      return p;
341cb0ef41Sopenharmony_ci    });
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci    await s.link(common.mustNotCall());
371cb0ef41Sopenharmony_ci    assert.strictEqual(await s.evaluate(), undefined);
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  for (const invalidName of [1, Symbol.iterator, {}, [], null, true, 0]) {
411cb0ef41Sopenharmony_ci    const s = new SyntheticModule([], () => {});
421cb0ef41Sopenharmony_ci    await s.link(() => {});
431cb0ef41Sopenharmony_ci    assert.throws(() => {
441cb0ef41Sopenharmony_ci      s.setExport(invalidName, undefined);
451cb0ef41Sopenharmony_ci    }, {
461cb0ef41Sopenharmony_ci      name: 'TypeError',
471cb0ef41Sopenharmony_ci    });
481cb0ef41Sopenharmony_ci  }
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  {
511cb0ef41Sopenharmony_ci    const s = new SyntheticModule([], () => {});
521cb0ef41Sopenharmony_ci    await s.link(() => {});
531cb0ef41Sopenharmony_ci    assert.throws(() => {
541cb0ef41Sopenharmony_ci      s.setExport('does not exist');
551cb0ef41Sopenharmony_ci    }, {
561cb0ef41Sopenharmony_ci      name: 'ReferenceError',
571cb0ef41Sopenharmony_ci    });
581cb0ef41Sopenharmony_ci  }
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  {
611cb0ef41Sopenharmony_ci    const s = new SyntheticModule([], () => {});
621cb0ef41Sopenharmony_ci    assert.throws(() => {
631cb0ef41Sopenharmony_ci      s.setExport('name', 'value');
641cb0ef41Sopenharmony_ci    }, {
651cb0ef41Sopenharmony_ci      code: 'ERR_VM_MODULE_STATUS',
661cb0ef41Sopenharmony_ci    });
671cb0ef41Sopenharmony_ci  }
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  {
701cb0ef41Sopenharmony_ci    assert.throws(() => {
711cb0ef41Sopenharmony_ci      SyntheticModule.prototype.setExport.call({}, 'foo');
721cb0ef41Sopenharmony_ci    }, {
731cb0ef41Sopenharmony_ci      code: 'ERR_VM_MODULE_NOT_MODULE',
741cb0ef41Sopenharmony_ci      message: /Provided module is not an instance of Module/
751cb0ef41Sopenharmony_ci    });
761cb0ef41Sopenharmony_ci  }
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci})().then(common.mustCall());
79