1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-15 23:35:34 +00:00

[spidermonkey] console.assert

This commit is contained in:
Witold Filipczyk 2024-04-11 17:27:15 +02:00
parent 7545b8093d
commit 4190613ed2
3 changed files with 47 additions and 7 deletions

View File

@ -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)
{

View File

@ -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

View File

@ -0,0 +1,4 @@
<script>
console.assert(0 % 2 === 0, "O is not even");
console.assert(1 % 2 === 0, "1 is not even");
</script>