summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-10-06 22:39:14 +0200
committerJo-Philipp Wich <jo@mein.io>2020-10-06 22:48:05 +0200
commit4d1c4e28b8d8368a105717e142f8e920cbf4ea0f (patch)
treeff28efff9b8232ec0bcaaed2805bb9533f6fcb17
parent1ecd1c828d8a0bd1446021912d68a42e24acdcc5 (diff)
eval: properly break out of switch/case on return/continue/exception
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--eval.c3
-rw-r--r--tests/02_runtime/04_switch_case93
2 files changed, 96 insertions, 0 deletions
diff --git a/eval.c b/eval.c
index 074baa5..fdc88b9 100644
--- a/eval.c
+++ b/eval.c
@@ -1449,6 +1449,9 @@ ut_execute_switch_case(struct ut_state *state, uint32_t off)
rv = NULL;
break;
}
+ else if (ut_is_type(rv, T_RETURN) || ut_is_type(rv, T_EXCEPTION) || ut_is_type(rv, T_CONTINUE)) {
+ break;
+ }
}
json_object_put(v[0]);
diff --git a/tests/02_runtime/04_switch_case b/tests/02_runtime/04_switch_case
index 9a871fd..bc8b80e 100644
--- a/tests/02_runtime/04_switch_case
+++ b/tests/02_runtime/04_switch_case
@@ -177,3 +177,96 @@ true
print(x, "\n");
%}
-- End --
+
+
+8. Ensure that `return` breaks out of switch statements.
+
+-- Expect stdout --
+one
+two
+-- End --
+
+-- Testcase --
+{%
+ function test(n) {
+ switch (n) {
+ case 1:
+ return "one";
+
+ case 2:
+ return "two";
+
+ default:
+ return "three";
+ }
+ }
+
+ print(test(1), "\n");
+ print(test(2), "\n");
+%}
+-- End --
+
+
+9. Ensure that `continue` breaks out of switch statements.
+
+-- Expect stdout --
+one
+two
+-- End --
+
+-- Testcase --
+{%
+ for (n in [1,2]) {
+ switch (n) {
+ case 1:
+ print("one\n");
+ continue;
+
+ case 2:
+ print("two\n");
+ continue;
+
+ default:
+ print("three\n");
+ }
+ }
+%}
+-- End --
+
+
+10. Ensure that exceptions break out of switch statements.
+
+-- Expect stdout --
+one
+-- End --
+
+-- Expect stderr --
+Died
+In line 6, byte 7:
+
+ ` die();`
+ Near here ------^
+
+
+-- End --
+
+-- Testcase --
+{%
+ function test(n) {
+ switch (n) {
+ case 1:
+ print("one\n");
+ die();
+
+ case 2:
+ print("two\n");
+ die();
+
+ default:
+ print("three\n");
+ }
+ }
+
+ print(test(1), "\n");
+%}
+-- End --