feat: add wrenGetSlotListSize and wrenGetSlotListValue API for list element access
Implement two new C API functions to retrieve list metadata and values from Wren slots: wrenGetSlotListSize returns the element count of a list in a slot, and wrenGetSlotListValue returns a captured WrenValue pointer for a specific index. Add corresponding foreign method bindings in test/api/slots.c and test cases in test/api/slots.wren that verify size retrieval and indexed value access on a three-element list. Also add Damien Radtke to AUTHORS.
This commit is contained in:
+16
-1
@@ -153,6 +153,19 @@ static void foreignClassAllocate(WrenVM* vm)
|
||||
wrenSetSlotNewForeign(vm, 0, 0, 4);
|
||||
}
|
||||
|
||||
static void getListSize(WrenVM* vm)
|
||||
{
|
||||
wrenSetSlotDouble(vm, 0, wrenGetSlotListSize(vm, 1));
|
||||
}
|
||||
|
||||
static void getListValue(WrenVM* vm)
|
||||
{
|
||||
int index = (int)wrenGetSlotDouble(vm, 2);
|
||||
WrenValue* value = wrenGetSlotListValue(vm, 1, index);
|
||||
wrenSetSlotValue(vm, 0, value);
|
||||
wrenReleaseValue(vm, value);
|
||||
}
|
||||
|
||||
WrenForeignMethodFn slotsBindMethod(const char* signature)
|
||||
{
|
||||
if (strcmp(signature, "static Slots.noSet") == 0) return noSet;
|
||||
@@ -161,6 +174,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;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -168,4 +183,4 @@ WrenForeignMethodFn slotsBindMethod(const char* signature)
|
||||
void slotsBindClass(const char* className, WrenForeignClassMethods* methods)
|
||||
{
|
||||
methods->allocate = foreignClassAllocate;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +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 class ForeignType {
|
||||
@@ -30,3 +32,7 @@ System.print(Slots.ensure())
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user