forked from sqreen/PyMiniRacer
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_wasm.py
48 lines (36 loc) · 1.09 KB
/
test_wasm.py
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
"""Test executing a WASM module."""
from os.path import abspath, dirname, getsize
from os.path import join as pathjoin
from py_mini_racer import MiniRacer
test_dir = dirname(abspath(__file__))
def test_add(gc_check):
fn = pathjoin(test_dir, "add.wasm")
mr = MiniRacer()
# 1. Allocate a buffer to hold the WASM module code
size = getsize(fn)
module_raw = mr.eval(
f"""
const moduleRaw = new SharedArrayBuffer({size});
moduleRaw
"""
)
# 2. Read the WASM module code
with open(fn, "rb") as f:
assert f.readinto(module_raw) == size
# 3. Instantiate the WASM module
mr.eval(
"""
var res = null;
WebAssembly.instantiate(new Uint8Array(moduleRaw)).then(result => {
res = result.instance;
}).catch(result => { res = result.message; });
"""
)
# 4. Wait for WASM module instantiation
while mr.eval("res") is None:
pass
assert mr.eval("typeof res !== 'string'")
# 5. Execute a WASM function
assert mr.eval("res.exports.addTwo(1, 2)") == 3
del module_raw
gc_check.check(mr)