Coverage for tests/test_linker.py: 100%
102 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-20 16:25 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-20 16:25 +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(TypeError):
103 linker.allow_shadowing = 2
104 with self.assertRaises(AttributeError):
105 Linker(2) # type: ignore
106 with self.assertRaises(AttributeError):
107 linker.instantiate(Store(), 3) # type: ignore
109 def test_module(self):
110 store = Store()
111 linker = Linker(store.engine)
112 module = Module(store.engine, """
113 (module
114 (func (export "f"))
115 )
116 """)
117 linker.define_module(store, "foo", module)
118 module = Module(store.engine, """
119 (module
120 (import "foo" "f" (func))
121 )
122 """)
123 linker.instantiate(store, module)
125 def test_get_default(self):
126 store = Store()
127 linker = Linker(store.engine)
128 linker.get_default(store, "foo")(store)
130 def test_get_one_by_name(self):
131 store = Store()
132 linker = Linker(store.engine)
133 with self.assertRaises(WasmtimeError):
134 linker.get(store, "foo", "bar")
135 module = Module(store.engine, """
136 (module
137 (func (export "f"))
138 )
139 """)
140 linker.define_module(store, "foo", module)
141 assert(isinstance(linker.get(store, "foo", "f"), Func))
143 def test_define_func(self):
144 engine = Engine()
145 linker = Linker(engine)
146 called = {}
147 called['hits'] = 0
149 def call():
150 called['hits'] += 1
152 linker.define_func('a', 'b', FuncType([], []), call)
153 module = Module(engine, """
154 (module
155 (import "a" "b" (func))
156 (start 0)
157 )
158 """)
159 assert(called['hits'] == 0)
160 linker.instantiate(Store(engine), module)
161 assert(called['hits'] == 1)
162 linker.instantiate(Store(engine), module)
163 assert(called['hits'] == 2)