-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddTen.py
More file actions
42 lines (33 loc) · 795 Bytes
/
addTen.py
File metadata and controls
42 lines (33 loc) · 795 Bytes
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
import binaryen
from binaryen.type import Int32, TypeNone
# Equivalent python function
def addTen(x):
return x + 10
mod = binaryen.Module()
mod.add_function(
b"addTen",
Int32,
Int32,
[Int32],
mod.block(
None,
[
mod.local_set(
1,
mod.binary(
binaryen.operations.AddInt32(),
mod.local_get(0, Int32),
mod.i32(10),
),
),
mod.Return(mod.local_get(1, Int32)),
],
TypeNone,
),
)
if not mod.validate():
raise RuntimeError("Invalid module!")
mod.add_function_export(b"addTen", b"addTen")
mod.optimize()
mod.print()
# Run the written binary with `wasmtime --invoke addTen addTen.wasm 12`