diff --git a/doc/api/cli.md b/doc/api/cli.md index 88972b74e8cd62..0bce5edb7ab4f6 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -33,7 +33,8 @@ If a file is found, its path will be passed to the * The program was started with a command-line flag that forces the entry point to be loaded with ECMAScript module loader, such as `--import`. -* The file has an `.mjs` extension. +* The file has an `.mjs` or `.wasm` (with `--experimental-wasm-modules`) + extension. * The file does not have a `.cjs` extension, and the nearest parent `package.json` file contains a top-level [`"type"`][] field with a value of `"module"`. diff --git a/lib/internal/modules/run_main.js b/lib/internal/modules/run_main.js index 044926b4138d79..48ea74f2a79ef5 100644 --- a/lib/internal/modules/run_main.js +++ b/lib/internal/modules/run_main.js @@ -62,6 +62,7 @@ function shouldUseESMLoader(mainPath) { // Determine the module format of the entry point. if (mainPath && StringPrototypeEndsWith(mainPath, '.mjs')) { return true; } + if (mainPath && StringPrototypeEndsWith(mainPath, '.wasm')) { return true; } if (!mainPath || StringPrototypeEndsWith(mainPath, '.cjs')) { return false; } if (getOptionValue('--experimental-strip-types')) { diff --git a/test/es-module/test-esm-wasm.mjs b/test/es-module/test-esm-wasm.mjs index cdfcad517efb7c..b7a9230dd184b2 100644 --- a/test/es-module/test-esm-wasm.mjs +++ b/test/es-module/test-esm-wasm.mjs @@ -91,6 +91,18 @@ describe('ESM: WASM modules', { concurrency: !process.env.TEST_PARALLEL }, () => match(stderr, /WebAssembly/); }); + it('should support top-level execution', async () => { + const { code, stderr, stdout } = await spawnPromisified(execPath, [ + '--no-warnings', + '--experimental-wasm-modules', + fixtures.path('es-modules/top-level-wasm.wasm'), + ]); + + strictEqual(stderr, ''); + strictEqual(stdout, '[Object: null prototype] { prop: \'hello world\' }\n'); + strictEqual(code, 0); + }); + it('should support static source phase imports', async () => { const { code, stderr, stdout } = await spawnPromisified(execPath, [ '--no-warnings', diff --git a/test/fixtures/es-modules/top-level-wasm.wasm b/test/fixtures/es-modules/top-level-wasm.wasm new file mode 100644 index 00000000000000..085472e7c35be7 Binary files /dev/null and b/test/fixtures/es-modules/top-level-wasm.wasm differ diff --git a/test/fixtures/es-modules/wasm-function.js b/test/fixtures/es-modules/wasm-function.js new file mode 100644 index 00000000000000..b33b08a10e7ad4 --- /dev/null +++ b/test/fixtures/es-modules/wasm-function.js @@ -0,0 +1,11 @@ +export function call1 (func, thisObj, arg0) { + return func.call(thisObj, arg0); +} + +export function call2 (func, thisObj, arg0, arg1) { + return func.call(thisObj, arg0, arg1); +} + +export function call3 (func, thisObj, arg0, arg1, arg2) { + return func.call(thisObj, arg0, arg1, arg2); +} diff --git a/test/fixtures/es-modules/wasm-object.js b/test/fixtures/es-modules/wasm-object.js new file mode 100644 index 00000000000000..70318fea8acead --- /dev/null +++ b/test/fixtures/es-modules/wasm-object.js @@ -0,0 +1,3 @@ +export const { get: getProperty, set: setProperty } = Reflect; +export const { create } = Object; +export const global = globalThis; diff --git a/test/fixtures/es-modules/wasm-string-constants.js b/test/fixtures/es-modules/wasm-string-constants.js new file mode 100644 index 00000000000000..89cbd44f3449bb --- /dev/null +++ b/test/fixtures/es-modules/wasm-string-constants.js @@ -0,0 +1,6 @@ +const console = 'console'; +const hello_world = 'hello world'; +const log = 'log'; +const prop = 'prop'; + +export { console, hello_world as 'hello world', log, prop }