radix_wasmi/engine/config.rs
1use super::stack::StackLimits;
2use wasmparser::WasmFeatures;
3
4/// The default amount of stacks kept in the cache at most.
5const DEFAULT_CACHED_STACKS: usize = 2;
6
7/// Configuration for an [`Engine`].
8///
9/// [`Engine`]: [`crate::Engine`]
10#[derive(Debug, Copy, Clone)]
11pub struct Config {
12 /// The limits set on the value stack and call stack.
13 stack_limits: StackLimits,
14 /// The amount of Wasm stacks to keep in cache at most.
15 cached_stacks: usize,
16 /// Is `true` if the `mutable-global` Wasm proposal is enabled.
17 mutable_global: bool,
18 /// Is `true` if the `sign-extension` Wasm proposal is enabled.
19 sign_extension: bool,
20 /// Is `true` if the `saturating-float-to-int` Wasm proposal is enabled.
21 saturating_float_to_int: bool,
22 /// Is `true` if the [`multi-value`] Wasm proposal is enabled.
23 multi_value: bool,
24}
25
26impl Default for Config {
27 fn default() -> Self {
28 Self {
29 stack_limits: StackLimits::default(),
30 cached_stacks: DEFAULT_CACHED_STACKS,
31 mutable_global: true,
32 sign_extension: true,
33 saturating_float_to_int: true,
34 multi_value: true,
35 }
36 }
37}
38
39impl Config {
40 /// Sets the [`StackLimits`] for the [`Config`].
41 pub fn set_stack_limits(&mut self, stack_limits: StackLimits) -> &mut Self {
42 self.stack_limits = stack_limits;
43 self
44 }
45
46 /// Returns the [`StackLimits`] of the [`Config`].
47 pub(super) fn stack_limits(&self) -> StackLimits {
48 self.stack_limits
49 }
50
51 /// Sets the maximum amount of cached stacks for reuse for the [`Config`].
52 ///
53 /// # Note
54 ///
55 /// Defaults to 2.
56 pub fn set_cached_stacks(&mut self, amount: usize) -> &mut Self {
57 self.cached_stacks = amount;
58 self
59 }
60
61 /// Returns the maximum amount of cached stacks for reuse of the [`Config`].
62 pub(super) fn cached_stacks(&self) -> usize {
63 self.cached_stacks
64 }
65
66 /// Enable or disable the [`mutable-global`] Wasm proposal for the [`Config`].
67 ///
68 /// # Note
69 ///
70 /// Enabled by default.
71 ///
72 /// [`mutable-global`]: https://github.com/WebAssembly/mutable-global
73 pub fn wasm_mutable_global(&mut self, enable: bool) -> &mut Self {
74 self.mutable_global = enable;
75 self
76 }
77
78 /// Enable or disable the [`sign-extension`] Wasm proposal for the [`Config`].
79 ///
80 /// # Note
81 ///
82 /// Enabled by default.
83 ///
84 /// [`sign-extension`]: https://github.com/WebAssembly/sign-extension-ops
85 pub fn wasm_sign_extension(&mut self, enable: bool) -> &mut Self {
86 self.sign_extension = enable;
87 self
88 }
89
90 /// Enable or disable the [`saturating-float-to-int`] Wasm proposal for the [`Config`].
91 ///
92 /// # Note
93 ///
94 /// Enabled by default.
95 ///
96 /// [`saturating-float-to-int`]:
97 /// https://github.com/WebAssembly/nontrapping-float-to-int-conversions
98 pub fn wasm_saturating_float_to_int(&mut self, enable: bool) -> &mut Self {
99 self.saturating_float_to_int = enable;
100 self
101 }
102
103 /// Enable or disable the [`multi-value`] Wasm proposal for the [`Config`].
104 ///
105 /// # Note
106 ///
107 /// Enabled by default.
108 ///
109 /// [`multi-value`]: https://github.com/WebAssembly/multi-value
110 pub fn wasm_multi_value(&mut self, enable: bool) -> &mut Self {
111 self.multi_value = enable;
112 self
113 }
114
115 /// Returns the [`WasmFeatures`] represented by the [`Config`].
116 pub fn wasm_features(&self) -> WasmFeatures {
117 WasmFeatures {
118 multi_value: self.multi_value,
119 mutable_global: self.mutable_global,
120 saturating_float_to_int: self.saturating_float_to_int,
121 sign_extension: self.sign_extension,
122 reference_types: false,
123 bulk_memory: false,
124 component_model: false,
125 simd: false,
126 relaxed_simd: false,
127 threads: false,
128 tail_call: false,
129 deterministic_only: true,
130 multi_memory: false,
131 exceptions: false,
132 memory64: false,
133 extended_const: false,
134 }
135 }
136}