1use crate::vm::handle::FunctionHandle;
2use crate::vm::types::{ArgIndex, CodeAddress, ConstantIndex, FramePointer};
3use crate::vm::value::Value;
4use std::collections::HashMap;
5
6#[derive(Debug, Clone)]
7pub struct Frame {
8 pub return_address: CodeAddress,
9 pub arg_count: ArgIndex,
10 pub base_pointer: FramePointer,
11 pub arguments: Vec<Value>,
12 pub closure_vars: HashMap<String, Value>,
13 pub function_handle: Option<FunctionHandle>,
14 pub this_value: Option<Value>,
15 pub constants: Vec<Value>,
16}
17
18impl Frame {
19 pub fn new() -> Self {
20 Self {
21 return_address: CodeAddress::new(0),
22 arg_count: ArgIndex::new(0),
23 base_pointer: FramePointer::new(0),
24 arguments: Vec::new(),
25 closure_vars: HashMap::new(),
26 function_handle: None,
27 this_value: None,
28 constants: Vec::new(),
29 }
30 }
31
32 pub fn with_return_address(return_address: CodeAddress) -> Self {
33 Self {
34 return_address,
35 arg_count: ArgIndex::new(0),
36 base_pointer: FramePointer::new(0),
37 arguments: Vec::new(),
38 closure_vars: HashMap::new(),
39 function_handle: None,
40 this_value: None,
41 constants: Vec::new(),
42 }
43 }
44
45 pub fn with_constants(constants: Vec<Value>) -> Self {
46 Self {
47 return_address: CodeAddress::new(0),
48 arg_count: ArgIndex::new(0),
49 base_pointer: FramePointer::new(0),
50 arguments: Vec::new(),
51 closure_vars: HashMap::new(),
52 function_handle: None,
53 this_value: None,
54 constants,
55 }
56 }
57
58 pub fn get_constant(&self, index: ConstantIndex) -> Option<Value> {
59 let idx = usize::from(index);
60 self.constants.get(idx).cloned()
61 }
62
63 pub fn set_constants(&mut self, constants: Vec<Value>) {
64 self.constants = constants;
65 }
66}
67
68impl Default for Frame {
69 fn default() -> Self {
70 Self {
71 return_address: CodeAddress::new(0),
72 arg_count: ArgIndex::new(0),
73 base_pointer: FramePointer::new(0),
74 arguments: Vec::new(),
75 closure_vars: HashMap::new(),
76 function_handle: None,
77 this_value: None,
78 constants: Vec::new(),
79 }
80 }
81}