docs: add type-checking guards to foreign argument getters and remove wrenReturnNull

Add runtime type validation to wrenGetArgumentBool, wrenGetArgumentDouble, and
wrenGetArgumentString in src/wren_vm.c, replacing TODO comments with actual
checks that return safe defaults (false, 0.0, NULL) on type mismatch.

Remove the unused wrenReturnNull function entirely from the VM implementation.

Update include/wren.h documentation for argument accessor functions to clarify
indexing semantics (including receiver at index 0) and document the new
type-checking behavior.
This commit is contained in:
Bob Nystrom
2015-01-22 15:05:55 +00:00
parent 6bd9da3a77
commit 8d281434e4
2 changed files with 46 additions and 35 deletions
+8 -12
View File
@@ -1193,7 +1193,8 @@ bool wrenGetArgumentBool(WrenVM* vm, int index)
ASSERT(index >= 0, "index cannot be negative.");
ASSERT(index < vm->foreignCallNumArgs, "Not that many arguments.");
// TODO: Check actual value type first.
if (!IS_BOOL(*(vm->foreignCallSlot + index))) return false;
return AS_BOOL(*(vm->foreignCallSlot + index));
}
@@ -1203,7 +1204,8 @@ double wrenGetArgumentDouble(WrenVM* vm, int index)
ASSERT(index >= 0, "index cannot be negative.");
ASSERT(index < vm->foreignCallNumArgs, "Not that many arguments.");
// TODO: Check actual value type first.
if (!IS_NUM(*(vm->foreignCallSlot + index))) return 0.0;
return AS_NUM(*(vm->foreignCallSlot + index));
}
@@ -1213,7 +1215,8 @@ const char* wrenGetArgumentString(WrenVM* vm, int index)
ASSERT(index >= 0, "index cannot be negative.");
ASSERT(index < vm->foreignCallNumArgs, "Not that many arguments.");
// TODO: Check actual value type first.
if (!IS_STRING(*(vm->foreignCallSlot + index))) return NULL;
return AS_CSTRING(*(vm->foreignCallSlot + index));
}
@@ -1233,18 +1236,11 @@ void wrenReturnDouble(WrenVM* vm, double value)
vm->foreignCallSlot = NULL;
}
void wrenReturnNull(WrenVM* vm)
{
ASSERT(vm->foreignCallSlot != NULL, "Must be in foreign call.");
*vm->foreignCallSlot = NULL_VAL;
vm->foreignCallSlot = NULL;
}
void wrenReturnString(WrenVM* vm, const char* text, int length)
{
ASSERT(vm->foreignCallSlot != NULL, "Must be in foreign call.");
ASSERT(text != NULL, "String cannot be NULL.");
size_t size = length;
if (length == -1) size = strlen(text);