summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--compiler.c4
-rw-r--r--tests/03_bugs/01_try_catch_stack_mismatch28
2 files changed, 30 insertions, 2 deletions
diff --git a/compiler.c b/compiler.c
index 3c6f6e1..436f91d 100644
--- a/compiler.c
+++ b/compiler.c
@@ -2485,9 +2485,9 @@ uc_compiler_compile_try(uc_compiler *compiler)
uc_compiler_parse_consume(compiler, TK_RBRACE);
- uc_compiler_set_jmpaddr(compiler, jmp_off, chunk->count);
-
uc_compiler_leave_scope(compiler);
+
+ uc_compiler_set_jmpaddr(compiler, jmp_off, chunk->count);
}
static void
diff --git a/tests/03_bugs/01_try_catch_stack_mismatch b/tests/03_bugs/01_try_catch_stack_mismatch
new file mode 100644
index 0000000..ae7c760
--- /dev/null
+++ b/tests/03_bugs/01_try_catch_stack_mismatch
@@ -0,0 +1,28 @@
+When compiling a try/catch statement with an exception variable, the catch
+skip jump incorrectly pointed to the POP instruction popping the exception
+variable off the stack, leading to a stack position mismatch between
+compiler and vm, causing local variables to yield wrong values at runtime.
+
+-- Expect stdout --
+1
+-- End --
+
+-- Testcase --
+{%
+ function f() {
+ let x;
+
+ try {
+ x = 1;
+ }
+ catch(e) {
+
+ }
+
+ // Before the fix, `x` incorrectly yielded the print function value
+ print(x, "\n");
+ }
+
+ f()
+%}
+-- End --