diff --git a/lunar_wave_vm/src/state.rs b/lunar_wave_vm/src/state.rs index 28437c3..9fe8b1c 100644 --- a/lunar_wave_vm/src/state.rs +++ b/lunar_wave_vm/src/state.rs @@ -16,7 +16,7 @@ pub struct Upvalue { pub kind: u8, } -#[derive (Clone, Debug)] +#[derive (Clone, Debug, Default)] pub struct Block { pub instructions: Vec , pub constants: Vec , @@ -202,7 +202,10 @@ impl State { pub fn new_with_args > (chunk: Chunk, mut si: Interner, args: I) -> Self { let upvalues = Self::upvalues_from_args (&mut si, args); - let current_block = Rc::clone (&chunk.blocks [0]); + let current_block = match chunk.blocks.get (0) { + Some (x) => Rc::clone (&x), + None => Default::default (), + }; Self { // TODO: Stack is actually supposed to grow to a limit of @@ -507,7 +510,10 @@ impl State { fn set_stack_top (&mut self, frame: StackFrame) { self.stack_top = frame; - self.current_block = Rc::clone (&self.chunk.blocks [frame.block_idx]); + self.current_block = match self.chunk.blocks.get (frame.block_idx) { + Some (x) => Rc::clone (&x), + None => Default::default (), + }; } fn fetch (&self) -> &Instruction { @@ -1041,8 +1047,8 @@ impl State { pub fn set_chunk (&mut self, chunk: Chunk) { self.stack = vec! []; - self.set_stack_top (Default::default ()); self.chunk = chunk; + self.set_stack_top (Default::default ()); } pub fn to_string (&mut self, s: &str) -> Value { diff --git a/lunar_wave_vm/src/tests.rs b/lunar_wave_vm/src/tests.rs index 2771e9f..4dfb972 100644 --- a/lunar_wave_vm/src/tests.rs +++ b/lunar_wave_vm/src/tests.rs @@ -39,8 +39,7 @@ fn run_bytecode (vm: &mut State, args: &[&str], bc: &[u8]) -> Vec { /// Takes arguments and Lua source code, /// invokes `luac` to compile it to bytecode, -/// runs it, -/// and returns the output +/// runs it, and returns the output fn run_source (vm: &mut State, args: &[&str], s: &str) -> Vec { let bc = loader::compile_bytecode (s.as_bytes ().to_vec ()).unwrap (); @@ -97,7 +96,7 @@ fn bools () { si.to_value ("print"), ], upvalues: vec! [], - }, + }.into (), Block { instructions: vec! [ Inst::Test (0, false), @@ -111,7 +110,7 @@ fn bools () { ], constants: vec! [], upvalues: vec! [], - }, + }.into (), ], }; @@ -173,7 +172,7 @@ fn floats () { upvalues: vec! [], }; let chunk = Chunk { - blocks: vec! [block], + blocks: vec! [block.into ()], }; let mut vm = crate::State::new_with_args (Chunk::default (), si, vec! [].into_iter());