def test_wat2wasm2instance(): wat = """ (module (type (func (param i32 i32) (result i32))) (func (type 0) local.get 0 local.get 1 i32.add) (export "sum" (func 0))) """ wasm_bytes = wat2wasm(wat) instance = Instance(wasm_bytes) assert instance.exports.sum(1, 2) == 3
def get_code(): # Let's declare the Wasm module with the text representation. wasm_bytes = wat2wasm(""" (module (type $sum_t (func (param i32 i32) (result i32))) (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32) local.get $x local.get $y i32.add) (export "add" (func $sum_f))) """) return wasm_bytes
def main(): # Create a store store = Store(engine.JIT(Compiler)) # Convert Wat file contents into Wasm binary code wat_file_name = str(sys.argv[1]) with open(wat_file_name) as wat_file: wat_source_code = wat_file.read() wasm_bytes = wat2wasm(wat_source_code) # Compile the Wasm module module = Module(store, wasm_bytes) # Obtain functions to be imported from the Wasm module import_object = make_import_object(store) # Instantiate the module instance = Instance(module, import_object) # Run start function and return to OS its exit code sys.exit(instance.exports.main())
# ```shell # $ python examples/engine_jit.py # ``` # # Ready? from wasmer import engine, wat2wasm, Store, Module, Instance from wasmer_compiler_cranelift import Compiler # Let's declare the Wasm module with the text representation. wasm_bytes = wat2wasm( """ (module (type $sum_t (func (param i32 i32) (result i32))) (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32) local.get $x local.get $y i32.add) (export "sum" (func $sum_f))) """ ) # Define the engine that will drive everything. # # In this case, the engine is `wasmer.engine.JIT` which roughly # means that the executable code will live in memory. engine = engine.JIT(Compiler) # Create a store, that holds the engine. store = Store(engine)
# If this module was written in Rust, it would have been: # # ```rs # extern "C" { # fn sum(x: i32, y: i32) -> i32; # } # # #[no_mangle] # pub extern "C" fn add_one(x: i32) -> i32 { # unsafe { sum(x, 1) } # } # ``` wasm_bytes = wat2wasm(""" (module (import "env" "sum" (func $sum (param i32 i32) (result i32))) (func (export "add_one") (param $x i32) (result i32) local.get $x i32.const 1 call $sum)) """) # Create a store. store = Store(engine.Universal(Compiler)) # Let's compile the Wasm module. module = Module(store, wasm_bytes) # Here we go. # # When creating an `Instance`, we can pass an `ImportObject`. All # entities that must be imported are registered inside the # `ImportObject`.
# ```shell # $ python examples/imports_exports.py # ``` # # Ready? from wasmer import engine, wat2wasm, Store, Module, ImportObject, Function, Global, Instance, Type, Value from wasmer_compiler_cranelift import Compiler # Let's declare the Wasm module with the text representation. # We are using the text representation of the module here wasm_bytes = wat2wasm(""" (module (func $host_function (import "" "host_function") (result i32)) (global $host_global (import "env" "host_global") i32) (func $function (export "guest_function") (result i32) (global.get $global)) (global $global (export "guest_global") i32 (i32.const 42)) (table $table (export "guest_table") 1 1 funcref) (memory $memory (export "guest_memory") 1)) """) # Create a store. Engines and compilers are explained in other # examples. store = Store(engine.Universal(Compiler)) # Let's compile the Wasm module. module = Module(store, wasm_bytes) # Let's write the Python function that is going to be imported, # i.e. called by the WebAssembly module.
from wasmer import engine, wat2wasm, Store, Module, Instance from wasmer_compiler_cranelift import Compiler TEST_BYTES = wat2wasm(""" (module (memory 16) (export "memory" (memory 0))) """) def test_benchmark_memory_view_int8_get(benchmark): store = Store(engine.JIT(Compiler)) module = Module(store, TEST_BYTES) instance = Instance(module) memory = instance.exports.memory.uint8_view() def bench(): _ = memory[0] benchmark(bench) def test_benchmark_memory_view_memoryview_get(benchmark): store = Store(engine.JIT(Compiler)) module = Module(store, TEST_BYTES) instance = Instance(module) memory = memoryview(instance.exports.memory.buffer) def bench(): _ = memory[0]
# ``` # # Ready? from wasmer import engine, wat2wasm, Store, Module, Instance from wasmer_compiler_cranelift import Compiler # Let's declare the Wasm module. # # We are using the text representation of the module here but you can # also load `.wasm` files using the `open` function. wasm_bytes = wat2wasm(""" (module (type $add_one_t (func (param i32) (result i32))) (func $add_one_f (type $add_one_t) (param $value i32) (result i32) local.get $value i32.const 1 i32.add) (export "add_one" (func $add_one_f))) """) # Create a store. Engines and compilers are explained in other # examples. store = Store(engine.JIT(Compiler)) # Let's compile the Wasm module. module = Module(store, wasm_bytes) # Let's instantiate the module! instance = Instance(module)
from wasmer_compiler_cranelift import Compiler # Let's declare the Wasm module with the text representation. # If this module was written in Rust, it would have been: # # ```rs # #[no_mangle] # pub extern fn hello() -> *const u8 { # b"Hello, World!\0".as_ptr() # } # ``` wasm_bytes = wat2wasm(""" (module (type $hello_t (func (result i32))) (func $hello (type $hello_t) (result i32) i32.const 42) (memory $memory 1) (export "hello" (func $hello)) (export "mem" (memory $memory)) (data (i32.const 42) "Hello, World!")) """) # Create a store. store = Store(engine.Universal(Compiler)) # Let's compile the Wasm module. module = Module(store, wasm_bytes) # Let's instantiate the Wasm module. instance = Instance(module) # OK, here go. First, let's call `hello`. It returns a pointer to the
# You can run the example directly by executing in Wasmer root:# # # ```shell # $ python examples/exports_global.py # ``` # # Ready? from wasmer import engine, wat2wasm, Store, Module, Instance, Global, Type from wasmer_compiler_cranelift import Compiler # Let's declare the Wasm module with the text representation. wasm_bytes = wat2wasm(""" (module (global $one (export "one") f32 (f32.const 1)) (global $some (export "some") (mut f32) (f32.const 0)) (func (export "get_one") (result f32) (global.get $one)) (func (export "get_some") (result f32) (global.get $some)) (func (export "set_some") (param f32) (global.set $some (local.get 0)))) """) # Create a store. store = Store(engine.Universal(Compiler)) # Let's compile the Wasm module. module = Module(store, wasm_bytes) # Let's instantiate the Wasm module. instance = Instance(module) # Here we go. #
# ``` # # Ready? from wasmer import engine, wat2wasm, Store, Module, ImportObject, Function, FunctionType, Type, Instance from wasmer_compiler_cranelift import Compiler # Let's declare the Wasm module with the text representation. wasm_bytes = wat2wasm(""" (module (type $run_t (func (param i32 i32) (result i32))) (type $early_exit_t (func (param) (result))) (import "env" "early_exit" (func $early_exit (type $early_exit_t))) (func $run (type $run_t) (param $x i32) (param $y i32) (result i32) (call $early_exit) (i32.add local.get $x local.get $y)) (export "run" (func $run))) """) # Create a store. store = Store(engine.Universal(Compiler)) # Let's compile the Wasm module. module = Module(store, wasm_bytes)
def test_wat2wasm(): assert wat2wasm('(module)') == b'\x00asm\x01\x00\x00\x00'
def load_code(filename): wasm_bytes = wat2wasm(open(filename, "r").read()) return wasm_bytes