From 98d440adaff5856fbc107d9a70ce26e9abff0b41 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Mon, 24 Mar 2025 14:09:21 -0700 Subject: [PATCH] esm: support top-level Wasm without package type --- doc/api/cli.md | 3 ++- lib/internal/modules/run_main.js | 1 + test/es-module/test-esm-wasm.mjs | 12 ++++++++++++ test/fixtures/es-modules/top-level-wasm.wasm | Bin 0 -> 490 bytes test/fixtures/es-modules/wasm-function.js | 11 +++++++++++ test/fixtures/es-modules/wasm-object.js | 3 +++ test/fixtures/es-modules/wasm-string-constants.js | 6 ++++++ 7 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/es-modules/top-level-wasm.wasm create mode 100644 test/fixtures/es-modules/wasm-function.js create mode 100644 test/fixtures/es-modules/wasm-object.js create mode 100644 test/fixtures/es-modules/wasm-string-constants.js 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 0000000000000000000000000000000000000000..085472e7c35be763f9b22af3cabfe176dcfef4ae GIT binary patch literal 490 zcmaJ;%TB{E5Ztw$2FL-6Lk~Hn3PR%0A})Lff6%(Yq^(?UWgAue4X7)(4O0N`A0kdQW#sM|!p0*F6xuUBtprY&iEuuWPW;;aeQBnvP?ifGekppy;h z7jO)>H?SDNv)0kpI7#p{jQsxbP1=*^p;=>`dj~8BD5kLY?Xn>slkq7sCGRfeEH{{7 zNk{E8L!H>dG^N;C=cqbUa9_-n0_7d5hZlTCqzh?SKn1m|mBzA$8nvi}78j~O%d&u= zyg6EY&0xq9B_}6}n|$Qeu*%q8NP@c~NdAKSgNv>~VKEq9a~L9LU)@3W^3`LnaNR5H Gp8o->dXiKC literal 0 HcmV?d00001 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 }