Coverage for examples/linking.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-20 16:25 +0000

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

2 

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

4 

5engine = Engine() 

6 

7# Load and compile our two modules 

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

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

10 

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

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

13linker = Linker(engine) 

14linker.define_wasi() 

15 

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

17store = Store(engine) 

18wasi = WasiConfig() 

19wasi.inherit_stdout() 

20store.set_wasi(wasi) 

21 

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

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

24linking2 = linker.instantiate(store, linking2) 

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

26 

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

28linking1 = linker.instantiate(store, linking1) 

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

30run(store)