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:
+6
-10
@@ -10,17 +10,13 @@ class List {
|
||||
}
|
||||
|
||||
+ that {
|
||||
var newList = []
|
||||
if (this.count > 0) {
|
||||
for (element in this) {
|
||||
newList.add(element)
|
||||
}
|
||||
var result = []
|
||||
for (element in this) {
|
||||
result.add(element)
|
||||
}
|
||||
if (that is Range || that.count > 0) {
|
||||
for (element in that) {
|
||||
newList.add(element)
|
||||
}
|
||||
for (element in that) {
|
||||
result.add(element)
|
||||
}
|
||||
return newList
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user