• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { spawnPromisified } from '../common/index.mjs';
2import * as fixtures from '../common/fixtures.mjs';
3import { strictEqual, match } from 'node:assert';
4import { execPath } from 'node:process';
5import { describe, it } from 'node:test';
6
7
8describe('ESM: WASM modules', { concurrency: true }, () => {
9  it('should load exports', async () => {
10    const { code, stderr, stdout } = await spawnPromisified(execPath, [
11      '--no-warnings',
12      '--experimental-wasm-modules',
13      '--input-type=module',
14      '--eval',
15      [
16        'import { strictEqual, match } from "node:assert";',
17        `import { add, addImported } from ${JSON.stringify(fixtures.fileURL('es-modules/simple.wasm'))};`,
18        `import { state } from ${JSON.stringify(fixtures.fileURL('es-modules/wasm-dep.mjs'))};`,
19        'strictEqual(state, "WASM Start Executed");',
20        'strictEqual(add(10, 20), 30);',
21        'strictEqual(addImported(0), 42);',
22        'strictEqual(state, "WASM JS Function Executed");',
23        'strictEqual(addImported(1), 43);',
24      ].join('\n'),
25    ]);
26
27    strictEqual(stderr, '');
28    strictEqual(stdout, '');
29    strictEqual(code, 0);
30  });
31
32  it('should not allow code injection through export names', async () => {
33    const { code, stderr, stdout } = await spawnPromisified(execPath, [
34      '--no-warnings',
35      '--experimental-wasm-modules',
36      '--input-type=module',
37      '--eval',
38      `import * as wasmExports from ${JSON.stringify(fixtures.fileURL('es-modules/export-name-code-injection.wasm'))};`,
39    ]);
40
41    strictEqual(stderr, '');
42    strictEqual(stdout, '');
43    strictEqual(code, 0);
44  });
45
46  it('should allow non-identifier export names', async () => {
47    const { code, stderr, stdout } = await spawnPromisified(execPath, [
48      '--no-warnings',
49      '--experimental-wasm-modules',
50      '--input-type=module',
51      '--eval',
52      [
53        'import { strictEqual } from "node:assert";',
54        `import * as wasmExports from ${JSON.stringify(fixtures.fileURL('es-modules/export-name-syntax-error.wasm'))};`,
55        'assert.strictEqual(wasmExports["?f!o:o<b>a[r]"]?.value, 12682);',
56      ].join('\n'),
57    ]);
58
59    strictEqual(stderr, '');
60    strictEqual(stdout, '');
61    strictEqual(code, 0);
62  });
63
64  it('should properly escape import names as well', async () => {
65    const { code, stderr, stdout } = await spawnPromisified(execPath, [
66      '--no-warnings',
67      '--experimental-wasm-modules',
68      '--input-type=module',
69      '--eval',
70      [
71        'import { strictEqual } from "node:assert";',
72        `import * as wasmExports from ${JSON.stringify(fixtures.fileURL('es-modules/import-name.wasm'))};`,
73        'assert.strictEqual(wasmExports.xor(), 12345);',
74      ].join('\n'),
75    ]);
76
77    strictEqual(stderr, '');
78    strictEqual(stdout, '');
79    strictEqual(code, 0);
80  });
81
82  it('should emit experimental warning', async () => {
83    const { code, signal, stderr } = await spawnPromisified(execPath, [
84      '--experimental-wasm-modules',
85      fixtures.path('es-modules/wasm-modules.mjs'),
86    ]);
87
88    strictEqual(code, 0);
89    strictEqual(signal, null);
90    match(stderr, /ExperimentalWarning/);
91    match(stderr, /WebAssembly/);
92  });
93});
94