From ee4af9b55cb4591e63c596af592abc33a8a8f315 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Tue, 20 Feb 2024 17:30:38 +0100 Subject: vm: rework object iteration Ensure that deleting object keys during iteration is safe by keeping a global chain of per-object iterators which are advanced to the next key when the entry that is about to be iterated is deleted. Signed-off-by: Jo-Philipp Wich --- tests/custom/02_runtime/08_object_iteration | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/custom/02_runtime/08_object_iteration (limited to 'tests') diff --git a/tests/custom/02_runtime/08_object_iteration b/tests/custom/02_runtime/08_object_iteration new file mode 100644 index 0000000..2e58cdf --- /dev/null +++ b/tests/custom/02_runtime/08_object_iteration @@ -0,0 +1,51 @@ +Testing object iteration behavior. + + +1. Testing that deleting properties during iteration is safe. + +-- Expect stdout -- +a +w +z +-- End -- + +-- Testcase -- +{% + o1 = { a: 1, b: 2, c: 3 }; + + for (k in o1) { + delete o1.a; + delete o1.b; + delete o1.c; + print(k, "\n"); + } + + o2 = { w: 1, x: 2, y: 3, z: 4 }; + + for (k in o2) { + delete o2.x; + delete o2.y; + print(k, "\n"); + } +%} +-- End -- + + +2. Test that reordering object properties during iteration is safe. + +-- Expect stdout -- +c +b +c +-- End -- + +-- Testcase -- +{% + o = { c: 1, b: 2, a: 3 }; + + for (k in o) { + sort(o); + print(k, "\n"); + } +%} +-- End -- -- cgit v1.2.3