fix: remove conditional guards in List.+ and handle empty list iteration in iterate native

The previous implementation of List.+ used conditional checks (`this.count > 0`, `that.count > 0`) before iterating, which caused the loop body to be skipped entirely for empty lists. This commit removes those guards, allowing the for-in loops to run unconditionally — the Wren VM's iterate/iterateValue protocol already handles the empty case correctly at the native level. Additionally, the `list_iterate` native in `wren_core.c` now returns `false` immediately when the list count is zero and the iterator argument is null, preventing an out-of-bounds index of 0 from being returned for empty lists. The test file `test/list/concat.wren` is renamed to `test/list/plus.wren` and extended with a check that the original list `a` remains unmodified after concatenation. A new test in `test/list/iterate.wren` verifies that iterating over an empty list returns `false`.
This commit is contained in:
Bob Nystrom
2014-02-15 01:24:06 +00:00
parent e690a48e45
commit 5f16a3a1cc
4 changed files with 25 additions and 22 deletions
+3
View File
@@ -5,3 +5,6 @@ IO.print(a.iterate(1)) // expect: 2
IO.print(a.iterate(2)) // expect: 3
IO.print(a.iterate(3)) // expect: false
IO.print(a.iterate(-1)) // expect: false
// Nothing to iterate in an empty list.
IO.print([].iterate(null)) // expect: false
@@ -13,3 +13,6 @@ IO.print(d) // expect: [1, 2, 3, 4, 5, 6]
IO.print(e) // expect: [1, 2, 3]
IO.print(f) // expect: [1, 2, 3]
IO.print(g) // expect: []
// Doesn't modify original list.
IO.print(a) // expect: [1, 2, 3]