Exit qjs on unhandled promise rejections

Fixes: https://github.com/quickjs-ng/quickjs/issues/790
This commit is contained in:
Saúl Ibarra Corretgé 2025-01-06 11:34:37 +01:00
parent 84d11c4f69
commit 4b8057d512
2 changed files with 4 additions and 10 deletions

12
qjs.c
View file

@ -389,7 +389,6 @@ void help(void)
" --exe select the executable to use as the base, defaults to the current one\n" " --exe select the executable to use as the base, defaults to the current one\n"
" --memory-limit n limit the memory usage to 'n' Kbytes\n" " --memory-limit n limit the memory usage to 'n' Kbytes\n"
" --stack-size n limit the stack size to 'n' Kbytes\n" " --stack-size n limit the stack size to 'n' Kbytes\n"
" --unhandled-rejection dump unhandled promise rejections\n"
"-q --quit just instantiate the interpreter and quit\n", JS_GetVersion()); "-q --quit just instantiate the interpreter and quit\n", JS_GetVersion());
exit(1); exit(1);
} }
@ -414,7 +413,6 @@ int main(int argc, char **argv)
int empty_run = 0; int empty_run = 0;
int module = -1; int module = -1;
int load_std = 0; int load_std = 0;
int dump_unhandled_promise_rejection = 0;
char *include_list[32]; char *include_list[32];
int i, include_count = 0; int i, include_count = 0;
int64_t memory_limit = -1; int64_t memory_limit = -1;
@ -514,10 +512,6 @@ int main(int argc, char **argv)
load_std = 1; load_std = 1;
continue; continue;
} }
if (!strcmp(longopt, "unhandled-rejection")) {
dump_unhandled_promise_rejection = 1;
continue;
}
if (opt == 'q' || !strcmp(longopt, "quit")) { if (opt == 'q' || !strcmp(longopt, "quit")) {
empty_run++; empty_run++;
continue; continue;
@ -622,10 +616,8 @@ start:
/* loader for ES6 modules */ /* loader for ES6 modules */
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL); JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
if (dump_unhandled_promise_rejection) { /* exit on unhandled promise rejections */
JS_SetHostPromiseRejectionTracker(rt, js_std_promise_rejection_tracker, JS_SetHostPromiseRejectionTracker(rt, js_std_promise_rejection_tracker, NULL);
NULL);
}
if (!empty_run) { if (!empty_run) {
js_std_add_helpers(ctx, argc - optind, argv + optind); js_std_add_helpers(ctx, argc - optind, argv + optind);

View file

@ -4172,6 +4172,8 @@ void js_std_promise_rejection_tracker(JSContext *ctx, JSValue promise,
if (!is_handled) { if (!is_handled) {
fprintf(stderr, "Possibly unhandled promise rejection: "); fprintf(stderr, "Possibly unhandled promise rejection: ");
js_std_dump_error1(ctx, reason); js_std_dump_error1(ctx, reason);
fflush(stderr);
exit(1);
} }
} }