1'use strict'; 2 3// Flags: --experimental-vm-modules 4 5const common = require('../common'); 6const assert = require('assert'); 7const { SourceTextModule } = require('vm'); 8 9async function testBasic() { 10 const m = new SourceTextModule('import.meta;', { 11 initializeImportMeta: common.mustCall((meta, module) => { 12 assert.strictEqual(module, m); 13 meta.prop = 42; 14 }) 15 }); 16 await m.link(common.mustNotCall()); 17 const { result } = await m.evaluate(); 18 assert.strictEqual(typeof result, 'object'); 19 assert.strictEqual(Object.getPrototypeOf(result), null); 20 assert.strictEqual(result.prop, 42); 21 assert.deepStrictEqual(Reflect.ownKeys(result), ['prop']); 22} 23 24async function testInvalid() { 25 for (const invalidValue of [ 26 null, {}, 0, Symbol.iterator, [], 'string', false 27 ]) { 28 assert.throws(() => { 29 new SourceTextModule('', { 30 initializeImportMeta: invalidValue 31 }); 32 }, { 33 code: 'ERR_INVALID_ARG_TYPE', 34 name: 'TypeError' 35 }); 36 } 37} 38 39(async () => { 40 await testBasic(); 41 await testInvalid(); 42})(); 43