void wrenPrintValue(Value value) { #if WREN_NAN_TAGGING if (IS_NUM(value)) { printf("%.14g", AS_NUM(value)); } else if (IS_OBJ(value)) { printObject(AS_OBJ(value)); } else { switch (GET_TAG(value)) { case TAG_FALSE: printf("false"); break; case TAG_NAN: printf("NaN"); break; case TAG_NULL: printf("null"); break; case TAG_TRUE: printf("true"); break; } } #else switch (value.type) { case VAL_FALSE: printf("false"); break; case VAL_NULL: printf("null"); break; case VAL_NUM: printf("%.14g", AS_NUM(value)); break; case VAL_TRUE: printf("true"); break; case VAL_OBJ: { printObject(AS_OBJ(value)); } } #endif }
void wrenDumpValue(Value value) { #if WREN_NAN_TAGGING if (IS_NUM(value)) { printf("%.14g", AS_NUM(value)); } else if (IS_OBJ(value)) { dumpObject(AS_OBJ(value)); } else { switch (GET_TAG(value)) { case TAG_FALSE: printf("false"); break; case TAG_NAN: printf("NaN"); break; case TAG_NULL: printf("null"); break; case TAG_TRUE: printf("true"); break; case TAG_UNDEFINED: UNREACHABLE(); } } #else switch (value.type) { case VAL_FALSE: printf("false"); break; case VAL_NULL: printf("null"); break; case VAL_NUM: printf("%.14g", AS_NUM(value)); break; case VAL_TRUE: printf("true"); break; case VAL_OBJ: dumpObject(AS_OBJ(value)); break; case VAL_UNDEFINED: UNREACHABLE(); } #endif }
uint32_t validateIndex(WrenVM* vm, Value arg, uint32_t count, const char* argName) { if (!validateNum(vm, arg, argName)) return UINT32_MAX; return validateIndexValue(vm, count, AS_NUM(arg), argName); }
bool validateInt(WrenVM* vm, Value arg, const char* argName) { // Make sure it's a number first. if (!validateNum(vm, arg, argName)) return false; return validateIntValue(vm, AS_NUM(arg), argName); }
uint32_t hash_value(value_t val) { #if NANTAG if (IS_PTR(val)) { return hash_ptr(AS_PTR(val)); } else { value_conv_t data; data.bits = val; return hash_number(data.bits); } #else // ! NANTAG switch (val.type) { case V_NIL: return 0; case V_TRUE: return 1; case V_FALSE: return 2; case V_UNDEFINED: return 3; case V_NUM: return hash_number(AS_NUM(val)); case V_PTR: return hash_ptr(AS_PTR(val)); default: return 0; } #endif // NANTAG }
// Validates that the argument at [argIndex] is an integer within `[0, count)`. // Also allows negative indices which map backwards from the end. Returns the // valid positive index value. If invalid, reports an error and returns -1. static int validateIndex(WrenVM* vm, Value* args, int count, int argIndex, const char* argName) { if (!validateNum(vm, args, argIndex, argName)) return -1; return validateIndexValue(vm, args, count, AS_NUM(args[argIndex]), argName); }
// Validates that the given argument in [args] is an integer. Returns true if // it is. If not, reports an error and returns false. static bool validateInt(WrenVM* vm, Value* args, int index, const char* argName) { // Make sure it's a number first. if (!validateNum(vm, args, index, argName)) return false; return validateIntValue(vm, args, AS_NUM(args[index]), argName); }
double wrenGetArgumentDouble(WrenVM* vm, int index) { ASSERT(vm->foreignCallSlot != NULL, "Must be in foreign call."); ASSERT(index >= 0, "index cannot be negative."); ASSERT(index < vm->foreignCallNumArgs, "Not that many arguments."); // TODO: Check actual value type first. return AS_NUM(*(vm->foreignCallSlot + index)); }
#include <stdio.h> #include "cardinal_config.h" #include "cardinal_value.h" #include "cardinal_debug.h" #include "cardinal_native.h" /////////////////////////////////////////////////////////////////////////////////// //// FUNCTION /////////////////////////////////////////////////////////////////////////////////// DEF_NATIVE(fn_create) ObjModule* module = AS_MODULE(args[1]); ObjList* constants = AS_LIST(args[2]); int numUpvalues = (int) AS_NUM(args[3]); int arity = (int) AS_NUM(args[4]); uint8_t* bytecode = (uint8_t*) AS_POINTER(args[5]); int size = (int) AS_NUM(args[6]); ObjString* sourcePath = AS_STRING(args[7]); ObjString* name = AS_STRING(args[8]); ObjList* lineData = AS_LIST(args[9]); ObjList* localList = AS_LIST(args[10]); ObjList* linesList = AS_LIST(args[11]); int* linesArray = new int[size]; SymbolTable locals; SymbolTable lines; cardinalSymbolTableInit(vm, &locals); cardinalSymbolTableInit(vm, &lines);