Fix upvalues

This commit is contained in:
Sofia 2026-03-20 15:24:50 +02:00
parent 84eb837534
commit 57b2793ede
2 changed files with 20 additions and 7 deletions

View File

@ -25,15 +25,27 @@ impl State {
} }
} }
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug)]
pub struct Scope { pub struct Scope {
pub locals: HashMap<String, u16>, pub locals: HashMap<String, u16>,
pub register_counter: LocalCounter, pub register_counter: LocalCounter,
pub highest_upvalue: u16, pub highest_upvalue: i32,
pub upvalues: HashMap<String, u16>, pub upvalues: HashMap<String, u16>,
pub is_vararg: bool, 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)] #[derive(Clone, Debug, Default)]
pub struct LocalCounter(u16, Vec<u16>); pub struct LocalCounter(u16, Vec<u16>);
@ -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(); inner_scope.upvalues = scope.upvalues.clone();
for (name, reg) in &scope.locals { 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); inner_scope.upvalues.insert(name.clone(), new_reg);
} }

View File

@ -661,13 +661,13 @@ impl ClosureRunner {
.closure .closure
.upvalues .upvalues
.iter() .iter()
.map(|(v, _)| *v) .map(|(v, _)| *v as i32)
.max() .max()
.unwrap_or(0); .unwrap_or(-1);
let mut upvalues = self.closure.upvalues.clone(); let mut upvalues = self.closure.upvalues.clone();
for (reg, value) in &self.stack { 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 upvalues
} }