dark_vm/utils/
frames.rs

1//! The Frame strut maintains information about the current frame.
2//! This includes caller position, parameters, and local variables.
3
4use super::store::Store;
5use crate::{errors::error::Error, values::value::Value};
6use std::{cell::RefCell, rc::Rc};
7
8#[derive(Debug, PartialEq)]
9pub struct Frame {
10    caller_position: usize,
11    pub name: String,
12    pub current_store: Rc<RefCell<Store>>,
13}
14
15impl Frame {
16    /// Constructs a new frame.
17    /// A Frame maintains the caller's position, along with its name.
18    /// In the future, it will maintain local variables and any parameters passed in.
19    ///
20    /// # Arguments
21    /// `caller_position` - The position where this frame was called or entered.
22    /// `name` - The name of this frame.
23    pub fn new(
24        caller_position: usize,
25        name: &str,
26        parent_store: Option<&Rc<RefCell<Store>>>,
27    ) -> Frame {
28        Frame {
29            caller_position,
30            name: name.to_owned(),
31            current_store: Rc::new(RefCell::new(Store::new(parent_store.cloned()))),
32        }
33    }
34
35    pub fn find(&self, name: &str, pos: usize) -> Result<Rc<Value>, Error> {
36        self.current_store.borrow().get(name, pos)
37    }
38
39    pub fn define(&mut self, name: &str, value: Rc<Value>) {
40        self.current_store.borrow_mut().define(name, value);
41    }
42
43    /// This function gets the position of the caller of this frame.
44    pub fn get_caller_position(&self) -> usize {
45        self.caller_position
46    }
47}