From 1c14312b1471af5340155217227deabd1e535549 Mon Sep 17 00:00:00 2001 From: Denneisk <20892685+Denneisk@users.noreply.github.com> Date: Mon, 16 Dec 2024 05:10:22 +0000 Subject: [PATCH] Fix try/catch would not pop excess scopes (#3216) * Fix try/catch would not pop excess scopes * Set scope directly * Add test --- data/expression2/tests/regressions/3080.txt | 40 +++++++++++++++++++ .../gmod_wire_expression2/base/compiler.lua | 7 +++- 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 data/expression2/tests/regressions/3080.txt diff --git a/data/expression2/tests/regressions/3080.txt b/data/expression2/tests/regressions/3080.txt new file mode 100644 index 00000000..e53317ef --- /dev/null +++ b/data/expression2/tests/regressions/3080.txt @@ -0,0 +1,40 @@ +## SHOULD_PASS:EXECUTE +@strict + +try { + error("A") +} catch(A:string) { + assert(A == "A") +} + +try { + if(1) { + error("B") + } +} catch(B:string) { + assert(B == "B") +} + +if(1) { + try { + error("C") + } catch(C:string) { + assert(C == "C") + } + + try { + if(1) { + error("D") + } + } catch(D:string) { + assert(D == "D") + } + + let Fn = function() { error("F") } + + try { + Fn() + } catch(F:string) { + assert(F == "F") + } +} diff --git a/lua/entities/gmod_wire_expression2/base/compiler.lua b/lua/entities/gmod_wire_expression2/base/compiler.lua index 202faba0..8b9ddef4 100644 --- a/lua/entities/gmod_wire_expression2/base/compiler.lua +++ b/lua/entities/gmod_wire_expression2/base/compiler.lua @@ -621,10 +621,13 @@ local CompileVisitors = { self.scope.data.ops = self.scope.data.ops + 5 return function(state) ---@param state RuntimeContext + local scope, scope_id = state.Scope, state.ScopeID state:PushScope() local ok, err = pcall(try_block, state) - state:PopScope() - if not ok then + if ok then + state:PopScope() + else + state.Scope, state.ScopeID = scope, scope_id -- Skip back any scopes that may have been created in try_block local catchable, msg = E2Lib.unpackException(err) if catchable then state:PushScope()