forked from Mirror/wren
value: Rename validateKeyTypeto wrenMapIsValidKey. (#965)
This commit is contained in:
@ -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.");
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
|
||||
Reference in New Issue
Block a user