Skip to content

Commit

Permalink
Use Wasm file from mdn examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kachkaev committed Oct 30, 2022
1 parent 2aea674 commit 79e0f40
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Time: <<REPLACED>>
Ran all test suites matching /native-esm-deep-cjs-reexport.test.js/i."
`;
exports[`runs Wasm test with native ESM 1`] = `
exports[`runs WebAssembly (Wasm) test with native ESM 1`] = `
"Test Suites: 1 passed, 1 total
Tests: 4 passed, 4 total
Snapshots: 0 total
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/nativeEsm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ onNodeVersions('>=16.9.0', () => {
});
});

test('runs Wasm test with native ESM', () => {
test('runs WebAssembly (Wasm) test with native ESM', () => {
const {exitCode, stderr, stdout} = runJest(DIR, ['native-esm-wasm.test.js'], {
nodeOptions: '--experimental-vm-modules --no-warnings',
});
Expand Down
Binary file removed e2e/native-esm/42.wasm
Binary file not shown.
26 changes: 21 additions & 5 deletions e2e/native-esm/__tests__/native-esm-wasm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,36 @@
// the point here is that it's the node core module
// eslint-disable-next-line no-restricted-imports
import {readFileSync} from 'fs';
// The file was generated by wasm-pack
import {getAnswer} from '../42.wasm';
// file origin: https://github.com/mdn/webassembly-examples/blob/2f2163287f86fe29deb162335bccca7d5d95ca4f/understanding-text-format/add.wasm
// source code: https://github.com/mdn/webassembly-examples/blob/2f2163287f86fe29deb162335bccca7d5d95ca4f/understanding-text-format/add.was
import {add} from '../add.wasm';

const wasmFileBuffer = readFileSync('42.wasm');
const wasmFileBuffer = readFileSync('add.wasm');

test('supports native wasm imports', () => {
expect(getAnswer()).toBe(42);
expect(add(1, 2)).toBe(3);

// because arguments are i32 (signed), fractional part is truncated
expect(add(0.99, 1.01)).toBe(1);

// because return value is i32 (signed), (2^31 - 1) + 1 overflows and becomes -2^31
expect(add(Math.pow(2, 31) - 1, 1)).toBe(-Math.pow(2, 31));

// invalid or missing arguments are treated as 0
expect(add('hello', 'world')).toBe(0);
expect(add()).toBe(0);
expect(add(null)).toBe(0);
expect(add({}, [])).toBe(0);

// redundant arguments are silently ignored
expect(add(1, 2, 3)).toBe(3);
});

test('supports imports from "data:application/wasm" URI with base64 encoding', async () => {
const importedWasmModule = await import(
`data:application/wasm;base64,${wasmFileBuffer.toString('base64')}`
);
expect(importedWasmModule.getAnswer()).toBe(42);
expect(importedWasmModule.add(0, 42)).toBe(42);
});

test('imports from "data:text/wasm" URI without explicit encoding fail', async () => {
Expand Down
Binary file added e2e/native-esm/add.wasm
Binary file not shown.

0 comments on commit 79e0f40

Please sign in to comment.