def __init__(self): self.store = Store() self.module = Module.from_file(self.store, "main.wasm") self.imports = return_wasi_imports(self.store, self.module) load_callback_type = FuncType([ValType.i32()], [ValType.i32()]) call_callback_type = FuncType( [ValType.i32(), ValType.i32()], [ValType.i32()]) unload_callback_type = FuncType([ValType.i32()], [ValType.i32()]) load_callback_func = Func(self.store, load_callback_type, load, access_caller=True) call_callback_func = Func(self.store, call_callback_type, call, access_caller=True) unload_callback_func = Func(self.store, unload_callback_type, unload, access_caller=True) self.instance = Instance( self.module, [ load_callback_func, call_callback_func, unload_callback_func, *self.imports, ], )
def invoke_wasm(data): wasm_key = data[1] operand_b = int(data[2]) wasm = execute('GET', wasm_key) wasm_bytes = bytes(wasm, 'ascii') store = Store() module = Module.validate(store, wasm_bytes) module = Module(store.engine, wasm_bytes) get_a_func = Func(store, FuncType([], [ValType.i32()]), get_a) instance = Instance(store, module, [get_a_func]) return instance.exports["run"](operand_b)
def __init__(self): wasm_cfg = Config() wasm_cfg.cache = True store = Store(Engine(wasm_cfg)) linker = Linker(store) wasi_cfg = WasiConfig() wasi_inst = WasiInstance(store, "wasi_snapshot_preview1", wasi_cfg) linker.define_wasi(wasi_inst) self.inst = None err_handler_type = FuncType([ValType.i32(), ValType.i32()], []) err_handler_func = Func(store, err_handler_type, self._err_handler) linker.define("env", "AStyleErrorHandler", err_handler_func) wasm_file = os.path.join(os.path.dirname(__file__), "libastyle.wasm") module = Module.from_file(store.engine, wasm_file) self.inst = linker.instantiate(module) self.inst.exports["_initialize"]() self._opts_ptr = WasmString.from_str(self.inst, "")
def type(self) -> FuncType: """ Gets the type of this func as a `FuncType` """ ptr = ffi.wasm_func_type(self._ptr) return FuncType._from_ptr(ptr, None)
# multi-value functions. from wasmtime import Config, Store, Engine, Module, FuncType, Func, ValType, Instance # Configure our `Store`, but be sure to use a `Config` that enables the # wasm multi-value feature since it's not stable yet. print("Initializing...") config = Config() config.wasm_multi_value = True store = Store(Engine(config)) print("Compiling module...") module = Module.from_file(store.engine, "examples/multi.wat") print("Creating callback...") callback_type = FuncType([ValType.i32(), ValType.i64()], [ValType.i64(), ValType.i32()]) def callback(a, b): return [b + 1, a + 1] callback_func = Func(store, callback_type, callback) print("Instantiating module...") instance = Instance(store, module, [callback_func]) print("Extracting export...") g = instance.exports["g"] print("Calling export \"g\"...")
def type(self): ptr = dll.wasm_func_type(self.__ptr__) return FuncType.__from_ptr__(ptr, None)
from wasmtime import Store, Module, Instance, Func, FuncType # Almost all operations in wasmtime require a contextual "store" argument to be # shared amongst objects store = Store() # Here we can compile a `Module` which is then ready for instantiation # afterwards module = Module.from_file(store.engine, './examples/hello.wat') # Our module needs one import, so we'll create that here. def say_hello(): print("Hello from Python!") hello = Func(store, FuncType([], []), say_hello) # And with all that we can instantiate our module and call the export! instance = Instance(store, module, [hello]) instance.exports["run"]()
def test_new(self): FuncType([], []) FuncType([ValType.i32()], [ValType.i64()])
vals[k] = v def get_f(k): print("get: {}".format(k)) if k in vals: return vals[k] else: return 0 def print_f(v): print("print: {}".format(v)) store = Store() module = Module.from_file(store, '../get_set.wat') set_type = FuncType([ValType.i32(), ValType.i32()], []) set_func = Func(store, set_type, set_f) get_type = FuncType([ValType.i32()], [ValType.i32()]) get_func = Func(store, get_type, get_f) print_type = FuncType([ValType.i32()], []) print_func = Func(store, print_type, print_f) instance = Instance(module, [set_func, get_func, print_func]) run = instance.exports['run'] run()