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:
Bob Nystrom
2016-02-19 15:18:00 +00:00
parent 5d04ed2aed
commit fe3997f825
6 changed files with 79 additions and 2 deletions
+9 -1
View File
@@ -63,7 +63,15 @@ static WrenForeignClassMethods bindForeignClass(
if (strcmp(module, "main") != 0) return methods;
foreignClassBindClass(className, &methods);
if (methods.allocate != NULL) return methods;
slotsBindClass(className, &methods);
if (methods.allocate != NULL) return methods;
fprintf(stderr,
"Unknown foreign class '%s' for test '%s'\n", className, testName);
exit(1);
return methods;
}
+25
View File
@@ -74,6 +74,20 @@ static void setSlots(WrenVM* vm)
}
}
static void slotTypes(WrenVM* vm)
{
bool result =
wrenGetSlotType(vm, 1) == WREN_TYPE_BOOL &&
wrenGetSlotType(vm, 2) == WREN_TYPE_FOREIGN &&
wrenGetSlotType(vm, 3) == WREN_TYPE_LIST &&
wrenGetSlotType(vm, 4) == WREN_TYPE_NULL &&
wrenGetSlotType(vm, 5) == WREN_TYPE_NUM &&
wrenGetSlotType(vm, 6) == WREN_TYPE_STRING &&
wrenGetSlotType(vm, 7) == WREN_TYPE_UNKNOWN;
wrenSetSlotBool(vm, 0, result);
}
static void ensure(WrenVM* vm)
{
int before = wrenGetSlotCount(vm);
@@ -134,13 +148,24 @@ static void ensureOutsideForeign(WrenVM* vm)
wrenSetSlotString(vm, 0, result);
}
static void foreignClassAllocate(WrenVM* vm)
{
wrenSetSlotNewForeign(vm, 0, 0, 4);
}
WrenForeignMethodFn slotsBindMethod(const char* signature)
{
if (strcmp(signature, "static Slots.noSet") == 0) return noSet;
if (strcmp(signature, "static Slots.getSlots(_,_,_,_,_)") == 0) return getSlots;
if (strcmp(signature, "static Slots.setSlots(_,_,_,_)") == 0) return setSlots;
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;
return NULL;
}
void slotsBindClass(const char* className, WrenForeignClassMethods* methods)
{
methods->allocate = foreignClassAllocate;
}
+1
View File
@@ -1,3 +1,4 @@
#include "wren.h"
WrenForeignMethodFn slotsBindMethod(const char* signature);
void slotsBindClass(const char* className, WrenForeignClassMethods* methods);
+8
View File
@@ -2,10 +2,15 @@ class Slots {
foreign static noSet
foreign static getSlots(bool, num, string, bytes, value)
foreign static setSlots(a, b, c, d)
foreign static slotTypes(bool, foreignObj, list, nullObj, num, string, unknown)
foreign static ensure()
foreign static ensureOutsideForeign()
}
foreign class ForeignType {
construct new() {}
}
// If nothing is set in the return slot, it retains its previous value, the
// receiver.
System.print(Slots.noSet == Slots) // expect: true
@@ -17,6 +22,9 @@ System.print(Slots.getSlots(true, "by\0te", 12.34, "str", value) == value)
System.print(Slots.setSlots(value, 0, 0, 0) == value)
// expect: true
System.print(Slots.slotTypes(false, ForeignType.new(), [], null, 1.2, "str", 1..2))
// expect: true
System.print(Slots.ensure())
// expect: 1 -> 20 (190)