From db84365c27b36fc20768f193e8c1a3d5e678d557 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Sun, 1 Oct 2023 20:31:30 -0500 Subject: [PATCH] :recycle: refactor: State owns its Chunk now --- lunar_wave_cli/src/main.rs | 2 +- lunar_wave_vm/src/loader.rs | 8 +- lunar_wave_vm/src/state.rs | 229 +++++++++++++++---------------- lunar_wave_vm/src/tests.rs | 44 +++--- lunar_wave_vm/tests/embedding.rs | 2 +- 5 files changed, 138 insertions(+), 147 deletions(-) diff --git a/lunar_wave_cli/src/main.rs b/lunar_wave_cli/src/main.rs index e983175..d4c61e3 100644 --- a/lunar_wave_cli/src/main.rs +++ b/lunar_wave_cli/src/main.rs @@ -51,7 +51,7 @@ fn main () -> Result <(), lwvm::StepError> { let upvalues = lwvm::State::upvalues_from_args ([exe_name].into_iter ().chain (args)); - let mut vm = lwvm::State::new (&chunk, &upvalues); + let mut vm = lwvm::State::new (chunk, upvalues); if std::env::var("LWVM_DEBUG").is_ok() { vm.debug_print = true; } diff --git a/lunar_wave_vm/src/loader.rs b/lunar_wave_vm/src/loader.rs index 16a63fc..0d31daa 100644 --- a/lunar_wave_vm/src/loader.rs +++ b/lunar_wave_vm/src/loader.rs @@ -9,11 +9,9 @@ use crate::{ }; pub fn compile_bytecode_from_file (path: &str) -> Vec { - use std::{ - process::{ - Command, - Stdio, - }, + use std::process::{ + Command, + Stdio, }; let child = Command::new ("luac5.4") diff --git a/lunar_wave_vm/src/state.rs b/lunar_wave_vm/src/state.rs index a551221..2e33800 100644 --- a/lunar_wave_vm/src/state.rs +++ b/lunar_wave_vm/src/state.rs @@ -1,5 +1,3 @@ -use std::rc::Rc; - use crate::{ instruction::Instruction, value::{ @@ -8,21 +6,21 @@ use crate::{ }, }; -#[derive (Debug)] +#[derive (Clone, Debug)] pub struct Upvalue { pub in_stack: bool, pub idx: u8, pub kind: u8, } -#[derive (Debug)] +#[derive (Clone, Debug)] pub struct Block { pub instructions: Vec , pub constants: Vec , pub upvalues: Vec , } -#[derive (Debug)] +#[derive (Clone, Debug)] pub struct Chunk { pub blocks: Vec , } @@ -47,7 +45,7 @@ pub struct Breakpoint { } #[derive (Debug)] -pub struct State <'a> { +pub struct State { pub registers: Vec , // Currently only used for native function calls top: usize, @@ -55,8 +53,8 @@ pub struct State <'a> { pub debug_print: bool, step_count: u32, - chunk: &'a Chunk, - upvalues: &'a [Value], + chunk: Chunk, + upvalues: Vec , } fn lw_io_write (l: &mut State, num_args: usize) -> usize { @@ -142,8 +140,8 @@ pub struct StepError { msg: &'static str, } -impl <'a> State <'a> { - pub fn new (chunk: &'a Chunk, upvalues: &'a [Value]) -> Self { +impl State { + pub fn new (chunk: Chunk, upvalues: Vec ) -> Self { Self { // TODO: Stack is actually supposed to grow to a limit of // idk 10,000. I thought it was fixed at 256. @@ -232,11 +230,10 @@ impl <'a> State <'a> { pub fn step (&mut self) -> Result