wasm/execution/
locals.rs

1use alloc::boxed::Box;
2use alloc::vec::Vec;
3
4use crate::core::reader::types::ValType;
5use crate::execution::assert_validated::UnwrapValidatedExt;
6use crate::execution::value::Value;
7
8/// A helper for managing values of locals (and parameters) during function execution.
9///
10/// Note: As of now this stores the [Value]s. In the future storing the raw bytes without information
11/// about a value's type may be preferred to minimize memory usage.
12pub struct Locals {
13    data: Box<[Value]>,
14}
15
16impl Locals {
17    pub fn new(
18        parameters: impl Iterator<Item = Value>,
19        locals: impl Iterator<Item = ValType>,
20    ) -> Self {
21        let data = parameters
22            .chain(locals.map(Value::default_from_ty))
23            .collect::<Vec<Value>>()
24            .into_boxed_slice();
25
26        Self { data }
27    }
28
29    pub fn get(&self, idx: usize) -> &Value {
30        self.data.get(idx).unwrap_validated()
31    }
32
33    pub fn get_ty(&self, idx: usize) -> ValType {
34        self.data.get(idx).unwrap_validated().to_ty()
35    }
36
37    pub fn get_mut(&mut self, idx: usize) -> &mut Value {
38        self.data.get_mut(idx).unwrap_validated()
39    }
40}