def test_benchmark_memory_view_bytearray_get(benchmark): memory = bytearray(wasmer.Instance(TEST_BYTES).memory.buffer) def bench(): _ = memory[0:255] benchmark(bench)
def test_benchmark_memory_view_int8_get(benchmark): memory = wasmer.Instance(TEST_BYTES).memory.uint8_view() def bench(): _ = memory[0:255] benchmark(bench)
def test_benchmark_memory_view_set_sequence_with_slice_assignment(benchmark): memory = wasmer.Instance(TEST_BYTES).memory.uint8_view() def bench(): memory[0:255] = range(0, 255) benchmark(bench)
def test_benchmark_nbody_with_wasmer(benchmark): instance = wasmer.Instance(TEST_BYTES) nbody = instance.exports.main def bench(): return nbody(N) assert benchmark(bench)
def test_benchmark_memory_view_set_sequence_with_a_loop(benchmark): memory = wasmer.Instance(TEST_BYTES).memory.uint8_view() def bench(): for nth in range(0, 255): memory[nth] = nth benchmark(bench)
def wasm(file='/me/dev/wasm/main42.wasm'): import wasmer bytes=open(file,'rb').read() wasmer.Module.validate(bytes) instance=wasmer.Instance(bytes) view = instance.memory.uint8_view(offset=0) result = instance.exports.main() print(result) print(wasm_string(result)) return result
def __init__(self, bytecode, images): buffer_ptr = 102400 # 100K buffer_size = 409600 # 400K self.instance = wasmer.Instance(bytecode) self.memory = self.instance.memory.uint8_view() self.instance.exports.set_ptrs(buffer_ptr, buffer_ptr + buffer_size) self.buffer_ptr = self.instance.exports.get_buffer_ptr() self.db_ptr = self.instance.exports.get_db_ptr() self.memory_ptr = self.instance.exports.get_memory_ptr() for image in images: db_write_ptr = self.instance.exports.get_db_write_ptr() self.memory[db_write_ptr:db_write_ptr + len(image)] = image self.instance.exports.on_write_to_db(len(image)) print(str(len(images)) + " images loaded")
def setup_image_wasm(): global wasm_process_image_lib global wasm_process_image_func wasm_process_image_lib = None exe_path, exe_name = os.path.split(sys.argv[0]) if len(exe_path) == 0: exe_path = '..' if os.path.isfile(os.path.join(exe_path, 'deca/wasm/dxgi.wasm')): with open(os.path.join(exe_path, 'deca/wasm/dxgi.wasm'), 'rb') as f: wasm_bytes = f.read() wasm_process_image_lib = wasmer.Instance(wasm_bytes) wasm_process_image_func = wasm_process_image_lib.exports.process_image return process_image_wasm
def emit_module(): #prog: kast): kast.Module code = '(module (func $main (result i32) (i32.const 42) (return)))' m1 = wasm.Module(code) for d in m1.definitions: print(d) if isinstance(d, wasm.Type): print(d.params) print(d.result) if isinstance(d, wasm.Func): print(d.locals) print(d.instructions) m1 = wasm.Module() # see wasm.instruction_table m1.add_definition(wasm.Type('$main_type', [], ['i32'])) # [(0, 'i32')] main_type = wasm.Ref('type', index=0, name='$main_type') m1.add_definition( wasm.Func( '$main', main_type, [], [wasm.Instruction('i32.const', (42)), wasm.Instruction('return')])) m1.add_definition(wasm.Memory('$0', 1)) m1.add_definition( wasm.Export("memory", "memory", wasm.Ref('memory', index=0, name='$0'))) m1.add_definition( wasm.Export("main", "func", wasm.Ref('func', index=0, name='$main'))) # wasmer.validate() print(m1.to_string()) wasm_bytes = m1.to_bytes() with open('out.wasm', 'wb') as f: f.write(wasm_bytes) instance = wasmer.Instance(wasm_bytes) print(instance.exports) # if 'main' in instance.exports: try: result = instance.exports.main() print(result) except Exception as e: print(e)
def read_str(instance, ptr, length): m = instance.memory.int32_view() ptr = ptr // m.bytes_per_element str = m[ptr:ptr + length] return ''.join(chr(c) for c in str) if __name__ == '__main__': wasm_bytes = open('target.wasm', 'rb').read() ins = wasmer.Instance(wasm_bytes) print(ins.exports) # memory = ins.memory.int32_view(ins.exports.getData()) str = 'hi_there' ptr = write_str(ins, str) print(read_str(ins, ptr, len(str))) ptr2 = write_str(ins, 'you') ptr3 = ins.exports.concat(ptr, ptr2, len(str), 3) print(read_str(ins, ptr3, len(str) + 3))
def __init__(self, *args, **kwargs): lib_env = { 'file_open': self.file_open, 'file_close': self.file_close, 'file_size': self.file_size, 'file_read': self.file_read, 'file_write': self.file_write, # 'db_print': self.db_print, # 'db_warn': self.db_warn, # 'db_error': self.db_error, 'dict_push': self.dict_push, 'dict_field_set': self.dict_field_set, 'list_push': self.list_push, 'list_append': self.list_append, 'hash_register': self.hash_register, 'hash32_push': self.hash32_push, 'hash48_push': self.hash48_push, 'hash64_push': self.hash64_push, 'bool_push': self.bool_push, 's8_push': self.s8_push, 'u8_push': self.u8_push, 's16_push': self.s16_push, 'u16_push': self.u16_push, 's32_push': self.s32_push, 'u32_push': self.u32_push, 's64_push': self.s64_push, 'u64_push': self.u64_push, 'f32_push': self.f32_push, 'f64_push': self.f64_push, 'str_push': self.str_push, 'enum_push': self.enum_push, 's8s_push': self.s8s_push, 'u8s_push': self.u8s_push, 's16s_push': self.s16s_push, 'u16s_push': self.u16s_push, 's32s_push': self.s32s_push, 'u32s_push': self.u32s_push, 's64s_push': self.s64s_push, 'u64s_push': self.u64s_push, 'f32s_push': self.f32s_push, 'f64s_push': self.f64s_push, 'xml_w_element_begin': self.xml_w_element_begin, 'xml_w_element_close': self.xml_w_element_close, 'xml_w_element_end': self.xml_w_element_end, 'xml_w_attr_set': self.xml_w_attr_set, 'xml_w_value_set': self.xml_w_value_set, 'timing_test': self.timing_test, } with open('deca_lib.wasm', 'rb') as f: wasm_bytes = f.read() self._module = wasmer.Module(wasm_bytes) if self._module.is_wasi_module: self._wasi = wasmer.Wasi('deca_lib_wasm') import_object = self._wasi.generate_import_object_for_module(self._module) else: self._wasi = None import_object = self._module.generate_import_object() env = {} for impt in self._module.imports: nm = impt['name'] if nm in lib_env: env[nm] = lib_env[nm] import_object.extend({"env": env}) self._instance = wasmer.Instance(wasm_bytes, import_object) self._instance.exports._initialize()
def __init__(self): lib_env = { 'db_print': self.db_print, 'db_warn': self.db_warn, 'db_error': self.db_error, 'dict_push': self.dict_push, 'dict_field_set': self.dict_field_set, 'list_push': self.list_push, 'list_append': self.list_append, 'hash_register': self.hash_register, 'hash32_push': self.hash32_push, 'hash48_push': self.hash48_push, 'hash64_push': self.hash64_push, 'bool_push': self.bool_push, 's8_push': self.s8_push, 'u8_push': self.u8_push, 's16_push': self.s16_push, 'u16_push': self.u16_push, 's32_push': self.s32_push, 'u32_push': self.u32_push, 's64_push': self.s64_push, 'u64_push': self.u64_push, 'f32_push': self.f32_push, 'f64_push': self.f64_push, 'str_push': self.str_push, 'enum_push': self.enum_push, 's8s_push': self.s8s_push, 'u8s_push': self.u8s_push, 's16s_push': self.s16s_push, 'u16s_push': self.u16s_push, 's32s_push': self.s32s_push, 'u32s_push': self.u32s_push, 's64s_push': self.s64s_push, 'u64s_push': self.u64s_push, 'f32s_push': self.f32s_push, 'f64s_push': self.f64s_push, } with open('deca_adf.wasm', 'rb') as f: wasm_bytes = f.read() self._module = wasmer.Module(wasm_bytes) if self._module.is_wasi_module: self._wasi = wasmer.Wasi('deca_adf_wasm') import_object = self._wasi.generate_import_object_for_module( self._module) else: self._wasi = None import_object = self._module.generate_import_object() env = {} for impt in self._module.imports: nm = impt['name'] if nm in lib_env: env[nm] = lib_env[nm] import_object.extend({"env": env}) self._instance = wasmer.Instance(wasm_bytes, import_object) self.adf_stack = []
#!/usr/bin/env python import io import sys import wasmer zstd_bytes = open("zstddec.wasm", "rb").read() instance = wasmer.Instance(zstd_bytes) def go(): e = instance.exports m = instance.memory buffInSize = e.ZSTD_DStreamInSize() buffOutSize = e.ZSTD_DStreamOutSize() buffInData = e.malloc(buffInSize) buffOutData = e.malloc(buffOutSize) inBuffer = e.malloc(32) outBuffer = e.malloc(32) # struct to pass inBufferView = m.uint32_view(offset=inBuffer) outBufferView = m.uint32_view(offset=outBuffer) inBufferDataView = m.uint8_view(offset=buffInData) outBufferDataView = m.uint8_view(offset=buffOutData) class ZSTDBuffer: def __init__(self, mem, *args): self.mem = mem