mirror of
https://github.com/CPunch/Cosmo.git
synced 2024-12-22 22:40:03 +00:00
Added assert() to base library
also fixed regression in call()
This commit is contained in:
parent
e7212b939d
commit
9012f9231b
@ -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 <boolean>, 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
|
||||
|
@ -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
|
@ -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;
|
||||
|
@ -7,8 +7,12 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
//#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;
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user