diff --git a/src/frame.rs b/src/frame.rs index 884a337..b7a2e95 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -6,6 +6,7 @@ pub struct Frame { pub base: usize, pub stack: Stack, pub locals: Vec, + pub can_extend_locals: bool, } impl Frame { @@ -17,6 +18,19 @@ impl Frame { base, stack: Stack::new(), locals: vec![0; num_locals], + can_extend_locals: false, + } + } + + pub fn new_extendable(base: usize) -> Frame { + if cfg!(feature = "debug:frame") { + println!("Initializing extendable frame"); + } + Frame { + base, + stack: Stack::new(), + locals: Vec::new(), + can_extend_locals: true, } } @@ -28,16 +42,24 @@ impl Frame { self.locals.is_empty() } - pub fn get(&self, offset: usize) -> Result { + pub fn get(&mut self, offset: usize) -> Result { if offset >= self.locals.len() { - return Err("Local variable out of range"); + if self.can_extend_locals { + self.locals.resize(offset + 1, 0); + } else { + return Err("Local variable out of range"); + } } Ok(self.locals[offset]) } pub fn set(&mut self, offset: usize, val: i32) -> Result<()> { if offset >= self.locals.len() { - return Err("Local variable out of range"); + if self.can_extend_locals { + self.locals.resize(offset + 1, 0); + } else { + return Err("Local variable out of range"); + } } self.locals[offset] = val; Ok(()) diff --git a/src/machine.rs b/src/machine.rs index b49cb8a..ef77ec6 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -16,7 +16,6 @@ use heap::Heap; use netstack::NetStack; pub const MAGIC_HEADER: u32 = 0x1DEA_DFAD; -const ANTI_BS_SIZE: usize = 0xFF; pub struct Machine { pub wide: bool, @@ -41,7 +40,7 @@ impl Machine { halted: false, pool, block, - frame: vec![Frame::new(ANTI_BS_SIZE, 0)], + frame: vec![Frame::new_extendable(0)], stream_in: Box::new(::std::io::stdin()), stream_out: Arc::new(Mutex::new(::std::io::stdout())),