Coverage for tests/test_linker.py: 100%
100 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
1import unittest
3from wasmtime import *
6class TestLinker(unittest.TestCase):
7 def test_define(self):
8 store = Store()
9 linker = Linker(store.engine)
10 linker.allow_shadowing = False
12 func = Func(store, FuncType([], []), lambda: None)
13 linker.define(store, "", "a", func)
15 g = Global(store, GlobalType(ValType.i32(), False), Val.i32(0))
16 linker.define(store, "", "c", g)
18 mem = Memory(store, MemoryType(Limits(1, None)))
19 linker.define(store, "", "e", mem)
21 module = Module(store.engine, """
22 (module (table (export "") 1 funcref))
23 """)
24 table = Instance(store, module, []).exports(store).by_index[0]
25 linker.define(store, "", "g", table)
27 with self.assertRaises(WasmtimeError):
28 linker.define(store, "", "a", func)
29 linker.allow_shadowing = True
30 linker.define(store, "", "a", func)
32 with self.assertRaises(TypeError):
33 linker.define(store, "", "", 2) # type: ignore
34 with self.assertRaises(AttributeError):
35 linker.define(store, 2, "", func) # type: ignore
36 with self.assertRaises(AttributeError):
37 linker.define(store, "", 2, func) # type: ignore
39 def test_define_instance(self):
40 store = Store()
41 linker = Linker(store.engine)
42 with self.assertRaises(TypeError):
43 linker.define_instance("x", 2) # type: ignore
45 module = Module(store.engine, "(module)")
46 linker.define_instance(store, "a", Instance(store, module, []))
48 module = Module(store.engine, "(module (func (export \"foo\")))")
49 instance = Instance(store, module, [])
50 linker.define_instance(store, "b", instance)
51 with self.assertRaises(WasmtimeError):
52 linker.define_instance(store, "b", instance)
53 linker.allow_shadowing = True
54 linker.define_instance(store, "b", instance)
56 def test_define_wasi(self):
57 linker = Linker(Engine())
58 linker.define_wasi()
60 def test_instantiate(self):
61 store = Store()
62 linker = Linker(store.engine)
64 module = Module(store.engine, "(module (func (export \"foo\")))")
65 instance = Instance(store, module, [])
66 linker.define_instance(store, "x", instance)
68 func = Func(store, FuncType([], []), lambda: None)
69 linker.define(store, "y", "z", func)
71 module = Module(store.engine, """
72 (module
73 (import "x" "foo" (func))
74 (import "y" "z" (func))
75 )
76 """)
77 linker.instantiate(store, module)
79 module = Module(store.engine, """
80 (module
81 (import "x" "foo" (func))
82 (import "y" "z" (global i32))
83 )
84 """)
85 with self.assertRaises(WasmtimeError):
86 linker.instantiate(store, module)
88 module = Module(store.engine, """
89 (module
90 (func unreachable)
91 (start 0)
92 )
93 """)
94 with self.assertRaises(Trap):
95 linker.instantiate(store, module)
97 module = Module(store.engine, "(module)")
98 linker.instantiate(store, module)
100 def test_errors(self):
101 linker = Linker(Engine())
102 with self.assertRaises(AttributeError):
103 Linker(2) # type: ignore
104 with self.assertRaises(AttributeError):
105 linker.instantiate(Store(), 3) # type: ignore
107 def test_module(self):
108 store = Store()
109 linker = Linker(store.engine)
110 module = Module(store.engine, """
111 (module
112 (func (export "f"))
113 )
114 """)
115 linker.define_module(store, "foo", module)
116 module = Module(store.engine, """
117 (module
118 (import "foo" "f" (func))
119 )
120 """)
121 linker.instantiate(store, module)
123 def test_get_default(self):
124 store = Store()
125 linker = Linker(store.engine)
126 linker.get_default(store, "foo")(store)
128 def test_get_one_by_name(self):
129 store = Store()
130 linker = Linker(store.engine)
131 with self.assertRaises(WasmtimeError):
132 linker.get(store, "foo", "bar")
133 module = Module(store.engine, """
134 (module
135 (func (export "f"))
136 )
137 """)
138 linker.define_module(store, "foo", module)
139 assert(isinstance(linker.get(store, "foo", "f"), Func))
141 def test_define_func(self):
142 engine = Engine()
143 linker = Linker(engine)
144 called = {}
145 called['hits'] = 0
147 def call():
148 called['hits'] += 1
150 linker.define_func('a', 'b', FuncType([], []), call)
151 module = Module(engine, """
152 (module
153 (import "a" "b" (func))
154 (start 0)
155 )
156 """)
157 assert(called['hits'] == 0)
158 linker.instantiate(Store(engine), module)
159 assert(called['hits'] == 1)
160 linker.instantiate(Store(engine), module)
161 assert(called['hits'] == 2)