From 60a0353a27219cb499c20004a0e4bbb432e6c64f Mon Sep 17 00:00:00 2001 From: Bob Nystrom Date: Thu, 28 Jul 2016 08:06:36 -0700 Subject: [PATCH] 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. --- src/include/wren.h | 13 +++++++------ src/vm/wren_vm.c | 39 +++++++++++++++++++-------------------- test/api/slots.c | 14 ++++++-------- test/api/slots.wren | 9 +++++---- 4 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/include/wren.h b/src/include/wren.h index 9edc4a97..20c5e7f0 100644 --- a/src/include/wren.h +++ b/src/include/wren.h @@ -359,12 +359,6 @@ double wrenGetSlotDouble(WrenVM* vm, int slot); // foreign class. void* wrenGetSlotForeign(WrenVM* vm, int slot); -// Returns the size of the list stored in [slot]. -int wrenGetSlotListSize(WrenVM* vm, int slot); - -// Reads a value from the list in [slot]. -WrenValue* wrenGetSlotListValue(WrenVM* vm, int slot, int index); - // Reads a string from [slot]. // // The memory for the returned string is owned by Wren. You can inspect it @@ -431,6 +425,13 @@ void wrenSetSlotString(WrenVM* vm, int slot, const char* text); // This does not release the handle for the value. void wrenSetSlotHandle(WrenVM* vm, int slot, WrenHandle* handle); +// Returns the number of elements in the list stored in [slot]. +int wrenGetListCount(WrenVM* vm, int slot); + +// Reads element [index] from the list in [listSlot] and stores it in +// [elementSlot]. +void wrenGetListElement(WrenVM* vm, int listSlot, int index, int elementSlot); + // Takes the value stored at [elementSlot] and inserts it into the list stored // at [listSlot] at [index]. // diff --git a/src/vm/wren_vm.c b/src/vm/wren_vm.c index d86d811c..2a87dd73 100644 --- a/src/vm/wren_vm.c +++ b/src/vm/wren_vm.c @@ -1553,26 +1553,6 @@ void* wrenGetSlotForeign(WrenVM* vm, int slot) return AS_FOREIGN(vm->apiStack[slot])->data; } -int wrenGetSlotListSize(WrenVM* vm, int slot) -{ - validateApiSlot(vm, slot); - ASSERT(IS_LIST(vm->apiStack[slot]), - "Slot must hold a list instance."); - - ValueBuffer elements = AS_LIST(vm->apiStack[slot])->elements; - return elements.count; -} - -WrenValue* wrenGetSlotListValue(WrenVM* vm, int slot, int index) -{ - validateApiSlot(vm, slot); - ASSERT(IS_LIST(vm->apiStack[slot]), - "Slot must hold a list instance."); - - ValueBuffer elements = AS_LIST(vm->apiStack[slot])->elements; - return wrenCaptureValue(vm, elements.data[index]); -} - const char* wrenGetSlotString(WrenVM* vm, int slot) { validateApiSlot(vm, slot); @@ -1649,6 +1629,25 @@ void wrenSetSlotHandle(WrenVM* vm, int slot, WrenHandle* handle) setSlot(vm, slot, handle->value); } +int wrenGetListCount(WrenVM* vm, int slot) +{ + validateApiSlot(vm, slot); + ASSERT(IS_LIST(vm->apiStack[slot]), "Slot must hold a list."); + + ValueBuffer elements = AS_LIST(vm->apiStack[slot])->elements; + return elements.count; +} + +void wrenGetListElement(WrenVM* vm, int listSlot, int index, int elementSlot) +{ + validateApiSlot(vm, listSlot); + validateApiSlot(vm, elementSlot); + ASSERT(IS_LIST(vm->apiStack[listSlot]), "Slot must hold a list."); + + ValueBuffer elements = AS_LIST(vm->apiStack[listSlot])->elements; + vm->apiStack[elementSlot] = elements.data[index]; +} + void wrenInsertInList(WrenVM* vm, int listSlot, int index, int elementSlot) { validateApiSlot(vm, listSlot); diff --git a/test/api/slots.c b/test/api/slots.c index 9cca9629..da9cea68 100644 --- a/test/api/slots.c +++ b/test/api/slots.c @@ -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; } diff --git a/test/api/slots.wren b/test/api/slots.wren index 63ec2a4f..ef0f8d79 100644 --- a/test/api/slots.wren +++ b/test/api/slots.wren @@ -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