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.
This commit is contained in:
25
language-bindings/python/wamr-api/README.md
Normal file
25
language-bindings/python/wamr-api/README.md
Normal file
@ -0,0 +1,25 @@
|
||||
# WARM API
|
||||
|
||||
## Examples
|
||||
|
||||
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
|
||||
|
||||
Install requirements,
|
||||
|
||||
```
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
Run the following command,
|
||||
|
||||
```sh
|
||||
ctypesgen ../../../../core/iwasm/include/wasm_export.h -l ../libs/libiwasm.so -o iwasm.py
|
||||
```
|
||||
1
language-bindings/python/wamr-api/requirements.txt
Normal file
1
language-bindings/python/wamr-api/requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
ctypesgen==1.1.1
|
||||
11
language-bindings/python/wamr-api/samples/compile.sh
Normal file
11
language-bindings/python/wamr-api/samples/compile.sh
Normal file
@ -0,0 +1,11 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
/opt/wasi-sdk/bin/clang \
|
||||
-O0 -z stack-size=4096 -Wl,--initial-memory=65536 \
|
||||
-Wl,--strip-all,--no-entry -nostdlib \
|
||||
-Wl,--export=sum\
|
||||
-Wl,--allow-undefined \
|
||||
-o test.wasm sum.c
|
||||
22
language-bindings/python/wamr-api/samples/main.py
Normal file
22
language-bindings/python/wamr-api/samples/main.py
Normal file
@ -0,0 +1,22 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
from wamr.wamrapi.wamr import Engine, Module, Instance, ExecEnv
|
||||
from ctypes import c_uint
|
||||
import pathlib
|
||||
|
||||
def main():
|
||||
engine = Engine()
|
||||
module = Module.from_file(engine, pathlib.Path(__file__).parent / "sum.wasm")
|
||||
module_inst = Instance(module)
|
||||
exec_env = ExecEnv(module_inst)
|
||||
|
||||
func = module_inst.lookup_function("sum")
|
||||
|
||||
argv = (c_uint * 2)(*[10, 11])
|
||||
exec_env.call(func, len(argv), argv)
|
||||
print(argv[0])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
12
language-bindings/python/wamr-api/samples/sum.c
Normal file
12
language-bindings/python/wamr-api/samples/sum.c
Normal file
@ -0,0 +1,12 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
sum(int a, int b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
Reference in New Issue
Block a user