Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ jobs:
python script/build_spec.py
python script/build_wasi.py
python test/example.py
python test/main.py
python test/spec.py
python test/wasi.py
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ $ python script/build_spec.py # Download spec tests at res/spec.
$ python script/build_wasi.py # Download wasi tests at res/wasi-testsuite.

$ python test/example.py # Test example.
$ python test/main.py # Test main.
$ python test/spec.py # Test spec.
$ python test/wasi.py # Test wasi.
```
Expand Down
61 changes: 61 additions & 0 deletions pywasm/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import argparse
import sys

import pywasm

parser = argparse.ArgumentParser()
parser.add_argument('--version', '-v', action='version', version=f'pywasm {pywasm.version}')
parser.add_argument('--func', default='_start', help='set func name')
parser.add_argument('--func-args', action='append', default=[], help='set func args, like arg')
parser.add_argument('--wasi-envs', action='append', default=[], help='set wasi envs, like key=val')
parser.add_argument('--wasi-dirs', action='append', default=[], help='set wasi dirs, like dir:dir(host)')
parser.add_argument('--wasi-args', action='append', default=[], help='set wasi args, like arg')
parser.add_argument('--wasi', choices=['preview1'], help='set wasi version')
parser.add_argument('file', help='wasm file')
args = parser.parse_args()


def main_wasi():
wasi_args = [args.file] + args.wasi_args
wasi_dirs = {}
wasi_envs = {}
for e in args.wasi_dirs:
spes = e.split(':', 1)
wasi_dirs[spes[0]] = spes[1]
for e in args.wasi_envs:
spes = e.split('=', 1)
wasi_envs[spes[0]] = spes[1]
if args.wasi == 'preview1':
runtime = pywasm.core.Runtime()
wasi = pywasm.wasi.Preview1(wasi_args, wasi_dirs, wasi_envs)
wasi.bind(runtime)
inst = runtime.instance_from_file(args.file)
code = wasi.main(runtime, inst)
sys.exit(code)


def main_wasm():
runtime = pywasm.core.Runtime()
inst = runtime.instance_from_file(args.file)
addr = [e for e in inst.exps if e.name == args.func][0].data.data
func = runtime.machine.store.func[addr]
assert len(func.type.args) == len(args.func_args)
func_args = []
for e in zip(func.type.args, args.func_args):
if e[0] in [pywasm.ValType.i32(), pywasm.ValType.i64()]:
func_args.append(int(e[1]))
continue
if e[0] in [pywasm.ValType.f32(), pywasm.ValType.f64()]:
func_args.append(float(e[1]))
continue
if e[0] == pywasm.ValType.v128():
func_args.append(bytearray.fromhex(e[1][2:]))
continue
assert 0
rets = runtime.invocate(inst, args.func, func_args)
print(rets)


if args.wasi:
main_wasi()
main_wasm()
7 changes: 6 additions & 1 deletion pywasm/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4161,7 +4161,12 @@ def instance_from_file(self, path: str) -> ModuleInst:
with open(path, 'rb') as f:
return self.instance(ModuleDesc.from_reader(f))

def invocate(self, module: ModuleInst, func: str, args: typing.List[int | float]) -> typing.List[int | float]:
def invocate(
self,
module: ModuleInst,
func: str,
args: typing.List[int | float | bytearray]
) -> typing.List[int | float | bytearray]:
# Once a module has been instantiated, any exported function can be invoked externally via its function address
# in the store and an appropriate list of argument values.
addr = [e for e in module.exps if e.name == func][0].data.data
Expand Down
32 changes: 0 additions & 32 deletions script/wasi.py

This file was deleted.

30 changes: 0 additions & 30 deletions script/wasi_testsuite_adapter.py

This file was deleted.

10 changes: 10 additions & 0 deletions test/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import subprocess


def call(cmd: str) -> subprocess.CompletedProcess[bytes]:
print(f'$ {cmd}')
return subprocess.run(cmd, shell=True, check=True)


call('python -m pywasm --func pi --func-args 7 example/pi/bin/pi.wasm')
call('python -m pywasm --wasi preview1 --wasi-dirs pywasm:pywasm example/wasi_ll/bin/wasi_ll.wasm')
Loading