Coverage for wasmtime/_config.py: 86%
126 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-20 16:25 +0000
« 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
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
16class Config(Managed["ctypes._Pointer[ffi.wasm_config_t]"]):
17 """
18 Global configuration, used to create an `Engine`.
20 A `Config` houses a number of configuration options which tweaks how wasm
21 code is compiled or generated.
22 """
24 def __init__(self) -> None:
25 self._set_ptr(ffi.wasm_config_new())
27 def _delete(self, ptr: "ctypes._Pointer[ffi.wasm_config_t]") -> None:
28 ffi.wasm_config_delete(ptr)
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 """
37 if not isinstance(enable, bool):
38 raise TypeError('expected a bool')
39 ffi.wasmtime_config_debug_info_set(self.ptr(), enable)
41 @setter_property
42 def wasm_threads(self, enable: bool) -> None:
43 """
44 Configures whether the wasm [threads proposal] is enabled.
46 [threads proposal]: https://github.com/webassembly/threads
47 """
49 if not isinstance(enable, bool):
50 raise TypeError('expected a bool')
51 ffi.wasmtime_config_wasm_threads_set(self.ptr(), enable)
53 @setter_property
54 def wasm_tail_call(self, enable: bool) -> None:
55 """
56 Configures whether the wasm [tail call proposal] is enabled.
58 [tail call proposal]: https://github.com/WebAssembly/tail-call
59 """
61 if not isinstance(enable, bool):
62 raise TypeError('expected a bool')
63 ffi.wasmtime_config_wasm_tail_call_set(self.ptr(), enable)
65 @setter_property
66 def wasm_reference_types(self, enable: bool) -> None:
67 """
68 Configures whether the wasm [reference types proposal] is enabled.
70 [reference types proposal]: https://github.com/webassembly/reference-types
71 """
73 if not isinstance(enable, bool):
74 raise TypeError('expected a bool')
75 ffi.wasmtime_config_wasm_reference_types_set(self.ptr(), enable)
77 @setter_property
78 def wasm_simd(self, enable: bool) -> None:
79 """
80 Configures whether the wasm [SIMD proposal] is enabled.
82 [SIMD proposal]: https://github.com/webassembly/simd
83 """
85 if not isinstance(enable, bool):
86 raise TypeError('expected a bool')
87 ffi.wasmtime_config_wasm_simd_set(self.ptr(), enable)
89 @setter_property
90 def wasm_bulk_memory(self, enable: bool) -> None:
91 """
92 Configures whether the wasm [bulk memory proposal] is enabled.
94 [bulk memory proposal]: https://github.com/webassembly/bulk-memory
95 """
97 if not isinstance(enable, bool):
98 raise TypeError('expected a bool')
99 ffi.wasmtime_config_wasm_bulk_memory_set(self.ptr(), enable)
101 @setter_property
102 def wasm_multi_value(self, enable: bool) -> None:
103 """
104 Configures whether the wasm [multi value proposal] is enabled.
106 [multi value proposal]: https://github.com/webassembly/multi-value
107 """
109 if not isinstance(enable, bool):
110 raise TypeError('expected a bool')
111 ffi.wasmtime_config_wasm_multi_value_set(self.ptr(), enable)
113 @setter_property
114 def wasm_multi_memory(self, enable: bool) -> None:
115 """
116 Configures whether the wasm [multi memory proposal] is enabled.
118 [multi memory proposal]: https://github.com/webassembly/multi-memory
119 """
121 if not isinstance(enable, bool):
122 raise TypeError('expected a bool')
123 ffi.wasmtime_config_wasm_multi_memory_set(self.ptr(), enable)
125 @setter_property
126 def wasm_memory64(self, enable: bool) -> None:
127 """
128 Configures whether the wasm [memory64 proposal] is enabled.
130 [memory64 proposal]: https://github.com/webassembly/memory64
131 """
133 if not isinstance(enable, bool):
134 raise TypeError('expected a bool')
135 ffi.wasmtime_config_wasm_memory64_set(self.ptr(), enable)
137 @setter_property
138 def wasm_relaxed_simd(self, enable: bool) -> None:
139 """
140 Configures whether the wasm [relaxed simd proposal] is enabled.
142 [relaxed simd proposal]: https://github.com/webassembly/relaxed-simd
143 """
145 if not isinstance(enable, bool):
146 raise TypeError('expected a bool')
147 ffi.wasmtime_config_wasm_relaxed_simd_set(self.ptr(), enable)
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.
156 [relaxed simd proposal]: https://github.com/webassembly/relaxed-simd
157 """
159 if not isinstance(enable, bool):
160 raise TypeError('expected a bool')
161 ffi.wasmtime_config_wasm_relaxed_simd_deterministic_set(self.ptr(), enable)
163 @setter_property
164 def strategy(self, strategy: str) -> None:
165 """
166 Configures the compilation strategy used for wasm code.
168 Acceptable values for `strategy` are:
170 * `"auto"`
171 * `"cranelift"`
172 """
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))
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)
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))
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))
207 @setter_property
208 def cache(self, enabled: typing.Union[bool, str]) -> None:
209 """
210 Configures whether code caching is enabled for this `Config`.
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.
216 More information about cache configuration can be found at
217 https://bytecodealliance.github.io/wasmtime/cli-cache.html
218 """
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)
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 """
239 if enabled:
240 val = 1
241 else:
242 val = 0
243 ffi.wasmtime_config_epoch_interruption_set(self.ptr(), val)
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.
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)
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.
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)