A Python wrapper for Binaryen
Project description
Binaryen.py
Binaryen.py is a python wrapper for Binaryen. Use this to create, optimise and interpret WebAssembly binaries in python.
Installation
Install through PyPI.
pip install binaryen.py
Windows, Mac (Intel & ARM) and Linux (manylinux, musllinux) are supported.
Build from source
- (If upgrading binaryen version) Update
BINARYEN_VERSION
inpyproject.toml
and.github/workflows/build.yaml
- Run
bash scripts/build_libbinaryen.sh
- Run
python -m pip install -e .
,cibuildwheel
,python -m pip wheel .
, or whichever supported build command you would prefer
How To Use
# 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,
Int32,
[Int32],
mod.block(
None,
[
mod.local_set(
2,
mod.binary(
binaryen.operations.AddInt32(),
mod.local_get(0, Int32),
mod.local_get(1, Int32),
),
),
mod.Return(mod.local_get(2, Int32)),
],
TypeNone,
),
)
mod.add_function_export(b"add", b"add")
mod.optimize()
mod.print()
Results in the following Wasm
(module
(type $0 (func (param i32 i32) (result i32)))
(export "add" (func $add))
(func $add (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
(i32.add
(local.get $0)
(local.get $1)
)
)
)
Reference the Binaryen header file to understand how to use Binaryen. This package makes no significant changes to how Binaryen is used. The majority of the work this module does is interoperating between C and Python and creating more pythonic classes for Modules, Expressions and Functions.
For more examples see examples.
Missing Functions
You can still call any missing functions that haven't been implemented by the wrapper yet by calling them directly. To do this use binaryen.lib.BinaryenFullFunctionName()
and call the full function name as described in the Binaryen header file.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for binaryen.py-117.0.0-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d91bf3033d1bbb79eb4bfb237f6c4a69d3d7f7a4eeaa965c348087e1a91632b |
|
MD5 | 52c0c8dd654a2a823a7e11eeaf1a5146 |
|
BLAKE2b-256 | 9addeab5dac9ea33d36c049f7443324275adb4f112bed340399a6efb37ecd720 |
Hashes for binaryen.py-117.0.0-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5d1c74a211b321f93888d5fee857108378e9d4269671226ef0d4232740f6b0b |
|
MD5 | 0606883f456025bc5a94659a5f663200 |
|
BLAKE2b-256 | a974a4e260de3db207ab57741be0809b2a7bf7bd4278e4c87cd000d59a9f2c7f |
Hashes for binaryen.py-117.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 041178b3c45e2ac8388fd33f1d85f2baae03a88d9e07458c9d1d2daed7c1fe90 |
|
MD5 | f1d2040c7a30a26c5e80dce7e36c5fc0 |
|
BLAKE2b-256 | 961402b6fbd2e9899481862fbacea9b28596f4da017faa878736ff64239a499e |
Hashes for binaryen.py-117.0.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb72c1f2976aa8aeaba351dd7ccdc45f73e1d97ff3c4bdda6acd060a1c6e05a8 |
|
MD5 | b2e9bc5eb19e21f5a7fe09734a442acb |
|
BLAKE2b-256 | e513719cc69d5eb939ce0836f065485ff39679b823f238a9b853695a456c82d4 |
Hashes for binaryen.py-117.0.0-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c1792af52330a44eb03dafcc17e452f2855a06b308324731d25dc18ffbbf32b |
|
MD5 | a292f24b58c7bb13792e10f55f8cc58b |
|
BLAKE2b-256 | 87cab35cd1b733949f859c0907027f2b4a019e2ffff9fffd41244271d0894c64 |