Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle unprintable objects better in print() #834

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3984,12 +3984,20 @@ static JSValue js_print(JSContext *ctx, JSValue this_val,
DWORD mode;
#endif
const char *s;
JSValue v;
DynBuf b;
int i;

dbuf_init(&b);
for(i = 0; i < argc; i++) {
s = JS_ToCString(ctx, argv[i]);
v = argv[i];
s = JS_ToCString(ctx, v);
if (!s && JS_IsObject(v)) {
JS_FreeValue(ctx, JS_GetException(ctx));
v = JS_ToObjectString(ctx, v);
s = JS_ToCString(ctx, v);
JS_FreeValue(ctx, v);
}
if (s) {
dbuf_printf(&b, "%s%s", &" "[!i], s);
JS_FreeCString(ctx, s);
Expand Down
5 changes: 5 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -37079,6 +37079,11 @@ static JSValue js_object_toString(JSContext *ctx, JSValue this_val,
return JS_ConcatString3(ctx, "[object ", tag, "]");
}

JSValue JS_ToObjectString(JSContext *ctx, JSValue val)
{
return js_object_toString(ctx, val, 0, NULL);
}

static JSValue js_object_toLocaleString(JSContext *ctx, JSValue this_val,
int argc, JSValue *argv)
{
Expand Down
1 change: 1 addition & 0 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ JS_EXTERN JSValue JS_NewObjectClass(JSContext *ctx, int class_id);
JS_EXTERN JSValue JS_NewObjectProto(JSContext *ctx, JSValue proto);
JS_EXTERN JSValue JS_NewObject(JSContext *ctx);
JS_EXTERN JSValue JS_ToObject(JSContext *ctx, JSValue val);
JS_EXTERN JSValue JS_ToObjectString(JSContext *ctx, JSValue val);

JS_EXTERN bool JS_IsFunction(JSContext* ctx, JSValue val);
JS_EXTERN bool JS_IsConstructor(JSContext* ctx, JSValue val);
Expand Down
25 changes: 17 additions & 8 deletions run-test262.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,26 +463,35 @@ static JSValue js_print_262(JSContext *ctx, JSValue this_val,
int argc, JSValue *argv)
{
ThreadLocalStorage *tls = JS_GetRuntimeOpaque(JS_GetRuntime(ctx));
const char *s;
JSValue v;
int i;
const char *str;

for (i = 0; i < argc; i++) {
str = JS_ToCString(ctx, argv[i]);
if (!str)
v = argv[i];
s = JS_ToCString(ctx, v);
// same logic as js_print in quickjs-libc.c
if (local && !s && JS_IsObject(v)) {
JS_FreeValue(ctx, JS_GetException(ctx));
v = JS_ToObjectString(ctx, v);
s = JS_ToCString(ctx, v);
JS_FreeValue(ctx, v);
}
if (!s)
return JS_EXCEPTION;
if (!strcmp(str, "Test262:AsyncTestComplete")) {
if (!strcmp(s, "Test262:AsyncTestComplete")) {
tls->async_done++;
} else if (js__strstart(str, "Test262:AsyncTestFailure", NULL)) {
} else if (js__strstart(s, "Test262:AsyncTestFailure", NULL)) {
tls->async_done = 2; /* force an error */
}
if (outfile) {
if (i != 0)
fputc(' ', outfile);
fputs(str, outfile);
fputs(s, outfile);
}
if (verbose > 1)
printf("%s%s", &" "[i < 1], str);
JS_FreeCString(ctx, str);
printf("%s%s", &" "[i < 1], s);
JS_FreeCString(ctx, s);
}
if (outfile)
fputc('\n', outfile);
Expand Down
1 change: 1 addition & 0 deletions tests.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ verbose=yes
testdir=tests

[exclude]
tests/empty.js
tests/fixture_cyclic_import.js
tests/microbench.js
tests/test_worker_module.js
2 changes: 2 additions & 0 deletions tests/bug832.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const m = await import("./empty.js")
print(m) // should not throw
Empty file added tests/empty.js
Empty file.
Loading