From 3c69f1e656c45971a84e5e1f8f2689d9424efbc1 Mon Sep 17 00:00:00 2001 From: Jonathan Hargreaves <32854503+jonathanharg@users.noreply.github.com> Date: Fri, 8 Mar 2024 14:28:27 +0000 Subject: [PATCH 1/7] Updated readme --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 33d6b32..b44b5e5 100644 --- a/README.md +++ b/README.md @@ -21,17 +21,19 @@ Windows, Mac (Intel & ARM) and Linux (manylinux, musllinux) are supported. ## How To Use ```py +import binaryen +from binaryen.type import Int32, TypeNone + # Equivalent python function def add(x, y): return x + y -func_inputs = binaryen.type.create([Int32, Int32]) mod = binaryen.Module() mod.add_function( b"add", - func_inputs, + binaryen.type.create([Int32, Int32]), Int32, [Int32], mod.block( @@ -51,6 +53,9 @@ mod.add_function( ), ) +if not mod.validate(): + raise RuntimeError("Invalid module!") + mod.add_function_export(b"add", b"add") mod.optimize() From 18bde2ac0075304e091026dbad1b09e24f684c78 Mon Sep 17 00:00:00 2001 From: Jonathan Hargreaves <32854503+jonathanharg@users.noreply.github.com> Date: Tue, 9 Apr 2024 13:54:06 -0400 Subject: [PATCH 2/7] fix: typehint for float constants --- binaryen/__module.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/binaryen/__module.py b/binaryen/__module.py index f57f1f7..70f6c53 100644 --- a/binaryen/__module.py +++ b/binaryen/__module.py @@ -60,10 +60,10 @@ def i32(self, value: int): def i64(self, value: int): return self.const(literal.int64(value)) - def f32(self, value: int): + def f32(self, value: float): return self.const(literal.float32(value)) - def f64(self, value: int): + def f64(self, value: float): return self.const(literal.float64(value)) def block( From 2c4e903b7541fde7c6f38a42e5abd3bb4bdcd969 Mon Sep 17 00:00:00 2001 From: Jonathan Hargreaves <32854503+jonathanharg@users.noreply.github.com> Date: Tue, 9 Apr 2024 14:39:35 -0400 Subject: [PATCH 3/7] moved binaryen_build script to scripts folder --- {binaryen => scripts}/binaryen_build.py | 0 setup.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename {binaryen => scripts}/binaryen_build.py (100%) diff --git a/binaryen/binaryen_build.py b/scripts/binaryen_build.py similarity index 100% rename from binaryen/binaryen_build.py rename to scripts/binaryen_build.py diff --git a/setup.py b/setup.py index a667393..26d2480 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,5 @@ from setuptools import setup setup( - cffi_modules=["binaryen/binaryen_build.py:ffibuilder"], + cffi_modules=["scripts/binaryen_build.py:ffibuilder"], ) From d071875c159a0de2abddcaa4e3c131ed81f19979 Mon Sep 17 00:00:00 2001 From: Jonathan Hargreaves <32854503+jonathanharg@users.noreply.github.com> Date: Wed, 17 Apr 2024 23:37:47 +0100 Subject: [PATCH 4/7] feat: Added support for globals --- binaryen/__global.py | 19 +++++++++++++++++++ binaryen/__module.py | 22 ++++++++++++++++++---- binaryen/internals.py | 1 - 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 binaryen/__global.py diff --git a/binaryen/__global.py b/binaryen/__global.py new file mode 100644 index 0000000..5cdb7d4 --- /dev/null +++ b/binaryen/__global.py @@ -0,0 +1,19 @@ +from ._binaryen import lib +from .__expression import Expression + + +class Global: + def __init__(self, ref): + self.ref = ref + + def get_name(self): + return str(lib.BinaryenGlobalGetName(self.ref)) + + def get_type(self): + return lib.BinaryenGlobalGetType(self.ref) + + def is_mutable(self): + return bool(lib.BinaryenGlobalIsMutable(self.ref)) + + def get_init_expr(self): + return Expression(lib.BinaryenGlobalGetInitExpr(self.ref)) diff --git a/binaryen/__module.py b/binaryen/__module.py index 70f6c53..f5a6670 100644 --- a/binaryen/__module.py +++ b/binaryen/__module.py @@ -4,11 +4,11 @@ from . import literal from .__expression import Block, Expression +from .__global import Global from .__feature import Feature from .__functionref import FunctionRef from ._binaryen import ffi, lib from .internals import ( - BinaryenGlobalRef, BinaryenHeapType, BinaryenLiteral, BinaryenOp, @@ -385,11 +385,25 @@ def add_function_export( def add_global( self, name: bytes, global_type: BinaryenType, mutable: bool, init: Expression - ) -> BinaryenGlobalRef: + ): ref = lib.BinaryenAddGlobal(self.ref, name, global_type, mutable, init.ref) - return ref + return Global(ref) + + def get_global(self, name: bytes): + ref = lib.BinaryenGetGlobal(self.ref, name) + if ref == ffi.NULL: + return None + return Global(ref) + + def remove_global(self, name: bytes): + lib.BinaryenRemoveGlobal(self.ref, name) + + def get_num_globals(self): + return int(lib.BinaryenGetNumGlobals(self.ref)) - # TODO: GetGlobal, RemoveGlobal, GetNumGlobals, GetGlobalByIndex + def get_global_by_index(self, index: int): + ref = lib.BinaryenGetGlobalByIndex(self.ref, index) + return Global(ref) # TODO: AddTag, GetTag, RemoveTag diff --git a/binaryen/internals.py b/binaryen/internals.py index d82f5d1..1da821a 100644 --- a/binaryen/internals.py +++ b/binaryen/internals.py @@ -22,4 +22,3 @@ class __BaseType: BinaryenExternalKind = __NewType("BinaryenExternalKind", __BaseType) BinaryenOp = __NewType("BinaryenOp", __BaseType) BinaryenLiteral = __NewType("BinaryenLiteral", __BaseType) -BinaryenGlobalRef = __NewType("BinaryenGlobalRef", __BaseType) From 863fb4a791448790f62cbb76e3ccd3471caa9d2d Mon Sep 17 00:00:00 2001 From: Jonathan Hargreaves <32854503+jonathanharg@users.noreply.github.com> Date: Sun, 21 Apr 2024 12:49:45 +0100 Subject: [PATCH 5/7] feat: Added BinaryenType to string converter --- binaryen/__global.py | 2 +- binaryen/__module.py | 9 ++------- binaryen/type/__init__.py | 1 + binaryen/type/to_str.py | 29 +++++++++++++++++++++++++++++ examples/unary.py | 21 +++++++++++++++++++++ 5 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 binaryen/type/to_str.py create mode 100644 examples/unary.py diff --git a/binaryen/__global.py b/binaryen/__global.py index 5cdb7d4..2daa391 100644 --- a/binaryen/__global.py +++ b/binaryen/__global.py @@ -1,5 +1,5 @@ -from ._binaryen import lib from .__expression import Expression +from ._binaryen import lib class Global: diff --git a/binaryen/__module.py b/binaryen/__module.py index f5a6670..3ec9924 100644 --- a/binaryen/__module.py +++ b/binaryen/__module.py @@ -4,16 +4,11 @@ from . import literal from .__expression import Block, Expression -from .__global import Global from .__feature import Feature from .__functionref import FunctionRef +from .__global import Global from ._binaryen import ffi, lib -from .internals import ( - BinaryenHeapType, - BinaryenLiteral, - BinaryenOp, - BinaryenType, -) +from .internals import BinaryenHeapType, BinaryenLiteral, BinaryenOp, BinaryenType from .type import TypeNone type BinaryenExportRef = Any diff --git a/binaryen/type/__init__.py b/binaryen/type/__init__.py index 61fb165..90eec05 100644 --- a/binaryen/type/__init__.py +++ b/binaryen/type/__init__.py @@ -6,6 +6,7 @@ from .._binaryen import ffi as __ffi from .._binaryen import lib as __lib from . import array_type, heap_type, signature_type, struct_type +from .to_str import to_str # These "types" are actually integers representing the type # e.g. none = 0, i32 = 2 etc. diff --git a/binaryen/type/to_str.py b/binaryen/type/to_str.py new file mode 100644 index 0000000..fecd5d5 --- /dev/null +++ b/binaryen/type/to_str.py @@ -0,0 +1,29 @@ +from .._binaryen import lib +from ..internals import BinaryenType + + +def to_str(type: BinaryenType) -> str: + return { + lib.BinaryenTypeNone(): "None", + lib.BinaryenTypeInt32(): "Int32", + lib.BinaryenTypeInt64(): "Int64", + lib.BinaryenTypeFloat32(): "Float32", + lib.BinaryenTypeFloat64(): "Float64", + lib.BinaryenTypeVec128(): "Vec128", + lib.BinaryenTypeFuncref(): "Funcref", + lib.BinaryenTypeExternref(): "Externref", + lib.BinaryenTypeAnyref(): "Anyref", + lib.BinaryenTypeEqref(): "Eqref", + lib.BinaryenTypeI31ref(): "I31ref", + lib.BinaryenTypeStructref(): "Structref", + lib.BinaryenTypeArrayref(): "Arrayref", + lib.BinaryenTypeStringref(): "Stringref", + lib.BinaryenTypeStringviewWTF8(): "StringviewWTF8", + lib.BinaryenTypeStringviewWTF16(): "StringviewWTF16", + lib.BinaryenTypeStringviewIter(): "StringviewIter", + lib.BinaryenTypeNullref(): "Nullref", + lib.BinaryenTypeNullExternref(): "NullExternref", + lib.BinaryenTypeNullFuncref(): "NullFuncref", + lib.BinaryenTypeUnreachable(): "Unreachable", + lib.BinaryenTypeAuto(): "Auto", + }[type] diff --git a/examples/unary.py b/examples/unary.py new file mode 100644 index 0000000..22c4d02 --- /dev/null +++ b/examples/unary.py @@ -0,0 +1,21 @@ +import binaryen + +mod = binaryen.Module() + +# f_sub = mod.unary(binaryen.operations.NegFloat32(), mod.f32(6.7)) +f_sub = mod.unary( + binaryen.operations.NegFloat32(), mod.local_get(0, binaryen.type.Float32) +) + +mod.add_function(b"float_test", binaryen.type.Float32, binaryen.type.Float32, [], f_sub) +mod.add_function_export(b"float_test", b"float_test") + +# i_sub = mod.binary(binaryen.operations.SubInt32(), mod.i32(0), mod.i32(10)) +# i_sub = mod.binary(binaryen.operations.S, mod.i32(0), mod.local_get(0, binaryen.type.Int32)) + +mod.add_function(b"int_test", binaryen.type.Int32, binaryen.type.Int32, [], i_sub) +mod.add_function_export(b"int_test", b"int_test") + +mod.optimize() + +mod.print() From 76a4ff7e439427ee0a9f784445a69643c3c79d8d Mon Sep 17 00:00:00 2001 From: Jonathan Hargreaves <32854503+jonathanharg@users.noreply.github.com> Date: Sun, 21 Apr 2024 19:09:56 +0100 Subject: [PATCH 6/7] fix: Fixed function typo in FunctionRef and added disabled feature --- binaryen/__functionref.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/binaryen/__functionref.py b/binaryen/__functionref.py index e2da6ba..73c7fd5 100644 --- a/binaryen/__functionref.py +++ b/binaryen/__functionref.py @@ -10,13 +10,13 @@ def get_name(self) -> str: return lib.BinaryenFunctionGetName(self.ref) def get_num_vars(self) -> int: - return lib.BinaryenGetNumVars(self.ref) + return lib.BinaryenFunctionGetNumVars(self.ref) def get_var(self, index: int) -> BinaryenType: return lib.BinaryenFunctionGetVar(self.ref, index) - # def add_var(self, type: Type) -> int: - # return lib.BinaryenFunctionAddVar(self.ref, type) + def add_var(self, type: BinaryenType) -> int: + return lib.BinaryenFunctionAddVar(self.ref, type) def get_num_locals(self) -> int: return lib.BinaryenFunctionGetNumLocals(self.ref) From 9ee12f284a29a2a405f16d941a2a137c7728cbc9 Mon Sep 17 00:00:00 2001 From: Jonathan Hargreaves <32854503+jonathanharg@users.noreply.github.com> Date: Sun, 21 Apr 2024 19:21:29 +0100 Subject: [PATCH 7/7] Bumped version number --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index e6ee8ad..bf2053a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "binaryen.py" -version = "117.0.0" +version = "117.1.0" description = "A Python wrapper for Binaryen" authors = [{ name = "Jonathan Hargreaves", email = "jhargreaves189@gmail.com" }] readme = "README.md"