♻️ refactor: hoist breakpoints out of State
parent
a9e14d0f47
commit
a460b5a932
|
@ -64,9 +64,5 @@ fn main () {
|
|||
vm.debug_print = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
vm.breakpoints = breakpoints;
|
||||
|
||||
vm.execute_chunk ();
|
||||
vm.execute_chunk (&breakpoints);
|
||||
}
|
||||
|
|
19
src/state.rs
19
src/state.rs
|
@ -54,7 +54,6 @@ pub struct State <'a> {
|
|||
stack: Vec <StackFrame>,
|
||||
|
||||
pub debug_print: bool,
|
||||
pub breakpoints: Vec <Breakpoint>,
|
||||
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 <Value> {
|
||||
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,
|
||||
|
|
|
@ -25,7 +25,7 @@ fn calculate_hash<T: Hash>(t: &T) -> u64 {
|
|||
fn run_chunk (args: &[&str], chunk: &Chunk) -> Vec <Value> {
|
||||
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 <Value> = 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 <Value> = 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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue