1 /**
2  * \file wasmtime/config.hh
3  */
4 
5 #ifndef WASMTIME_CONFIG_HH
6 #define WASMTIME_CONFIG_HH
7 
8 #include <wasmtime/conf.h>
9 #include <wasmtime/config.h>
10 #include <wasmtime/error.hh>
11 #include <wasmtime/helpers.hh>
12 #include <wasmtime/types/memory.hh>
13 
14 namespace wasmtime {
15 
16 /// \brief Strategies passed to `Config::strategy`
17 enum class Strategy {
18   /// Automatically selects the compilation strategy
19   Auto = WASMTIME_STRATEGY_AUTO,
20   /// Requires Cranelift to be used for compilation
21   Cranelift = WASMTIME_STRATEGY_CRANELIFT,
22 };
23 
24 /// \brief Values passed to `Config::cranelift_opt_level`
25 enum class OptLevel {
26   /// No extra optimizations performed
27   None = WASMTIME_OPT_LEVEL_NONE,
28   /// Optimize for speed
29   Speed = WASMTIME_OPT_LEVEL_SPEED,
30   /// Optimize for speed and generated code size
31   SpeedAndSize = WASMTIME_OPT_LEVEL_SPEED_AND_SIZE,
32 };
33 
34 /// \brief Values passed to `Config::profiler`
35 enum class ProfilingStrategy {
36   /// No profiling enabled
37   None = WASMTIME_PROFILING_STRATEGY_NONE,
38   /// Profiling hooks via perf's jitdump
39   Jitdump = WASMTIME_PROFILING_STRATEGY_JITDUMP,
40   /// Profiling hooks via VTune
41   Vtune = WASMTIME_PROFILING_STRATEGY_VTUNE,
42   /// Profiling hooks via perfmap
43   Perfmap = WASMTIME_PROFILING_STRATEGY_PERFMAP,
44 };
45 
46 #ifdef WASMTIME_FEATURE_POOLING_ALLOCATOR
47 /**
48  * \brief Pool allocation configuration for Wasmtime.
49  *
50  * For more information be sure to consult the [rust
51  * documentation](https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html).
52  */
53 class PoolAllocationConfig {
54   WASMTIME_OWN_WRAPPER(PoolAllocationConfig,
55                        wasmtime_pooling_allocation_config);
56 
PoolAllocationConfig()57   PoolAllocationConfig() : ptr(wasmtime_pooling_allocation_config_new()) {}
58 
59   /// \brief Configures the maximum number of “unused warm slots” to retain in
60   /// the pooling allocator.
61   ///
62   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.max_unused_warm_slots.
max_unused_warm_slots(uint32_t max)63   void max_unused_warm_slots(uint32_t max) {
64     wasmtime_pooling_allocation_config_max_unused_warm_slots_set(ptr.get(),
65                                                                  max);
66   }
67 
68   /// \brief The target number of decommits to do per batch.
69   ///
70   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.decommit_batch_size.
decommit_batch_size(size_t batch_size)71   void decommit_batch_size(size_t batch_size) {
72     wasmtime_pooling_allocation_config_decommit_batch_size_set(ptr.get(),
73                                                                batch_size);
74   }
75 
76 #ifdef WASMTIME_FEATURE_ASYNC
77   /// \brief How much memory, in bytes, to keep resident for async stacks
78   /// allocated with the pooling allocator.
79   ///
80   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.async_stack_keep_resident.
async_stack_keep_resident(size_t size)81   void async_stack_keep_resident(size_t size) {
82     wasmtime_pooling_allocation_config_async_stack_keep_resident_set(ptr.get(),
83                                                                      size);
84   }
85 #endif // WASMTIME_FEATURE_ASYNC
86 
87   /// \brief How much memory, in bytes, to keep resident for each linear memory
88   /// after deallocation.
89   ///
90   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.linear_memory_keep_resident.
linear_memory_keep_resident(size_t size)91   void linear_memory_keep_resident(size_t size) {
92     wasmtime_pooling_allocation_config_linear_memory_keep_resident_set(
93         ptr.get(), size);
94   }
95 
96   /// \brief How much memory, in bytes, to keep resident for each table after
97   /// deallocation.
98   ///
99   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.table_keep_resident.
table_keep_resident(size_t size)100   void table_keep_resident(size_t size) {
101     wasmtime_pooling_allocation_config_table_keep_resident_set(ptr.get(), size);
102   }
103 
104   /// \brief The maximum number of concurrent component instances supported
105   /// (default is 1000).
106   ///
107   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.total_component_instances.
total_component_instances(uint32_t count)108   void total_component_instances(uint32_t count) {
109     wasmtime_pooling_allocation_config_total_component_instances_set(ptr.get(),
110                                                                      count);
111   }
112 
113   /// \brief The maximum size, in bytes, allocated for a component instance’s
114   /// VMComponentContext metadata.
115   ///
116   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.max_component_instance_size.
max_component_instance_size(size_t size)117   void max_component_instance_size(size_t size) {
118     wasmtime_pooling_allocation_config_max_component_instance_size_set(
119         ptr.get(), size);
120   }
121 
122   /// \brief The maximum number of core instances a single component may contain
123   /// (default is unlimited).
124   ///
125   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.max_core_instances_per_component.
max_core_instances_per_component(uint32_t count)126   void max_core_instances_per_component(uint32_t count) {
127     wasmtime_pooling_allocation_config_max_core_instances_per_component_set(
128         ptr.get(), count);
129   }
130 
131   /// \brief The maximum number of Wasm linear memories that a single component
132   /// may transitively contain (default is unlimited).
133   ///
134   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.max_memories_per_component.
max_memories_per_component(uint32_t count)135   void max_memories_per_component(uint32_t count) {
136     wasmtime_pooling_allocation_config_max_memories_per_component_set(ptr.get(),
137                                                                       count);
138   }
139 
140   /// \brief The maximum number of tables that a single component may
141   /// transitively contain (default is unlimited).
142   ///
143   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.max_tables_per_component.
max_tables_per_component(uint32_t count)144   void max_tables_per_component(uint32_t count) {
145     wasmtime_pooling_allocation_config_max_tables_per_component_set(ptr.get(),
146                                                                     count);
147   }
148 
149   /// \brief The maximum number of concurrent Wasm linear memories supported
150   /// (default is 1000).
151   ///
152   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.total_memories.
total_memories(uint32_t count)153   void total_memories(uint32_t count) {
154     wasmtime_pooling_allocation_config_total_memories_set(ptr.get(), count);
155   }
156 
157   /// \brief The maximum number of concurrent tables supported (default is
158   /// 1000).
159   ///
160   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.total_tables.
total_tables(uint32_t count)161   void total_tables(uint32_t count) {
162     wasmtime_pooling_allocation_config_total_tables_set(ptr.get(), count);
163   }
164 
165 #ifdef WASMTIME_FEATURE_ASYNC
166   /// \brief The maximum number of execution stacks allowed for asynchronous
167   /// execution, when enabled (default is 1000).
168   ///
169   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.total_stacks.
total_stacks(uint32_t count)170   void total_stacks(uint32_t count) {
171     wasmtime_pooling_allocation_config_total_stacks_set(ptr.get(), count);
172   }
173 #endif // WASMTIME_FEATURE_ASYNC
174 
175   /// \brief The maximum number of concurrent core instances supported (default
176   /// is 1000).
177   ///
178   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.total_core_instances.
total_core_instances(uint32_t count)179   void total_core_instances(uint32_t count) {
180     wasmtime_pooling_allocation_config_total_core_instances_set(ptr.get(),
181                                                                 count);
182   }
183 
184   /// \brief The maximum size, in bytes, allocated for a core instance’s
185   /// VMContext metadata.
186   ///
187   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.max_core_instance_size.
max_core_instance_size(size_t size)188   void max_core_instance_size(size_t size) {
189     wasmtime_pooling_allocation_config_max_core_instance_size_set(ptr.get(),
190                                                                   size);
191   }
192 
193   /// \brief The maximum number of defined tables for a core module (default is
194   /// 1).
195   ///
196   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.max_tables_per_module.
max_tables_per_module(uint32_t tables)197   void max_tables_per_module(uint32_t tables) {
198     wasmtime_pooling_allocation_config_max_tables_per_module_set(ptr.get(),
199                                                                  tables);
200   }
201 
202   /// \brief The maximum table elements for any table defined in a module
203   /// (default is 20000).
204   ///
205   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.table_elements.
table_elements(size_t elements)206   void table_elements(size_t elements) {
207     wasmtime_pooling_allocation_config_table_elements_set(ptr.get(), elements);
208   }
209 
210   /// \brief The maximum number of defined linear memories for a module (default
211   /// is 1).
212   ///
213   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.max_memories_per_module.
max_memories_per_module(uint32_t memories)214   void max_memories_per_module(uint32_t memories) {
215     wasmtime_pooling_allocation_config_max_memories_per_module_set(ptr.get(),
216                                                                    memories);
217   }
218 
219   /// \brief The maximum byte size that any WebAssembly linear memory may grow
220   /// to.
221   ///
222   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.max_memory_size.
max_memory_size(size_t bytes)223   void max_memory_size(size_t bytes) {
224     wasmtime_pooling_allocation_config_max_memory_size_set(ptr.get(), bytes);
225   }
226 
227   /// \brief The maximum number of concurrent GC heaps supported (default is
228   /// 1000).
229   ///
230   /// https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html#method.total_gc_heaps.
total_gc_heaps(uint32_t count)231   void total_gc_heaps(uint32_t count) {
232     wasmtime_pooling_allocation_config_total_gc_heaps_set(ptr.get(), count);
233   }
234 };
235 #endif // WASMTIME_FEATURE_POOLING_ALLOCATOR
236 
237 /**
238  * \brief Configuration for Wasmtime.
239  *
240  * This class is used to configure Wasmtime's compilation and various other
241  * settings such as enabled WebAssembly proposals.
242  *
243  * For more information be sure to consult the [rust
244  * documentation](https://docs.wasmtime.dev/api/wasmtime/struct.Config.html).
245  */
246 class Config {
247   WASMTIME_OWN_WRAPPER(Config, wasm_config);
248 
249   /// \brief Creates configuration with all the default settings.
Config()250   Config() : ptr(wasm_config_new()) {}
251 
252   /// \brief Configures whether dwarf debuginfo is emitted for assisting
253   /// in-process debugging.
254   ///
255   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.debug_info
debug_info(bool enable)256   void debug_info(bool enable) {
257     wasmtime_config_debug_info_set(ptr.get(), enable);
258   }
259 
260   /// \brief Configures whether epochs are enabled which can be used to
261   /// interrupt currently executing WebAssembly.
262   ///
263   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.epoch_interruption
epoch_interruption(bool enable)264   void epoch_interruption(bool enable) {
265     wasmtime_config_epoch_interruption_set(ptr.get(), enable);
266   }
267 
268   /// \brief Configures whether WebAssembly code will consume fuel and trap when
269   /// it runs out.
270   ///
271   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.consume_fuel
consume_fuel(bool enable)272   void consume_fuel(bool enable) {
273     wasmtime_config_consume_fuel_set(ptr.get(), enable);
274   }
275 
276   /// \brief Configures the maximum amount of native stack wasm can consume.
277   ///
278   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.max_wasm_stack
max_wasm_stack(size_t stack)279   void max_wasm_stack(size_t stack) {
280     wasmtime_config_max_wasm_stack_set(ptr.get(), stack);
281   }
282 
283 #ifdef WASMTIME_FEATURE_THREADS
284   /// \brief Configures whether the WebAssembly threads proposal is enabled
285   ///
286   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_threads
wasm_threads(bool enable)287   void wasm_threads(bool enable) {
288     wasmtime_config_wasm_threads_set(ptr.get(), enable);
289   }
290 #endif // WASMTIME_FEATURE_THREADS
291 
292   /// \brief Configures whether wasm shared memories can be created.
293   ///
294   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.shared_memory
shared_memory(bool enable)295   void shared_memory(bool enable) {
296     wasmtime_config_shared_memory_set(ptr.get(), enable);
297   }
298 
299   /// \brief Configures whether the WebAssembly tail call proposal is enabled
300   ///
301   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_tail_call
wasm_tail_call(bool enable)302   void wasm_tail_call(bool enable) {
303     wasmtime_config_wasm_tail_call_set(ptr.get(), enable);
304   }
305 
306   /// \brief Configures whether the WebAssembly reference types proposal is
307   /// enabled
308   ///
309   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_reference_types
wasm_reference_types(bool enable)310   void wasm_reference_types(bool enable) {
311     wasmtime_config_wasm_reference_types_set(ptr.get(), enable);
312   }
313 
314   /// \brief Configures whether the WebAssembly simd proposal is enabled
315   ///
316   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_simd
wasm_simd(bool enable)317   void wasm_simd(bool enable) {
318     wasmtime_config_wasm_simd_set(ptr.get(), enable);
319   }
320 
321   /// \brief Configures whether the WebAssembly relaxed simd proposal is enabled
322   ///
323   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_relaxed_simd
wasm_relaxed_simd(bool enable)324   void wasm_relaxed_simd(bool enable) {
325     wasmtime_config_wasm_relaxed_simd_set(ptr.get(), enable);
326   }
327 
328   /// \brief Configures whether the WebAssembly relaxed simd proposal supports
329   /// its deterministic behavior.
330   ///
331   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_relaxed_simd_deterministic
wasm_relaxed_simd_deterministic(bool enable)332   void wasm_relaxed_simd_deterministic(bool enable) {
333     wasmtime_config_wasm_relaxed_simd_deterministic_set(ptr.get(), enable);
334   }
335 
336   /// \brief Configures whether the WebAssembly bulk memory proposal is enabled
337   ///
338   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_bulk_memory
wasm_bulk_memory(bool enable)339   void wasm_bulk_memory(bool enable) {
340     wasmtime_config_wasm_bulk_memory_set(ptr.get(), enable);
341   }
342 
343   /// \brief Configures whether the WebAssembly multi value proposal is enabled
344   ///
345   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_multi_value
wasm_multi_value(bool enable)346   void wasm_multi_value(bool enable) {
347     wasmtime_config_wasm_multi_value_set(ptr.get(), enable);
348   }
349 
350   /// \brief Configures whether the WebAssembly multi memory proposal is enabled
351   ///
352   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_multi_memory
wasm_multi_memory(bool enable)353   void wasm_multi_memory(bool enable) {
354     wasmtime_config_wasm_multi_memory_set(ptr.get(), enable);
355   }
356 
357   /// \brief Configures whether the WebAssembly memory64 proposal is enabled
358   ///
359   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_memory64
wasm_memory64(bool enable)360   void wasm_memory64(bool enable) {
361     wasmtime_config_wasm_memory64_set(ptr.get(), enable);
362   }
363 
364   /// \brief Configures whether the WebAssembly Garbage Collection proposal will
365   /// be enabled
366   ///
367   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_gc
wasm_gc(bool enable)368   void wasm_gc(bool enable) { wasmtime_config_wasm_gc_set(ptr.get(), enable); }
369 
370   /// \brief Configures whether the WebAssembly function references proposal
371   /// will be enabled
372   ///
373   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_function_references
wasm_function_references(bool enable)374   void wasm_function_references(bool enable) {
375     wasmtime_config_wasm_function_references_set(ptr.get(), enable);
376   }
377 
378   /// \brief Configures whether the WebAssembly wide arithmetic proposal will be
379   /// enabled
380   ///
381   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_wide_arithmetic
wasm_wide_arithmetic(bool enable)382   void wasm_wide_arithmetic(bool enable) {
383     wasmtime_config_wasm_wide_arithmetic_set(ptr.get(), enable);
384   }
385 
386   /// \brief Configures whether the WebAssembly exceptions proposal will be
387   /// enabled
388   ///
389   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_exceptions
wasm_exceptions(bool enable)390   void wasm_exceptions(bool enable) {
391     wasmtime_config_wasm_exceptions_set(ptr.get(), enable);
392   }
393 
394   /// \brief Configures whether the WebAssembly custom-page-sizes proposal will
395   /// be enabled
396   ///
397   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_custom_page_sizes
wasm_custom_page_sizes(bool enable)398   void wasm_custom_page_sizes(bool enable) {
399     wasmtime_config_wasm_custom_page_sizes_set(ptr.get(), enable);
400   }
401 
402 #ifdef WASMTIME_FEATURE_COMPONENT_MODEL
403   /// \brief Configures whether the WebAssembly component model proposal will be
404   /// enabled
405   ///
406   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_component_model
wasm_component_model(bool enable)407   void wasm_component_model(bool enable) {
408     wasmtime_config_wasm_component_model_set(ptr.get(), enable);
409   }
410 
411   /// \brief Configures whether the WebAssembly component model map type will be
412   /// enabled
413   ///
414   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_component_model_map
wasm_component_model_map(bool enable)415   void wasm_component_model_map(bool enable) {
416     wasmtime_config_wasm_component_model_map_set(ptr.get(), enable);
417   }
418 #endif // WASMTIME_FEATURE_COMPONENT_MODEL
419 
420 #ifdef WASMTIME_FEATURE_PARALLEL_COMPILATION
421   /// \brief Configure whether wasmtime should compile a module using multiple
422   /// threads.
423   ///
424   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.parallel_compilation
parallel_compilation(bool enable)425   void parallel_compilation(bool enable) {
426     wasmtime_config_parallel_compilation_set(ptr.get(), enable);
427   }
428 #endif // WASMTIME_FEATURE_PARALLEL_COMPILATION
429 
430 #ifdef WASMTIME_FEATURE_COMPILER
431   /// \brief Configures compilation strategy for wasm code.
432   ///
433   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.strategy
strategy(Strategy strategy)434   void strategy(Strategy strategy) {
435     wasmtime_config_strategy_set(ptr.get(),
436                                  static_cast<wasmtime_strategy_t>(strategy));
437   }
438 
439   /// \brief Configures whether cranelift's debug verifier is enabled
440   ///
441   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.cranelift_debug_verifier
cranelift_debug_verifier(bool enable)442   void cranelift_debug_verifier(bool enable) {
443     wasmtime_config_cranelift_debug_verifier_set(ptr.get(), enable);
444   }
445 
446   /// \brief Configures whether cranelift's nan canonicalization
447   ///
448   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.cranelift_nan_canonicalization
cranelift_nan_canonicalization(bool enable)449   void cranelift_nan_canonicalization(bool enable) {
450     wasmtime_config_cranelift_nan_canonicalization_set(ptr.get(), enable);
451   }
452 
453   /// \brief Configures cranelift's optimization level
454   ///
455   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.cranelift_opt_level
cranelift_opt_level(OptLevel level)456   void cranelift_opt_level(OptLevel level) {
457     wasmtime_config_cranelift_opt_level_set(
458         ptr.get(), static_cast<wasmtime_opt_level_t>(level));
459   }
460 
461   /// \brief Enable the specified Cranelift flag
462   ///
463   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.cranelift_flag_enable
cranelift_flag_enable(const std::string & flag)464   void cranelift_flag_enable(const std::string &flag) {
465     wasmtime_config_cranelift_flag_enable(ptr.get(), flag.c_str());
466   }
467 
468   /// \brief Configure the specified Cranelift flag
469   ///
470   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.cranelift_flag_set
cranelift_flag_set(const std::string & flag,const std::string & value)471   void cranelift_flag_set(const std::string &flag, const std::string &value) {
472     wasmtime_config_cranelift_flag_set(ptr.get(), flag.c_str(), value.c_str());
473   }
474 #endif // WASMTIME_FEATURE_COMPILER
475 
476   /// \brief Configures an active wasm profiler
477   ///
478   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.profiler
profiler(ProfilingStrategy profiler)479   void profiler(ProfilingStrategy profiler) {
480     wasmtime_config_profiler_set(
481         ptr.get(), static_cast<wasmtime_profiling_strategy_t>(profiler));
482   }
483 
484   /// \brief Configures the size of the initial linear memory allocation.
485   ///
486   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.memory_reservation
memory_reservation(size_t size)487   void memory_reservation(size_t size) {
488     wasmtime_config_memory_reservation_set(ptr.get(), size);
489   }
490 
491   /// \brief Configures the size of the bytes to reserve beyond the end of
492   /// linear memory to grow into.
493   ///
494   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.memory_reservation_for_growth
memory_reservation_for_growth(size_t size)495   void memory_reservation_for_growth(size_t size) {
496     wasmtime_config_memory_reservation_for_growth_set(ptr.get(), size);
497   }
498 
499   /// \brief Configures the size of memory's guard region
500   ///
501   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.memory_guard_size
memory_guard_size(size_t size)502   void memory_guard_size(size_t size) {
503     wasmtime_config_memory_guard_size_set(ptr.get(), size);
504   }
505 
506   /// \brief Configures whether the base pointer of linear memory is allowed to
507   /// move.
508   ///
509   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.memory_may_move
memory_may_move(bool enable)510   void memory_may_move(bool enable) {
511     wasmtime_config_memory_may_move_set(ptr.get(), enable);
512   }
513 
514   /// \brief Configures whether CoW is enabled.
515   ///
516   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.memory_init_cow
memory_init_cow(bool enable)517   void memory_init_cow(bool enable) {
518     wasmtime_config_memory_init_cow_set(ptr.get(), enable);
519   }
520 
521   /// \brief Configures whether native unwind information is emitted.
522   ///
523   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.native_unwind_info
native_unwind_info(bool enable)524   void native_unwind_info(bool enable) {
525     wasmtime_config_native_unwind_info_set(ptr.get(), enable);
526   }
527 
528   /// \brief Configures whether mach ports are used on macOS
529   ///
530   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.macos_use_mach_ports
macos_use_mach_ports(bool enable)531   void macos_use_mach_ports(bool enable) {
532     wasmtime_config_macos_use_mach_ports_set(ptr.get(), enable);
533   }
534 
535   /// \brief Configures Wasmtime to not use signals-based trap handlers
536   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.signals_based_traps
signals_based_traps(bool enable)537   void signals_based_traps(bool enable) {
538     wasmtime_config_signals_based_traps_set(ptr.get(), enable);
539   }
540 
541 #ifdef WASMTIME_FEATURE_CACHE
542   /// \brief Loads the default cache configuration present on the system.
543   ///
544   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.cache_config_load_default
cache_load_default()545   Result<std::monostate> cache_load_default() {
546     auto *error = wasmtime_config_cache_config_load(ptr.get(), nullptr);
547     if (error != nullptr) {
548       return Error(error);
549     }
550     return std::monostate();
551   }
552 
553   /// \brief Loads cache configuration from the specified filename.
554   ///
555   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.cache_config_load
cache_load(const std::string & path)556   Result<std::monostate> cache_load(const std::string &path) {
557     auto *error = wasmtime_config_cache_config_load(ptr.get(), path.c_str());
558     if (error != nullptr) {
559       return Error(error);
560     }
561     return std::monostate();
562   }
563 #endif // WASMTIME_FEATURE_CACHE
564 
565 private:
raw_finalize(void * env)566   template <typename T> static void raw_finalize(void *env) {
567     std::unique_ptr<T> ptr(reinterpret_cast<T *>(env));
568   }
569 
570   template <typename M>
raw_get_memory(void * env,size_t * byte_size,size_t * byte_capacity)571   static uint8_t *raw_get_memory(void *env, size_t *byte_size,
572                                  size_t *byte_capacity) {
573     M *memory = reinterpret_cast<M *>(env);
574     return memory->get_memory(byte_size, byte_capacity);
575   }
576 
577   template <typename M>
raw_grow_memory(void * env,size_t new_size)578   static wasmtime_error_t *raw_grow_memory(void *env, size_t new_size) {
579     M *memory = reinterpret_cast<M *>(env);
580     Result<std::monostate> result = memory->grow_memory(new_size);
581     if (!result)
582       return result.err().capi_release();
583     return nullptr;
584   }
585 
586   template <typename T>
587   static wasmtime_error_t *
raw_new_memory(void * env,const wasm_memorytype_t * ty,size_t minimum,size_t maximum,size_t reserved_size_in_bytes,size_t guard_size_in_bytes,wasmtime_linear_memory_t * memory_ret)588   raw_new_memory(void *env, const wasm_memorytype_t *ty, size_t minimum,
589                  size_t maximum, size_t reserved_size_in_bytes,
590                  size_t guard_size_in_bytes,
591                  wasmtime_linear_memory_t *memory_ret) {
592     using Memory = typename T::Memory;
593     T *creator = reinterpret_cast<T *>(env);
594     Result<Memory> result =
595         creator->new_memory(MemoryType::Ref(ty), minimum, maximum,
596                             reserved_size_in_bytes, guard_size_in_bytes);
597     if (!result) {
598       return result.err().capi_release();
599     }
600     Memory memory = result.unwrap();
601     memory_ret->env = std::make_unique<Memory>(memory).release();
602     memory_ret->finalizer = raw_finalize<Memory>;
603     memory_ret->get_memory = raw_get_memory<Memory>;
604     memory_ret->grow_memory = raw_grow_memory<Memory>;
605     return nullptr;
606   }
607 
608 public:
609   /// \brief Configures a custom memory creator for this configuration and
610   /// eventual Engine.
611   ///
612   /// This can be used to use `creator` to allocate linear memories for the
613   /// engine that this configuration will be used for.
host_memory_creator(T creator)614   template <typename T> void host_memory_creator(T creator) {
615     wasmtime_memory_creator_t config = {0};
616     config.env = std::make_unique<T>(creator).release();
617     config.finalizer = raw_finalize<T>;
618     config.new_memory = raw_new_memory<T>;
619     wasmtime_config_host_memory_creator_set(ptr.get(), &config);
620   }
621 
622 #ifdef WASMTIME_FEATURE_POOLING_ALLOCATOR
623   /// \brief Enables and configures the pooling allocation strategy.
624   ///
625   /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.allocation_strategy
pooling_allocation_strategy(const PoolAllocationConfig & config)626   void pooling_allocation_strategy(const PoolAllocationConfig &config) {
627     wasmtime_pooling_allocation_strategy_set(ptr.get(), config.capi());
628   }
629 #endif // WASMTIME_FEATURE_POOLING_ALLOCATOR
630 
631   /**
632    * \brief Specifies whether support for concurrent execution of WebAssembly is
633    * supported within this store.
634    *
635    * For more information see the Rust documentation at
636    * https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.concurrency_support.
637    */
concurrency_support(bool enable)638   void concurrency_support(bool enable) {
639     wasmtime_config_concurrency_support_set(ptr.get(), enable);
640   }
641 };
642 
643 } // namespace wasmtime
644 
645 #endif // WASMTIME_CONFIG_HH
646