|
8 | 8 | // the point here is that it's the node core module |
9 | 9 | // eslint-disable-next-line no-restricted-imports |
10 | 10 | import {readFileSync} from 'fs'; |
11 | | -// The file was generated by wasm-pack |
12 | | -import {getAnswer} from '../42.wasm'; |
| 11 | +// file origin: https://github.com/mdn/webassembly-examples/blob/2f2163287f86fe29deb162335bccca7d5d95ca4f/understanding-text-format/add.wasm |
| 12 | +// source code: https://github.com/mdn/webassembly-examples/blob/2f2163287f86fe29deb162335bccca7d5d95ca4f/understanding-text-format/add.was |
| 13 | +import {add} from '../add.wasm'; |
13 | 14 |
|
14 | | -const wasmFileBuffer = readFileSync('42.wasm'); |
| 15 | +const wasmFileBuffer = readFileSync('add.wasm'); |
15 | 16 |
|
16 | 17 | test('supports native wasm imports', () => { |
17 | | - expect(getAnswer()).toBe(42); |
| 18 | + expect(add(1, 2)).toBe(3); |
| 19 | + |
| 20 | + // because arguments are i32 (signed), fractional part is truncated |
| 21 | + expect(add(0.99, 1.01)).toBe(1); |
| 22 | + |
| 23 | + // because return value is i32 (signed), (2^31 - 1) + 1 overflows and becomes -2^31 |
| 24 | + expect(add(Math.pow(2, 31) - 1, 1)).toBe(-Math.pow(2, 31)); |
| 25 | + |
| 26 | + // invalid or missing arguments are treated as 0 |
| 27 | + expect(add('hello', 'world')).toBe(0); |
| 28 | + expect(add()).toBe(0); |
| 29 | + expect(add(null)).toBe(0); |
| 30 | + expect(add({}, [])).toBe(0); |
| 31 | + |
| 32 | + // redundant arguments are silently ignored |
| 33 | + expect(add(1, 2, 3)).toBe(3); |
18 | 34 | }); |
19 | 35 |
|
20 | 36 | test('supports imports from "data:application/wasm" URI with base64 encoding', async () => { |
21 | 37 | const importedWasmModule = await import( |
22 | 38 | `data:application/wasm;base64,${wasmFileBuffer.toString('base64')}` |
23 | 39 | ); |
24 | | - expect(importedWasmModule.getAnswer()).toBe(42); |
| 40 | + expect(importedWasmModule.add(0, 42)).toBe(42); |
25 | 41 | }); |
26 | 42 |
|
27 | 43 | test('imports from "data:text/wasm" URI without explicit encoding fail', async () => { |
|
0 commit comments