Coverage for examples/multi.py: 100%
31 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-20 16:25 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-20 16:25 +0000
1# This is an example of working with multi-value modules and dealing with
2# multi-value functions.
4from wasmtime import Config, Store, Engine, Module, FuncType, Func, ValType, Instance
6# Configure our `Store`, but be sure to use a `Config` that enables the
7# wasm multi-value feature since it's not stable yet.
8print("Initializing...")
9config = Config()
10config.wasm_multi_value = True
11store = Store(Engine(config))
13print("Compiling module...")
14module = Module.from_file(store.engine, "examples/multi.wat")
16print("Creating callback...")
17callback_type = FuncType([ValType.i32(), ValType.i64()], [ValType.i64(), ValType.i32()])
20def callback(a, b):
21 return [b + 1, a + 1]
24callback_func = Func(store, callback_type, callback)
26print("Instantiating module...")
27instance = Instance(store, module, [callback_func])
29print("Extracting export...")
30g = instance.exports(store)["g"]
32print("Calling export \"g\"...")
33results = g(store, 1, 3)
34print("> {} {}".format(results[0], results[1]))
36assert(results[0] == 4)
37assert(results[1] == 2)
39print("Calling export \"round_trip_many\"...")
40round_trip_many = instance.exports(store)["round_trip_many"]
41results = round_trip_many(store, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
43print("Printing result...")
44print(">")
45for r in results:
46 print(" %d" % r)
47assert(len(results) == 10)
48for i, r in enumerate(results):
49 assert(i == r)