diff --git a/src/compile.rs b/src/compile.rs index 69fcdb6..3cea605 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -25,15 +25,27 @@ impl State { } } -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug)] pub struct Scope { pub locals: HashMap, pub register_counter: LocalCounter, - pub highest_upvalue: u16, + pub highest_upvalue: i32, pub upvalues: HashMap, pub is_vararg: bool, } +impl Default for Scope { + fn default() -> Self { + Self { + locals: Default::default(), + register_counter: Default::default(), + highest_upvalue: -1, + upvalues: Default::default(), + is_vararg: Default::default(), + } + } +} + #[derive(Clone, Debug, Default)] pub struct LocalCounter(u16, Vec); @@ -866,11 +878,12 @@ impl Expression { } } - inner_scope.highest_upvalue = scope.highest_upvalue + scope.register_counter.0; + inner_scope.highest_upvalue = + scope.highest_upvalue + scope.register_counter.0 as i32; inner_scope.upvalues = scope.upvalues.clone(); for (name, reg) in &scope.locals { - let new_reg = *reg + scope.highest_upvalue + 1; + let new_reg = *reg + (scope.highest_upvalue + 1) as u16; inner_scope.upvalues.insert(name.clone(), new_reg); } diff --git a/src/vm.rs b/src/vm.rs index 3a87950..34b8fa3 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -661,13 +661,13 @@ impl ClosureRunner { .closure .upvalues .iter() - .map(|(v, _)| *v) + .map(|(v, _)| *v as i32) .max() - .unwrap_or(0); + .unwrap_or(-1); let mut upvalues = self.closure.upvalues.clone(); for (reg, value) in &self.stack { - upvalues.insert(reg + highest_upvalue + 1, value.clone()); + upvalues.insert(reg + (highest_upvalue + 1) as u16, value.clone()); } upvalues }