|
| 1 | +use core::fmt; |
| 2 | + |
| 3 | +/// Default initial size for the 32-bit value stack (i32, f32 values). |
| 4 | +pub const DEFAULT_VALUE_STACK_32_INIT_SIZE: usize = 32 * 1024; // 32KB |
| 5 | + |
| 6 | +/// Default initial size for the 64-bit value stack (i64, f64 values). |
| 7 | +pub const DEFAULT_VALUE_STACK_64_INIT_SIZE: usize = 16 * 1024; // 16KB |
| 8 | + |
| 9 | +/// Default initial size for the 128-bit value stack (v128 values). |
| 10 | +pub const DEFAULT_VALUE_STACK_128_INIT_SIZE: usize = 8 * 1024; // 8KB |
| 11 | + |
| 12 | +/// Default initial size for the reference value stack (funcref, externref values). |
| 13 | +pub const DEFAULT_VALUE_STACK_REF_INIT_SIZE: usize = 1024; // 1KB |
| 14 | + |
| 15 | +/// Default initial size for the block stack. |
| 16 | +pub const DEFAULT_BLOCK_STACK_INIT_SIZE: usize = 128; |
| 17 | + |
| 18 | +/// Configuration for the WebAssembly interpreter's stack preallocation. |
| 19 | +/// |
| 20 | +/// This struct allows you to configure how much space is preallocated for the |
| 21 | +/// different parts of the stack that the interpreter uses to store values. |
| 22 | +#[derive(Debug, Clone)] |
| 23 | +pub struct StackConfig { |
| 24 | + value_stack_32_init_size: Option<usize>, |
| 25 | + value_stack_64_init_size: Option<usize>, |
| 26 | + value_stack_128_init_size: Option<usize>, |
| 27 | + value_stack_ref_init_size: Option<usize>, |
| 28 | + block_stack_init_size: Option<usize>, |
| 29 | +} |
| 30 | + |
| 31 | +impl StackConfig { |
| 32 | + /// Create a new stack configuration with default settings. |
| 33 | + pub fn new() -> Self { |
| 34 | + Self { |
| 35 | + value_stack_32_init_size: None, |
| 36 | + value_stack_64_init_size: None, |
| 37 | + value_stack_128_init_size: None, |
| 38 | + value_stack_ref_init_size: None, |
| 39 | + block_stack_init_size: None, |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Get the initial size for the 32-bit value stack. |
| 44 | + pub fn value_stack_32_init_size(&self) -> usize { |
| 45 | + self.value_stack_32_init_size.unwrap_or(DEFAULT_VALUE_STACK_32_INIT_SIZE) |
| 46 | + } |
| 47 | + |
| 48 | + /// Get the initial size for the 64-bit value stack. |
| 49 | + pub fn value_stack_64_init_size(&self) -> usize { |
| 50 | + self.value_stack_64_init_size.unwrap_or(DEFAULT_VALUE_STACK_64_INIT_SIZE) |
| 51 | + } |
| 52 | + |
| 53 | + /// Get the initial size for the 128-bit value stack. |
| 54 | + pub fn value_stack_128_init_size(&self) -> usize { |
| 55 | + self.value_stack_128_init_size.unwrap_or(DEFAULT_VALUE_STACK_128_INIT_SIZE) |
| 56 | + } |
| 57 | + |
| 58 | + /// Get the initial size for the reference value stack. |
| 59 | + pub fn value_stack_ref_init_size(&self) -> usize { |
| 60 | + self.value_stack_ref_init_size.unwrap_or(DEFAULT_VALUE_STACK_REF_INIT_SIZE) |
| 61 | + } |
| 62 | + |
| 63 | + /// Get the initial size for the block stack. |
| 64 | + pub fn block_stack_init_size(&self) -> usize { |
| 65 | + self.block_stack_init_size.unwrap_or(DEFAULT_BLOCK_STACK_INIT_SIZE) |
| 66 | + } |
| 67 | + |
| 68 | + /// Set the initial capacity for the 32-bit value stack. |
| 69 | + pub fn with_value_stack_32_init_size(mut self, capacity: usize) -> Self { |
| 70 | + self.value_stack_32_init_size = Some(capacity); |
| 71 | + self |
| 72 | + } |
| 73 | + |
| 74 | + /// Set the initial capacity for the 64-bit value stack. |
| 75 | + pub fn with_value_stack_64_init_size(mut self, capacity: usize) -> Self { |
| 76 | + self.value_stack_64_init_size = Some(capacity); |
| 77 | + self |
| 78 | + } |
| 79 | + |
| 80 | + /// Set the initial capacity for the 128-bit value stack. |
| 81 | + pub fn with_value_stack_128_init_size(mut self, capacity: usize) -> Self { |
| 82 | + self.value_stack_128_init_size = Some(capacity); |
| 83 | + self |
| 84 | + } |
| 85 | + |
| 86 | + /// Set the initial capacity for the reference value stack. |
| 87 | + pub fn with_value_stack_ref_init_size(mut self, capacity: usize) -> Self { |
| 88 | + self.value_stack_ref_init_size = Some(capacity); |
| 89 | + self |
| 90 | + } |
| 91 | + |
| 92 | + /// Set the initial capacity for the block stack. |
| 93 | + pub fn with_block_stack_init_size(mut self, capacity: usize) -> Self { |
| 94 | + self.block_stack_init_size = Some(capacity); |
| 95 | + self |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl Default for StackConfig { |
| 100 | + fn default() -> Self { |
| 101 | + Self::new() |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl fmt::Display for StackConfig { |
| 106 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 107 | + write!(f, "StackConfig {{ ")?; |
| 108 | + write!(f, "value_stack_32: {}, ", self.value_stack_32_init_size())?; |
| 109 | + write!(f, "value_stack_64: {}, ", self.value_stack_64_init_size())?; |
| 110 | + write!(f, "value_stack_128: {}, ", self.value_stack_128_init_size())?; |
| 111 | + write!(f, "value_stack_ref: {}, ", self.value_stack_ref_init_size())?; |
| 112 | + write!(f, "block_stack: {} }}", self.block_stack_init_size())?; |
| 113 | + Ok(()) |
| 114 | + } |
| 115 | +} |
0 commit comments