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