def test_wasi_state_builder(): state_builder = \ wasi.StateBuilder("test-program"). \ argument("--foo"). \ environments({"ABC": "DEF", "X": "YZ"}). \ map_directory("the_host_current_dir", ".") assert isinstance(state_builder, wasi.StateBuilder)
def test_wasi_env_memory(): store = Store() wasi_env = wasi.StateBuilder("foo").finalize() import_object = wasi_env.generate_import_object(store, wasi.Version.LATEST) instance = Instance(Module(store, TEST_BYTES), import_object) wasi_env.memory = instance.exports.memory
def test_wasi(): store = Store() wasi_env = \ wasi.StateBuilder("test-program"). \ argument("--foo"). \ environments({"ABC": "DEF", "X": "YZ"}). \ map_directory("the_host_current_dir", "."). \ finalize() import_object = wasi_env.generate_import_object(store, wasi.Version.LATEST) instance = Instance(Module(store, TEST_BYTES), import_object) instance.exports._start()
def test_wasi_imports(): env = wasi.StateBuilder("foo").finalize() import_object = env.generate_imports(Store(), wasi.Version.LATEST) assert isinstance(import_object, dict) assert sorted(import_object['wasi_snapshot_preview1'].keys()) == [ 'args_get', 'args_sizes_get', 'clock_res_get', 'clock_time_get', 'environ_get', 'environ_sizes_get', 'fd_advise', 'fd_allocate', 'fd_close', 'fd_datasync', 'fd_fdstat_get', 'fd_fdstat_set_flags', 'fd_fdstat_set_rights', 'fd_filestat_get', 'fd_filestat_set_size', 'fd_filestat_set_times', 'fd_pread', 'fd_prestat_dir_name', 'fd_prestat_get', 'fd_pwrite', 'fd_read', 'fd_readdir', 'fd_renumber', 'fd_seek', 'fd_sync', 'fd_tell', 'fd_write', 'path_create_directory', 'path_filestat_get', 'path_filestat_set_times', 'path_link', 'path_open', 'path_readlink', 'path_remove_directory', 'path_rename', 'path_symlink', 'path_unlink_file', 'poll_oneoff', 'proc_exit', 'proc_raise', 'random_get', 'sched_yield', 'sock_recv', 'sock_send', 'sock_shutdown' ]
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.uint8_view(string_ptr) memory[0:length_of_string] = prepared_string memory[length_of_string] = 0 return (string_ptr, length_of_string) relative_dir = 'lib/python' wasm_bytes = open(f'{relative_dir}/smartcore_wasi_lib.wasm', 'rb').read() store = Store(engine.JIT(Compiler)) module = Module(store, wasm_bytes) wasi_version = wasi.get_version(module, strict=True) wasi_env = wasi.StateBuilder('smartcore-wasi-lib').preopen_directory( ".").finalize() import_object = wasi_env.generate_import_object(store, wasi_version) instance = Instance(module, import_object) (file_ptr, file_len) = get_string_ptr(f'{relative_dir}/iris_knn.model', instance) instance.exports.init(file_ptr) perfomances = [] num_executions = 1000 if os.environ.get("noe") is not None: num_executions = int(os.environ.get("noe")) print(f"Executing {num_executions} times") start_time = datetime.datetime.now() for i in range(num_executions): t1 = time.monotonic_ns()
def test_wasi_import_object(): env = wasi.StateBuilder("foo").finalize() assert isinstance(env.generate_import_object(Store(), wasi.Version.LATEST), ImportObject)
def test_wasi_env(): assert isinstance(wasi.StateBuilder("foo").finalize(), wasi.Environment)
# same way. So, to ensure compatibility, we need to know the version. wasi_version = wasi.get_version(module, strict=True) # Second, create a `wasi.Environment`. It contains everything related # to WASI. To build such an environment, we must use the # `wasi.StateBuilder`. # # In this case, we specify the program name is `wasi_test_program`. We # also specify the program is invoked with the `--test` argument, in # addition to two environment variable: `COLOR` and # `APP_SHOULD_LOG`. Finally, we map the `the_host_current_dir` to the # current directory. There it is: wasi_env = \ wasi.StateBuilder('wasi_test_program'). \ argument('--test'). \ environment('COLOR', 'true'). \ environment('APP_SHOULD_LOG', 'false'). \ map_directory('the_host_current_dir', '.'). \ finalize() # From the WASI environment, we generate a custom import object. Why? # Because WASI is, from the user perspective, a bunch of # imports. Consequently `generate_import_object`… generates a # pre-configured import object. # # Do you remember when we said WASI has multiple versions? Well, we # need the WASI version here! import_object = wasi_env.generate_import_object(store, wasi_version) # Now we can instantiate the module. instance = Instance(module, import_object)