Coverage for tests/component/test_value.py: 100%

83 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2025-12-01 19:40 +0000

1import unittest 

2 

3from wasmtime import Store, WasmtimeError 

4from wasmtime.component import * 

5 

6 

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) 

14 

15 def test_resource_host(self): 

16 store = Store() 

17 r1 = ResourceHost.own(42, 1) 

18 

19 r2 = r1.to_any(store) 

20 self.assertEqual(r2.type, ResourceType.host(1)) 

21 self.assertTrue(r2.owned) 

22 

23 r3 = r2.to_host(store) 

24 self.assertTrue(r3.owned) 

25 self.assertEqual(r3.rep, 42) 

26 self.assertEqual(r3.type, 1) 

27 

28 with self.assertRaises(WasmtimeError): 

29 r2.drop(store) 

30 

31 r4 = r3.to_any(store) 

32 r4.drop(store) 

33 

34 r5 = ResourceHost.borrow(84, 2) 

35 self.assertFalse(r5.owned) 

36 self.assertEqual(r5.rep, 84) 

37 self.assertEqual(r5.type, 2) 

38 

39 with self.assertRaises(WasmtimeError): 

40 ResourceHost() 

41 

42 def test_resource_host_dtor(self): 

43 store = Store() 

44 linker = Linker(store.engine) 

45 

46 def drop(_store, rep): 

47 self.assertEqual(rep, 100) 

48 

49 with linker.root() as l: 

50 l.add_resource('t', ResourceType.host(23), drop) 

51 

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) 

74 

75 with self.assertRaises(WasmtimeError): 

76 f(store, ResourceHost.own(1, 2)) 

77 

78 instance = linker.instantiate(store, component) 

79 f = instance.get_func(store, 'drop') 

80 assert(f is not None) 

81 f(store, ResourceHost.own(100, 23)) 

82 

83 def test_exception_in_host_resource_dtor(self): 

84 store = Store() 

85 linker = Linker(store.engine) 

86 

87 def drop(_store, _rep): 

88 raise RuntimeError('oh no') 

89 

90 with linker.root() as l: 

91 l.add_resource('t', ResourceType.host(2), drop) 

92 

93 component = Component(store.engine, """ 

94 (component 

95 (import "t" (type $t (sub resource))) 

96 (core func $drop (canon resource.drop $t)) 

97 (core module $a 

98 (import "" "drop" (func $drop (param i32))) 

99 (func (export "drop") (param i32) 

100 local.get 0 

101 call $drop) 

102 ) 

103 (core instance $a (instantiate $a 

104 (with "" (instance 

105 (export "drop" (func $drop)) 

106 )) 

107 )) 

108 (func (export "drop") (param "x" (own $t)) 

109 (canon lift (core func $a "drop"))) 

110 ) 

111 """) 

112 instance = linker.instantiate(store, component) 

113 f = instance.get_func(store, 'drop') 

114 assert(f is not None) 

115 

116 with self.assertRaises(RuntimeError) as cm: 

117 f(store, ResourceHost.own(1, 2)) 

118 self.assertEqual(str(cm.exception), 'oh no') 

119 

120 def test_resource_any(self): 

121 with self.assertRaises(WasmtimeError): 

122 ResourceAny() 

123 

124 store = Store() 

125 linker = Linker(store.engine) 

126 

127 component = Component(store.engine, """ 

128 (component 

129 (type $t' (resource (rep i32))) 

130 (export $t "t" (type $t')) 

131 (core func $new (canon resource.new $t)) 

132 (core func $drop (canon resource.drop $t)) 

133 (core module $a 

134 (import "" "new" (func $new (param i32) (result i32))) 

135 (import "" "drop" (func $drop (param i32))) 

136 (func (export "new") (param i32) (result i32) 

137 local.get 0 

138 call $new) 

139 (func (export "drop") (param i32) 

140 local.get 0 

141 call $drop) 

142 ) 

143 (core instance $a (instantiate $a 

144 (with "" (instance 

145 (export "new" (func $new)) 

146 (export "drop" (func $drop)) 

147 )) 

148 )) 

149 (func (export "new") (param "x" u32) (result (own $t)) 

150 (canon lift (core func $a "new"))) 

151 (func (export "drop") (param "x" (own $t)) 

152 (canon lift (core func $a "drop"))) 

153 ) 

154 """) 

155 instance = linker.instantiate(store, component) 

156 new = instance.get_func(store, 'new') 

157 drop = instance.get_func(store, 'drop') 

158 assert(new is not None) 

159 assert(drop is not None) 

160 

161 r1 = new(store, 100) 

162 new.post_return(store) 

163 r2 = new(store, 200) 

164 new.post_return(store) 

165 

166 with self.assertRaises(WasmtimeError): 

167 r1.to_host(store) 

168 

169 r1.drop(store) 

170 drop(store, r2) 

171 drop.post_return(store) 

172 

173 with self.assertRaises(WasmtimeError): 

174 drop(store, ResourceHost.own(1, 1))