forked from Mirror/wren
Tweak list C API:
- Rename "size" -> "count" to be consistent with other usage. - Remove "Slot" from the function names, since that's implicit. - Make the getListElement() just move the element to another slot. This matches other APIs where we distinguish accessing a value and converting it to some specific type.
This commit is contained in:
@ -153,17 +153,15 @@ static void foreignClassAllocate(WrenVM* vm)
|
||||
wrenSetSlotNewForeign(vm, 0, 0, 4);
|
||||
}
|
||||
|
||||
static void getListSize(WrenVM* vm)
|
||||
static void getListCount(WrenVM* vm)
|
||||
{
|
||||
wrenSetSlotDouble(vm, 0, wrenGetSlotListSize(vm, 1));
|
||||
wrenSetSlotDouble(vm, 0, wrenGetListCount(vm, 1));
|
||||
}
|
||||
|
||||
static void getListValue(WrenVM* vm)
|
||||
static void getListElement(WrenVM* vm)
|
||||
{
|
||||
int index = (int)wrenGetSlotDouble(vm, 2);
|
||||
WrenValue* value = wrenGetSlotListValue(vm, 1, index);
|
||||
wrenSetSlotValue(vm, 0, value);
|
||||
wrenReleaseValue(vm, value);
|
||||
wrenGetListElement(vm, 1, index, 0);
|
||||
}
|
||||
|
||||
WrenForeignMethodFn slotsBindMethod(const char* signature)
|
||||
@ -174,8 +172,8 @@ WrenForeignMethodFn slotsBindMethod(const char* signature)
|
||||
if (strcmp(signature, "static Slots.slotTypes(_,_,_,_,_,_,_)") == 0) return slotTypes;
|
||||
if (strcmp(signature, "static Slots.ensure()") == 0) return ensure;
|
||||
if (strcmp(signature, "static Slots.ensureOutsideForeign()") == 0) return ensureOutsideForeign;
|
||||
if (strcmp(signature, "static Slots.getListSize(_)") == 0) return getListSize;
|
||||
if (strcmp(signature, "static Slots.getListValue(_,_)") == 0) return getListValue;
|
||||
if (strcmp(signature, "static Slots.getListCount(_)") == 0) return getListCount;
|
||||
if (strcmp(signature, "static Slots.getListElement(_,_)") == 0) return getListElement;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -5,8 +5,8 @@ class Slots {
|
||||
foreign static slotTypes(bool, foreignObj, list, nullObj, num, string, unknown)
|
||||
foreign static ensure()
|
||||
foreign static ensureOutsideForeign()
|
||||
foreign static getListSize(list)
|
||||
foreign static getListValue(list, index)
|
||||
foreign static getListCount(list)
|
||||
foreign static getListElement(list, index)
|
||||
}
|
||||
|
||||
foreign class ForeignType {
|
||||
@ -34,5 +34,6 @@ System.print(Slots.ensureOutsideForeign())
|
||||
// expect: 0 -> 20 (190)
|
||||
|
||||
var ducks = ["Huey", "Dewey", "Louie"]
|
||||
System.print(Slots.getListSize(ducks)) // expect: 3
|
||||
System.print(Slots.getListValue(ducks, 1)) // expect: Dewey
|
||||
System.print(Slots.getListCount(ducks)) // expect: 3
|
||||
System.print(Slots.getListElement(ducks, 0)) // expect: Huey
|
||||
System.print(Slots.getListElement(ducks, 1)) // expect: Dewey
|
||||
|
||||
Reference in New Issue
Block a user