feat: add wrenGetSlotType API to query low-level type of object in a slot
Introduce a new public C API function wrenGetSlotType() that returns the underlying representation type (bool, num, foreign, list, null, string, or unknown) of the object stored in a given VM slot. This allows C extensions to inspect slot contents without needing to call individual type-checking functions or handle errors from mismatched accessors. The implementation in wren_vm.c uses the existing IS_* macros to determine the type and returns the corresponding WrenType enum value. A new WrenType enum is added to wren.h with WREN_TYPE_BOOL, WREN_TYPE_NUM, WREN_TYPE_FOREIGN, WREN_TYPE_LIST, WREN_TYPE_NULL, WREN_TYPE_STRING, and WREN_TYPE_UNKNOWN. Test coverage is added in test/api/slots.c with a slotTypes() foreign method that verifies all seven type values, and a ForeignType foreign class is introduced in slots.wren to provide a foreign object for testing WREN_TYPE_FOREIGN. The test harness in main.c is updated to support binding foreign class allocate methods via slotsBindClass().
This commit is contained in:
@@ -1512,6 +1512,20 @@ static void validateApiSlot(WrenVM* vm, int slot)
|
||||
ASSERT(slot < wrenGetSlotCount(vm), "Not that many slots.");
|
||||
}
|
||||
|
||||
// Gets the type of the object in [slot].
|
||||
WrenType wrenGetSlotType(WrenVM* vm, int slot)
|
||||
{
|
||||
validateApiSlot(vm, slot);
|
||||
if (IS_BOOL(vm->apiStack[slot])) return WREN_TYPE_BOOL;
|
||||
if (IS_NUM(vm->apiStack[slot])) return WREN_TYPE_NUM;
|
||||
if (IS_FOREIGN(vm->apiStack[slot])) return WREN_TYPE_FOREIGN;
|
||||
if (IS_LIST(vm->apiStack[slot])) return WREN_TYPE_LIST;
|
||||
if (IS_NULL(vm->apiStack[slot])) return WREN_TYPE_NULL;
|
||||
if (IS_STRING(vm->apiStack[slot])) return WREN_TYPE_STRING;
|
||||
|
||||
return WREN_TYPE_UNKNOWN;
|
||||
}
|
||||
|
||||
bool wrenGetSlotBool(WrenVM* vm, int slot)
|
||||
{
|
||||
validateApiSlot(vm, slot);
|
||||
|
||||
Reference in New Issue
Block a user