From 9012f9231bcb2f51e9509321fd16d04ef5f6b455 Mon Sep 17 00:00:00 2001 From: CPunch Date: Sun, 27 Dec 2020 13:36:17 -0600 Subject: [PATCH] Added assert() to base library also fixed regression in call() --- src/cbaselib.c | 28 ++++++++++++++++++++++++++-- src/cbaselib.h | 2 +- src/cobj.h | 2 +- src/cosmo.h | 4 ++++ src/cvm.c | 2 +- 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/cbaselib.c b/src/cbaselib.c index b6734bf..b763892 100644 --- a/src/cbaselib.c +++ b/src/cbaselib.c @@ -5,9 +5,15 @@ #include "cmem.h" void cosmoB_loadLibrary(CState *state) { + // print cosmoV_pushString(state, "print"); cosmoV_pushCFunction(state, cosmoB_print); - cosmoV_register(state, 1); // sets "print" global to cosmoB_print + + // assert (for unit testing) + cosmoV_pushString(state, "assert"); + cosmoV_pushCFunction(state, cosmoB_assert); + + cosmoV_register(state, 2); } int cosmoB_print(CState *state, int nargs, CValue *args) { @@ -20,6 +26,24 @@ int cosmoB_print(CState *state, int nargs, CValue *args) { return 0; // print doesn't return any args } +int cosmoB_assert(CState *state, int nargs, CValue *args) { + if (nargs != 1) { + cosmoV_error(state, "assert() expected 1 argument, got %d!", nargs); + return 0; // nothing pushed onto the stack to return + } + + if (!IS_BOOLEAN(args[0])) { + cosmoV_error(state, "assert() expected , got %s!", cosmoV_typeStr(args[0])); + return 0; + } + + if (!cosmoV_readBoolean(args[0])) { // expression passed was false, error! + cosmoV_error(state, "assert() failed!"); + } // else do nothing :) + + return 0; +} + int cosmoB_dsetProto(CState *state, int nargs, CValue *args) { if (nargs == 2) { CObjObject *obj = cosmoV_readObject(args[0]); // object to set proto too @@ -35,7 +59,7 @@ int cosmoB_dsetProto(CState *state, int nargs, CValue *args) { int cosmoB_dgetProto(CState *state, int nargs, CValue *args) { if (nargs != 1) { - cosmoV_error(state, "Expected 1 parameter, got %d!", nargs); + cosmoV_error(state, "Expected 1 argument, got %d!", nargs); } cosmoV_pushValue(state, cosmoV_newObj(cosmoV_readObject(args[0])->proto)); // just return the proto diff --git a/src/cbaselib.h b/src/cbaselib.h index 9d9ef26..2b92971 100644 --- a/src/cbaselib.h +++ b/src/cbaselib.h @@ -7,6 +7,6 @@ COSMO_API void cosmoB_loadLibrary(CState *state); COSMO_API void cosmoB_loadDebug(CState *state); COSMO_API int cosmoB_print(CState *state, int nargs, CValue *args); -COSMO_API int cosmoB_foreach(CState *state, int nargs, CValue *args); +COSMO_API int cosmoB_assert(CState *state, int nargs, CValue *args); #endif \ No newline at end of file diff --git a/src/cobj.h b/src/cobj.h index 9e9a38a..e93e47e 100644 --- a/src/cobj.h +++ b/src/cobj.h @@ -64,7 +64,7 @@ typedef struct CObjFunction { CChunk chunk; int args; int upvals; - int variadic; + bool variadic; CObjString *name; CObjString *module; // name of the "module" } CObjFunction; diff --git a/src/cosmo.h b/src/cosmo.h index 7ee9b36..67b9185 100644 --- a/src/cosmo.h +++ b/src/cosmo.h @@ -7,8 +7,12 @@ #include #include +#include + //#define NAN_BOXXED +#define COSMOASSERT(x) assert(x) + // forward declare *most* stuff so our headers are cleaner typedef struct CState CState; typedef struct CChunk CChunk; diff --git a/src/cvm.c b/src/cvm.c index ba70e29..97c818c 100644 --- a/src/cvm.c +++ b/src/cvm.c @@ -173,7 +173,7 @@ bool call(CState *state, CObjClosure *closure, int args, int nresults, int offse state->top -= extraArgs; pushCallFrame(state, closure, func->args + 1); - } else if (args < func->args) { // too few args passed, obvious user error + } else if (args != func->args) { // mismatched args cosmoV_error(state, "Expected %d arguments for %s, got %d!", closure->function->args, closure->function->name == NULL ? UNNAMEDCHUNK : closure->function->name->str, args); return false; } else {