wabt.py
is a Python wrapper for the WebAssembly Binary Toolkit (WABT). It allows you to interact with WABT tools programmatically, making it easy to work with WebAssembly binaries and text formats in Python.
- Run WABT tools programmatically: Includes support for tools like
wat2wasm
,wasm2wat
,wasm-validate
,wasm-decompile
, and more. - Cross-platform support: Works on Windows, macOS, and Linux.
- Automatic updates: Automatically downloads and updates the latest WABT binaries.
- Simple API: Provides a Pythonic interface for interacting with WABT tools.
You can install wabt
via pip:
pip install wabt
To use the library, first initialize the Wabt
class. This will ensure the latest WABT binaries are downloaded and ready to use.
from wabt import Wabt
wabt = Wabt()
Convert a WebAssembly text file (.wat
) to a binary file (.wasm
).
wabt.wat_to_wasm("example.wat", output="example.wasm")
Convert a WebAssembly binary file (.wasm
) to a text file (.wat
).
wabt.wasm_to_wat("example.wasm", output="example.wat")
Validate a WebAssembly binary file.
output = wabt.wasm_validate("example.wasm")
print(output)
Decompile a WebAssembly binary file into a human-readable format.
wabt.wasm_decompile("example.wasm", output="example.dcmp")
Run a Spectest JSON file in the interpreter.
wabt.spectest_interp("test.json")
The following WABT tools are supported:
wat2wasm
: Convert WAT (WebAssembly Text) to WASM (WebAssembly Binary).wasm2wat
: Convert WASM to WAT.wasm-validate
: Validate a WebAssembly binary file.wasm-decompile
: Decompile a WebAssembly binary file.wasm-interp
: Run a WebAssembly binary in a stack-based interpreter.wasm-objdump
: Print information about the contents of WebAssembly binaries.wasm-stats
: Output statistics about a WebAssembly binary.wasm-strip
: Remove sections from a WebAssembly binary file.wast2json
: Convert a WAST (WebAssembly Spec Test) file to JSON.wat-desugar
: Format a WAT file.wasm2c
: Convert a WebAssembly binary to C code.
Most methods accept additional options to customize the behavior of the WABT tools. For example, you can validate a WASM file with verbose output by passing the --verbose
option.
wabt.wasm_validate("example.wasm", options=["--verbose"])
If you want to skip the automatic update process (e.g., for offline use), you can initialize the Wabt
class with the skip_update
parameter set to True
.
wabt = Wabt(skip_update=True)
- Python 3.6 or higher
- Internet connection (for downloading WABT binaries)
This library is built on top of the WebAssembly Binary Toolkit (WABT).