Skip to content

Commit c1d8ec7

Browse files
committed
Add a main entry for package
1 parent 10b6696 commit c1d8ec7

File tree

7 files changed

+79
-63
lines changed

7 files changed

+79
-63
lines changed

.github/workflows/develop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ jobs:
2525
python script/build_spec.py
2626
python script/build_wasi.py
2727
python test/example.py
28+
python test/main.py
2829
python test/spec.py
2930
python test/wasi.py

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ $ python script/build_spec.py # Download spec tests at res/spec.
5656
$ python script/build_wasi.py # Download wasi tests at res/wasi-testsuite.
5757

5858
$ python test/example.py # Test example.
59+
$ python test/main.py # Test main.
5960
$ python test/spec.py # Test spec.
6061
$ python test/wasi.py # Test wasi.
6162
```

pywasm/__main__.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import argparse
2+
import sys
3+
4+
import pywasm
5+
6+
parser = argparse.ArgumentParser()
7+
parser.add_argument('--version', '-v', action='version', version=f'pywasm {pywasm.version}')
8+
parser.add_argument('--func', default='_start', help='set func name')
9+
parser.add_argument('--func-args', action='append', default=[], help='set func args, like arg')
10+
parser.add_argument('--wasi-envs', action='append', default=[], help='set wasi envs, like key=val')
11+
parser.add_argument('--wasi-dirs', action='append', default=[], help='set wasi dirs, like dir:dir(host)')
12+
parser.add_argument('--wasi-args', action='append', default=[], help='set wasi args, like arg')
13+
parser.add_argument('--wasi', choices=['preview1'], help='set wasi version')
14+
parser.add_argument('file', help='wasm file')
15+
args = parser.parse_args()
16+
17+
18+
def main_wasi():
19+
wasi_args = [args.file] + args.wasi_args
20+
wasi_dirs = {}
21+
wasi_envs = {}
22+
for e in args.wasi_dirs:
23+
spes = e.split(':', 1)
24+
wasi_dirs[spes[0]] = spes[1]
25+
for e in args.wasi_envs:
26+
spes = e.split('=', 1)
27+
wasi_envs[spes[0]] = spes[1]
28+
if args.wasi == 'preview1':
29+
runtime = pywasm.core.Runtime()
30+
wasi = pywasm.wasi.Preview1(wasi_args, wasi_dirs, wasi_envs)
31+
wasi.bind(runtime)
32+
inst = runtime.instance_from_file(args.file)
33+
code = wasi.main(runtime, inst)
34+
sys.exit(code)
35+
36+
37+
def main_wasm():
38+
runtime = pywasm.core.Runtime()
39+
inst = runtime.instance_from_file(args.file)
40+
addr = [e for e in inst.exps if e.name == args.func][0].data.data
41+
func = runtime.machine.store.func[addr]
42+
assert len(func.type.args) == len(args.func_args)
43+
func_args = []
44+
for e in zip(func.type.args, args.func_args):
45+
if e[0] in [pywasm.ValType.i32(), pywasm.ValType.i64()]:
46+
func_args.append(int(e[1]))
47+
continue
48+
if e[0] in [pywasm.ValType.f32(), pywasm.ValType.f64()]:
49+
func_args.append(float(e[1]))
50+
continue
51+
if e[0] == pywasm.ValType.v128():
52+
func_args.append(bytearray.fromhex(e[1][2:]))
53+
continue
54+
assert 0
55+
rets = runtime.invocate(inst, args.func, func_args)
56+
print(rets)
57+
58+
59+
if args.wasi:
60+
main_wasi()
61+
main_wasm()

pywasm/core.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4161,7 +4161,12 @@ def instance_from_file(self, path: str) -> ModuleInst:
41614161
with open(path, 'rb') as f:
41624162
return self.instance(ModuleDesc.from_reader(f))
41634163

4164-
def invocate(self, module: ModuleInst, func: str, args: typing.List[int | float]) -> typing.List[int | float]:
4164+
def invocate(
4165+
self,
4166+
module: ModuleInst,
4167+
func: str,
4168+
args: typing.List[int | float | bytearray]
4169+
) -> typing.List[int | float | bytearray]:
41654170
# Once a module has been instantiated, any exported function can be invoked externally via its function address
41664171
# in the store and an appropriate list of argument values.
41674172
addr = [e for e in module.exps if e.name == func][0].data.data

script/wasi.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

script/wasi_testsuite_adapter.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

test/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import subprocess
2+
3+
4+
def call(cmd: str) -> subprocess.CompletedProcess[bytes]:
5+
print(f'$ {cmd}')
6+
return subprocess.run(cmd, shell=True, check=True)
7+
8+
9+
call('python -m pywasm --func pi --func-args 7 example/pi/bin/pi.wasm')
10+
call('python -m pywasm --wasi preview1 --wasi-dirs pywasm:pywasm example/wasi_ll/bin/wasi_ll.wasm')

0 commit comments

Comments
 (0)