Coverage for tests/test_linker.py: 100%
138 statements
« prev ^ index » next coverage.py v7.11.3, created at 2026-05-07 14:30 +0000
« prev ^ index » next coverage.py v7.11.3, created at 2026-05-07 14:30 +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)
163 def test_define_unknown_imports_as_traps(self):
164 engine = Engine()
165 linker = Linker(engine)
166 module = Module(engine, """
167 (module
168 (import "env" "missing" (func))
169 )
170 """)
171 linker.define_unknown_imports_as_traps(module)
172 store = Store(engine)
173 instance = linker.instantiate(store, module)
174 self.assertIsNotNone(instance)
176 with self.assertRaises(TypeError):
177 linker.define_unknown_imports_as_traps("not a module") # type: ignore
179 def test_define_unknown_imports_as_default_values(self):
180 engine = Engine()
181 linker = Linker(engine)
182 module = Module(engine, """
183 (module
184 (import "env" "missing" (func (result i32)))
185 )
186 """)
187 store = Store(engine)
188 linker.define_unknown_imports_as_default_values(store, module)
189 instance = linker.instantiate(store, module)
190 self.assertIsNotNone(instance)
192 with self.assertRaises(TypeError):
193 linker.define_unknown_imports_as_default_values(store, "not a module") # type: ignore
195 def test_instantiate_pre(self):
196 engine = Engine()
197 linker = Linker(engine)
198 module = Module(engine, """
199 (module
200 (func (export "f") (result i32)
201 i32.const 42
202 )
203 )
204 """)
205 pre = linker.instantiate_pre(module)
206 self.assertIsNotNone(pre)
208 store1 = Store(engine)
209 instance1 = pre.instantiate(store1)
210 f1 = instance1.exports(store1)["f"]
211 assert isinstance(f1, Func)
212 self.assertEqual(f1(store1), 42)
214 store2 = Store(engine)
215 instance2 = pre.instantiate(store2)
216 f2 = instance2.exports(store2)["f"]
217 assert isinstance(f2, Func)
218 self.assertEqual(f2(store2), 42)
220 with self.assertRaises(TypeError):
221 linker.instantiate_pre("not a module") # type: ignore