Coverage for examples/multi.py: 9%
34 statements
« prev ^ index » next coverage.py v7.11.3, created at 2025-12-01 19:40 +0000
« prev ^ index » next coverage.py v7.11.3, created at 2025-12-01 19:40 +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
6def run():
7 # Configure our `Store`, but be sure to use a `Config` that enables the
8 # wasm multi-value feature since it's not stable yet.
9 print("Initializing...")
10 config = Config()
11 config.wasm_multi_value = True
12 store = Store(Engine(config))
14 print("Compiling module...")
15 module = Module.from_file(store.engine, "examples/multi.wat")
17 print("Creating callback...")
18 callback_type = FuncType([ValType.i32(), ValType.i64()], [ValType.i64(), ValType.i32()])
21 def callback(a, b):
22 return [b + 1, a + 1]
25 callback_func = Func(store, callback_type, callback)
27 print("Instantiating module...")
28 instance = Instance(store, module, [callback_func])
30 print("Extracting export...")
31 g = instance.exports(store)["g"]
33 print("Calling export \"g\"...")
34 results = g(store, 1, 3)
35 print("> {} {}".format(results[0], results[1]))
37 assert(results[0] == 4)
38 assert(results[1] == 2)
40 print("Calling export \"round_trip_many\"...")
41 round_trip_many = instance.exports(store)["round_trip_many"]
42 results = round_trip_many(store, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
44 print("Printing result...")
45 print(">")
46 for r in results:
47 print(" %d" % r)
48 assert(len(results) == 10)
49 for i, r in enumerate(results):
50 assert(i == r)
52if __name__ == '__main__':
53 run()