Coverage for tests/test_instance.py: 100%

123 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-20 16:25 +0000

1import unittest 

2 

3from wasmtime import * 

4 

5 

6class TestInstance(unittest.TestCase): 

7 def test_smoke(self): 

8 store = Store() 

9 module = Module(store.engine, '(module)') 

10 Instance(store, module, []) 

11 

12 def test_export_func(self): 

13 store = Store() 

14 module = Module(store.engine, '(module (func (export "")))') 

15 instance = Instance(store, module, []) 

16 self.assertEqual(len(instance.exports(store)), 1) 

17 extern = instance.exports(store).by_index[0] 

18 assert(isinstance(extern, Func)) 

19 assert(isinstance(extern.type(store), FuncType)) 

20 

21 extern(store) 

22 

23 assert(instance.exports(store)[''] is not None) 

24 with self.assertRaises(KeyError): 

25 instance.exports(store)['x'] 

26 with self.assertRaises(IndexError): 

27 instance.exports(store).by_index[100] 

28 assert(instance.exports(store).get('x') is None) 

29 

30 def test_export_global(self): 

31 store = Store() 

32 module = Module( 

33 store.engine, '(module (global (export "") i32 (i32.const 3)))') 

34 instance = Instance(store, module, []) 

35 self.assertEqual(len(instance.exports(store)), 1) 

36 extern = instance.exports(store).by_index[0] 

37 assert(isinstance(extern, Global)) 

38 self.assertEqual(extern.value(store), 3) 

39 assert(isinstance(extern.type(store), GlobalType)) 

40 

41 def test_export_memory(self): 

42 store = Store() 

43 module = Module(store.engine, '(module (memory (export "") 1))') 

44 instance = Instance(store, module, []) 

45 self.assertEqual(len(instance.exports(store)), 1) 

46 extern = instance.exports(store).by_index[0] 

47 assert(isinstance(extern, Memory)) 

48 self.assertEqual(extern.size(store), 1) 

49 

50 def test_export_table(self): 

51 store = Store() 

52 module = Module(store.engine, '(module (table (export "") 1 funcref))') 

53 instance = Instance(store, module, []) 

54 self.assertEqual(len(instance.exports(store)), 1) 

55 extern = instance.exports(store).by_index[0] 

56 assert(isinstance(extern, Table)) 

57 

58 def test_multiple_exports(self): 

59 store = Store() 

60 module = Module(store.engine, """ 

61 (module 

62 (func (export "a")) 

63 (func (export "b")) 

64 (global (export "c") i32 (i32.const 0)) 

65 ) 

66 """) 

67 instance = Instance(store, module, []) 

68 exports = instance.exports(store) 

69 self.assertEqual(len(exports), 3) 

70 assert isinstance(exports.by_index[0], Func) 

71 assert isinstance(exports.by_index[1], Func) 

72 assert isinstance(exports.by_index[2], Global) 

73 # Test that exports acts like a normal map 

74 assert "a" in exports 

75 assert "b" in exports 

76 assert "d" not in exports 

77 assert set(exports) == {"a", "b", "c"} 

78 assert set(exports.values()) == set(exports.by_index) 

79 assert exports.get("d", 7) == 7 

80 assert isinstance(exports.get("b", 7), Func) 

81 

82 def test_import_func(self): 

83 store = Store() 

84 module = Module(store.engine, """ 

85 (module 

86 (import "" "" (func)) 

87 (start 0) 

88 ) 

89 """) 

90 hit = [] 

91 func = Func(store, FuncType([], []), lambda: hit.append(True)) 

92 Instance(store, module, [func]) 

93 assert(len(hit) == 1) 

94 Instance(store, module, [func]) 

95 assert(len(hit) == 2) 

96 

97 def test_import_global(self): 

98 store = Store() 

99 module = Module(store.engine, """ 

100 (module 

101 (import "" "" (global (mut i32))) 

102 (func (export "") (result i32) 

103 global.get 0) 

104 (func (export "update") 

105 i32.const 5 

106 global.set 0) 

107 ) 

108 """) 

109 g = Global(store, GlobalType(ValType.i32(), True), 2) 

110 instance = Instance(store, module, [g]) 

111 f = instance.exports(store).by_index[0] 

112 assert(isinstance(f, Func)) 

113 

114 self.assertEqual(f(store), 2) 

115 g.set_value(store, 4) 

116 self.assertEqual(f(store), 4) 

117 

118 instance2 = Instance(store, module, [g]) 

119 f2 = instance2.exports(store).by_index[0] 

120 assert(isinstance(f2, Func)) 

121 self.assertEqual(f(store), 4) 

122 self.assertEqual(f2(store), 4) 

123 

124 update = instance.exports(store).by_index[1] 

125 assert(isinstance(update, Func)) 

126 update(store) 

127 self.assertEqual(f(store), 5) 

128 self.assertEqual(f2(store), 5) 

129 

130 def test_import_memory(self): 

131 store = Store() 

132 module = Module(store.engine, """ 

133 (module 

134 (import "" "" (memory 1)) 

135 ) 

136 """) 

137 m = Memory(store, MemoryType(Limits(1, None))) 

138 Instance(store, module, [m]) 

139 

140 def test_import_table(self): 

141 store = Store() 

142 module = Module(store.engine, """ 

143 (module 

144 (table (export "") 1 funcref) 

145 ) 

146 """) 

147 table = Instance(store, module, []).exports(store).by_index[0] 

148 

149 module = Module(store.engine, """ 

150 (module 

151 (import "" "" (table 1 funcref)) 

152 ) 

153 """) 

154 Instance(store, module, [table]) 

155 

156 def test_invalid(self): 

157 store = Store() 

158 with self.assertRaises(AttributeError): 

159 Instance(store, 1, []) # type: ignore 

160 with self.assertRaises(TypeError): 

161 Instance(store, Module(store.engine, '(module (import "" "" (func)))'), [1]) # type: ignore 

162 

163 val = Func(store, FuncType([], []), lambda: None) 

164 module = Module(store.engine, '(module (import "" "" (func)))') 

165 Instance(store, module, [val]) 

166 with self.assertRaises(WasmtimeError): 

167 Instance(store, module, []) 

168 with self.assertRaises(WasmtimeError): 

169 Instance(store, module, [val, val]) 

170 

171 module = Module(store.engine, '(module (import "" "" (global i32)))') 

172 with self.assertRaises(WasmtimeError): 

173 Instance(store, module, [val]) 

174 

175 def test_start_trap(self): 

176 store = Store() 

177 module = Module(store.engine, '(module (func unreachable) (start 0))') 

178 with self.assertRaises(Trap): 

179 Instance(store, module, [])