Skip to content

Commit

Permalink
Add stub for ESM
Browse files Browse the repository at this point in the history
PR-URL: #117
  • Loading branch information
tshemsedinov committed Oct 25, 2023
1 parent c530668 commit f2e0a2a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions metavm.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const CONTEXT_OPTIONS = {
const MODULE_TYPE = {
METARHIA: 1,
COMMONJS: 2,
ECMA: 3,
};

const DEFAULT = {
Expand Down Expand Up @@ -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 || '.';
Expand Down
18 changes: 18 additions & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

0 comments on commit f2e0a2a

Please sign in to comment.