From 392e7d848a6f5a1dd5bf5fc279c42ba7250f9341 Mon Sep 17 00:00:00 2001 From: AzerothA Date: Sat, 16 Dec 2023 14:47:45 +0330 Subject: [PATCH] Bug fix --- figc/src/codegen/codegen.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/figc/src/codegen/codegen.rs b/figc/src/codegen/codegen.rs index 4c5d9a2..4dd0380 100644 --- a/figc/src/codegen/codegen.rs +++ b/figc/src/codegen/codegen.rs @@ -1120,8 +1120,11 @@ impl ImportContext { pub struct GlobalContext { section: GlobalSection, - /// - globals: BTreeMap, + /// <(id, global_name), id> + /// ^ + /// | + /// we need this id to sort the Map correctly + globals: BTreeMap<(u32, String), (u32, GlobalType, ConstExpr)>, globals_id: u32, } @@ -1138,7 +1141,7 @@ impl GlobalContext { pub fn add_global_int(&mut self, name: &str, init: ConstExpr, mutable: bool) -> u32 { let id = self.globals_id; self.globals.insert( - name.to_string(), + (id, name.to_string()), ( id, GlobalType { @@ -1156,18 +1159,18 @@ impl GlobalContext { /// Start pop all the globals and apply them pub fn apply_globals(&mut self) { - while let Some((_key, val)) = self.globals.pop_last() { + while let Some((_key, val)) = self.globals.pop_first() { self.section.global(val.1, &val.2); } } pub fn set_global(&mut self, name: &str, value: ConstExpr) { - let global = self.globals.get_mut(name).unwrap(); + let global = self.globals.get_mut(&(self.globals_id, name.to_string())).unwrap(); *global = (global.0, global.1, value); } pub fn get_global(&self, name: &String) -> Option<&(u32, GlobalType, ConstExpr)> { - self.globals.get(name) + self.globals.get(&(self.globals_id, name.clone())) } pub fn get_section(&self) -> GlobalSection {