1
0
forked from Mirror/wren

Add API to get the type of object in a slot.

This commit is contained in:
Bob Nystrom
2016-02-19 07:18:00 -08:00
parent fa0a507b48
commit ef3aa07d84
6 changed files with 79 additions and 2 deletions

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;
}

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;
}

View File

@ -1,3 +1,4 @@
#include "wren.h"
WrenForeignMethodFn slotsBindMethod(const char* signature);
void slotsBindClass(const char* className, WrenForeignClassMethods* methods);

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)