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
+13 -12
View File
@@ -53,18 +53,14 @@ static const char* libSource =
" }\n"
"\n"
" + that {\n"
" var newList = []\n"
" if (this.count > 0) {\n"
" for (element in this) {\n"
" newList.add(element)\n"
" }\n"
" var result = []\n"
" for (element in this) {\n"
" result.add(element)\n"
" }\n"
" if (that is Range || that.count > 0) {\n"
" for (element in that) {\n"
" newList.add(element)\n"
" }\n"
" for (element in that) {\n"
" result.add(element)\n"
" }\n"
" return newList\n"
" return result\n"
" }\n"
"}\n";
@@ -336,12 +332,17 @@ DEF_NATIVE(list_insert)
DEF_NATIVE(list_iterate)
{
ObjList* list = AS_LIST(args[0]);
// If we're starting the iteration, return the first index.
if (IS_NULL(args[1])) RETURN_NUM(0);
if (IS_NULL(args[1]))
{
if (list->count == 0) RETURN_FALSE;
RETURN_NUM(0);
}
if (!validateInt(vm, args, 1, "Iterator")) return PRIM_ERROR;
ObjList* list = AS_LIST(args[0]);
int index = (int)AS_NUM(args[1]);
// Stop if we're out of bounds.