Skip to content

Commit d9eb2c7

Browse files
committed
Cleanup and version bump
1 parent 6b64674 commit d9eb2c7

File tree

11 files changed

+90
-287
lines changed

11 files changed

+90
-287
lines changed

CHANGELOG.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

examples/add.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,40 @@ def add(x, y):
77
return x + y
88

99

10-
myModule = binaryen.Module()
11-
myModule.add_function(
10+
mod = binaryen.Module()
11+
mod.add_function(
1212
b"add",
1313
binaryen.type.create([Int32, Int32]),
1414
Int32,
1515
[Int32],
16-
myModule.block(
16+
mod.block(
1717
None,
1818
[
19-
myModule.local_set(
19+
mod.local_set(
2020
2,
21-
myModule.binary(
21+
mod.binary(
2222
binaryen.operations.AddInt32(),
23-
myModule.local_get(0, Int32),
24-
myModule.local_get(1, Int32),
23+
mod.local_get(0, Int32),
24+
mod.local_get(1, Int32),
2525
),
2626
),
27-
myModule.Return(myModule.local_get(2, Int32)),
27+
mod.Return(mod.local_get(2, Int32)),
2828
],
2929
TypeNone,
3030
),
3131
)
3232

33-
if not myModule.validate():
34-
raise Exception("Invalid module!")
33+
if not mod.validate():
34+
raise RuntimeError("Invalid module!")
3535

36-
myModule.add_function_export(b"add", b"add")
36+
mod.add_function_export(b"add", b"add")
3737

38-
myModule.optimize()
38+
mod.optimize()
3939

40-
myModule.write_text("out.wat")
40+
mod.print()
41+
mod.write_binary("add.wasm")
42+
# You can print the module with `mod.print()`
43+
# Write it to a text format with `mod.write_text("fileName.wat")`
44+
# Write it to a binary format with `mod.write_binary("fileName.wasm")`
4145

42-
# Can either print with `myModule.print()` or write to file with `myModule.write_binary(__file__)`
43-
44-
# Run the written binary with `wasmtime add.wasm --invoke add 85 20`
46+
# Run the written binary with `wasmtime --invoke add add.wasm 85 20`

examples/addTen.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,36 @@ def addTen(x):
77
return x + 10
88

99

10-
myModule = binaryen.Module()
11-
myModule.add_function(
10+
mod = binaryen.Module()
11+
mod.add_function(
1212
b"addTen",
1313
Int32,
1414
Int32,
1515
[Int32],
16-
myModule.block(
16+
mod.block(
1717
None,
1818
[
19-
myModule.local_set(
19+
mod.local_set(
2020
1,
21-
myModule.binary(
21+
mod.binary(
2222
binaryen.operations.AddInt32(),
23-
myModule.local_get(0, Int32),
24-
myModule.i32(10),
23+
mod.local_get(0, Int32),
24+
mod.i32(10),
2525
),
2626
),
27-
myModule.Return(myModule.local_get(1, Int32)),
27+
mod.Return(mod.local_get(1, Int32)),
2828
],
2929
TypeNone,
3030
),
3131
)
3232

33-
if not myModule.validate():
34-
raise Exception("Invalid module!")
33+
if not mod.validate():
34+
raise RuntimeError("Invalid module!")
3535

36-
myModule.add_function_export(b"addTen", b"addTen")
36+
mod.add_function_export(b"addTen", b"addTen")
3737

38-
myModule.optimize()
38+
mod.optimize()
3939

40-
myModule.print()
40+
mod.print()
4141

42-
# Can either print with `myModule.print()` or write to file with `myModule.write_binary(__file__)`
43-
44-
# Run the written binary with `wasmtime addTen.wasm --invoke addTen 12`
42+
# Run the written binary with `wasmtime --invoke addTen addTen.wasm 12`

examples/array.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import binaryen
22
from binaryen.type import Int32, NotPacked
33

4+
# Enable WasmGC and reference types
45
mod = binaryen.Module()
56
mod.set_feature(binaryen.Feature.GC | binaryen.Feature.ReferenceTypes)
67

8+
# Create the Array type
79
tb = binaryen.TypeBuilder(1)
810
tb.set_array_type(0, Int32, NotPacked, True)
911
Int32ArrayHeap = tb.build()[0]
@@ -28,16 +30,7 @@
2830
mod.add_function_export(b"indexArray", b"indexArray")
2931
mod.auto_drop()
3032

31-
# mod.optimize()
32-
33-
mod.print()
34-
mod.write_text("array.wat")
35-
mod.write_binary("array.wasm")
36-
37-
3833
if not mod.validate():
3934
raise RuntimeError("Invalid module!")
4035

41-
# Can either print with `myModule.print()` or write to file with `myModule.write_binary(__file__)`
42-
43-
# Run the written binary with `wasmtime addTen.wasm --invoke addTen 12`
36+
mod.print()

examples/features.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from binaryen import Feature, Module
22

3-
myModule = Module()
4-
print(myModule.get_features())
5-
myModule.set_feature(Feature.GC | Feature.ReferenceTypes)
6-
print(myModule.get_features())
7-
print(list(myModule.get_features()))
3+
mod = Module()
4+
print(mod.get_features())
5+
6+
mod.set_feature(Feature.GC | Feature.ReferenceTypes)
7+
8+
print(mod.get_features())
9+
print(list(mod.get_features()))

examples/fib.py

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import binaryen
2-
from binaryen.type import Int32, TypeNone
2+
from binaryen.type import Int32
33

44

55
# Equivalent python function
@@ -10,64 +10,61 @@ def fib(n):
1010
return fib(n - 1) + fib(n - 2)
1111

1212

13-
myModule = binaryen.Module()
14-
myModule.add_function(
13+
mod = binaryen.Module()
14+
15+
n = mod.local_get(0, Int32)
16+
17+
condition = mod.binary(
18+
binaryen.operations.LeSInt32(),
19+
n,
20+
mod.i32(1),
21+
)
22+
23+
n_minus_one = mod.binary(
24+
binaryen.operations.SubInt32(),
25+
n,
26+
mod.i32(1),
27+
)
28+
29+
n_minus_two = mod.binary(
30+
binaryen.operations.SubInt32(),
31+
n,
32+
mod.i32(2),
33+
)
34+
35+
mod.add_function(
1536
b"fib",
1637
Int32,
1738
Int32,
1839
[],
19-
myModule.block(
20-
None,
21-
[
22-
myModule.If(
23-
myModule.binary(
24-
binaryen.operations.LeSInt32(),
25-
myModule.local_get(0, Int32),
26-
myModule.i32(1),
40+
mod.If(
41+
condition,
42+
mod.Return(n),
43+
mod.Return(
44+
mod.binary(
45+
binaryen.operations.AddInt32(),
46+
mod.call(
47+
b"fib",
48+
[n_minus_one],
49+
Int32,
2750
),
28-
myModule.Return(myModule.local_get(0, Int32)),
29-
myModule.Return(
30-
myModule.binary(
31-
binaryen.operations.AddInt32(),
32-
myModule.call(
33-
b"fib",
34-
[
35-
myModule.binary(
36-
binaryen.operations.SubInt32(),
37-
myModule.local_get(0, Int32),
38-
myModule.i32(1),
39-
)
40-
],
41-
Int32,
42-
),
43-
myModule.call(
44-
b"fib",
45-
[
46-
myModule.binary(
47-
binaryen.operations.SubInt32(),
48-
myModule.local_get(0, Int32),
49-
myModule.const(binaryen.literal.int32(2)),
50-
)
51-
],
52-
Int32,
53-
),
54-
)
51+
mod.call(
52+
b"fib",
53+
[n_minus_two],
54+
Int32,
5555
),
5656
)
57-
],
58-
TypeNone,
57+
),
5958
),
6059
)
6160

62-
if not myModule.validate():
63-
raise Exception("Invalid module!")
64-
65-
myModule.add_function_export(b"fib", b"fib")
61+
if not mod.validate():
62+
raise RuntimeError("Invalid module!")
6663

67-
myModule.optimize()
64+
mod.add_function_export(b"fib", b"fib")
6865

69-
myModule.print()
66+
mod.optimize()
7067

71-
# Can either print with `myModule.print()` or write to file with `myModule.write_binary(__file__)`
68+
mod.print()
7269

73-
# Run the written binary with `wasmtime fib.wasm --invoke fib 23`
70+
# Run the written binary with `wasmtime --invoke fib fib.wasm 23`

examples/hello_world.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@
4545

4646

4747
mod.add_function_export(b"main", b"main")
48-
# mod.set_start(func)
48+
mod.set_start(func)
4949

5050
mod.auto_drop()
5151
mod.optimize()
5252
mod.validate()
5353

5454
mod.print()
55-
mod.write_text("hello_world.wat")
5655
mod.write_binary("hello_world.wasm")
56+
# wasmtime hello_world.wasm

0 commit comments

Comments
 (0)