Skip to content

Commit ddde7d4

Browse files
committed
test(import): add test for calling JS func in Rust
🐀 the use case would be having lifecycle hook in wasm code
1 parent 48a168d commit ddde7d4

File tree

5 files changed

+106
-17
lines changed

5 files changed

+106
-17
lines changed

test/export.spec.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,4 @@ describe('test export option', () => {
5252
expect(wasmInstance).toBeInstanceOf(WebAssembly.Instance);
5353
expect(wasmInstance.exports.add(1, 2)).toBe(3); // can be used directly, not suitable for big size wasm
5454
});
55-
56-
test('export of WebAssembly.instantiate', async () => {
57-
const bundle = await rollup({
58-
input,
59-
plugins: [rust({ export: 'promise' })]
60-
});
61-
62-
const compileWasm = await require(bundle);
63-
64-
// Usage
65-
const { module, instance } = await compileWasm;
66-
const { add } = instance.exports;
67-
68-
expect(module).toBeInstanceOf(WebAssembly.Module);
69-
expect(instance).toBeInstanceOf(WebAssembly.Instance);
70-
expect(add(1, 2)).toBe(3);
71-
});
7255
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "adder"
3+
version = "0.1.0"
4+
authors = ["Fahmi Akbar Wildana <fahmi.a.w@gmail.com>"]
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
path = "lib.rs"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './lib.rs';

test/fixtures/hook_function/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[link(wasm_import_module = "hook")]
2+
extern {
3+
fn before(a: u8, b: u8);
4+
fn after(c: u8);
5+
}
6+
7+
#[no_mangle]
8+
pub fn add(a: u8, b: u8) -> u8 {
9+
unsafe { before(a, b) };
10+
let c = a + b;
11+
unsafe { after(c) };
12+
return c;
13+
}

test/import.spec.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// @flow
2+
import { rollup } from 'rollup';
3+
import { require } from './util';
4+
import rust from '../src/main.js';
5+
6+
describe('test export option for importjs', () => {
7+
const input = 'test/fixtures/hook_function/index.js';
8+
9+
test('export of WebAssembly.compile', async () => {
10+
const bundle = await rollup({
11+
input,
12+
plugins: [rust({ export: 'async-module' })]
13+
});
14+
15+
const compileWasm = await require(bundle);
16+
17+
// Usage
18+
const importObj = {
19+
hook: {
20+
before: jest.fn(),
21+
after: jest.fn()
22+
}
23+
};
24+
const module = await compileWasm();
25+
const instance = new WebAssembly.Instance(module, importObj);
26+
const { add } = instance.exports;
27+
28+
expect(module).toBeInstanceOf(WebAssembly.Module);
29+
expect(instance).toBeInstanceOf(WebAssembly.Instance);
30+
// $FlowFixMe: false alarm
31+
expect(add(1, 2)).toBe(3);
32+
expect(importObj.hook.before).toHaveBeenCalledWith(1, 2);
33+
expect(importObj.hook.after).toHaveBeenCalledWith(3);
34+
});
35+
36+
test('export of WebAssembly.instantiate(WebAssembly.Module)', async () => {
37+
const bundle = await rollup({
38+
input,
39+
plugins: [rust({ export: 'async-instance' })]
40+
});
41+
42+
const compileWasm = await require(bundle);
43+
44+
// Usage
45+
const importObj = {
46+
hook: {
47+
before: jest.fn(),
48+
after: jest.fn()
49+
}
50+
};
51+
const instance = await compileWasm(importObj);
52+
const { add } = instance.exports;
53+
54+
expect(instance).toBeInstanceOf(WebAssembly.Instance);
55+
expect(add(1, 2)).toBe(3);
56+
expect(importObj.hook.before).toHaveBeenCalledWith(1, 2);
57+
expect(importObj.hook.after).toHaveBeenCalledWith(3);
58+
});
59+
60+
test('export of WebAssembly.instantiate(Buffer)', async () => {
61+
const bundle = await rollup({
62+
input,
63+
plugins: [rust({ export: 'async' })]
64+
});
65+
66+
const compileWasm = await require(bundle);
67+
68+
// Usage
69+
const importObj = {
70+
hook: {
71+
before: jest.fn(),
72+
after: jest.fn()
73+
}
74+
};
75+
const { module, instance } = await compileWasm(importObj);
76+
const { add } = instance.exports;
77+
78+
expect(module).toBeInstanceOf(WebAssembly.Module);
79+
expect(instance).toBeInstanceOf(WebAssembly.Instance);
80+
expect(add(1, 2)).toBe(3);
81+
expect(importObj.hook.before).toHaveBeenCalledWith(1, 2);
82+
expect(importObj.hook.after).toHaveBeenCalledWith(3);
83+
});
84+
});

0 commit comments

Comments
 (0)