From 4190613ed2266b2f1a5829f2baca0b27873ff6a5 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Thu, 11 Apr 2024 17:27:15 +0200 Subject: [PATCH] [spidermonkey] console.assert --- src/ecmascript/spidermonkey/console.cpp | 37 +++++++++++++++++++++++++ src/ecmascript/spidermonkey/util.h | 13 ++++----- test/ecmascript/console.assert.html | 4 +++ 3 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 test/ecmascript/console.assert.html diff --git a/src/ecmascript/spidermonkey/console.cpp b/src/ecmascript/spidermonkey/console.cpp index 5af2ded0..8d0e0e5c 100644 --- a/src/ecmascript/spidermonkey/console.cpp +++ b/src/ecmascript/spidermonkey/console.cpp @@ -53,15 +53,52 @@ const JSClass console_class = { &console_ops }; +static bool console_assert(JSContext *ctx, unsigned int argc, JS::Value *vp); static bool console_error(JSContext *ctx, unsigned int argc, JS::Value *vp); static bool console_log(JSContext *ctx, unsigned int argc, JS::Value *vp); const spidermonkeyFunctionSpec console_funcs[] = { + { "assert", console_assert, 2 }, { "log", console_log, 1 }, { "error", console_error, 1 }, { NULL } }; +static bool +console_assert(JSContext *ctx, unsigned int argc, JS::Value *vp) +{ + JS::CallArgs args = CallArgsFromVp(argc, vp); + args.rval().setUndefined(); + + if (argc < 1 || !get_opt_bool("ecmascript.enable_console_log", NULL)) { + return true; + } + bool res = jsval_to_boolean(ctx, args[0]); + + if (res) { + return true; + } + FILE *log = fopen(console_error_filename, "a"); + + if (!log) { + return true; + } + fprintf(log, "Assertion failed:"); + + for (int i = 1; i < argc; i++) { + char *val = jsval_to_string(ctx, args[i]); + + if (val) { + fprintf(log, " %s", val); + mem_free(val); + } + } + fprintf(log, "\n"); + fclose(log); + + return true; +} + static bool console_log_common(JSContext *ctx, unsigned int argc, JS::Value *vp, const char *log_filename) { diff --git a/src/ecmascript/spidermonkey/util.h b/src/ecmascript/spidermonkey/util.h index a2ebf574..b7360502 100644 --- a/src/ecmascript/spidermonkey/util.h +++ b/src/ecmascript/spidermonkey/util.h @@ -7,7 +7,7 @@ static void string_to_jsval(JSContext *ctx, JS::Value *vp, char *string); static void astring_to_jsval(JSContext *ctx, JS::Value *vp, char *string); -static int jsval_to_boolean(JSContext *ctx, JS::Value *vp); +static bool jsval_to_boolean(JSContext *ctx, JS::HandleValue hvp); /** Inline functions */ @@ -28,13 +28,12 @@ astring_to_jsval(JSContext *ctx, JS::Value *vp, char *string) mem_free_if(string); } -static inline int -jsval_to_boolean(JSContext *ctx, JS::Value *vp) -{ - JS::RootedValue r_vp(ctx, *vp); - return (int)r_vp.toBoolean(); -} +static inline bool +jsval_to_boolean(JSContext *ctx, JS::HandleValue hvp) +{ + return hvp.toBoolean(); +} /* Since SpiderMonkey 52 the Mutable Handle Object * is different for String and Number and must be diff --git a/test/ecmascript/console.assert.html b/test/ecmascript/console.assert.html new file mode 100644 index 00000000..5750944a --- /dev/null +++ b/test/ecmascript/console.assert.html @@ -0,0 +1,4 @@ +