jetcrab\runtime/
context.rs

1use crate::runtime::object::Object;
2use crate::vm::value::Value;
3use crate::vm::heap::Heap;
4use std::collections::HashMap;
5
6pub struct Context {
7    pub global_object: Object,
8    pub variables: HashMap<String, Value>,
9    pub this_value: Value,
10    pub heap: Option<Heap>,
11}
12
13impl Default for Context {
14    fn default() -> Self {
15        Self::new()
16    }
17}
18
19impl Context {
20    pub fn new() -> Self {
21        let global_object = Object::new();
22
23        Self {
24            global_object,
25            variables: HashMap::new(),
26            this_value: Value::Undefined,
27            heap: None,
28        }
29    }
30
31    pub fn set_variable(&mut self, name: String, value: Value) {
32        self.variables.insert(name, value);
33    }
34
35    pub fn get_variable(&self, name: &str) -> Option<&Value> {
36        self.variables.get(name)
37    }
38
39    pub fn has_variable(&self, name: &str) -> bool {
40        self.variables.contains_key(name)
41    }
42
43    pub fn set_this(&mut self, value: Value) {
44        self.this_value = value;
45    }
46
47    pub fn get_this(&self) -> &Value {
48        &self.this_value
49    }
50
51    pub fn set_heap(&mut self, heap: Heap) {
52        self.heap = Some(heap);
53    }
54
55    pub fn get_heap(&mut self) -> Option<&mut Heap> {
56        self.heap.as_mut()
57    }
58}