11cb0ef41Sopenharmony_ciimport { spawnPromisified } from '../common/index.mjs';
21cb0ef41Sopenharmony_ciimport * as fixtures from '../common/fixtures.mjs';
31cb0ef41Sopenharmony_ciimport { strictEqual, match } from 'node:assert';
41cb0ef41Sopenharmony_ciimport { execPath } from 'node:process';
51cb0ef41Sopenharmony_ciimport { describe, it } from 'node:test';
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_cidescribe('ESM: WASM modules', { concurrency: true }, () => {
91cb0ef41Sopenharmony_ci  it('should load exports', async () => {
101cb0ef41Sopenharmony_ci    const { code, stderr, stdout } = await spawnPromisified(execPath, [
111cb0ef41Sopenharmony_ci      '--no-warnings',
121cb0ef41Sopenharmony_ci      '--experimental-wasm-modules',
131cb0ef41Sopenharmony_ci      '--input-type=module',
141cb0ef41Sopenharmony_ci      '--eval',
151cb0ef41Sopenharmony_ci      [
161cb0ef41Sopenharmony_ci        'import { strictEqual, match } from "node:assert";',
171cb0ef41Sopenharmony_ci        `import { add, addImported } from ${JSON.stringify(fixtures.fileURL('es-modules/simple.wasm'))};`,
181cb0ef41Sopenharmony_ci        `import { state } from ${JSON.stringify(fixtures.fileURL('es-modules/wasm-dep.mjs'))};`,
191cb0ef41Sopenharmony_ci        'strictEqual(state, "WASM Start Executed");',
201cb0ef41Sopenharmony_ci        'strictEqual(add(10, 20), 30);',
211cb0ef41Sopenharmony_ci        'strictEqual(addImported(0), 42);',
221cb0ef41Sopenharmony_ci        'strictEqual(state, "WASM JS Function Executed");',
231cb0ef41Sopenharmony_ci        'strictEqual(addImported(1), 43);',
241cb0ef41Sopenharmony_ci      ].join('\n'),
251cb0ef41Sopenharmony_ci    ]);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    strictEqual(stderr, '');
281cb0ef41Sopenharmony_ci    strictEqual(stdout, '');
291cb0ef41Sopenharmony_ci    strictEqual(code, 0);
301cb0ef41Sopenharmony_ci  });
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  it('should not allow code injection through export names', async () => {
331cb0ef41Sopenharmony_ci    const { code, stderr, stdout } = await spawnPromisified(execPath, [
341cb0ef41Sopenharmony_ci      '--no-warnings',
351cb0ef41Sopenharmony_ci      '--experimental-wasm-modules',
361cb0ef41Sopenharmony_ci      '--input-type=module',
371cb0ef41Sopenharmony_ci      '--eval',
381cb0ef41Sopenharmony_ci      `import * as wasmExports from ${JSON.stringify(fixtures.fileURL('es-modules/export-name-code-injection.wasm'))};`,
391cb0ef41Sopenharmony_ci    ]);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    strictEqual(stderr, '');
421cb0ef41Sopenharmony_ci    strictEqual(stdout, '');
431cb0ef41Sopenharmony_ci    strictEqual(code, 0);
441cb0ef41Sopenharmony_ci  });
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  it('should allow non-identifier export names', async () => {
471cb0ef41Sopenharmony_ci    const { code, stderr, stdout } = await spawnPromisified(execPath, [
481cb0ef41Sopenharmony_ci      '--no-warnings',
491cb0ef41Sopenharmony_ci      '--experimental-wasm-modules',
501cb0ef41Sopenharmony_ci      '--input-type=module',
511cb0ef41Sopenharmony_ci      '--eval',
521cb0ef41Sopenharmony_ci      [
531cb0ef41Sopenharmony_ci        'import { strictEqual } from "node:assert";',
541cb0ef41Sopenharmony_ci        `import * as wasmExports from ${JSON.stringify(fixtures.fileURL('es-modules/export-name-syntax-error.wasm'))};`,
551cb0ef41Sopenharmony_ci        'assert.strictEqual(wasmExports["?f!o:o<b>a[r]"]?.value, 12682);',
561cb0ef41Sopenharmony_ci      ].join('\n'),
571cb0ef41Sopenharmony_ci    ]);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci    strictEqual(stderr, '');
601cb0ef41Sopenharmony_ci    strictEqual(stdout, '');
611cb0ef41Sopenharmony_ci    strictEqual(code, 0);
621cb0ef41Sopenharmony_ci  });
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  it('should properly escape import names as well', async () => {
651cb0ef41Sopenharmony_ci    const { code, stderr, stdout } = await spawnPromisified(execPath, [
661cb0ef41Sopenharmony_ci      '--no-warnings',
671cb0ef41Sopenharmony_ci      '--experimental-wasm-modules',
681cb0ef41Sopenharmony_ci      '--input-type=module',
691cb0ef41Sopenharmony_ci      '--eval',
701cb0ef41Sopenharmony_ci      [
711cb0ef41Sopenharmony_ci        'import { strictEqual } from "node:assert";',
721cb0ef41Sopenharmony_ci        `import * as wasmExports from ${JSON.stringify(fixtures.fileURL('es-modules/import-name.wasm'))};`,
731cb0ef41Sopenharmony_ci        'assert.strictEqual(wasmExports.xor(), 12345);',
741cb0ef41Sopenharmony_ci      ].join('\n'),
751cb0ef41Sopenharmony_ci    ]);
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci    strictEqual(stderr, '');
781cb0ef41Sopenharmony_ci    strictEqual(stdout, '');
791cb0ef41Sopenharmony_ci    strictEqual(code, 0);
801cb0ef41Sopenharmony_ci  });
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  it('should emit experimental warning', async () => {
831cb0ef41Sopenharmony_ci    const { code, signal, stderr } = await spawnPromisified(execPath, [
841cb0ef41Sopenharmony_ci      '--experimental-wasm-modules',
851cb0ef41Sopenharmony_ci      fixtures.path('es-modules/wasm-modules.mjs'),
861cb0ef41Sopenharmony_ci    ]);
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci    strictEqual(code, 0);
891cb0ef41Sopenharmony_ci    strictEqual(signal, null);
901cb0ef41Sopenharmony_ci    match(stderr, /ExperimentalWarning/);
911cb0ef41Sopenharmony_ci    match(stderr, /WebAssembly/);
921cb0ef41Sopenharmony_ci  });
931cb0ef41Sopenharmony_ci});
94