Skip to content

Commit

Permalink
Simplify exiting interpreter with exception
Browse files Browse the repository at this point in the history
- Avoid keeping the exception object around
- Avoid passing the responsibility of freeing the exeption object to the
  caller
  • Loading branch information
saghul committed Jan 8, 2025
1 parent 6d64933 commit 97ea19d
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 67 deletions.
11 changes: 4 additions & 7 deletions gen/function_source.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
int main(int argc, char **argv)
{
int r;
JSValue ret;
JSRuntime *rt;
JSContext *ctx;
r = 0;
Expand All @@ -69,14 +68,12 @@ int main(int argc, char **argv)
ctx = JS_NewCustomContext(rt);
js_std_add_helpers(ctx, argc, argv);
js_std_eval_binary(ctx, qjsc_function_source, qjsc_function_source_size, 0);
ret = js_std_loop(ctx);
if (JS_IsException(ret)) {
js_std_dump_error1(ctx, ret);
r = 1;
r = js_std_loop(ctx);
if (r) {
js_std_dump_error(ctx);
}
JS_FreeValue(ctx, ret);
JS_FreeContext(ctx);
js_std_free_handlers(rt);
JS_FreeContext(ctx);
JS_FreeRuntime(rt);
return r;
}
11 changes: 4 additions & 7 deletions gen/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
int main(int argc, char **argv)
{
int r;
JSValue ret;
JSRuntime *rt;
JSContext *ctx;
r = 0;
Expand All @@ -42,14 +41,12 @@ int main(int argc, char **argv)
ctx = JS_NewCustomContext(rt);
js_std_add_helpers(ctx, argc, argv);
js_std_eval_binary(ctx, qjsc_hello, qjsc_hello_size, 0);
ret = js_std_loop(ctx);
if (JS_IsException(ret)) {
js_std_dump_error1(ctx, ret);
r = 1;
r = js_std_loop(ctx);
if (r) {
js_std_dump_error(ctx);
}
JS_FreeValue(ctx, ret);
JS_FreeContext(ctx);
js_std_free_handlers(rt);
JS_FreeContext(ctx);
JS_FreeRuntime(rt);
return r;
}
11 changes: 4 additions & 7 deletions gen/hello_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
int main(int argc, char **argv)
{
int r;
JSValue ret;
JSRuntime *rt;
JSContext *ctx;
r = 0;
Expand All @@ -96,14 +95,12 @@ int main(int argc, char **argv)
ctx = JS_NewCustomContext(rt);
js_std_add_helpers(ctx, argc, argv);
js_std_eval_binary(ctx, qjsc_hello_module, qjsc_hello_module_size, 0);
ret = js_std_loop(ctx);
if (JS_IsException(ret)) {
js_std_dump_error1(ctx, ret);
r = 1;
r = js_std_loop(ctx);
if (r) {
js_std_dump_error(ctx);
}
JS_FreeValue(ctx, ret);
JS_FreeContext(ctx);
js_std_free_handlers(rt);
JS_FreeContext(ctx);
JS_FreeRuntime(rt);
return r;
}
11 changes: 4 additions & 7 deletions gen/test_fib.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
int main(int argc, char **argv)
{
int r;
JSValue ret;
JSRuntime *rt;
JSContext *ctx;
r = 0;
Expand All @@ -70,14 +69,12 @@ int main(int argc, char **argv)
ctx = JS_NewCustomContext(rt);
js_std_add_helpers(ctx, argc, argv);
js_std_eval_binary(ctx, qjsc_test_fib, qjsc_test_fib_size, 0);
ret = js_std_loop(ctx);
if (JS_IsException(ret)) {
js_std_dump_error1(ctx, ret);
r = 1;
r = js_std_loop(ctx);
if (r) {
js_std_dump_error(ctx);
}
JS_FreeValue(ctx, ret);
JS_FreeContext(ctx);
js_std_free_handlers(rt);
JS_FreeContext(ctx);
JS_FreeRuntime(rt);
return r;
}
12 changes: 6 additions & 6 deletions qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ int main(int argc, char **argv)
JSContext *ctx;
JSValue ret = JS_UNDEFINED;
struct trace_malloc_data trace_data = { NULL };
int r = 0;
int optind = 1;
char *compile_file = NULL;
char *exe = NULL;
Expand Down Expand Up @@ -683,17 +684,16 @@ int main(int argc, char **argv)
}
if (standalone || compile_file) {
if (JS_IsException(ret)) {
ret = JS_GetException(ctx);
r = 1;
} else {
JS_FreeValue(ctx, ret);
ret = js_std_loop(ctx);
r = js_std_loop(ctx);
}
} else {
ret = js_std_loop(ctx);
r = js_std_loop(ctx);
}
if (!JS_IsUndefined(ret)) {
js_std_dump_error1(ctx, ret);
JS_FreeValue(ctx, ret);
if (r) {
js_std_dump_error(ctx);
goto fail;
}
}
Expand Down
11 changes: 4 additions & 7 deletions qjsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ static const char main_c_template1[] =
"int main(int argc, char **argv)\n"
"{\n"
" int r;\n"
" JSValue ret;\n"
" JSRuntime *rt;\n"
" JSContext *ctx;\n"
" r = 0;\n"
Expand All @@ -325,14 +324,12 @@ static const char main_c_template1[] =
;

static const char main_c_template2[] =
" ret = js_std_loop(ctx);\n"
" if (JS_IsException(ret)) {\n"
" js_std_dump_error1(ctx, ret);\n"
" r = 1;\n"
" r = js_std_loop(ctx);\n"
" if (r) {\n"
" js_std_dump_error(ctx);\n"
" }\n"
" JS_FreeValue(ctx, ret);\n"
" JS_FreeContext(ctx);\n"
" js_std_free_handlers(rt);\n"
" JS_FreeContext(ctx);\n"
" JS_FreeRuntime(rt);\n"
" return r;\n"
"}\n";
Expand Down
25 changes: 7 additions & 18 deletions quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ typedef struct JSThreadState {
struct list_head port_list; /* list of JSWorkerMessageHandler.link */
int eval_script_recurse; /* only used in the main thread */
int64_t next_timer_id; /* for setTimeout / setInterval */
JSValue exc; /* current exception from one of our handlers */
BOOL can_js_os_poll;
/* not used in the main thread */
JSWorkerMessagePipe *recv_pipe, *send_pipe;
Expand Down Expand Up @@ -2274,12 +2273,8 @@ static int call_handler(JSContext *ctx, JSValue func)
ret = JS_Call(ctx, func1, JS_UNDEFINED, 0, NULL);
JS_FreeValue(ctx, func1);
r = 0;
if (JS_IsException(ret)) {
JSRuntime *rt = JS_GetRuntime(ctx);
JSThreadState *ts = js_get_thread_state(rt);
ts->exc = JS_GetException(ctx);
if (JS_IsException(ret))
r = -1;
}
JS_FreeValue(ctx, ret);
return r;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -4178,23 +4172,20 @@ 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(;;) {
/* execute the pending jobs */
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;
}
}
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions quickjs-libc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions run-test262.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 97ea19d

Please sign in to comment.