From b59c060ccdb7be18714bdd06a06dba15d9cce5fd Mon Sep 17 00:00:00 2001 From: Michel Hermier Date: Mon, 15 Jun 2020 20:31:29 +0200 Subject: [PATCH] Small tweaks to error handling. (#762) * wren/primitive: Remove duplicated declaration introduced in 9f64c05fa. * wren/primitive: Allow RETURN_ERROR_FMT to have any number of arguments. * wren/vm: Remove extra validateApiSlot in wrenGetVariable. (The slot validation is guaranted by setSlot later in the function.) * wren/primitive: Use RETURN_ERROR_FMT in validateFn. --- src/vm/wren_primitive.c | 4 +--- src/vm/wren_primitive.h | 5 ++--- src/vm/wren_vm.c | 3 +-- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/vm/wren_primitive.c b/src/vm/wren_primitive.c index 17b045c1..3671e22f 100644 --- a/src/vm/wren_primitive.c +++ b/src/vm/wren_primitive.c @@ -23,9 +23,7 @@ static uint32_t validateIndexValue(WrenVM* vm, uint32_t count, double value, bool validateFn(WrenVM* vm, Value arg, const char* argName) { if (IS_CLOSURE(arg)) return true; - - vm->fiber->error = wrenStringFormat(vm, "$ must be a function.", argName); - return false; + RETURN_ERROR_FMT("$ must be a function.", argName); } bool validateNum(WrenVM* vm, Value arg, const char* argName) diff --git a/src/vm/wren_primitive.h b/src/vm/wren_primitive.h index 813300dc..d8cdbe98 100644 --- a/src/vm/wren_primitive.h +++ b/src/vm/wren_primitive.h @@ -43,17 +43,16 @@ return false; \ } while (false) -#define RETURN_ERROR_FMT(msg, arg) \ +#define RETURN_ERROR_FMT(...) \ do \ { \ - vm->fiber->error = wrenStringFormat(vm, msg, arg); \ + vm->fiber->error = wrenStringFormat(vm, __VA_ARGS__); \ return false; \ } while (false) // Validates that the given [arg] is a function. Returns true if it is. If not, // reports an error and returns false. bool validateFn(WrenVM* vm, Value arg, const char* argName); -bool validateFn(WrenVM* vm, Value arg, const char* argName); // Validates that the given [arg] is a Num. Returns true if it is. If not, // reports an error and returns false. diff --git a/src/vm/wren_vm.c b/src/vm/wren_vm.c index 194149c5..163b1ae7 100644 --- a/src/vm/wren_vm.c +++ b/src/vm/wren_vm.c @@ -1835,8 +1835,7 @@ void wrenGetVariable(WrenVM* vm, const char* module, const char* name, { ASSERT(module != NULL, "Module cannot be NULL."); ASSERT(name != NULL, "Variable name cannot be NULL."); - validateApiSlot(vm, slot); - + Value moduleName = wrenStringFormat(vm, "$", module); wrenPushRoot(vm, AS_OBJ(moduleName));