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
+22 -1
View File
@@ -162,12 +162,30 @@ typedef struct
int heapGrowthPercent;
} WrenConfiguration;
typedef enum {
typedef enum
{
WREN_RESULT_SUCCESS,
WREN_RESULT_COMPILE_ERROR,
WREN_RESULT_RUNTIME_ERROR
} WrenInterpretResult;
// The type of an object stored in a slot.
//
// This is not necessarily the object's *class*, but instead its low level
// representation type.
typedef enum
{
WREN_TYPE_BOOL,
WREN_TYPE_NUM,
WREN_TYPE_FOREIGN,
WREN_TYPE_LIST,
WREN_TYPE_NULL,
WREN_TYPE_STRING,
// The object is of a type that isn't accessible by the C API.
WREN_TYPE_UNKNOWN
} WrenType;
// Initializes [configuration] with all of its default values.
//
// Call this before setting the particular fields you care about.
@@ -263,6 +281,9 @@ int wrenGetSlotCount(WrenVM* vm);
// It is an error to call this from a finalizer.
void wrenEnsureSlots(WrenVM* vm, int numSlots);
// Gets the type of the object in [slot].
WrenType wrenGetSlotType(WrenVM* vm, int slot);
// Reads a boolean value from [slot].
//
// It is an error to call this if the slot does not contain a boolean value.