mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 22:28:45 +01:00
Add wrenSetListElement, correctly allow negative indices on wrenGetListElement
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user