Skip to content

Commit 97ea19d

Browse files
committed
Simplify exiting interpreter with exception
- Avoid keeping the exception object around - Avoid passing the responsibility of freeing the exeption object to the caller
1 parent 6d64933 commit 97ea19d

File tree

9 files changed

+37
-67
lines changed

9 files changed

+37
-67
lines changed

gen/function_source.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
5858
int main(int argc, char **argv)
5959
{
6060
int r;
61-
JSValue ret;
6261
JSRuntime *rt;
6362
JSContext *ctx;
6463
r = 0;
@@ -69,14 +68,12 @@ int main(int argc, char **argv)
6968
ctx = JS_NewCustomContext(rt);
7069
js_std_add_helpers(ctx, argc, argv);
7170
js_std_eval_binary(ctx, qjsc_function_source, qjsc_function_source_size, 0);
72-
ret = js_std_loop(ctx);
73-
if (JS_IsException(ret)) {
74-
js_std_dump_error1(ctx, ret);
75-
r = 1;
71+
r = js_std_loop(ctx);
72+
if (r) {
73+
js_std_dump_error(ctx);
7674
}
77-
JS_FreeValue(ctx, ret);
78-
JS_FreeContext(ctx);
7975
js_std_free_handlers(rt);
76+
JS_FreeContext(ctx);
8077
JS_FreeRuntime(rt);
8178
return r;
8279
}

gen/hello.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
3131
int main(int argc, char **argv)
3232
{
3333
int r;
34-
JSValue ret;
3534
JSRuntime *rt;
3635
JSContext *ctx;
3736
r = 0;
@@ -42,14 +41,12 @@ int main(int argc, char **argv)
4241
ctx = JS_NewCustomContext(rt);
4342
js_std_add_helpers(ctx, argc, argv);
4443
js_std_eval_binary(ctx, qjsc_hello, qjsc_hello_size, 0);
45-
ret = js_std_loop(ctx);
46-
if (JS_IsException(ret)) {
47-
js_std_dump_error1(ctx, ret);
48-
r = 1;
44+
r = js_std_loop(ctx);
45+
if (r) {
46+
js_std_dump_error(ctx);
4947
}
50-
JS_FreeValue(ctx, ret);
51-
JS_FreeContext(ctx);
5248
js_std_free_handlers(rt);
49+
JS_FreeContext(ctx);
5350
JS_FreeRuntime(rt);
5451
return r;
5552
}

gen/hello_module.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
8585
int main(int argc, char **argv)
8686
{
8787
int r;
88-
JSValue ret;
8988
JSRuntime *rt;
9089
JSContext *ctx;
9190
r = 0;
@@ -96,14 +95,12 @@ int main(int argc, char **argv)
9695
ctx = JS_NewCustomContext(rt);
9796
js_std_add_helpers(ctx, argc, argv);
9897
js_std_eval_binary(ctx, qjsc_hello_module, qjsc_hello_module_size, 0);
99-
ret = js_std_loop(ctx);
100-
if (JS_IsException(ret)) {
101-
js_std_dump_error1(ctx, ret);
102-
r = 1;
98+
r = js_std_loop(ctx);
99+
if (r) {
100+
js_std_dump_error(ctx);
103101
}
104-
JS_FreeValue(ctx, ret);
105-
JS_FreeContext(ctx);
106102
js_std_free_handlers(rt);
103+
JS_FreeContext(ctx);
107104
JS_FreeRuntime(rt);
108105
return r;
109106
}

gen/test_fib.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
5959
int main(int argc, char **argv)
6060
{
6161
int r;
62-
JSValue ret;
6362
JSRuntime *rt;
6463
JSContext *ctx;
6564
r = 0;
@@ -70,14 +69,12 @@ int main(int argc, char **argv)
7069
ctx = JS_NewCustomContext(rt);
7170
js_std_add_helpers(ctx, argc, argv);
7271
js_std_eval_binary(ctx, qjsc_test_fib, qjsc_test_fib_size, 0);
73-
ret = js_std_loop(ctx);
74-
if (JS_IsException(ret)) {
75-
js_std_dump_error1(ctx, ret);
76-
r = 1;
72+
r = js_std_loop(ctx);
73+
if (r) {
74+
js_std_dump_error(ctx);
7775
}
78-
JS_FreeValue(ctx, ret);
79-
JS_FreeContext(ctx);
8076
js_std_free_handlers(rt);
77+
JS_FreeContext(ctx);
8178
JS_FreeRuntime(rt);
8279
return r;
8380
}

qjs.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ int main(int argc, char **argv)
399399
JSContext *ctx;
400400
JSValue ret = JS_UNDEFINED;
401401
struct trace_malloc_data trace_data = { NULL };
402+
int r = 0;
402403
int optind = 1;
403404
char *compile_file = NULL;
404405
char *exe = NULL;
@@ -683,17 +684,16 @@ int main(int argc, char **argv)
683684
}
684685
if (standalone || compile_file) {
685686
if (JS_IsException(ret)) {
686-
ret = JS_GetException(ctx);
687+
r = 1;
687688
} else {
688689
JS_FreeValue(ctx, ret);
689-
ret = js_std_loop(ctx);
690+
r = js_std_loop(ctx);
690691
}
691692
} else {
692-
ret = js_std_loop(ctx);
693+
r = js_std_loop(ctx);
693694
}
694-
if (!JS_IsUndefined(ret)) {
695-
js_std_dump_error1(ctx, ret);
696-
JS_FreeValue(ctx, ret);
695+
if (r) {
696+
js_std_dump_error(ctx);
697697
goto fail;
698698
}
699699
}

qjsc.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,6 @@ static const char main_c_template1[] =
315315
"int main(int argc, char **argv)\n"
316316
"{\n"
317317
" int r;\n"
318-
" JSValue ret;\n"
319318
" JSRuntime *rt;\n"
320319
" JSContext *ctx;\n"
321320
" r = 0;\n"
@@ -325,14 +324,12 @@ static const char main_c_template1[] =
325324
;
326325

327326
static const char main_c_template2[] =
328-
" ret = js_std_loop(ctx);\n"
329-
" if (JS_IsException(ret)) {\n"
330-
" js_std_dump_error1(ctx, ret);\n"
331-
" r = 1;\n"
327+
" r = js_std_loop(ctx);\n"
328+
" if (r) {\n"
329+
" js_std_dump_error(ctx);\n"
332330
" }\n"
333-
" JS_FreeValue(ctx, ret);\n"
334-
" JS_FreeContext(ctx);\n"
335331
" js_std_free_handlers(rt);\n"
332+
" JS_FreeContext(ctx);\n"
336333
" JS_FreeRuntime(rt);\n"
337334
" return r;\n"
338335
"}\n";

quickjs-libc.c

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ typedef struct JSThreadState {
161161
struct list_head port_list; /* list of JSWorkerMessageHandler.link */
162162
int eval_script_recurse; /* only used in the main thread */
163163
int64_t next_timer_id; /* for setTimeout / setInterval */
164-
JSValue exc; /* current exception from one of our handlers */
165164
BOOL can_js_os_poll;
166165
/* not used in the main thread */
167166
JSWorkerMessagePipe *recv_pipe, *send_pipe;
@@ -2274,12 +2273,8 @@ static int call_handler(JSContext *ctx, JSValue func)
22742273
ret = JS_Call(ctx, func1, JS_UNDEFINED, 0, NULL);
22752274
JS_FreeValue(ctx, func1);
22762275
r = 0;
2277-
if (JS_IsException(ret)) {
2278-
JSRuntime *rt = JS_GetRuntime(ctx);
2279-
JSThreadState *ts = js_get_thread_state(rt);
2280-
ts->exc = JS_GetException(ctx);
2276+
if (JS_IsException(ret))
22812277
r = -1;
2282-
}
22832278
JS_FreeValue(ctx, ret);
22842279
return r;
22852280
}
@@ -3543,10 +3538,10 @@ static void *worker_func(void *opaque)
35433538
js_std_dump_error(ctx);
35443539
JS_FreeValue(ctx, val);
35453540

3546-
JS_FreeValue(ctx, js_std_loop(ctx));
3541+
js_std_loop(ctx);
35473542

3548-
JS_FreeContext(ctx);
35493543
js_std_free_handlers(rt);
3544+
JS_FreeContext(ctx);
35503545
JS_FreeRuntime(rt);
35513546
return NULL;
35523547
}
@@ -4082,7 +4077,6 @@ void js_std_init_handlers(JSRuntime *rt)
40824077
init_list_head(&ts->port_list);
40834078

40844079
ts->next_timer_id = 1;
4085-
ts->exc = JS_UNDEFINED;
40864080

40874081
js_set_thread_state(rt, ts);
40884082
JS_AddRuntimeFinalizer(rt, js_std_finalize, ts);
@@ -4140,7 +4134,7 @@ static void js_dump_obj(JSContext *ctx, FILE *f, JSValue val)
41404134
}
41414135
}
41424136

