Coverage for examples/multi.py: 100%

31 statements  

« 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. 

3 

4from wasmtime import Config, Store, Engine, Module, FuncType, Func, ValType, Instance 

5 

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)) 

12 

13print("Compiling module...") 

14module = Module.from_file(store.engine, "examples/multi.wat") 

15 

16print("Creating callback...") 

17callback_type = FuncType([ValType.i32(), ValType.i64()], [ValType.i64(), ValType.i32()]) 

18 

19 

20def callback(a, b): 

21 return [b + 1, a + 1] 

22 

23 

24callback_func = Func(store, callback_type, callback) 

25 

26print("Instantiating module...") 

27instance = Instance(store, module, [callback_func]) 

28 

29print("Extracting export...") 

30g = instance.exports(store)["g"] 

31 

32print("Calling export \"g\"...") 

33results = g(store, 1, 3) 

34print("> {} {}".format(results[0], results[1])) 

35 

36assert(results[0] == 4) 

37assert(results[1] == 2) 

38 

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) 

42 

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)