Add wrenSetListElement, correctly allow negative indices on wrenGetListElement

This commit is contained in:
ruby0x1
2020-12-03 10:30:47 -08:00
parent 999acba06f
commit 97ebcc72c3
6 changed files with 86 additions and 3 deletions

View File

@ -15,6 +15,14 @@ static void insertNumber(WrenVM* vm, int index, double value)
wrenInsertInList(vm, 0, index, 1);
}
// Helper function to append a double in a slot then insert it into the list at
// slot zero.
static void appendNumber(WrenVM* vm, double value)
{
wrenSetSlotDouble(vm, 1, value);
wrenInsertInList(vm, 0, -1, 1);
}
static void insert(WrenVM* vm)
{
wrenSetSlotNewList(vm, 0);
@ -37,10 +45,40 @@ static void insert(WrenVM* vm)
insertNumber(vm, -3, 9.0);
}
static void get(WrenVM* vm)
{
int listSlot = 1;
int index = (int)wrenGetSlotDouble(vm, 2);
wrenGetListElement(vm, listSlot, index, 0);
}
static void set(WrenVM* vm)
{
wrenSetSlotNewList(vm, 0);
wrenEnsureSlots(vm, 2);
appendNumber(vm, 1.0);
appendNumber(vm, 2.0);
appendNumber(vm, 3.0);
appendNumber(vm, 4.0);
//list[2] = 33
wrenSetSlotDouble(vm, 1, 33);
wrenSetListElement(vm, 0, 2, 1);
//list[-1] = 44
wrenSetSlotDouble(vm, 1, 44);
wrenSetListElement(vm, 0, -1, 1);
}
WrenForeignMethodFn listsBindMethod(const char* signature)
{
if (strcmp(signature, "static Lists.newList()") == 0) return newList;
if (strcmp(signature, "static Lists.insert()") == 0) return insert;
if (strcmp(signature, "static Lists.set()") == 0) return set;
if (strcmp(signature, "static Lists.get(_,_)") == 0) return get;
return NULL;
}

View File

@ -1,6 +1,8 @@
class Lists {
foreign static newList()
foreign static insert()
foreign static set()
foreign static get(list, index)
}
var list = Lists.newList()
@ -8,3 +10,7 @@ System.print(list is List) // expect: true
System.print(list.count) // expect: 0
System.print(Lists.insert()) // expect: [4, 5, 6, 1, 2, 3, 9, 8, 7]
System.print(Lists.set()) // expect: [1, 2, 33, 44]
System.print(Lists.get([1,2,3,4], -2)) // expect: 3
System.print(Lists.get([1,2,3,4], 1)) // expect: 2