From b4f022849727952b2b310df0bad573586f15d456 Mon Sep 17 00:00:00 2001 From: tonibofarull Date: Tue, 28 Feb 2023 09:19:17 +0100 Subject: [PATCH] Python WAMR API binding: Add malloc/free and register_native (#1989) --- language-bindings/python/setup.py | 16 ++++++++-- .../python/src/wamr/wamrapi/wamr.py | 30 +++++++++++++++++-- language-bindings/python/wamr-api/README.md | 28 +++++++++-------- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/language-bindings/python/setup.py b/language-bindings/python/setup.py index bf0ba53e..fb7993e6 100755 --- a/language-bindings/python/setup.py +++ b/language-bindings/python/setup.py @@ -9,9 +9,10 @@ # pylint: disable=missing-module-docstring import pathlib -from setuptools import setup +from setuptools import setup, find_packages from setuptools.command.develop import develop from setuptools.command.install import install +from setuptools.command.egg_info import egg_info from subprocess import check_call @@ -19,19 +20,25 @@ def build_library(): cur_path = pathlib.Path(__file__).parent check_call(f"{cur_path}/utils/create_lib.sh".split()) + class PreDevelopCommand(develop): - """Pre-installation for development mode.""" def run(self): build_library() develop.run(self) + class PreInstallCommand(install): - """Pre-installation for installation mode.""" def run(self): build_library() install.run(self) +class PreEggInfoCommand(egg_info): + def run(self): + build_library() + egg_info.run(self) + + with open("README.md") as f: readme = f.read() @@ -43,6 +50,8 @@ setup( version="0.1.0", description="A WebAssembly runtime powered by WAMR", long_description=readme, + packages=find_packages(where="src"), + package_dir={"": "src"}, author="The WAMR Project Developers", author_email="hello@bytecodealliance.org", url="https://github.com/bytecodealliance/wasm-micro-runtime", @@ -51,5 +60,6 @@ setup( cmdclass={ 'develop': PreDevelopCommand, 'install': PreInstallCommand, + 'egg_info': PreEggInfoCommand, }, ) diff --git a/language-bindings/python/src/wamr/wamrapi/wamr.py b/language-bindings/python/src/wamr/wamrapi/wamr.py index e8d496c3..abbd2322 100644 --- a/language-bindings/python/src/wamr/wamrapi/wamr.py +++ b/language-bindings/python/src/wamr/wamrapi/wamr.py @@ -10,7 +10,7 @@ from ctypes import cast from ctypes import create_string_buffer from ctypes import POINTER from ctypes import pointer - +from wamr.wamrapi.iwasm import String from wamr.wamrapi.iwasm import Alloc_With_Pool from wamr.wamrapi.iwasm import RuntimeInitArgs from wamr.wamrapi.iwasm import wasm_exec_env_t @@ -27,10 +27,15 @@ from wamr.wamrapi.iwasm import wasm_runtime_instantiate from wamr.wamrapi.iwasm import wasm_runtime_load from wamr.wamrapi.iwasm import wasm_runtime_lookup_function from wamr.wamrapi.iwasm import wasm_runtime_unload +from wamr.wamrapi.iwasm import wasm_runtime_module_malloc +from wamr.wamrapi.iwasm import wasm_runtime_module_free +from wamr.wamrapi.iwasm import wasm_runtime_register_natives +from wamr.wamrapi.iwasm import NativeSymbol class Engine: def __init__(self): + self._native_symbols = dict() self.init_args = self._get_init_args() wasm_runtime_full_init(pointer(self.init_args)) @@ -47,6 +52,21 @@ class Engine: init_args.mem_alloc_option.pool.heap_size = heap_size return init_args + def register_natives(self, module_name: str, native_symbols: list[NativeSymbol]) -> None: + module_name = String.from_param(module_name) + # WAMR does not copy the symbols. We must store them. + for native in native_symbols: + self._native_symbols[str(native.symbol)] = (module_name, native) + + if not wasm_runtime_register_natives( + module_name, + cast( + (NativeSymbol * len(native_symbols))(*native_symbols), + POINTER(NativeSymbol) + ), + len(native_symbols) + ): + raise Exception("Error while registering symbols") class Module: __create_key = object() @@ -87,7 +107,13 @@ class Instance: print("deleting Instance") wasm_runtime_deinstantiate(self.module_inst) - def lookup_function(self, name: str): + def malloc(self, nbytes: int, native_handler) -> c_uint: + return wasm_runtime_module_malloc(self.module_inst, nbytes, native_handler) + + def free(self, wasm_handler) -> None: + wasm_runtime_module_free(self.module_inst, wasm_handler) + + def lookup_function(self, name: str) -> wasm_function_inst_t: func = wasm_runtime_lookup_function(self.module_inst, name, None) if not func: raise Exception("Error while looking-up function") diff --git a/language-bindings/python/wamr-api/README.md b/language-bindings/python/wamr-api/README.md index eb89b421..5ee672e2 100644 --- a/language-bindings/python/wamr-api/README.md +++ b/language-bindings/python/wamr-api/README.md @@ -1,16 +1,8 @@ # WARM API -## Examples +## Setup -Copy in `language-bindings/python/wamr/libs` the library `libiwasm` generated from `product-mini/platforms`. - -There is a [simple example](./samples/main.py) to show how to use bindings. - -``` -python samples/main.py -``` - -## Update WAMR API bindings +### Pre-requisites Install requirements, @@ -18,8 +10,20 @@ Install requirements, pip install -r requirements.txt ``` -Run the following command, +### Build native lib and update bindings + +The following command builds the iwasm library and generates the Python bindings, ```sh -ctypesgen ../../../../core/iwasm/include/wasm_export.h -l ../libs/libiwasm.so -o iwasm.py +bash language-bindings/python/utils/create_lib.sh +``` + +This will build and copy libiwasm into the package. + +## Examples + +There is a [simple example](./samples/main.py) to show how to use bindings. + +``` +python samples/main.py ```