def test_store_universal(): store = Store(engine.Universal()) assert store.engine_name == 'universal' assert store.compiler_name == None
# 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))) """ ) # 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. # # An `Instance` has an `exports` getter, which returns the same # `Exports` object (per `Instance`). `Exports.__getattr__` is the only # API to get an export. It will return either a `Function`, a # `Memory`, a `Global` or a `Table`. # # Let's call the `sum` function with 1 and 2.
(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.Universal` which roughly # means that the executable code will live in memory. # # This is _the_ place to pass the compiler. Note that `Compiler` is # not instantiated, we pass the class only. engine = engine.Universal(Compiler) # Create a store, that holds the engine. store = Store(engine) # Let's compile the Wasm module with the Cranelift compiler. module = Module(store, wasm_bytes) # Let's instantiate the Wasm module. instance = Instance(module) # Let's call the `sum` exported function. sum = instance.exports.sum results = sum(1, 2) print(results)