Coverage for examples/linking.py: 17%

18 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2025-12-01 19:40 +0000

1# Example of instantiating two modules which link to each other. 

2 

3from wasmtime import Engine, Store, Module, Linker, WasiConfig 

4 

5def run(): 

6 engine = Engine() 

7 

8 # Load and compile our two modules 

9 linking1 = Module.from_file(engine, "examples/linking1.wat") 

10 linking2 = Module.from_file(engine, "examples/linking2.wat") 

11 

12 # Set up our linker which is going to be linking modules together. We 

13 # want our linker to have wasi available, so we set that up here as well. 

14 linker = Linker(engine) 

15 linker.define_wasi() 

16 

17 # Create a `Store` to hold instances, and configure wasi state 

18 store = Store(engine) 

19 wasi = WasiConfig() 

20 wasi.inherit_stdout() 

21 store.set_wasi(wasi) 

22 

23 # Instantiate our first module which only uses WASI, then register that 

24 # instance with the linker since the next linking will use it. 

25 linking2 = linker.instantiate(store, linking2) 

26 linker.define_instance(store, "linking2", linking2) 

27 

28 # And with that we can perform the final link and the execute the module. 

29 linking1 = linker.instantiate(store, linking1) 

30 run = linking1.exports(store)["run"] 

31 run(store) 

32 

33if __name__ == '__main__': 

34 run()