def init(): # TODO: loader can not load module using WASI # https://github.com/bytecodealliance/wasmtime-py/pull/44 # import wasmtime.loader # import benchmark_wasm_wasi wasm_cfg = wasmtime.Config() wasm_cfg.cache = True wasi_cfg = wasmtime.WasiConfig() store = wasmtime.Store(wasmtime.Engine(wasm_cfg)) linker = wasmtime.Linker(store) linker.define_wasi(wasmtime.WasiInstance(store, "wasi_snapshot_preview1", wasi_cfg)) wasm = pkg_resources.read_binary(__package__, "benchmark_wasm.wasm") instance = linker.instantiate(wasmtime.Module(store.engine, wasm)) return instance
def _run_wasm_app(wasm_filename, argv): module_binary = importlib_resources.read_binary(__package__, wasm_filename) module_digest = hashlib.sha1(module_binary).digest() wasi_cfg = wasmtime.WasiConfig() wasi_cfg.argv = argv wasi_cfg.preopen_dir(str(importlib_resources.files(__package__) / "share"), "/share") wasi_cfg.preopen_dir("/", "/") wasi_cfg.preopen_dir(".", ".") wasi_cfg.inherit_stdin() wasi_cfg.inherit_stdout() wasi_cfg.inherit_stderr() engine = wasmtime.Engine() cache_path = pathlib.Path( os.getenv("YOWASP_CACHE_DIR", appdirs.user_cache_dir("yowasp"))) cache_path.mkdir(parents=True, exist_ok=True) cache_filename = (cache_path / "{}-cache".format(wasm_filename)) digest_filename = (cache_path / "{}-digest".format(wasm_filename)) try: with digest_filename.open("rb") as digest_file: if digest_file.read() != module_digest: raise Exception("cache miss") module = wasmtime.Module.deserialize_file(engine, str(cache_filename)) except: print("Preparing to run {}. This might take a while...".format( argv[0]), file=sys.stderr) module = wasmtime.Module(engine, module_binary) with cache_filename.open("wb") as cache_file: cache_file.write(module.serialize()) with digest_filename.open("wb") as digest_file: digest_file.write(module_digest) linker = wasmtime.Linker(engine) linker.define_wasi() store = wasmtime.Store(engine) store.set_wasi(wasi_cfg) app = linker.instantiate(store, module) linker.define_instance(store, "app", app) try: app.exports(store)["_start"](store) return 0 except wasmtime.ExitTrap as trap: return trap.code
prepared_string = bytes(string, 'utf-8') length_of_string = len(prepared_string) + 1 string_ptr = instance.exports["allocate"](length_of_string) memory = instance.exports["memory"] for idx in range(length_of_string - 1): memory.data_ptr[string_ptr + idx] = prepared_string[idx] memory.data_ptr[string_ptr + length_of_string] = 0 return (string_ptr, length_of_string) relative_dir = 'lib/python' wasm_cfg = wasmtime.Config() wasm_cfg.cache = True wasi_cfg = wasmtime.WasiConfig() wasi_cfg.argv = () wasi_cfg.preopen_dir(".", "/") wasi_cfg.inherit_stdin() wasi_cfg.inherit_stdout() wasi_cfg.inherit_stderr() wasm_bytes = open(f'{relative_dir}/smartcore_wasi_lib.wasm', 'rb').read() store = wasmtime.Store(wasmtime.Engine(wasm_cfg)) linker = wasmtime.Linker(store) module = wasmtime.Module(store.engine, wasm_bytes) wasi = linker.define_wasi( wasmtime.WasiInstance(store, "wasi_snapshot_preview1", wasi_cfg)) smartcore_wasi = linker.instantiate(module) (file_ptr, file_len) = get_string_ptr(f'{relative_dir}/iris_knn.model',