4143-
void js_std_dump_error1(JSContext *ctx, JSValue exception_val)
4137+
static void js_std_dump_error1(JSContext *ctx, JSValue exception_val)
41444138
{
41454139
JSValue val;
41464140
BOOL is_error;
@@ -4178,23 +4172,20 @@ void js_std_promise_rejection_tracker(JSContext *ctx, JSValue promise,
41784172
}
41794173

41804174
/* main loop which calls the user JS callbacks */
4181-
JSValue js_std_loop(JSContext *ctx)
4175+
int js_std_loop(JSContext *ctx)
41824176
{
41834177
JSRuntime *rt = JS_GetRuntime(ctx);
41844178
JSThreadState *ts = js_get_thread_state(rt);
41854179
JSContext *ctx1;
4186-
JSValue ret;
41874180
int err;
41884181

41894182
for(;;) {
41904183
/* execute the pending jobs */
41914184
for(;;) {
41924185
err = JS_ExecutePendingJob(JS_GetRuntime(ctx), &ctx1);
41934186
if (err <= 0) {
4194-
if (err < 0) {
4195-
ts->exc = JS_GetException(ctx1);
4187+
if (err < 0)
41964188
goto done;
4197-
}
41984189
break;
41994190
}
42004191
}
@@ -4203,9 +4194,7 @@ JSValue js_std_loop(JSContext *ctx)
42034194
break;
42044195
}
42054196
done:
4206-
ret = ts->exc;
4207-
ts->exc = JS_UNDEFINED;
4208-
return ret;
4197+
return JS_HasException(ctx);
42094198
}
42104199

42114200
/* Wait for a promise and execute pending jobs while waiting for

quickjs-libc.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ JSModuleDef *js_init_module_std(JSContext *ctx, const char *module_name);
3737
JSModuleDef *js_init_module_os(JSContext *ctx, const char *module_name);
3838
JSModuleDef *js_init_module_bjson(JSContext *ctx, const char *module_name);
3939
void js_std_add_helpers(JSContext *ctx, int argc, char **argv);
40-
JSValue js_std_loop(JSContext *ctx);
40+
int js_std_loop(JSContext *ctx);
4141
JSValue js_std_await(JSContext *ctx, JSValue obj);
4242
void js_std_init_handlers(JSRuntime *rt);
4343
void js_std_free_handlers(JSRuntime *rt);
4444
void js_std_dump_error(JSContext *ctx);
45-
void js_std_dump_error1(JSContext *ctx, JSValue exception_val);
4645
uint8_t *js_load_file(JSContext *ctx, size_t *pbuf_len, const char *filename);
4746
int js_module_set_import_meta(JSContext *ctx, JSValue func_val,
4847
JS_BOOL use_realpath, JS_BOOL is_main);

run-test262.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,12 +1555,9 @@ static int eval_buf(JSContext *ctx, const char *buf, size_t buf_len,
15551555
}
15561556

15571557
if (local) {
1558-
JSValue val = js_std_loop(ctx);
1559-
if (JS_IsException(val)) {
1560-
js_std_dump_error1(ctx, val);
1561-
ret = -1;
1562-
}
1563-
JS_FreeValue(ctx, val);
1558+
ret = js_std_loop(ctx);
1559+
if (ret)
1560+
js_std_dump_error(ctx);
15641561
}
15651562

15661563
JS_FreeCString(ctx, error_name);

0 commit comments

Comments
 (0)