From 1307bdfb64438adfbbff1a7e4dddc344599dee0b Mon Sep 17 00:00:00 2001 From: Michel Hermier Date: Thu, 8 Apr 2021 18:10:55 +0200 Subject: [PATCH] value: Rename `validateKeyType`to `wrenMapIsValidKey`. (#965) --- src/vm/wren_primitive.c | 13 +------------ src/vm/wren_primitive.h | 5 ----- src/vm/wren_value.h | 15 +++++++++++++++ src/vm/wren_vm.c | 4 ++-- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/vm/wren_primitive.c b/src/vm/wren_primitive.c index 6ead2867..ed1882ea 100644 --- a/src/vm/wren_primitive.c +++ b/src/vm/wren_primitive.c @@ -45,20 +45,9 @@ bool validateInt(WrenVM* vm, Value arg, const char* argName) return validateIntValue(vm, AS_NUM(arg), argName); } -inline bool validateKeyType(WrenVM* vm, Value arg) -{ - if (IS_BOOL(arg) || IS_CLASS(arg) || IS_NULL(arg) || - IS_NUM(arg) || IS_RANGE(arg) || IS_STRING(arg)) - { - return true; - } - - return false; -} - bool validateKey(WrenVM* vm, Value arg) { - if (validateKeyType(vm, arg)) return true; + if (wrenMapIsValidKey(arg)) return true; RETURN_ERROR("Key must be a value type."); } diff --git a/src/vm/wren_primitive.h b/src/vm/wren_primitive.h index 4b7c9346..a6421c1d 100644 --- a/src/vm/wren_primitive.h +++ b/src/vm/wren_primitive.h @@ -79,11 +79,6 @@ bool validateIntValue(WrenVM* vm, double value, const char* argName); // reports an error and returns false. bool validateInt(WrenVM* vm, Value arg, const char* argName); -// Validates that [arg] is a valid object for use as a map key. Returns true if -// it is and returns false otherwise. Use validateKey usually, for a runtime error. -// This separation exists to aid the API in surfacing errors to the developer as well. -bool validateKeyType(WrenVM * vm, Value arg); - // Validates that [arg] is a valid object for use as a map key. Returns true if // it is. If not, reports an error and returns false. bool validateKey(WrenVM * vm, Value arg); diff --git a/src/vm/wren_value.h b/src/vm/wren_value.h index c1da47e8..98ec952a 100644 --- a/src/vm/wren_value.h +++ b/src/vm/wren_value.h @@ -686,6 +686,11 @@ int wrenListIndexOf(WrenVM* vm, ObjList* list, Value value); // Creates a new empty map. ObjMap* wrenNewMap(WrenVM* vm); +// Validates that [arg] is a valid object for use as a map key. Returns true if +// it is and returns false otherwise. Use validateKey usually, for a runtime error. +// This separation exists to aid the API in surfacing errors to the developer as well. +static inline bool wrenMapIsValidKey(Value arg); + // Looks up [key] in [map]. If found, returns the value. Otherwise, returns // `UNDEFINED_VAL`. Value wrenMapGet(ObjMap* map, Value key); @@ -869,4 +874,14 @@ static inline Value wrenNumToValue(double num) #endif } +static inline bool wrenMapIsValidKey(Value arg) +{ + return IS_BOOL(arg) + || IS_CLASS(arg) + || IS_NULL(arg) + || IS_NUM(arg) + || IS_RANGE(arg) + || IS_STRING(arg); +} + #endif diff --git a/src/vm/wren_vm.c b/src/vm/wren_vm.c index c1f7ce8f..13b7233b 100644 --- a/src/vm/wren_vm.c +++ b/src/vm/wren_vm.c @@ -1826,7 +1826,7 @@ bool wrenGetMapContainsKey(WrenVM* vm, int mapSlot, int keySlot) ASSERT(IS_MAP(vm->apiStack[mapSlot]), "Slot must hold a map."); Value key = vm->apiStack[keySlot]; - ASSERT(validateKeyType(vm, key), "Key must be a value type"); + ASSERT(wrenMapIsValidKey(key), "Key must be a value type"); if (!validateKey(vm, key)) return false; ObjMap* map = AS_MAP(vm->apiStack[mapSlot]); @@ -1859,7 +1859,7 @@ void wrenSetMapValue(WrenVM* vm, int mapSlot, int keySlot, int valueSlot) ASSERT(IS_MAP(vm->apiStack[mapSlot]), "Must insert into a map."); Value key = vm->apiStack[keySlot]; - ASSERT(validateKeyType(vm, key), "Key must be a value type"); + ASSERT(wrenMapIsValidKey(key), "Key must be a value type"); if (!validateKey(vm, key)) { return;