Coverage for wasmtime/_config.py: 85%
130 statements
« prev ^ index » next coverage.py v7.11.3, created at 2025-12-01 19:40 +0000
« prev ^ index » next coverage.py v7.11.3, created at 2025-12-01 19:40 +0000
1from . import _ffi as ffi
2import ctypes
3from wasmtime import WasmtimeError, Managed
4import typing
7def setter_property(fset: typing.Callable) -> property:
8 prop = property(fset=fset)
9 if fset.__doc__:
10 prop.__doc__ = fset.__doc__
11 prop.__doc__ += "\n\n Note that this field can only be set, it cannot be read"
12 return prop
15class Config(Managed["ctypes._Pointer[ffi.wasm_config_t]"]):
16 """
17 Global configuration, used to create an `Engine`.
19 A `Config` houses a number of configuration options which tweaks how wasm
20 code is compiled or generated.
21 """
23 def __init__(self) -> None:
24 self._set_ptr(ffi.wasm_config_new())
26 def _delete(self, ptr: "ctypes._Pointer[ffi.wasm_config_t]") -> None:
27 ffi.wasm_config_delete(ptr)
29 @setter_property
30 def debug_info(self, enable: bool) -> None:
31 """
32 Configures whether DWARF debug information is emitted for the generated
33 code. This can improve profiling and the debugging experience.
34 """
36 if not isinstance(enable, bool):
37 raise TypeError('expected a bool')
38 ffi.wasmtime_config_debug_info_set(self.ptr(), enable)
40 @setter_property
41 def wasm_threads(self, enable: bool) -> None:
42 """
43 Configures whether the wasm [threads proposal] is enabled.
45 [threads proposal]: https://github.com/webassembly/threads
46 """
48 if not isinstance(enable, bool):
49 raise TypeError('expected a bool')
50 ffi.wasmtime_config_wasm_threads_set(self.ptr(), enable)
52 @setter_property
53 def wasm_tail_call(self, enable: bool) -> None:
54 """
55 Configures whether the wasm [tail call proposal] is enabled.
57 [tail call proposal]: https://github.com/WebAssembly/tail-call
58 """
60 if not isinstance(enable, bool):
61 raise TypeError('expected a bool')
62 ffi.wasmtime_config_wasm_tail_call_set(self.ptr(), enable)
64 @setter_property
65 def wasm_reference_types(self, enable: bool) -> None:
66 """
67 Configures whether the wasm [reference types proposal] is enabled.
69 [reference types proposal]: https://github.com/webassembly/reference-types
70 """
72 if not isinstance(enable, bool):
73 raise TypeError('expected a bool')
74 ffi.wasmtime_config_wasm_reference_types_set(self.ptr(), enable)
76 @setter_property
77 def wasm_simd(self, enable: bool) -> None:
78 """
79 Configures whether the wasm [SIMD proposal] is enabled.
81 [SIMD proposal]: https://github.com/webassembly/simd
82 """
84 if not isinstance(enable, bool):
85 raise TypeError('expected a bool')
86 ffi.wasmtime_config_wasm_simd_set(self.ptr(), enable)
88 @setter_property
89 def wasm_bulk_memory(self, enable: bool) -> None:
90 """
91 Configures whether the wasm [bulk memory proposal] is enabled.
93 [bulk memory proposal]: https://github.com/webassembly/bulk-memory
94 """
96 if not isinstance(enable, bool):
97 raise TypeError('expected a bool')
98 ffi.wasmtime_config_wasm_bulk_memory_set(self.ptr(), enable)
100 @setter_property
101 def wasm_multi_value(self, enable: bool) -> None:
102 """
103 Configures whether the wasm [multi value proposal] is enabled.
105 [multi value proposal]: https://github.com/webassembly/multi-value
106 """
108 if not isinstance(enable, bool):
109 raise TypeError('expected a bool')
110 ffi.wasmtime_config_wasm_multi_value_set(self.ptr(), enable)
112 @setter_property
113 def wasm_multi_memory(self, enable: bool) -> None:
114 """
115 Configures whether the wasm [multi memory proposal] is enabled.
117 [multi memory proposal]: https://github.com/webassembly/multi-memory
118 """
120 if not isinstance(enable, bool):
121 raise TypeError('expected a bool')
122 ffi.wasmtime_config_wasm_multi_memory_set(self.ptr(), enable)
124 @setter_property
125 def wasm_memory64(self, enable: bool) -> None:
126 """
127 Configures whether the wasm [memory64 proposal] is enabled.
129 [memory64 proposal]: https://github.com/webassembly/memory64
130 """
132 if not isinstance(enable, bool):
133 raise TypeError('expected a bool')
134 ffi.wasmtime_config_wasm_memory64_set(self.ptr(), enable)
136 @setter_property
137 def wasm_relaxed_simd(self, enable: bool) -> None:
138 """
139 Configures whether the wasm [relaxed simd proposal] is enabled.
141 [relaxed simd proposal]: https://github.com/webassembly/relaxed-simd
142 """
144 if not isinstance(enable, bool):
145 raise TypeError('expected a bool')
146 ffi.wasmtime_config_wasm_relaxed_simd_set(self.ptr(), enable)
148 @setter_property
149 def wasm_relaxed_simd_deterministic(self, enable: bool) -> None:
150 """
151 Configures whether the wasm [relaxed simd proposal] is deterministic
152 in is execution as opposed to having the most optimal implementation for
153 the current platform.
155 [relaxed simd proposal]: https://github.com/webassembly/relaxed-simd
156 """
158 if not isinstance(enable, bool):
159 raise TypeError('expected a bool')
160 ffi.wasmtime_config_wasm_relaxed_simd_deterministic_set(self.ptr(), enable)
162 @setter_property
163 def wasm_exceptions(self, enable: bool) -> None:
164 """
165 Configures whether the wasm [exceptions proposal] is enabled.
167 [exceptions proposal]: https://github.com/WebAssembly/exception-handling
168 """
170 if not isinstance(enable, bool):
171 raise TypeError('expected a bool')
172 ffi.wasmtime_config_wasm_exceptions_set(self.ptr(), enable)
174 @setter_property
175 def strategy(self, strategy: str) -> None:
176 """
177 Configures the compilation strategy used for wasm code.
179 Acceptable values for `strategy` are:
181 * `"auto"`
182 * `"cranelift"`
183 """
185 if strategy == "auto":
186 ffi.wasmtime_config_strategy_set(self.ptr(), 0)
187 elif strategy == "cranelift":
188 ffi.wasmtime_config_strategy_set(self.ptr(), 1)
189 else:
190 raise WasmtimeError("unknown strategy: " + str(strategy))
192 @setter_property
193 def cranelift_debug_verifier(self, enable: bool) -> None:
194 if not isinstance(enable, bool):
195 raise TypeError('expected a bool')
196 ffi.wasmtime_config_cranelift_debug_verifier_set(self.ptr(), enable)
198 @setter_property
199 def cranelift_opt_level(self, opt_level: str) -> None:
200 if opt_level == "none":
201 ffi.wasmtime_config_cranelift_opt_level_set(self.ptr(), 0)
202 elif opt_level == "speed":
203 ffi.wasmtime_config_cranelift_opt_level_set(self.ptr(), 1)
204 elif opt_level == "speed_and_size":
205 ffi.wasmtime_config_cranelift_opt_level_set(self.ptr(), 2)
206 else:
207 raise WasmtimeError("unknown opt level: " + str(opt_level))
209 @setter_property
210 def profiler(self, profiler: str) -> None:
211 if profiler == "none":
212 ffi.wasmtime_config_profiler_set(self.ptr(), 0)
213 elif profiler == "jitdump":
214 ffi.wasmtime_config_profiler_set(self.ptr(), 1)
215 else:
216 raise WasmtimeError("unknown profiler: " + str(profiler))
218 @setter_property
219 def cache(self, enabled: typing.Union[bool, str]) -> None:
220 """
221 Configures whether code caching is enabled for this `Config`.
223 The value `True` can be passed in here to enable the default caching
224 configuration and location, or a path to a file can be passed in which
225 is a path to a TOML configuration file for the cache.
227 More information about cache configuration can be found at
228 https://bytecodealliance.github.io/wasmtime/cli-cache.html
229 """
231 if isinstance(enabled, bool):
232 if not enabled:
233 raise WasmtimeError("caching cannot be explicitly disabled")
234 error = ffi.wasmtime_config_cache_config_load(self.ptr(), None)
235 elif isinstance(enabled, str):
236 error = ffi.wasmtime_config_cache_config_load(self.ptr(),
237 ctypes.c_char_p(enabled.encode('utf-8')),
238 )
239 else:
240 raise TypeError("expected string or bool")
241 if error:
242 raise WasmtimeError._from_ptr(error)
244 @setter_property
245 def epoch_interruption(self, enabled: bool) -> None:
246 """
247 Configures whether wasm execution can be interrupted via epoch
248 increments.
249 """
251 if enabled:
252 val = 1
253 else:
254 val = 0
255 ffi.wasmtime_config_epoch_interruption_set(self.ptr(), val)
257 @setter_property
258 def consume_fuel(self, instances: bool) -> None:
259 """
260 Configures whether wasm code will consume *fuel* as part of its
261 execution.
263 Fuel consumption allows WebAssembly to trap when fuel runs out.
264 Currently stores start with 0 fuel if this is enabled.
265 """
266 if not isinstance(instances, bool):
267 raise TypeError('expected an bool')
268 ffi.wasmtime_config_consume_fuel_set(self.ptr(), instances)
270 @setter_property
271 def parallel_compilation(self, enable: bool) -> None:
272 """
273 Configures whether parallel compilation is enabled for functions
274 within a module.
276 This is enabled by default.
277 """
278 if not isinstance(enable, bool):
279 raise TypeError('expected a bool')
280 ffi.wasmtime_config_parallel_compilation_set(self.ptr(), enable)