From f2e0a2a6ff36189976c5db73bb9428c3dbb976e5 Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Tue, 24 Oct 2023 22:27:43 +0300 Subject: [PATCH] Add stub for ESM PR-URL: https://github.com/metarhia/metavm/pull/117 --- metavm.js | 4 ++++ test/unit.js | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/metavm.js b/metavm.js index 45c990b..5ccbc12 100644 --- a/metavm.js +++ b/metavm.js @@ -19,6 +19,7 @@ const CONTEXT_OPTIONS = { const MODULE_TYPE = { METARHIA: 1, COMMONJS: 2, + ECMA: 3, }; const DEFAULT = { @@ -87,6 +88,9 @@ const internalRequire = require; class MetaScript { constructor(name, src, options = {}) { + if (options.type === MODULE_TYPE.ECMA) { + throw new Error('ECMAScript modules is not supported'); + } this.name = name; this.dirname = options.dirname || process.cwd(); this.relative = options.relative || '.'; diff --git a/test/unit.js b/test/unit.js index ab2da8b..22055e5 100644 --- a/test/unit.js +++ b/test/unit.js @@ -455,3 +455,21 @@ test('Check native fetch', async () => { assert.ok(proto); assert.strictEqual(proto.constructor.name, 'AsyncFunction'); }); + +test('ECMAScript modules', async () => { + const sandbox = {}; + sandbox.global = sandbox; + const src = `const fn = x => x; +export { fn }; +`; + try { + const ms = metavm.createScript('Example', src, { + context: metavm.createContext(sandbox), + dirname: __dirname, + type: metavm.MODULE_TYPE.ECMA, + }); + test.fail(ms); + } catch (err) { + assert.ok(err); + } +});