def test_call_i32_i64_f32_f64_f64(self): self.assertEqual( round( Instance(TEST_BYTES).call('i32_i64_f32_f64_f64', [ Value.i32(1), Value.i64(2), Value.f32(3.4), Value.f64(5.6) ]), 6), 1 + 2 + 3.4 + 5.6)
def test_import_global(): store = Store() module = Module( store, """ (module (import "env" "global" (global $global (mut i32))) (func (export "read_g") (result i32) global.get $global) (func (export "write_g") (param i32) local.get 0 global.set $global)) """) global_ = Global(store, Value.i32(7), mutable=True) import_object = ImportObject() import_object.register("env", {"global": global_}) instance = Instance(module, import_object) assert instance.exports.read_g() == 7 global_.value = 153 assert instance.exports.read_g() == 153 instance.exports.write_g(11) assert global_.value == 11
def test_constructor(): store = Store() global_ = Global(store, Value.i32(42)) assert global_.value == 42 type = global_.type assert type.type == Type.I32 assert type.mutable == False global_ = Global(store, Value.i64(153), mutable=False) assert global_.value == 153 type = global_.type assert type.type == Type.I64 assert type.mutable == False
def test_constructor_mutable(): store = Store() global_ = Global(store, Value.i32(42), mutable=True) assert global_.value == 42 type = global_.type assert type.type == Type.I32 assert type.mutable == True global_.value = 153 assert global_.value == 153
def test_call_f32_f32(self): self.assertEqual( Instance(TEST_BYTES).call('f32_f32', [Value.f32(7.)]), 7.)
def test_call_i32_i32(self): self.assertEqual( Instance(TEST_BYTES).call('i32_i32', [Value.i32(7)]), 7)
def test_call_i64_i64(self): self.assertEqual( Instance(TEST_BYTES).call('i64_i64', [Value.i64(7)]), 7)
def test_cannot_construct(): Value()
def test_basic_sum(self): self.assertEqual( Instance(TEST_BYTES).call( 'sum', [Value.i32(1), Value.i32(2)]), 3)
def test_f64(self): self.assertEqual(repr(Value.f64(4.2)), 'F64(4.2)')
def test_v128(): assert repr(Value.v128(340282366920938463463374607431768211455)) == 'V128(340282366920938463463374607431768211455)'
def test_f32(self): self.assertEqual(repr(Value.f32(4.2)), 'F32(4.2)')
def test_i64(self): self.assertEqual(repr(Value.i64(42)), 'I64(42)')
def test_i64(): assert repr(Value.i64(42)) == 'I64(42)'
def test_f32(): assert repr(Value.f32(4.2)) == 'F32(4.2)'
def test_i32(): assert repr(Value.i32(42)) == 'I32(42)'
from wasmer import Instance, Value import os __dir__ = os.path.dirname(os.path.realpath(__file__)) wasm_bytes = open(__dir__ + '/simple.wasm', 'rb').read() instance = Instance(wasm_bytes) result = instance.call('sum', [Value.i32(5), Value.i32(37)]) print(result) # 42!
def test_f64_auto_cast(self): self.assertEqual(repr(Value.f64(42)), 'F64(42.0)')
def test_call_f64_f64(self): self.assertEqual( Instance(TEST_BYTES).call('f64_f64', [Value.f64(7.)]), 7.)
def test_i32(self): self.assertEqual(repr(Value.i32(42)), 'I32(42)')
def test_f32_auto_cast(): assert repr(Value.f32(42)) == 'F32(42.0)'
def test_f32_auto_cast(self): self.assertEqual(repr(Value.f32(42)), 'F32(42.0)')
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. def host_function_implementation() -> int: return 42 host_function = Function(store, host_function_implementation) # Let's then create a global that is going to be imported. host_global = Global(store, Value.i32(42)) # Create an import object. # # Imports are stored in namespaces. We'll need to register each of the # namespaces with a name and add the imported entities there. # # Note that the namespace can also have an empty name. # # Our module requires us to import: # * A function `host_function` in a namespace with an empty name; # * A global `host_global` in the `env` namespace. # # Let's do this! import_object = ImportObject() import_object.register("", {
def test_f64(): assert repr(Value.f64(4.2)) == 'F64(4.2)'
def test_f64_auto_cast(): assert repr(Value.f64(42)) == 'F64(42.0)'
def test_cannot_construct(self): Value()