From 5b8dc30bb86c01b76e2354b7a7caa43e5debe4d9 Mon Sep 17 00:00:00 2001 From: Inversion Date: Wed, 10 Feb 2021 21:48:41 -0800 Subject: [PATCH 1/5] Added error to the base library --- src/cbaselib.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/cbaselib.c b/src/cbaselib.c index 7776836..5a5e7d8 100644 --- a/src/cbaselib.c +++ b/src/cbaselib.c @@ -110,6 +110,22 @@ int cosmoB_loadstring(CState *state, int nargs, CValue *args) { return 2; // , or } +int cosmoB_error(CState *state, int nargs, CValue *args) { + if (nargs < 1) { + cosmoV_error(state, "error() expected 1 argument, got %d!", nargs); + return 0; + } + + if (!IS_STRING(args[0])) { + cosmoV_typeError(state, "error()", "", "%s", cosmoV_typeStr(args[0])); + return 0; + } + + cosmoV_error(state, "%s", cosmoO_readCString(cosmoV_readString(args[0]))); + + return 0; +} + void cosmoB_loadLibrary(CState *state) { const char *identifiers[] = { "print", @@ -118,7 +134,8 @@ void cosmoB_loadLibrary(CState *state) { "pcall", "tonumber", "tostring", - "loadstring" + "loadstring", + "error" }; CosmoCFunction baseLib[] = { @@ -128,7 +145,8 @@ void cosmoB_loadLibrary(CState *state) { cosmoB_pcall, cosmoB_tonumber, cosmoB_tostring, - cosmoB_loadstring + cosmoB_loadstring, + cosmoB_error }; int i; From 2e395065f8073ab40b9b219357d97483c6bb4877 Mon Sep 17 00:00:00 2001 From: Inversion Date: Wed, 10 Feb 2021 22:02:35 -0800 Subject: [PATCH 2/5] Add string.len to base library --- src/cbaselib.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/cbaselib.c b/src/cbaselib.c index 5a5e7d8..c834ccb 100644 --- a/src/cbaselib.c +++ b/src/cbaselib.c @@ -510,13 +510,25 @@ int cosmoB_sChar(CState *state, int nargs, CValue *args) { return 1; } +int cosmoB_sLen(CState *state, int nargs, CValue *args) { + if (nargs < 1) { + cosmoV_error(state, "string.len() expected 1 argument, got %d", nargs); + return 0; + } + + cosmoV_pushNumber(state, strlen(cosmoO_readCString(cosmoV_readString(args[0])))); + + return 1; +} + void cosmoB_loadStrLib(CState *state) { const char *identifiers[] = { "sub", "find", "split", "byte", - "char" + "char", + "len" }; CosmoCFunction strLib[] = { @@ -524,7 +536,8 @@ void cosmoB_loadStrLib(CState *state) { cosmoB_sFind, cosmoB_sSplit, cosmoB_sByte, - cosmoB_sChar + cosmoB_sChar, + cosmoB_sLen }; // make string library object From 7caa696aa24e823c0f92897723a5ccbf7c142168 Mon Sep 17 00:00:00 2001 From: Inversion Date: Wed, 10 Feb 2021 22:14:31 -0800 Subject: [PATCH 3/5] Added cosmoV_readCString for convenience --- src/cobj.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cobj.h b/src/cobj.h index 6a77a6f..b5fde5f 100644 --- a/src/cobj.h +++ b/src/cobj.h @@ -120,6 +120,7 @@ typedef struct CObjUpval { #define IS_CLOSURE(x) isObjType(x, COBJ_CLOSURE) #define cosmoV_readString(x) ((CObjString*)cosmoV_readRef(x)) +#define cosmoV_readCString(x) (((CObjString*)cosmoV_readRef(x))->str) #define cosmoV_readObject(x) ((CObjObject*)cosmoV_readRef(x)) #define cosmoV_readTable(x) ((CObjTable*)cosmoV_readRef(x)) #define cosmoV_readFunction(x) ((CObjFunction*)cosmoV_readRef(x)) From 27aedd2969cf6f8ae748b0fc400561ce0823f8b5 Mon Sep 17 00:00:00 2001 From: Inversion Date: Wed, 10 Feb 2021 22:14:58 -0800 Subject: [PATCH 4/5] Updated baselib in accordance with cosmoV_readCString --- src/cbaselib.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cbaselib.c b/src/cbaselib.c index c834ccb..8bf93ba 100644 --- a/src/cbaselib.c +++ b/src/cbaselib.c @@ -121,7 +121,7 @@ int cosmoB_error(CState *state, int nargs, CValue *args) { return 0; } - cosmoV_error(state, "%s", cosmoO_readCString(cosmoV_readString(args[0]))); + cosmoV_error(state, "%s", cosmoV_readCString(args[0])); return 0; } @@ -516,7 +516,7 @@ int cosmoB_sLen(CState *state, int nargs, CValue *args) { return 0; } - cosmoV_pushNumber(state, strlen(cosmoO_readCString(cosmoV_readString(args[0])))); + cosmoV_pushNumber(state, strlen(cosmoV_readCString(args[0]))); return 1; } From 44f1674b096a2e7f775084ec842136cf07e08328 Mon Sep 17 00:00:00 2001 From: Inversion Date: Wed, 10 Feb 2021 22:24:31 -0800 Subject: [PATCH 5/5] Added argument type check to string.len --- src/cbaselib.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/cbaselib.c b/src/cbaselib.c index 8bf93ba..0b6bc01 100644 --- a/src/cbaselib.c +++ b/src/cbaselib.c @@ -516,6 +516,11 @@ int cosmoB_sLen(CState *state, int nargs, CValue *args) { return 0; } + if (!IS_STRING(args[0])) { + cosmoV_typeError(state, "string.len", "", "%s", cosmoV_typeStr(args[0])); + return 0; + } + cosmoV_pushNumber(state, strlen(cosmoV_readCString(args[0]))); return 1;