mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-05-09 13:16:26 +00:00
Python WAMR API binding: Add malloc/free and register_native (#1989)
This commit is contained in:
parent
4ca57a0228
commit
b4f0228497
|
@ -9,9 +9,10 @@
|
||||||
# pylint: disable=missing-module-docstring
|
# pylint: disable=missing-module-docstring
|
||||||
|
|
||||||
import pathlib
|
import pathlib
|
||||||
from setuptools import setup
|
from setuptools import setup, find_packages
|
||||||
from setuptools.command.develop import develop
|
from setuptools.command.develop import develop
|
||||||
from setuptools.command.install import install
|
from setuptools.command.install import install
|
||||||
|
from setuptools.command.egg_info import egg_info
|
||||||
from subprocess import check_call
|
from subprocess import check_call
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,19 +20,25 @@ def build_library():
|
||||||
cur_path = pathlib.Path(__file__).parent
|
cur_path = pathlib.Path(__file__).parent
|
||||||
check_call(f"{cur_path}/utils/create_lib.sh".split())
|
check_call(f"{cur_path}/utils/create_lib.sh".split())
|
||||||
|
|
||||||
|
|
||||||
class PreDevelopCommand(develop):
|
class PreDevelopCommand(develop):
|
||||||
"""Pre-installation for development mode."""
|
|
||||||
def run(self):
|
def run(self):
|
||||||
build_library()
|
build_library()
|
||||||
develop.run(self)
|
develop.run(self)
|
||||||
|
|
||||||
|
|
||||||
class PreInstallCommand(install):
|
class PreInstallCommand(install):
|
||||||
"""Pre-installation for installation mode."""
|
|
||||||
def run(self):
|
def run(self):
|
||||||
build_library()
|
build_library()
|
||||||
install.run(self)
|
install.run(self)
|
||||||
|
|
||||||
|
|
||||||
|
class PreEggInfoCommand(egg_info):
|
||||||
|
def run(self):
|
||||||
|
build_library()
|
||||||
|
egg_info.run(self)
|
||||||
|
|
||||||
|
|
||||||
with open("README.md") as f:
|
with open("README.md") as f:
|
||||||
readme = f.read()
|
readme = f.read()
|
||||||
|
|
||||||
|
@ -43,6 +50,8 @@ setup(
|
||||||
version="0.1.0",
|
version="0.1.0",
|
||||||
description="A WebAssembly runtime powered by WAMR",
|
description="A WebAssembly runtime powered by WAMR",
|
||||||
long_description=readme,
|
long_description=readme,
|
||||||
|
packages=find_packages(where="src"),
|
||||||
|
package_dir={"": "src"},
|
||||||
author="The WAMR Project Developers",
|
author="The WAMR Project Developers",
|
||||||
author_email="hello@bytecodealliance.org",
|
author_email="hello@bytecodealliance.org",
|
||||||
url="https://github.com/bytecodealliance/wasm-micro-runtime",
|
url="https://github.com/bytecodealliance/wasm-micro-runtime",
|
||||||
|
@ -51,5 +60,6 @@ setup(
|
||||||
cmdclass={
|
cmdclass={
|
||||||
'develop': PreDevelopCommand,
|
'develop': PreDevelopCommand,
|
||||||
'install': PreInstallCommand,
|
'install': PreInstallCommand,
|
||||||
|
'egg_info': PreEggInfoCommand,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
|
@ -10,7 +10,7 @@ from ctypes import cast
|
||||||
from ctypes import create_string_buffer
|
from ctypes import create_string_buffer
|
||||||
from ctypes import POINTER
|
from ctypes import POINTER
|
||||||
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 Alloc_With_Pool
|
||||||
from wamr.wamrapi.iwasm import RuntimeInitArgs
|
from wamr.wamrapi.iwasm import RuntimeInitArgs
|
||||||
from wamr.wamrapi.iwasm import wasm_exec_env_t
|
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_load
|
||||||
from wamr.wamrapi.iwasm import wasm_runtime_lookup_function
|
from wamr.wamrapi.iwasm import wasm_runtime_lookup_function
|
||||||
from wamr.wamrapi.iwasm import wasm_runtime_unload
|
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:
|
class Engine:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self._native_symbols = dict()
|
||||||
self.init_args = self._get_init_args()
|
self.init_args = self._get_init_args()
|
||||||
wasm_runtime_full_init(pointer(self.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
|
init_args.mem_alloc_option.pool.heap_size = heap_size
|
||||||
return init_args
|
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:
|
class Module:
|
||||||
__create_key = object()
|
__create_key = object()
|
||||||
|
@ -87,7 +107,13 @@ class Instance:
|
||||||
print("deleting Instance")
|
print("deleting Instance")
|
||||||
wasm_runtime_deinstantiate(self.module_inst)
|
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)
|
func = wasm_runtime_lookup_function(self.module_inst, name, None)
|
||||||
if not func:
|
if not func:
|
||||||
raise Exception("Error while looking-up function")
|
raise Exception("Error while looking-up function")
|
||||||
|
|
|
@ -1,16 +1,8 @@
|
||||||
# WARM API
|
# WARM API
|
||||||
|
|
||||||
## Examples
|
## Setup
|
||||||
|
|
||||||
Copy in `language-bindings/python/wamr/libs` the library `libiwasm` generated from `product-mini/platforms`.
|
### Pre-requisites
|
||||||
|
|
||||||
There is a [simple example](./samples/main.py) to show how to use bindings.
|
|
||||||
|
|
||||||
```
|
|
||||||
python samples/main.py
|
|
||||||
```
|
|
||||||
|
|
||||||
## Update WAMR API bindings
|
|
||||||
|
|
||||||
Install requirements,
|
Install requirements,
|
||||||
|
|
||||||
|
@ -18,8 +10,20 @@ Install requirements,
|
||||||
pip install -r requirements.txt
|
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
|
```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
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in New Issue
Block a user