From 1200e2d512d06bba36ff7b55549778f18d8da3db Mon Sep 17 00:00:00 2001 From: Inversion Date: Tue, 16 Feb 2021 15:53:07 -0800 Subject: [PATCH 1/3] Add optional custom error message to assert --- src/cbaselib.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/cbaselib.c b/src/cbaselib.c index 198f1f7..2441d10 100644 --- a/src/cbaselib.c +++ b/src/cbaselib.c @@ -24,18 +24,23 @@ int cosmoB_print(CState *state, int nargs, CValue *args) { } int cosmoB_assert(CState *state, int nargs, CValue *args) { - if (nargs != 1) { - cosmoV_error(state, "assert() expected 1 argument, got %d!", nargs); + if (nargs < 1 || nargs > 2) { + cosmoV_error(state, "assert() expected 1 or 2 arguments, got %d!", nargs); return 0; // nothing pushed onto the stack to return } - if (!IS_BOOLEAN(args[0])) { - cosmoV_typeError(state, "assert()", "", "%s", cosmoV_typeStr(args[0])); + if (!IS_BOOLEAN(args[0]) || (nargs == 2 && !IS_STRING(args[1]))) { + cosmoV_typeError(state, "assert()", ", ", "%s, %s", cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1])); return 0; } if (!cosmoV_readBoolean(args[0])) { // expression passed was false, error! - cosmoV_error(state, "assert() failed!"); + if (nargs == 2) { + cosmoV_error(state, "%s", cosmoV_readCString(args[1])); + } + else { // optional custom error message + cosmoV_error(state, "%s", "assert() failed!"); + } } // else do nothing :) return 0; From 9e6c6038f1be47f8c0e30bed32695537eae322f5 Mon Sep 17 00:00:00 2001 From: Inversion Date: Tue, 16 Feb 2021 15:53:20 -0800 Subject: [PATCH 2/3] Update standard library documentation --- docs/stdlib.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/stdlib.md b/docs/stdlib.md index 4007c9c..38624ee 100644 --- a/docs/stdlib.md +++ b/docs/stdlib.md @@ -14,7 +14,7 @@ Includes misc. functions. The "junk drawer" of the standard library. Without the | tostring | `()` -> `` | Converts the datatype to a ``, if a `` is passed `__tostring` metamethod is invoked | `tostring(12)` -> `"12"` | | error | `()` | Throws an error with the passed `` | `error("error!")` | | pcall | `()` -> `, or ` | Tries a protected call on the passed function, if an error is thrown, `` will be false and the 2nd result will be the error message | `pcall(error("Hello world!"))` -> `false, "Hello world!"` | -| assert | `()` | If the passed `` is false, an error is thrown | `assert(1 == 1)` | +| assert | `(, )` | If the passed `` is false, an error is thrown, optionally uses custom error message | `assert(1 == 1, "Error Message!")` | | loadstring | `()` -> `, or ` | If the `` compiled successfully, 1st result will be true and the 2nd result will be the newly compiled function. If there was a compiler/lexer error, the 1st result will be false and the 2nd result will be the error | `loadstring("print(\"hi\")")()` | > -> means 'returns' From b30616bb3c30032794a9a6d6a18c42ef37f2d259 Mon Sep 17 00:00:00 2001 From: Inversion Date: Tue, 16 Feb 2021 16:07:10 -0800 Subject: [PATCH 3/3] Temporary fix for possible bug --- src/cbaselib.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cbaselib.c b/src/cbaselib.c index 2441d10..6d7e52d 100644 --- a/src/cbaselib.c +++ b/src/cbaselib.c @@ -30,7 +30,12 @@ int cosmoB_assert(CState *state, int nargs, CValue *args) { } if (!IS_BOOLEAN(args[0]) || (nargs == 2 && !IS_STRING(args[1]))) { - cosmoV_typeError(state, "assert()", ", ", "%s, %s", cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1])); + if (nargs == 2) { + cosmoV_typeError(state, "assert()", ", ", "%s, %s", cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1])); + } + else { + cosmoV_typeError(state, "assert()", "", "%s", cosmoV_typeStr(args[0])); + } return 0; }