From 97ea19dc81b574a9a12e1e778a35fc29b91564de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Mon, 6 Jan 2025 11:17:22 +0100 Subject: [PATCH] Simplify exiting interpreter with exception - Avoid keeping the exception object around - Avoid passing the responsibility of freeing the exeption object to the caller --- gen/function_source.c | Bin 2928 -> 2850 bytes gen/hello.c | Bin 1559 -> 1481 bytes gen/hello_module.c | Bin 4035 -> 3957 bytes gen/test_fib.c | Bin 2864 -> 2786 bytes qjs.c | 12 ++++++------ qjsc.c | 11 ++++------- quickjs-libc.c | 25 +++++++------------------ quickjs-libc.h | 3 +-- run-test262.c | 9 +++------ 9 files changed, 21 insertions(+), 39 deletions(-) diff --git a/gen/function_source.c b/gen/function_source.c index 489dfa28a0859d9ef0ff5eea9f43818caeca2b1c..00b9aab1da5217f11ea73ce4fb0766a88ca26fef 100644 GIT binary patch delta 73 zcmew$wn%J)9Oq_T&RoXHbGW2ciZm6fxfFmPt2n;6BqctjG`Ao=wWuh+NF%wVLem;3 YS36msTaDW}Kd&UUq68#5*^^rx0AC>&Q~&?~ delta 165 zcmZ1^_Caie94BX3VoqtQLQ!hTW(m$>MyAw~$=zJi^0da RCtqV#n|zX0VDeX1B>*D#7G(ec delta 138 zcmX@fJ)LL6O=ixp#GKMpg`(7w&6kTcd^P&E@4%kypB~G09_(0aR2}S diff --git a/gen/hello_module.c b/gen/hello_module.c index b6f1470bbc1ff69e6de4223951f141521d695f07..31987a576471e20f3c3582d47055b65b866376c5 100644 GIT binary patch delta 45 zcmX>s|5a{7Iq&8+-gL&v@A;$|izdtROLA!>msDt4b15j)PHy5?n_S8-FnJ-r5&&pR B4p0C9 delta 165 zcmew=cUXQyIWK2eVoqtQLQ!hT=0e_FMyAw~$@loA>%D^GJ&Ro{l2Z#xGV}8^fYO?p z3e{W+K#)}&UtE$BpHiAz5T9C9lwV}1kz7)t12RI>8myv7!B)W##HLJ$YEe;skw$V!g{C!7 Yu6FW!E;VlF{JfIXiV~3MWKM2%0K?iD9smFU delta 165 zcmaDPxexc = JS_GetException(ctx); + if (JS_IsException(ret)) r = -1; - } JS_FreeValue(ctx, ret); return r; } @@ -3543,10 +3538,10 @@ static void *worker_func(void *opaque) js_std_dump_error(ctx); JS_FreeValue(ctx, val); - JS_FreeValue(ctx, js_std_loop(ctx)); + js_std_loop(ctx); - JS_FreeContext(ctx); js_std_free_handlers(rt); + JS_FreeContext(ctx); JS_FreeRuntime(rt); return NULL; } @@ -4082,7 +4077,6 @@ void js_std_init_handlers(JSRuntime *rt) init_list_head(&ts->port_list); ts->next_timer_id = 1; - ts->exc = JS_UNDEFINED; js_set_thread_state(rt, ts); JS_AddRuntimeFinalizer(rt, js_std_finalize, ts); @@ -4140,7 +4134,7 @@ static void js_dump_obj(JSContext *ctx, FILE *f, JSValue val) } } -void js_std_dump_error1(JSContext *ctx, JSValue exception_val) +static void js_std_dump_error1(JSContext *ctx, JSValue exception_val) { JSValue val; BOOL is_error; @@ -4178,12 +4172,11 @@ void js_std_promise_rejection_tracker(JSContext *ctx, JSValue promise, } /* main loop which calls the user JS callbacks */ -JSValue js_std_loop(JSContext *ctx) +int js_std_loop(JSContext *ctx) { JSRuntime *rt = JS_GetRuntime(ctx); JSThreadState *ts = js_get_thread_state(rt); JSContext *ctx1; - JSValue ret; int err; for(;;) { @@ -4191,10 +4184,8 @@ JSValue js_std_loop(JSContext *ctx) for(;;) { err = JS_ExecutePendingJob(JS_GetRuntime(ctx), &ctx1); if (err <= 0) { - if (err < 0) { - ts->exc = JS_GetException(ctx1); + if (err < 0) goto done; - } break; } } @@ -4203,9 +4194,7 @@ JSValue js_std_loop(JSContext *ctx) break; } done: - ret = ts->exc; - ts->exc = JS_UNDEFINED; - return ret; + return JS_HasException(ctx); } /* Wait for a promise and execute pending jobs while waiting for diff --git a/quickjs-libc.h b/quickjs-libc.h index f80cf71..19fad58 100644 --- a/quickjs-libc.h +++ b/quickjs-libc.h @@ -37,12 +37,11 @@ JSModuleDef *js_init_module_std(JSContext *ctx, const char *module_name); JSModuleDef *js_init_module_os(JSContext *ctx, const char *module_name); JSModuleDef *js_init_module_bjson(JSContext *ctx, const char *module_name); void js_std_add_helpers(JSContext *ctx, int argc, char **argv); -JSValue js_std_loop(JSContext *ctx); +int js_std_loop(JSContext *ctx); JSValue js_std_await(JSContext *ctx, JSValue obj); void js_std_init_handlers(JSRuntime *rt); void js_std_free_handlers(JSRuntime *rt); void js_std_dump_error(JSContext *ctx); -void js_std_dump_error1(JSContext *ctx, JSValue exception_val); uint8_t *js_load_file(JSContext *ctx, size_t *pbuf_len, const char *filename); int js_module_set_import_meta(JSContext *ctx, JSValue func_val, JS_BOOL use_realpath, JS_BOOL is_main); diff --git a/run-test262.c b/run-test262.c index 8261abb..d721d12 100644 --- a/run-test262.c +++ b/run-test262.c @@ -1555,12 +1555,9 @@ static int eval_buf(JSContext *ctx, const char *buf, size_t buf_len, } if (local) { - JSValue val = js_std_loop(ctx); - if (JS_IsException(val)) { - js_std_dump_error1(ctx, val); - ret = -1; - } - JS_FreeValue(ctx, val); + ret = js_std_loop(ctx); + if (ret) + js_std_dump_error(ctx); } JS_FreeCString(ctx, error_name);