import io import pytest from pywasm import binary from pywasm import Runtime from .helpers import wat2wasm def run(code_wat): code_wasm = wat2wasm(code_wat) module = binary.Module.from_reader(io.BytesIO(code_wasm)) runtime = Runtime(module, {}, {}) out_put = runtime.exec('testEntry', []) return (runtime, out_put) @pytest.mark.parametrize('size,offset,exp_out_put', [ ('32', 0, 0x3020100), ('32', 1, 0x4030201), ('64', 0, 0x706050403020100), ('64', 2, 0x908070605040302), ]) def test_i32_64_load(size, offset, exp_out_put): code_wat = f""" (module (memory 1) (data (memory 0) (i32.const 0) "\\00\\01\\02\\03\\04\\05\\06\\07\\08\\09\\10") (func (export "testEntry") (result i{size}) i32.const {offset} i{size}.load return )) """ (_, out_put) = run(code_wat) assert exp_out_put == out_put def test_load_then_store(): code_wat = """ (module (memory 1) (data (memory 0) (i32.const 0) "\\04\\00\\00\\00") (func (export "testEntry") (result i32) (local $my_memory_value i32) ;; Load i32 from address 0 i32.const 0 i32.load ;; Add 8 to the loaded value i32.const 8 i32.add local.set $my_memory_value ;; Store back to the memory i32.const 0 local.get $my_memory_value i32.store ;; Return something i32.const 9 return )) """ (runtime, out_put) = run(code_wat) assert 9 == out_put assert (b'\x0c'+ b'\00' * 23) == runtime.store.mems[0].data[:24]