Skip to content

Commit be06b3e

Browse files
author
Fabrice Bellard
committed
added JS_PrintValue() and use it in console.log(), print() and the REPL (bellard#256)
1 parent 30fe3de commit be06b3e

File tree

5 files changed

+690
-378
lines changed

5 files changed

+690
-378
lines changed

quickjs-libc.c

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,13 @@ static JSValue js_std_file_printf(JSContext *ctx, JSValueConst this_val,
10831083
return js_printf_internal(ctx, argc, argv, f);
10841084
}
10851085

1086+
static JSValue js_std_file_printObject(JSContext *ctx, JSValueConst this_val,
1087+
int argc, JSValueConst *argv)
1088+
{
1089+
JS_PrintValue(ctx, stdout, argv[0], NULL);
1090+
return JS_UNDEFINED;
1091+
}
1092+
10861093
static JSValue js_std_file_flush(JSContext *ctx, JSValueConst this_val,
10871094
int argc, JSValueConst *argv)
10881095
{
@@ -1540,6 +1547,7 @@ static const JSCFunctionListEntry js_std_funcs[] = {
15401547
JS_PROP_INT32_DEF("SEEK_CUR", SEEK_CUR, JS_PROP_CONFIGURABLE ),
15411548
JS_PROP_INT32_DEF("SEEK_END", SEEK_END, JS_PROP_CONFIGURABLE ),
15421549
JS_OBJECT_DEF("Error", js_std_error_props, countof(js_std_error_props), JS_PROP_CONFIGURABLE),
1550+
JS_CFUNC_DEF("__printObject", 1, js_std_file_printObject ),
15431551
};
15441552

15451553
static const JSCFunctionListEntry js_std_file_proto_funcs[] = {
@@ -3891,17 +3899,23 @@ static JSValue js_print(JSContext *ctx, JSValueConst this_val,
38913899
int argc, JSValueConst *argv)
38923900
{
38933901
int i;
3894-
const char *str;
3895-
size_t len;
3896-
3902+
JSValueConst v;
3903+
38973904
for(i = 0; i < argc; i++) {
38983905
if (i != 0)
38993906
putchar(' ');
3900-
str = JS_ToCStringLen(ctx, &len, argv[i]);
3901-
if (!str)
3902-
return JS_EXCEPTION;
3903-
fwrite(str, 1, len, stdout);
3904-
JS_FreeCString(ctx, str);
3907+
v = argv[i];
3908+
if (JS_IsString(v)) {
3909+
const char *str;
3910+
size_t len;
3911+
str = JS_ToCStringLen(ctx, &len, v);
3912+
if (!str)
3913+
return JS_EXCEPTION;
3914+
fwrite(str, 1, len, stdout);
3915+
JS_FreeCString(ctx, str);
3916+
} else {
3917+
JS_PrintValue(ctx, stdout, v, NULL);
3918+
}
39053919
}
39063920
putchar('\n');
39073921
return JS_UNDEFINED;
@@ -4012,33 +4026,10 @@ void js_std_free_handlers(JSRuntime *rt)
40124026
JS_SetRuntimeOpaque(rt, NULL); /* fail safe */
40134027
}
40144028

4015-
static void js_dump_obj(JSContext *ctx, FILE *f, JSValueConst val)
4016-
{
4017-
const char *str;
4018-
4019-
str = JS_ToCString(ctx, val);
4020-
if (str) {
4021-
fprintf(f, "%s\n", str);
4022-
JS_FreeCString(ctx, str);
4023-
} else {
4024-
fprintf(f, "[exception]\n");
4025-
}
4026-
}
4027-
40284029
static void js_std_dump_error1(JSContext *ctx, JSValueConst exception_val)
40294030
{
4030-
JSValue val;
4031-
BOOL is_error;
4032-
4033-
is_error = JS_IsError(ctx, exception_val);
4034-
js_dump_obj(ctx, stderr, exception_val);
4035-
if (is_error) {
4036-
val = JS_GetPropertyStr(ctx, exception_val, "stack");
4037-
if (!JS_IsUndefined(val)) {
4038-
js_dump_obj(ctx, stderr, val);
4039-
}
4040-
JS_FreeValue(ctx, val);
4041-
}
4031+
JS_PrintValue(ctx, stderr, exception_val, NULL);
4032+
fputc('\n', stderr);
40424033
}
40434034

40444035
void js_std_dump_error(JSContext *ctx)

0 commit comments

Comments
 (0)