Coverage for tests/component/test_value.py: 100%
84 statements
« prev ^ index » next coverage.py v7.11.3, created at 2026-09-21 18:56 +0000
« prev ^ index » next coverage.py v7.11.3, created at 2026-09-21 18:56 +0000
1import unittest
3from wasmtime import Store, WasmtimeError
4from wasmtime.component import *
7class TestValue(unittest.TestCase):
8 def test_resource_type(self):
9 r1 = ResourceType.host(1)
10 r2 = ResourceType.host(2)
11 r3 = ResourceType.host(1)
12 self.assertNotEqual(r1, r2)
13 self.assertEqual(r1, r3)
15 def test_resource_host(self):
16 store = Store()
17 r1 = ResourceHost.own(42, 1)
19 r2 = r1.to_any(store)
20 self.assertEqual(r2.type, ResourceType.host(1))
21 self.assertTrue(r2.owned)
23 r3 = r2.to_host(store)
24 self.assertTrue(r3.owned)
25 self.assertEqual(r3.rep, 42)
26 self.assertEqual(r3.type, 1)
28 with self.assertRaises(WasmtimeError):
29 r2.drop(store)
31 r4 = r3.to_any(store)
32 r4.drop(store)
34 r5 = ResourceHost.borrow(84, 2)
35 self.assertFalse(r5.owned)
36 self.assertEqual(r5.rep, 84)
37 self.assertEqual(r5.type, 2)
39 with self.assertRaises(WasmtimeError):
40 ResourceHost()
42 def test_resource_host_dtor(self):
43 store = Store()
44 linker = Linker(store.engine)
46 def drop(_store, rep):
47 self.assertEqual(rep, 100)
49 with linker.root() as l:
50 l.add_resource('t', ResourceType.host(23), drop)
52 component = Component(store.engine, """
53 (component
54 (import "t" (type $t (sub resource)))
55 (core func $drop (canon resource.drop $t))
56 (core module $a
57 (import "" "drop" (func $drop (param i32)))
58 (func (export "drop") (param i32)
59 local.get 0
60 call $drop)
61 )
62 (core instance $a (instantiate $a
63 (with "" (instance
64 (export "drop" (func $drop))
65 ))
66 ))
67 (func (export "drop") (param "x" (own $t))
68 (canon lift (core func $a "drop")))
69 )
70 """)
71 instance = linker.instantiate(store, component)
72 f = instance.get_func(store, 'drop')
73 assert(f is not None)
75 with self.assertRaises(WasmtimeError):
76 f(store, ResourceHost.own(1, 2))
78 store = Store(store.engine)
79 instance = linker.instantiate(store, component)
80 f = instance.get_func(store, 'drop')
81 assert(f is not None)
82 f(store, ResourceHost.own(100, 23))
84 def test_exception_in_host_resource_dtor(self):
85 store = Store()
86 linker = Linker(store.engine)
88 def drop(_store, _rep):
89 raise RuntimeError('oh no')
91 with linker.root() as l:
92 l.add_resource('t', ResourceType.host(2), drop)
94 component = Component(store.engine, """
95 (component
96 (import "t" (type $t (sub resource)))
97 (core func $drop (canon resource.drop $t))
98 (core module $a
99 (import "" "drop" (func $drop (param i32)))
100 (func (export "drop") (param i32)
101 local.get 0
102 call $drop)
103 )
104 (core instance $a (instantiate $a
105 (with "" (instance
106 (export "drop" (func $drop))
107 ))
108 ))
109 (func (export "drop") (param "x" (own $t))
110 (canon lift (core func $a "drop")))
111 )
112 """)
113 instance = linker.instantiate(store, component)
114 f = instance.get_func(store, 'drop')
115 assert(f is not None)
117 with self.assertRaises(RuntimeError) as cm:
118 f(store, ResourceHost.own(1, 2))
119 self.assertEqual(str(cm.exception), 'oh no')
121 def test_resource_any(self):
122 with self.assertRaises(WasmtimeError):
123 ResourceAny()
125 store = Store()
126 linker = Linker(store.engine)
128 component = Component(store.engine, """
129 (component
130 (type $t' (resource (rep i32)))
131 (export $t "t" (type $t'))
132 (core func $new (canon resource.new $t))
133 (core func $drop (canon resource.drop $t))
134 (core module $a
135 (import "" "new" (func $new (param i32) (result i32)))
136 (import "" "drop" (func $drop (param i32)))
137 (func (export "new") (param i32) (result i32)
138 local.get 0
139 call $new)
140 (func (export "drop") (param i32)
141 local.get 0
142 call $drop)
143 )
144 (core instance $a (instantiate $a
145 (with "" (instance
146 (export "new" (func $new))
147 (export "drop" (func $drop))
148 ))
149 ))
150 (func (export "new") (param "x" u32) (result (own $t))
151 (canon lift (core func $a "new")))
152 (func (export "drop") (param "x" (own $t))
153 (canon lift (core func $a "drop")))
154 )
155 """)
156 instance = linker.instantiate(store, component)
157 new = instance.get_func(store, 'new')
158 drop = instance.get_func(store, 'drop')
159 assert(new is not None)
160 assert(drop is not None)
162 r1 = new(store, 100)
163 new.post_return(store)
164 r2 = new(store, 200)
165 new.post_return(store)
167 with self.assertRaises(WasmtimeError):
168 r1.to_host(store)
170 r1.drop(store)
171 drop(store, r2)
172 drop.post_return(store)
174 with self.assertRaises(WasmtimeError):
175 drop(store, ResourceHost.own(1, 1))