From a460b5a932cbf749446757b310da86acb42f270a Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Fri, 29 Sep 2023 16:32:29 -0500 Subject: [PATCH] :recycle: refactor: hoist breakpoints out of State --- src/main.rs | 6 +----- src/state.rs | 19 +++++++++---------- src/tests.rs | 8 ++++---- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 69c4ea4..f1083c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,9 +64,5 @@ fn main () { vm.debug_print = true; } - - - vm.breakpoints = breakpoints; - - vm.execute_chunk (); + vm.execute_chunk (&breakpoints); } diff --git a/src/state.rs b/src/state.rs index edeeeff..1b4d656 100644 --- a/src/state.rs +++ b/src/state.rs @@ -54,7 +54,6 @@ pub struct State <'a> { stack: Vec , pub debug_print: bool, - pub breakpoints: Vec , step_count: u32, chunk: &'a Chunk, upvalues: &'a [Value], @@ -93,7 +92,6 @@ impl <'a> State <'a> { }, ], debug_print: false, - breakpoints: Default::default(), step_count: 0, chunk, upvalues, @@ -144,13 +142,6 @@ impl <'a> State <'a> { let frame = self.stack.last_mut ().unwrap ().clone (); let block = chunk.blocks.get (frame.block_idx).unwrap (); - for bp in &self.breakpoints { - if frame.block_idx == bp.block_idx && frame.program_counter == bp.program_counter - { - dbg! (&self); - } - } - let mut next_pc = frame.program_counter; let pc = usize::try_from (frame.program_counter).expect ("program_counter is not a valid usize"); @@ -650,11 +641,19 @@ impl <'a> State <'a> { None } - pub fn execute_chunk (&mut self) + pub fn execute_chunk (&mut self, breakpoints: &[Breakpoint]) -> Vec { let max_iters = 2000; for _ in 0..max_iters { + let frame = self.stack.last ().unwrap (); + for bp in breakpoints { + if frame.block_idx == bp.block_idx && frame.program_counter == bp.program_counter + { + dbg! (&self); + } + } + match self.step () { None => (), Some (StepOutput::ChunkReturned (x)) => return x, diff --git a/src/tests.rs b/src/tests.rs index 92e24ff..cb67431 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -25,7 +25,7 @@ fn calculate_hash(t: &T) -> u64 { fn run_chunk (args: &[&str], chunk: &Chunk) -> Vec { let upvalues = State::upvalues_from_args (args.into_iter ().map (|s| s.to_string ())); let mut vm = State::new (chunk, &upvalues); - vm.execute_chunk () + vm.execute_chunk (&[]) } /// Takes arguments and Lua bytecode, loads it, runs it, @@ -120,7 +120,7 @@ fn bools () { let upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ())); let mut vm = State::new (&chunk, &upvalues); - let actual = vm.execute_chunk (); + let actual = vm.execute_chunk (&[]); assert_eq! (actual, expected); } } @@ -176,7 +176,7 @@ fn floats () { let expected: Vec = expected; let upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ())); let mut vm = State::new (&chunk, &upvalues); - let actual = vm.execute_chunk (); + let actual = vm.execute_chunk (&[]); assert_eq! (actual, expected); } @@ -198,7 +198,7 @@ fn fma () { let expected: Vec = expected; let upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ())); let mut vm = State::new (&chunk, &upvalues); - let actual = vm.execute_chunk (); + let actual = vm.execute_chunk (&[]); assert_eq! (actual, expected); }