From 8d71546dd0e64530d6d5899d8de21d2a702dfa8e Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Wed, 17 Mar 2021 13:05:18 +0200 Subject: [PATCH] Update examples --- platforms/python/README.md | 11 ++---- .../{simple-benchmark.py => 00-fibonacci.py} | 25 ++++++++++++ .../examples/{coremark.py => 01-coremark.py} | 6 --- platforms/python/examples/02-metered.py | 38 +++++++++++++++++++ .../{asyncified.py => 03-asyncified.py} | 0 test/run-spec-test.py | 7 ++-- 6 files changed, 71 insertions(+), 16 deletions(-) rename platforms/python/examples/{simple-benchmark.py => 00-fibonacci.py} (68%) rename platforms/python/examples/{coremark.py => 01-coremark.py} (80%) create mode 100755 platforms/python/examples/02-metered.py rename platforms/python/examples/{asyncified.py => 03-asyncified.py} (100%) diff --git a/platforms/python/README.md b/platforms/python/README.md index d43d897..2d59fb9 100644 --- a/platforms/python/README.md +++ b/platforms/python/README.md @@ -15,15 +15,12 @@ pip3 install dist/pywasm3-*.tar.gz ## Usage example ```py -import wasm3 +import wasm3, base64 # WebAssembly binary -WASM = bytes.fromhex(""" - 00 61 73 6d 01 00 00 00 01 06 01 60 01 7e 01 7e - 03 02 01 00 07 07 01 03 66 69 62 00 00 0a 1f 01 - 1d 00 20 00 42 02 54 04 40 20 00 0f 0b 20 00 42 - 02 7d 10 00 20 00 42 01 7d 10 00 7c 0f 0b -""") +WASM = base64.b64decode("AGFzbQEAAAABBgFgAX4" + "BfgMCAQAHBwEDZmliAAAKHwEdACAAQgJUBEAgAA" + "8LIABCAn0QACAAQgF9EAB8Dws=") env = wasm3.Environment() rt = env.new_runtime(1024) diff --git a/platforms/python/examples/simple-benchmark.py b/platforms/python/examples/00-fibonacci.py similarity index 68% rename from platforms/python/examples/simple-benchmark.py rename to platforms/python/examples/00-fibonacci.py index 0732e53..132e3c0 100755 --- a/platforms/python/examples/simple-benchmark.py +++ b/platforms/python/examples/00-fibonacci.py @@ -3,6 +3,31 @@ import wasm3 import base64, time, timeit +""" +Input module: + (module + (type (;0;) (func (param i64) (result i64))) + (func (;0;) (type 0) (param i64) (result i64) + local.get 0 + i64.const 2 + i64.lt_u + if ;; label = @1 + local.get 0 + return + end + local.get 0 + i64.const 2 + i64.sub + call 0 + local.get 0 + i64.const 1 + i64.sub + call 0 + i64.add + return) + (export "fib" (func 0))) +""" + # WebAssembly binary WASM = base64.b64decode(""" AGFzbQEAAAABBgFgAX4BfgMCAQAHBwEDZmliAAAKHwEdACAAQgJUBEAgAA8LIABCAn0QACAAQgF9 diff --git a/platforms/python/examples/coremark.py b/platforms/python/examples/01-coremark.py similarity index 80% rename from platforms/python/examples/coremark.py rename to platforms/python/examples/01-coremark.py index f9b2454..1ba904d 100755 --- a/platforms/python/examples/coremark.py +++ b/platforms/python/examples/01-coremark.py @@ -19,9 +19,6 @@ with open(wasm_fn, "rb") as f: rt.load(mod) mod.link_function("env", "clock_ms", "i()", clock_ms) -# Gas metering will only apply to metered (pre-instrumented) modules -mod.gasLimit = 200_000_000 - wasm_run = rt.find_function("run") print("Running CoreMark 1.0...") @@ -32,6 +29,3 @@ if res > 1: else: print("Error") -if mod.gasUsed: - print(f"Gas used: {mod.gasUsed}") - diff --git a/platforms/python/examples/02-metered.py b/platforms/python/examples/02-metered.py new file mode 100755 index 0000000..9428642 --- /dev/null +++ b/platforms/python/examples/02-metered.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +import wasm3 +import os, time + +scriptpath = os.path.dirname(os.path.realpath(__file__)) +wasm_fn = os.path.join(scriptpath, "./wasm/coremark-metered.wasm") + +print("Initializing Wasm3 engine...") + +def clock_ms(): + return int(round(time.time() * 1000)) + +env = wasm3.Environment() +rt = env.new_runtime(4096) + +with open(wasm_fn, "rb") as f: + mod = env.parse_module(f.read()) + rt.load(mod) + mod.link_function("env", "clock_ms", "i()", clock_ms) + +# Gas metering will only apply to metered (pre-instrumented) modules +mod.gasLimit = 200_000_000 + +wasm_run = rt.find_function("run") + +print("Running CoreMark 1.0...") +try: + res = wasm_run() + + if res > 1: + print(f"Result: {res:.3f}") + else: + print("Error") +finally: + if mod.gasUsed: + print(f"Gas used: {mod.gasUsed}") + diff --git a/platforms/python/examples/asyncified.py b/platforms/python/examples/03-asyncified.py similarity index 100% rename from platforms/python/examples/asyncified.py rename to platforms/python/examples/03-asyncified.py diff --git a/test/run-spec-test.py b/test/run-spec-test.py index 2f9f829..03d6b8f 100755 --- a/test/run-spec-test.py +++ b/test/run-spec-test.py @@ -3,9 +3,10 @@ # Author: Volodymyr Shymanskyy # Usage: # ./run-spec-test.py -# ./run-spec-test.py ./core/i32.json -# ./run-spec-test.py ./core/float_exprs.json --line 2070 -# ./run-spec-test.py ./proposals/tail-call/*.json +# ./run-spec-test.py --spec=opam-1.1.1 +# ./run-spec-test.py .spec-v1.1/core/i32.json +# ./run-spec-test.py .spec-v1.1/core/float_exprs.json --line 2070 +# ./run-spec-test.py .spec-v1.1/proposals/tail-call/*.json # ./run-spec-test.py --exec "../build-custom/wasm3 --repl" # # Running WASI version with different engines: