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_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_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
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("", {