♻️ refactor: hoist breakpoints out of State

main
_ 2023-09-29 16:32:29 -05:00
parent a9e14d0f47
commit a460b5a932
3 changed files with 14 additions and 19 deletions

View File

@ -64,9 +64,5 @@ fn main () {
vm.debug_print = true; vm.debug_print = true;
} }
vm.execute_chunk (&breakpoints);
vm.breakpoints = breakpoints;
vm.execute_chunk ();
} }

View File

@ -54,7 +54,6 @@ pub struct State <'a> {
stack: Vec <StackFrame>, stack: Vec <StackFrame>,
pub debug_print: bool, pub debug_print: bool,
pub breakpoints: Vec <Breakpoint>,
step_count: u32, step_count: u32,
chunk: &'a Chunk, chunk: &'a Chunk,
upvalues: &'a [Value], upvalues: &'a [Value],
@ -93,7 +92,6 @@ impl <'a> State <'a> {
}, },
], ],
debug_print: false, debug_print: false,
breakpoints: Default::default(),
step_count: 0, step_count: 0,
chunk, chunk,
upvalues, upvalues,
@ -144,13 +142,6 @@ impl <'a> State <'a> {
let frame = self.stack.last_mut ().unwrap ().clone (); let frame = self.stack.last_mut ().unwrap ().clone ();
let block = chunk.blocks.get (frame.block_idx).unwrap (); 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 mut next_pc = frame.program_counter;
let pc = usize::try_from (frame.program_counter).expect ("program_counter is not a valid usize"); 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 None
} }
pub fn execute_chunk (&mut self) pub fn execute_chunk (&mut self, breakpoints: &[Breakpoint])
-> Vec <Value> { -> Vec <Value> {
let max_iters = 2000; let max_iters = 2000;
for _ in 0..max_iters { 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 () { match self.step () {
None => (), None => (),
Some (StepOutput::ChunkReturned (x)) => return x, Some (StepOutput::ChunkReturned (x)) => return x,

View File

@ -25,7 +25,7 @@ fn calculate_hash<T: Hash>(t: &T) -> u64 {
fn run_chunk (args: &[&str], chunk: &Chunk) -> Vec <Value> { fn run_chunk (args: &[&str], chunk: &Chunk) -> Vec <Value> {
let upvalues = State::upvalues_from_args (args.into_iter ().map (|s| s.to_string ())); let upvalues = State::upvalues_from_args (args.into_iter ().map (|s| s.to_string ()));
let mut vm = State::new (chunk, &upvalues); let mut vm = State::new (chunk, &upvalues);
vm.execute_chunk () vm.execute_chunk (&[])
} }
/// Takes arguments and Lua bytecode, loads it, runs it, /// 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 upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ()));
let mut vm = State::new (&chunk, &upvalues); let mut vm = State::new (&chunk, &upvalues);
let actual = vm.execute_chunk (); let actual = vm.execute_chunk (&[]);
assert_eq! (actual, expected); assert_eq! (actual, expected);
} }
} }
@ -176,7 +176,7 @@ fn floats () {
let expected: Vec <Value> = expected; let expected: Vec <Value> = expected;
let upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ())); let upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ()));
let mut vm = State::new (&chunk, &upvalues); let mut vm = State::new (&chunk, &upvalues);
let actual = vm.execute_chunk (); let actual = vm.execute_chunk (&[]);
assert_eq! (actual, expected); assert_eq! (actual, expected);
} }
@ -198,7 +198,7 @@ fn fma () {
let expected: Vec <Value> = expected; let expected: Vec <Value> = expected;
let upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ())); let upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ()));
let mut vm = State::new (&chunk, &upvalues); let mut vm = State::new (&chunk, &upvalues);
let actual = vm.execute_chunk (); let actual = vm.execute_chunk (&[]);
assert_eq! (actual, expected); assert_eq! (actual, expected);
} }