Skip to content

Commit 3cc132e

Browse files
authored
Add WAMR API bindings in Python (#1959)
Before adding the new bindings: 1. Moved wasm-c-api in a subfolder wasmcapi in the package. 2. Adapted the tests to be able to run in this new structure. New: 1. Added the WAMR API in another folder wamrapi in the same level as wasm-c-api. 2. Created an OOP proposal. 3. Added an example using this proposal.
1 parent f60c3c6 commit 3cc132e

30 files changed

+2053
-27
lines changed

language-bindings/python/MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include src/wamr/libs/*

language-bindings/python/README.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
# wamr-python
22

3-
## Installation
3+
The WAMR Python package contains a set of high-level bindings for WAMR API and WASM-C-API.
44

5-
### Installing from the source code
5+
## Installation
66

7-
Installing from local source tree is in _development mode_. The package appears to be installed but still is editable from the source tree.
7+
To Install from local source tree in _development mode_ run the following command,
88

99
```bash
10-
$ python -m pip install -e /path/to/wamr-root/binding/python
10+
python -m pip install -e .
1111
```
1212

13-
## Usage
13+
In this mode the package appears to be installed but still is editable from the source tree.
1414

15-
```python
16-
import wamr.ffi as ffi
17-
```
15+
## Usage
1816

19-
### Preparation
17+
From the same package you can use two set of APIs.
2018

21-
The binding will load the shared library _libiwasm.so_ from the WAMR repo. So before running the binding, you need to build the library yourself.
19+
To use the WAMR API you can import the symbols as follows,
2220

23-
The default compile options are good enough.
21+
```py
22+
from wamr.wamrapi.wamr import Engine, Module, Instance, ExecEnv
23+
```
2424

25-
Please be aware that `wasm_frame_xxx` and `wasm_trap_xxx` only work well when enabling `WAMR_BUILD_DUMP_CALL_STACK`.
25+
In the order hand, to use the WASM-C-API,
2626

27-
### Examples
27+
```py
28+
import wamr.wasmcapi.ffi as ffi
29+
```
2830

29-
There is a [simple example](./samples/hello_procedural.py) to show how to use bindings. Actually, the python binding follows C-APIs. There it should be easy if be familiar with _programming with wasm-c-api_.
31+
For more information:
3032

31-
Unit test cases under _./tests_ could be another but more complete references.
33+
* [WAMR API](./wamr_api)
34+
* [WASM-C-API](./wasm_c_api)

language-bindings/python/setup.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,28 @@
88
# pylint: disable=missing-function-docstring
99
# pylint: disable=missing-module-docstring
1010

11-
from setuptools import setup, find_packages
11+
import pathlib
12+
from setuptools import setup
13+
from setuptools.command.develop import develop
14+
from setuptools.command.install import install
15+
from subprocess import check_call
16+
17+
18+
def build_library():
19+
cur_path = pathlib.Path(__file__).parent
20+
check_call(f"{cur_path}/utils/create_lib.sh".split())
21+
22+
class PreDevelopCommand(develop):
23+
"""Pre-installation for development mode."""
24+
def run(self):
25+
build_library()
26+
develop.run(self)
27+
28+
class PreInstallCommand(install):
29+
"""Pre-installation for installation mode."""
30+
def run(self):
31+
build_library()
32+
install.run(self)
1233

1334

1435
with open("README.md") as f:
@@ -24,7 +45,11 @@
2445
long_description=readme,
2546
author="The WAMR Project Developers",
2647
author_email="[email protected]",
27-
url="https://github.com/bytecodealliance/wamr-python",
48+
url="https://github.com/bytecodealliance/wasm-micro-runtime",
2849
license=license,
29-
packages=["wamr"],
50+
include_package_data=True,
51+
cmdclass={
52+
'develop': PreDevelopCommand,
53+
'install': PreInstallCommand,
54+
},
3055
)

language-bindings/python/src/wamr/__init__.py

Whitespace-only changes.

language-bindings/python/src/wamr/libs/.placeholder

Whitespace-only changes.

language-bindings/python/src/wamr/wamrapi/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)