示例#1
0
def call_function(wasm, func, *args):
    env = m3.Environment()
    rt = env.new_runtime(4096)
    mod = env.parse_module(wasm)
    rt.load(mod)
    f = rt.find_function(func)
    return f.call_argv(*args)
示例#2
0
def test_m3(capfd):
    env = m3.Environment()
    rt = env.new_runtime(1024)
    assert isinstance(rt, m3.Runtime)
    mod = env.parse_module(FIB64_WASM)
    assert isinstance(mod, m3.Module)
    assert mod.name == '.unnamed'
    rt.load(mod)
    assert rt.get_memory(0) is None  # XXX
#     rt.print_info()
#     assert capfd.readouterr().out == """
# -- m3 runtime -------------------------------------------------
#  stack-size: 1024
#
#  module [0]  name: '.unnamed'; funcs: 1
# ----------------------------------------------------------------
# """
    with pytest.raises(RuntimeError):
        rt.find_function('not_existing')

    func = rt.find_function('fib')
    assert isinstance(func, m3.Function)
    assert func.call_argv('5') == 5
    assert func.call_argv('10') == 55
    assert func.name == 'fib'
    assert func.num_args == 1
    assert func.num_rets == 1
    assert func.arg_types == (2,)
    assert func.ret_types == (2,)
    assert func(0) == 0
    assert func(1) == 1
    rt.load(env.parse_module(ADD_WASM))
    add = rt.find_function('add')
    assert add(2, 3) == 5
示例#3
0
def test_dynamic_callback():
    env = m3.Environment()
    rt = env.new_runtime(1024)
    mod = env.parse_module(DYN_CALLBACK_WASM)
    rt.load(mod)
    dynCall_iii = rt.find_function("dynCall_iii")

    def pass_fptr(fptr):
        if fptr == 0:
            assert dynCall_iii(fptr, 12, 34) == 46
        elif fptr == 1:
            # TODO: call by table index directly here
            assert dynCall_iii(fptr, 12, 34) == 408
        else:
            raise Exception("Strange function ptr")

    mod.link_function("env", "pass_fptr", "v(i)", pass_fptr)

    # Indirect calls
    assert dynCall_iii(0, 12, 34) == 46
    assert dynCall_iii(1, 12, 34) == 408

    # Recursive exported function call (single calls)
    call_pass_fptr = rt.find_function("call_pass_fptr")
    base = 0
    call_pass_fptr(base+0)
    call_pass_fptr(base+1)

    # Recursive exported function call (multiple calls)
    rt.find_function("run_test")()
示例#4
0
def run_wasm():
    env = wasm3.Environment()
    rt = env.new_runtime(4096)
    mod = env.parse_module(WASM)
    rt.load(mod)
    wasm_fib = rt.find_function("fib")
    assert wasm_fib(N) == RES
示例#5
0
 def __init__(self, wasm):
     self.env = m3.Environment()
     self.rt = self.env.new_runtime(1024)
     self.mod = self.env.parse_module(wasm)
     self.rt.load(self.mod)
     self.mem = self.rt.get_memory(0)
     self.mod.link_function("env", "callback", "i(ii)", self.func)
     self.run_callback = self.rt.find_function("run_callback")
示例#6
0
def test_callback():
    env = m3.Environment()
    rt = env.new_runtime(1024)
    mod = env.parse_module(CALLBACK_WASM)
    rt.load(mod)
    mem = rt.get_memory(0)

    def func(x, y):
        assert x == 123
        assert y == 456
        return x*y
    mod.link_function("env", "callback", "i(ii)", func)
    run_callback = rt.find_function("run_callback")
    assert run_callback(123, 456) == 123*456
示例#7
0
#!/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-minimal.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)

wasm_run = rt.find_function("run")

print("Running CoreMark 1.0...")
res = wasm_run()

if res > 1:
    print(f"Result: {res:.3f}")
else:
    print("Error")