Coverage for wasmtime/_config.py: 86%

126 statements  

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

1from . import _ffi as ffi 

2from ctypes import * 

3import ctypes 

4from wasmtime import WasmtimeError, Managed 

5import typing 

6 

7 

8def setter_property(fset: typing.Callable) -> property: 

9 prop = property(fset=fset) 

10 if fset.__doc__: 

11 prop.__doc__ = fset.__doc__ 

12 prop.__doc__ += "\n\n Note that this field can only be set, it cannot be read" 

13 return prop 

14 

15 

16class Config(Managed["ctypes._Pointer[ffi.wasm_config_t]"]): 

17 """ 

18 Global configuration, used to create an `Engine`. 

19 

20 A `Config` houses a number of configuration options which tweaks how wasm 

21 code is compiled or generated. 

22 """ 

23 

24 def __init__(self) -> None: 

25 self._set_ptr(ffi.wasm_config_new()) 

26 

27 def _delete(self, ptr: "ctypes._Pointer[ffi.wasm_config_t]") -> None: 

28 ffi.wasm_config_delete(ptr) 

29 

30 @setter_property 

31 def debug_info(self, enable: bool) -> None: 

32 """ 

33 Configures whether DWARF debug information is emitted for the generated 

34 code. This can improve profiling and the debugging experience. 

35 """ 

36 

37 if not isinstance(enable, bool): 

38 raise TypeError('expected a bool') 

39 ffi.wasmtime_config_debug_info_set(self.ptr(), enable) 

40 

41 @setter_property 

42 def wasm_threads(self, enable: bool) -> None: 

43 """ 

44 Configures whether the wasm [threads proposal] is enabled. 

45 

46 [threads proposal]: https://github.com/webassembly/threads 

47 """ 

48 

49 if not isinstance(enable, bool): 

50 raise TypeError('expected a bool') 

51 ffi.wasmtime_config_wasm_threads_set(self.ptr(), enable) 

52 

53 @setter_property 

54 def wasm_tail_call(self, enable: bool) -> None: 

55 """ 

56 Configures whether the wasm [tail call proposal] is enabled. 

57 

58 [tail call proposal]: https://github.com/WebAssembly/tail-call 

59 """ 

60 

61 if not isinstance(enable, bool): 

62 raise TypeError('expected a bool') 

63 ffi.wasmtime_config_wasm_tail_call_set(self.ptr(), enable) 

64 

65 @setter_property 

66 def wasm_reference_types(self, enable: bool) -> None: 

67 """ 

68 Configures whether the wasm [reference types proposal] is enabled. 

69 

70 [reference types proposal]: https://github.com/webassembly/reference-types 

71 """ 

72 

73 if not isinstance(enable, bool): 

74 raise TypeError('expected a bool') 

75 ffi.wasmtime_config_wasm_reference_types_set(self.ptr(), enable) 

76 

77 @setter_property 

78 def wasm_simd(self, enable: bool) -> None: 

79 """ 

80 Configures whether the wasm [SIMD proposal] is enabled. 

81 

82 [SIMD proposal]: https://github.com/webassembly/simd 

83 """ 

84 

85 if not isinstance(enable, bool): 

86 raise TypeError('expected a bool') 

87 ffi.wasmtime_config_wasm_simd_set(self.ptr(), enable) 

88 

89 @setter_property 

90 def wasm_bulk_memory(self, enable: bool) -> None: 

91 """ 

92 Configures whether the wasm [bulk memory proposal] is enabled. 

93 

94 [bulk memory proposal]: https://github.com/webassembly/bulk-memory 

95 """ 

96 

97 if not isinstance(enable, bool): 

98 raise TypeError('expected a bool') 

99 ffi.wasmtime_config_wasm_bulk_memory_set(self.ptr(), enable) 

100 

101 @setter_property 

102 def wasm_multi_value(self, enable: bool) -> None: 

103 """ 

104 Configures whether the wasm [multi value proposal] is enabled. 

105 

106 [multi value proposal]: https://github.com/webassembly/multi-value 

107 """ 

108 

109 if not isinstance(enable, bool): 

110 raise TypeError('expected a bool') 

111 ffi.wasmtime_config_wasm_multi_value_set(self.ptr(), enable) 

112 

113 @setter_property 

114 def wasm_multi_memory(self, enable: bool) -> None: 

115 """ 

116 Configures whether the wasm [multi memory proposal] is enabled. 

117 

118 [multi memory proposal]: https://github.com/webassembly/multi-memory 

119 """ 

120 

121 if not isinstance(enable, bool): 

122 raise TypeError('expected a bool') 

123 ffi.wasmtime_config_wasm_multi_memory_set(self.ptr(), enable) 

124 

125 @setter_property 

126 def wasm_memory64(self, enable: bool) -> None: 

127 """ 

128 Configures whether the wasm [memory64 proposal] is enabled. 

129 

130 [memory64 proposal]: https://github.com/webassembly/memory64 

131 """ 

132 

133 if not isinstance(enable, bool): 

134 raise TypeError('expected a bool') 

135 ffi.wasmtime_config_wasm_memory64_set(self.ptr(), enable) 

136 

137 @setter_property 

138 def wasm_relaxed_simd(self, enable: bool) -> None: 

