Skip to content

Commit

Permalink
Merge pull request #2 from jonathanharg/next
Browse files Browse the repository at this point in the history
Version 117.1.0
  • Loading branch information
jonathanharg committed Apr 21, 2024
2 parents d9eb2c7 + 9ee12f2 commit b223b2d
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 20 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions binaryen/__functionref.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions binaryen/__global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from .__expression import Expression
from ._binaryen import lib


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))
33 changes: 21 additions & 12 deletions binaryen/__module.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@
from .__expression import Block, Expression
from .__feature import Feature
from .__functionref import FunctionRef
from .__global import Global
from ._binaryen import ffi, lib
from .internals import (
BinaryenGlobalRef,
BinaryenHeapType,
BinaryenLiteral,
BinaryenOp,
BinaryenType,
)
from .internals import BinaryenHeapType, BinaryenLiteral, BinaryenOp, BinaryenType
from .type import TypeNone

type BinaryenExportRef = Any
Expand Down Expand Up @@ -60,10 +55,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(
Expand Down Expand Up @@ -385,11 +380,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

Expand Down
1 change: 0 additions & 1 deletion binaryen/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ class __BaseType:
BinaryenExternalKind = __NewType("BinaryenExternalKind", __BaseType)
BinaryenOp = __NewType("BinaryenOp", __BaseType)
BinaryenLiteral = __NewType("BinaryenLiteral", __BaseType)
BinaryenGlobalRef = __NewType("BinaryenGlobalRef", __BaseType)
1 change: 1 addition & 0 deletions binaryen/type/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions binaryen/type/to_str.py
Original file line number Diff line number Diff line change
@@ -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]
21 changes: 21 additions & 0 deletions examples/unary.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from setuptools import setup

setup(
cffi_modules=["binaryen/binaryen_build.py:ffibuilder"],
cffi_modules=["scripts/binaryen_build.py:ffibuilder"],
)

0 comments on commit b223b2d

Please sign in to comment.