Skip to main content

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

  1. (If upgrading binaryen version) Update BINARYEN_VERSION in pyproject.toml and .github/workflows/build.yaml
  2. Run bash scripts/build_libbinaryen.sh
  3. Run python -m pip install -e ., cibuildwheel, python -m pip wheel ., or whichever supported build command you would prefer

How To Use

import binaryen
from binaryen.type import Int32, TypeNone


# Equivalent python function
def add(x, y):
    return x + y


mod = binaryen.Module()
mod.add_function(
    b"add",
    binaryen.type.create([Int32, Int32]),
    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,
    ),
)

if not mod.validate():
    raise RuntimeError("Invalid module!")

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

binaryen_py-117.1.1.tar.gz (25.7 kB view hashes)

Uploaded Source

Built Distributions

binaryen.py-117.1.1-cp312-cp312-win_amd64.whl (2.9 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

binaryen.py-117.1.1-cp312-cp312-musllinux_1_1_x86_64.whl (8.6 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

binaryen.py-117.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.1 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

binaryen.py-117.1.1-cp312-cp312-macosx_11_0_arm64.whl (5.4 MB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

binaryen.py-117.1.1-cp312-cp312-macosx_10_9_x86_64.whl (6.2 MB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page