From 126e5eba61c3ca2b74fb9e7d52c91d90e8315a0c Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Fri, 10 Jan 2025 12:46:21 +0100 Subject: Properly fill the context stack when forcing it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When calling `force_context_switch` the stack was mindlessly pushing the given name without taking into consideration how scopes are to be laid out. This made some variables/addresses that were previously preserved no longer reachable when crunching pending nodes. This patch also makes `force_context_switch` reset the stack before doing anything at all, which means that `force_context_pop` was no longer relevant. Signed-off-by: Miquel Sabaté Solà --- lib/xixanta/src/object.rs | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'lib/xixanta/src/object.rs') diff --git a/lib/xixanta/src/object.rs b/lib/xixanta/src/object.rs index ad02747..a92f9fb 100644 --- a/lib/xixanta/src/object.rs +++ b/lib/xixanta/src/object.rs @@ -339,20 +339,25 @@ impl Context { } /// Change the current context to the given one identified by `name`, - /// disregarding any check. This is to be used when switching a context to - /// set a very specific value for that context. You should call - /// `force_context_pop` immediately. - pub fn force_context_switch(&mut self, name: &String) { - self.stack.push(name.to_owned()); - } - - /// Remove the last context being used if any. In contrast with - /// `context_pop`, this one does not error out, but does nothing in case we - /// are in the global context. This is to be used in conjunction with - /// `force_context_switch`. - pub fn force_context_pop(&mut self) { - if !self.stack.is_empty() { - self.stack.truncate(self.stack.len() - 1); + /// disregarding any check. This is a destructive operation and will clear + /// out any previous context. Hence, only call this when you are sure that + /// there's no reliance on a specific context in the future. + pub fn force_context_switch(&mut self, name: &str) { + let mut ax = String::with_capacity(name.len()); + + // Clear the context stack as we will set it manually. + self.stack.truncate(0); + + // Forcing a context switch is not as simple as pushing the given name, + // but we have to make sure that the whole scope hierarchy is laid out + // so the code that fetches variables continues to work (e.g. if + // "A::B::C", then the stack must look like ["A", "A::B", "A::B::C"]). + for n in name.split("::") { + if !ax.is_empty() { + ax += "::"; + } + ax += n; + self.stack.push(ax.clone()); } } -- cgit v1.2.3