added JS_PrintValue() and use it in console.log(), print() and the REPL (#256)

This commit is contained in:
Fabrice Bellard
2025-04-30 13:40:15 +02:00
parent 30fe3de91d
commit be06b3e92b
5 changed files with 752 additions and 440 deletions

View File

@@ -376,22 +376,29 @@ static JSValue js_print(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int i;
const char *str;
JSValueConst v;
if (outfile) {
for (i = 0; i < argc; i++) {
if (i != 0)
fputc(' ', outfile);
str = JS_ToCString(ctx, argv[i]);
if (!str)
return JS_EXCEPTION;
if (!strcmp(str, "Test262:AsyncTestComplete")) {
async_done++;
} else if (strstart(str, "Test262:AsyncTestFailure", NULL)) {
async_done = 2; /* force an error */
v = argv[i];
if (JS_IsString(v)) {
const char *str;
size_t len;
str = JS_ToCStringLen(ctx, &len, v);
if (!str)
return JS_EXCEPTION;
if (!strcmp(str, "Test262:AsyncTestComplete")) {
async_done++;
} else if (strstart(str, "Test262:AsyncTestFailure", NULL)) {
async_done = 2; /* force an error */
}
fwrite(str, 1, len, outfile);
JS_FreeCString(ctx, str);
} else {
JS_PrintValue(ctx, outfile, v, NULL);
}
fputs(str, outfile);
JS_FreeCString(ctx, str);
}
fputc('\n', outfile);
}