1
0
forked from Mirror/wren

Fix iterating over an empty list.

This commit is contained in:
Bob Nystrom
2014-02-14 17:24:06 -08:00
parent e519ecbc49
commit ca7ff222fe
4 changed files with 25 additions and 22 deletions

View File

@ -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
}
}

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.

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

View File

@ -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]