Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/olivi-r/wasmpy

WebAssembly in Python.
https://github.com/olivi-r/wasmpy

amd64 assembly cpython i386 i686 ia32 python python3 wasm wast wat webassembly webassembly-python webassembly-runtime x86 x86-64

Last synced: about 2 months ago
JSON representation

WebAssembly in Python.

Awesome Lists containing this project

README

        

[![AppVeyor Build status](https://ci.appveyor.com/api/projects/status/6jo7yag38m5ilv6h?svg=true)](https://ci.appveyor.com/project/olivi-r/wasmpy)

# wasmpy

WebAssembly in Python.

Wasmpy is a lightweight layer that sits between Python and WebAssembly. When attempting to import a WebAssembly file, the file is converted into machine code for native speeds.

# Installing

```sh
python3 -m pip install wasmpy
```

# Usage

To get started, first import the `wasmpy` module to register the WebAssembly import hooks.

Then you can just `import wasm_or_wat_file` to load a WebAssembly module.

## Usage with Python modules

```
|- my_module
|- __init__.py
|- wasm_math.wat
```

```python
# __init__.py

import wasmpy
from .wasm_math import add
```

```webassembly
;; wasm_math.wat

(module
(func (export "add") (param i32 i32) (result i32)
(i32.add (local.get 0) (local.get 1))
)
)
```

```python
>>> import my_module
>>> my_module.add(45, 960)
1005
>>>
```

## Invalid function names

Functions exported from WebAssembly can also be accessed from the module by using their name as a key.

For exported names that aren't valid Python identifiers or which start with a `_`, this is the only valid way of accessing these functions.

```webassembly
;; wasm_math.wat

(module
(func (export "add one") (param i32) (result i32)
(i32.add (local.get 0) (i32.const 1))
)
)
```

```python
>>> import wasmpy
>>> import wasm_math
>>> wasm_math["add one"](11)
12
>>>
```

# Building From Source

On Windows this requires [mingw-w64](https://www.mingw-w64.org/downloads/)

```sh
git clone https://github.com/olivi-r/wasmpy.git
cd wasmpy
make -f .mk
python3 -m pip install .
```

# Limitations

Wasmpy is still in active development, and only supports x86/x86-64 Windows and Linux machines and lacks some key features:

- most memory instructions
- most control instructions
- imports
- tables

# Goals

- Current target:

- Reach compatability with the [MVP](https://www.w3.org/TR/wasm-core-1)

- Future Goals
- Native support for more architectures, particularly those supported by [manylinux](https://github.com/pypa/manylinux)
- Interfacing with the [Python C API](https://docs.python.org/3/c-api) from WebAssembly (in conjunction with the Wasmpy sister project [wasmpy-build](https://github.com/olivi-r/wasmpy-build)) to allow compiled extension modules that are platform independent
- Compatibility with [Pyodide](https://github.com/pyodide/pyodide)
- Python implementation support for Jython, PyPy etc.
- Support for [WebAssembly proposals](https://github.com/WebAssembly/proposals)
- Support for [WASI](https://wasi.dev) snapshots, as well as support for supersets such as [WASIX](https://wasix.org)