Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nyzd committed Dec 16, 2023
1 parent 597146f commit 392e7d8
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions figc/src/codegen/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1120,8 +1120,11 @@ impl ImportContext {

pub struct GlobalContext {
section: GlobalSection,
/// <global_name, id>
globals: BTreeMap<String, (u32, GlobalType, ConstExpr)>,
/// <(id, global_name), id>
/// ^
/// |
/// we need this id to sort the Map correctly
globals: BTreeMap<(u32, String), (u32, GlobalType, ConstExpr)>,
globals_id: u32,
}

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 392e7d8

Please sign in to comment.