139 """ 

140 Configures whether the wasm [relaxed simd proposal] is enabled. 

141 

142 [relaxed simd proposal]: https://github.com/webassembly/relaxed-simd 

143 """ 

144 

145 if not isinstance(enable, bool): 

146 raise TypeError('expected a bool') 

147 ffi.wasmtime_config_wasm_relaxed_simd_set(self.ptr(), enable) 

148 

149 @setter_property 

150 def wasm_relaxed_simd_deterministic(self, enable: bool) -> None: 

151 """ 

152 Configures whether the wasm [relaxed simd proposal] is deterministic 

153 in is execution as opposed to having the most optimal implementation for 

154 the current platform. 

155 

156 [relaxed simd proposal]: https://github.com/webassembly/relaxed-simd 

157 """ 

158 

159 if not isinstance(enable, bool): 

160 raise TypeError('expected a bool') 

161 ffi.wasmtime_config_wasm_relaxed_simd_deterministic_set(self.ptr(), enable) 

162 

163 @setter_property 

164 def strategy(self, strategy: str) -> None: 

165 """ 

166 Configures the compilation strategy used for wasm code. 

167 

168 Acceptable values for `strategy` are: 

169 

170 * `"auto"` 

171 * `"cranelift"` 

172 """ 

173 

174 if strategy == "auto": 

175 ffi.wasmtime_config_strategy_set(self.ptr(), 0) 

176 elif strategy == "cranelift": 

177 ffi.wasmtime_config_strategy_set(self.ptr(), 1) 

178 else: 

179 raise WasmtimeError("unknown strategy: " + str(strategy)) 

180 

181 @setter_property 

182 def cranelift_debug_verifier(self, enable: bool) -> None: 

183 if not isinstance(enable, bool): 

184 raise TypeError('expected a bool') 

185 ffi.wasmtime_config_cranelift_debug_verifier_set(self.ptr(), enable) 

186 

187 @setter_property 

188 def cranelift_opt_level(self, opt_level: str) -> None: 

189 if opt_level == "none": 

190 ffi.wasmtime_config_cranelift_opt_level_set(self.ptr(), 0) 

191 elif opt_level == "speed": 

192 ffi.wasmtime_config_cranelift_opt_level_set(self.ptr(), 1) 

193 elif opt_level == "speed_and_size": 

194 ffi.wasmtime_config_cranelift_opt_level_set(self.ptr(), 2) 

195 else: 

196 raise WasmtimeError("unknown opt level: " + str(opt_level)) 

197 

198 @setter_property 

199 def profiler(self, profiler: str) -> None: 

200 if profiler == "none": 

201 ffi.wasmtime_config_profiler_set(self.ptr(), 0) 

202 elif profiler == "jitdump": 

203 ffi.wasmtime_config_profiler_set(self.ptr(), 1) 

204 else: 

205 raise WasmtimeError("unknown profiler: " + str(profiler)) 

206 

207 @setter_property 

208 def cache(self, enabled: typing.Union[bool, str]) -> None: 

209 """ 

210 Configures whether code caching is enabled for this `Config`. 

211 

212 The value `True` can be passed in here to enable the default caching 

213 configuration and location, or a path to a file can be passed in which 

214 is a path to a TOML configuration file for the cache. 

215 

216 More information about cache configuration can be found at 

217 https://bytecodealliance.github.io/wasmtime/cli-cache.html 

218 """ 

219 

220 if isinstance(enabled, bool): 

221 if not enabled: 

222 raise WasmtimeError("caching cannot be explicitly disabled") 

223 error = ffi.wasmtime_config_cache_config_load(self.ptr(), None) 

224 elif isinstance(enabled, str): 

225 error = ffi.wasmtime_config_cache_config_load(self.ptr(), 

226 c_char_p(enabled.encode('utf-8'))) 

227 else: 

228 raise TypeError("expected string or bool") 

229 if error: 

230 raise WasmtimeError._from_ptr(error) 

231 

232 @setter_property 

233 def epoch_interruption(self, enabled: bool) -> None: 

234 """ 

235 Configures whether wasm execution can be interrupted via epoch 

236 increments. 

237 """ 

238 

239 if enabled: 

240 val = 1 

241 else: 

242 val = 0 

243 ffi.wasmtime_config_epoch_interruption_set(self.ptr(), val) 

244 

245 @setter_property 

246 def consume_fuel(self, instances: bool) -> None: 

247 """ 

248 Configures whether wasm code will consume *fuel* as part of its 

249 execution. 

250 

251 Fuel consumption allows WebAssembly to trap when fuel runs out. 

252 Currently stores start with 0 fuel if this is enabled. 

253 """ 

254 if not isinstance(instances, bool): 

255 raise TypeError('expected an bool') 

256 ffi.wasmtime_config_consume_fuel_set(self.ptr(), instances) 

257 

258 @setter_property 

259 def parallel_compilation(self, enable: bool) -> None: 

260 """ 

261 Configures whether parallel compilation is enabled for functions 

262 within a module. 

263 

264 This is enabled by default. 

265 """ 

266 if not isinstance(enable, bool): 

267 raise TypeError('expected a bool') 

268 ffi.wasmtime_config_parallel_compilation_set(self.ptr(), enable)