Skip to content

Commit b223b2d

Browse files
authored
Merge pull request #2 from jonathanharg/next
Version 117.1.0
2 parents d9eb2c7 + 9ee12f2 commit b223b2d

File tree

11 files changed

+103
-20
lines changed

11 files changed

+103
-20
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ Windows, Mac (Intel & ARM) and Linux (manylinux, musllinux) are supported.
2121
## How To Use
2222

2323
```py
24+
import binaryen
25+
from binaryen.type import Int32, TypeNone
26+
2427

2528
# Equivalent python function
2629
def add(x, y):
2730
return x + y
2831

29-
func_inputs = binaryen.type.create([Int32, Int32])
3032

3133
mod = binaryen.Module()
3234
mod.add_function(
3335
b"add",
34-
func_inputs,
36+
binaryen.type.create([Int32, Int32]),
3537
Int32,
3638
[Int32],
3739
mod.block(
@@ -51,6 +53,9 @@ mod.add_function(
5153
),
5254
)
5355

56+
if not mod.validate():
57+
raise RuntimeError("Invalid module!")
58+
5459
mod.add_function_export(b"add", b"add")
5560

5661
mod.optimize()

binaryen/__functionref.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ def get_name(self) -> str:
1010
return lib.BinaryenFunctionGetName(self.ref)
1111

1212
def get_num_vars(self) -> int:
13-
return lib.BinaryenGetNumVars(self.ref)
13+
return lib.BinaryenFunctionGetNumVars(self.ref)
1414

1515
def get_var(self, index: int) -> BinaryenType:
1616
return lib.BinaryenFunctionGetVar(self.ref, index)
1717

18-
# def add_var(self, type: Type) -> int:
19-
# return lib.BinaryenFunctionAddVar(self.ref, type)
18+
def add_var(self, type: BinaryenType) -> int:
19+
return lib.BinaryenFunctionAddVar(self.ref, type)
2020

2121
def get_num_locals(self) -> int:
2222
return lib.BinaryenFunctionGetNumLocals(self.ref)

binaryen/__global.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from .__expression import Expression
2+
from ._binaryen import lib
3+
4+
5+
class Global:
6+
def __init__(self, ref):
7+
self.ref = ref
8+
9+
def get_name(self):
10+
return str(lib.BinaryenGlobalGetName(self.ref))
11+
12+
def get_type(self):
13+
return lib.BinaryenGlobalGetType(self.ref)
14+
15+
def is_mutable(self):
16+
return bool(lib.BinaryenGlobalIsMutable(self.ref))
17+
18+
def get_init_expr(self):
19+
return Expression(lib.BinaryenGlobalGetInitExpr(self.ref))

binaryen/__module.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@
66
from .__expression import Block, Expression
77
from .__feature import Feature
88
from .__functionref import FunctionRef
9+
from .__global import Global
910
from ._binaryen import ffi, lib
10-
from .internals import (
11-
BinaryenGlobalRef,
12-
BinaryenHeapType,
13-
BinaryenLiteral,
14-
BinaryenOp,
15-
BinaryenType,
16-
)
11+
from .internals import BinaryenHeapType, BinaryenLiteral, BinaryenOp, BinaryenType
1712
from .type import TypeNone
1813

1914
type BinaryenExportRef = Any
@@ -60,10 +55,10 @@ def i32(self, value: int):
6055
def i64(self, value: int):
6156
return self.const(literal.int64(value))
6257

63-
def f32(self, value: int):
58+
def f32(self, value: float):
6459
return self.const(literal.float32(value))
6560

66-
def f64(self, value: int):
61+
def f64(self, value: float):
6762
return self.const(literal.float64(value))
6863

6964
def block(
@@ -385,11 +380,25 @@ def add_function_export(
385380

386381
def add_global(
387382
self, name: bytes, global_type: BinaryenType, mutable: bool, init: Expression
388-
) -> BinaryenGlobalRef:
383+
):
389384
ref = lib.BinaryenAddGlobal(self.ref, name, global_type, mutable, init.ref)
390-
return ref
385+
return Global(ref)
386+
387+
def get_global(self, name: bytes):
388+
ref = lib.BinaryenGetGlobal(self.ref, name)
389+
if ref == ffi.NULL:
390+
return None
391+
return Global(ref)
392+
393+
def remove_global(self, name: bytes):
394+
lib.BinaryenRemoveGlobal(self.ref, name)
395+
396+
def get_num_globals(self):
397+
return int(lib.BinaryenGetNumGlobals(self.ref))
391398

392-
# TODO: GetGlobal, RemoveGlobal, GetNumGlobals, GetGlobalByIndex
399+
def get_global_by_index(self, index: int):
400+
ref = lib.BinaryenGetGlobalByIndex(self.ref, index)
401+
return Global(ref)
393402

394403
# TODO: AddTag, GetTag, RemoveTag
395404

binaryen/internals.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,3 @@ class __BaseType:
2222
BinaryenExternalKind = __NewType("BinaryenExternalKind", __BaseType)
2323
BinaryenOp = __NewType("BinaryenOp", __BaseType)
2424
BinaryenLiteral = __NewType("BinaryenLiteral", __BaseType)
25-
BinaryenGlobalRef = __NewType("BinaryenGlobalRef", __BaseType)

binaryen/type/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .._binaryen import ffi as __ffi
77
from .._binaryen import lib as __lib
88
from . import array_type, heap_type, signature_type, struct_type
9+
from .to_str import to_str
910

1011
# These "types" are actually integers representing the type
1112
# e.g. none = 0, i32 = 2 etc.

binaryen/type/to_str.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from .._binaryen import lib
2+
from ..internals import BinaryenType
3+
4+
5+
def to_str(type: BinaryenType) -> str:
6+
return {
7+
lib.BinaryenTypeNone(): "None",
8+
lib.BinaryenTypeInt32(): "Int32",
9+
lib.BinaryenTypeInt64(): "Int64",
10+
lib.BinaryenTypeFloat32(): "Float32",
11+
lib.BinaryenTypeFloat64(): "Float64",
12+
lib.BinaryenTypeVec128(): "Vec128",
13+
lib.BinaryenTypeFuncref(): "Funcref",
14+
lib.BinaryenTypeExternref(): "Externref",
15+
lib.BinaryenTypeAnyref(): "Anyref",
16+
lib.BinaryenTypeEqref(): "Eqref",
17+
lib.BinaryenTypeI31ref(): "I31ref",
18+
lib.BinaryenTypeStructref(): "Structref",
19+
lib.BinaryenTypeArrayref(): "Arrayref",
20+
lib.BinaryenTypeStringref(): "Stringref",
21+
lib.BinaryenTypeStringviewWTF8(): "StringviewWTF8",
22+
lib.BinaryenTypeStringviewWTF16(): "StringviewWTF16",
23+
lib.BinaryenTypeStringviewIter(): "StringviewIter",
24+
lib.BinaryenTypeNullref(): "Nullref",
25+
lib.BinaryenTypeNullExternref(): "NullExternref",
26+
lib.BinaryenTypeNullFuncref(): "NullFuncref",
27+
lib.BinaryenTypeUnreachable(): "Unreachable",
28+
lib.BinaryenTypeAuto(): "Auto",
29+
}[type]

examples/unary.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import binaryen
2+
3+
mod = binaryen.Module()
4+
5+
# f_sub = mod.unary(binaryen.operations.NegFloat32(), mod.f32(6.7))
6+
f_sub = mod.unary(
7+
binaryen.operations.NegFloat32(), mod.local_get(0, binaryen.type.Float32)
8+
)
9+
10+
mod.add_function(b"float_test", binaryen.type.Float32, binaryen.type.Float32, [], f_sub)
11+
mod.add_function_export(b"float_test", b"float_test")
12+
13+
# i_sub = mod.binary(binaryen.operations.SubInt32(), mod.i32(0), mod.i32(10))
14+
# i_sub = mod.binary(binaryen.operations.S, mod.i32(0), mod.local_get(0, binaryen.type.Int32))
15+
16+
mod.add_function(b"int_test", binaryen.type.Int32, binaryen.type.Int32, [], i_sub)
17+
mod.add_function_export(b"int_test", b"int_test")
18+
19+
mod.optimize()
20+
21+
mod.print()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "binaryen.py"
7-
version = "117.0.0"
7+
version = "117.1.0"
88
description = "A Python wrapper for Binaryen"
99
authors = [{ name = "Jonathan Hargreaves", email = "[email protected]" }]
1010
readme = "README.md"

0 commit comments

Comments
 (0)