feat: add wrenGetSlotListSize and wrenGetSlotListValue API functions for list introspection

Add two new C API functions to the Wren VM that allow foreign methods to query the size of a list stored in a slot and retrieve individual values from it by index. The implementation validates the slot holds a list, extracts the internal ValueBuffer, and returns the count or a captured WrenValue respectively. Corresponding test bindings (getListSize, getListValue) and Wren test cases are included to verify the new functionality.
This commit is contained in:
Bob Nystrom
2016-07-28 14:53:19 +00:00
5 changed files with 49 additions and 1 deletions
+20
View File
@@ -1553,6 +1553,26 @@ 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);