Skip to content

Commit cbf9a64

Browse files
guybedfordRafaelGSS
authored andcommitted
esm: support top-level Wasm without package type
PR-URL: #57610 Reviewed-By: Jordan Harband <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 5d3106d commit cbf9a64

File tree

7 files changed

+35
-1
lines changed

7 files changed

+35
-1
lines changed

doc/api/cli.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ If a file is found, its path will be passed to the
3333

3434
* The program was started with a command-line flag that forces the entry
3535
point to be loaded with ECMAScript module loader, such as `--import`.
36-
* The file has an `.mjs` extension.
36+
* The file has an `.mjs` or `.wasm` (with `--experimental-wasm-modules`)
37+
extension.
3738
* The file does not have a `.cjs` extension, and the nearest parent
3839
`package.json` file contains a top-level [`"type"`][] field with a value of
3940
`"module"`.

lib/internal/modules/run_main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ function shouldUseESMLoader(mainPath) {
6262

6363
// Determine the module format of the entry point.
6464
if (mainPath && StringPrototypeEndsWith(mainPath, '.mjs')) { return true; }
65+
if (mainPath && StringPrototypeEndsWith(mainPath, '.wasm')) { return true; }
6566
if (!mainPath || StringPrototypeEndsWith(mainPath, '.cjs')) { return false; }
6667

6768
if (getOptionValue('--experimental-strip-types')) {

test/es-module/test-esm-wasm.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ describe('ESM: WASM modules', { concurrency: !process.env.TEST_PARALLEL }, () =>
9191
match(stderr, /WebAssembly/);
9292
});
9393

94+
it('should support top-level execution', async () => {
95+
const { code, stderr, stdout } = await spawnPromisified(execPath, [
96+
'--no-warnings',
97+
'--experimental-wasm-modules',
98+
fixtures.path('es-modules/top-level-wasm.wasm'),
99+
]);
100+
101+
strictEqual(stderr, '');
102+
strictEqual(stdout, '[Object: null prototype] { prop: \'hello world\' }\n');
103+
strictEqual(code, 0);
104+
});
105+
94106
it('should support static source phase imports', async () => {
95107
const { code, stderr, stdout } = await spawnPromisified(execPath, [
96108
'--no-warnings',
490 Bytes
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function call1 (func, thisObj, arg0) {
2+
return func.call(thisObj, arg0);
3+
}
4+
5+
export function call2 (func, thisObj, arg0, arg1) {
6+
return func.call(thisObj, arg0, arg1);
7+
}
8+
9+
export function call3 (func, thisObj, arg0, arg1, arg2) {
10+
return func.call(thisObj, arg0, arg1, arg2);
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const { get: getProperty, set: setProperty } = Reflect;
2+
export const { create } = Object;
3+
export const global = globalThis;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const console = 'console';
2+
const hello_world = 'hello world';
3+
const log = 'log';
4+
const prop = 'prop';
5+
6+
export { console, hello_world as 'hello world', log, prop }

0 commit comments

Comments
 (0)