From 74111beec39e349e6eaa374acb08ab86403478a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Tue, 29 Nov 2022 12:56:49 +0100 Subject: [PATCH 1/4] core: make TJS_EvalModule private --- src/private.h | 1 + src/tjs.h | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/private.h b/src/private.h index 5709d89f..f1a48e2d 100644 --- a/src/private.h +++ b/src/private.h @@ -106,5 +106,6 @@ void tjs__add_stdlib(JSContext *ctx); uv_loop_t *TJS_GetLoop(TJSRuntime *qrt); TJSRuntime *TJS_NewRuntimeWorker(void); TJSRuntime *TJS_NewRuntimeInternal(bool is_worker, TJSRunOptions *options); +JSValue TJS_EvalModule(JSContext *ctx, const char *filename, bool is_main); #endif diff --git a/src/tjs.h b/src/tjs.h index 50061ef3..833f7ffa 100644 --- a/src/tjs.h +++ b/src/tjs.h @@ -47,7 +47,6 @@ JSContext *TJS_GetJSContext(TJSRuntime *qrt); TJSRuntime *TJS_GetRuntime(JSContext *ctx); int TJS_Run(TJSRuntime *qrt); void TJS_Stop(TJSRuntime *qrt); -JSValue TJS_EvalModule(JSContext *ctx, const char *filename, bool is_main); int TJS_RunMain(TJSRuntime *qrt); #endif From 902db1dea87bccee69e466fbe6f93fbf3e267f05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Tue, 29 Nov 2022 13:59:55 +0100 Subject: [PATCH 2/4] main: remove unneeded async --- src/js/runMain.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/runMain.js b/src/js/runMain.js index f0a5cb00..25b05278 100644 --- a/src/js/runMain.js +++ b/src/js/runMain.js @@ -2,7 +2,7 @@ import { getopts } from '@tjs/std'; -(async () => { +(() => { const { evalFile, evalScript, From ed1457d6d4d5ef0e641fa6d8b6c5c89a9453fb9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Tue, 29 Nov 2022 14:15:40 +0100 Subject: [PATCH 3/4] core: simplify code for running the main entrypoint --- src/cli.c | 5 ----- src/tjs.h | 1 - src/vm.c | 19 ++++++++++--------- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/cli.c b/src/cli.c index be5a953c..5f8ca868 100644 --- a/src/cli.c +++ b/src/cli.c @@ -210,11 +210,6 @@ int main(int argc, char **argv) { qrt = TJS_NewRuntimeOptions(&runOptions); ctx = TJS_GetJSContext(qrt); - if (TJS_RunMain(qrt)) { - exit_code = EXIT_FAILURE; - goto exit; - } - exit_code = TJS_Run(qrt); exit: diff --git a/src/tjs.h b/src/tjs.h index 833f7ffa..41965c7f 100644 --- a/src/tjs.h +++ b/src/tjs.h @@ -47,6 +47,5 @@ JSContext *TJS_GetJSContext(TJSRuntime *qrt); TJSRuntime *TJS_GetRuntime(JSContext *ctx); int TJS_Run(TJSRuntime *qrt); void TJS_Stop(TJSRuntime *qrt); -int TJS_RunMain(TJSRuntime *qrt); #endif diff --git a/src/vm.c b/src/vm.c index ec2d5367..3293ae75 100644 --- a/src/vm.c +++ b/src/vm.c @@ -335,20 +335,28 @@ static void uv__check_cb(uv_check_t *handle) { /* main loop which calls the user JS callbacks */ int TJS_Run(TJSRuntime *qrt) { + int ret = 0; + CHECK_EQ(uv_prepare_start(&qrt->jobs.prepare, uv__prepare_cb), 0); uv_unref((uv_handle_t *) &qrt->jobs.prepare); CHECK_EQ(uv_check_start(&qrt->jobs.check, uv__check_cb), 0); uv_unref((uv_handle_t *) &qrt->jobs.check); /* Use the async handle to keep the worker alive even when there is nothing to do. */ - if (!qrt->is_worker) + if (!qrt->is_worker) { uv_unref((uv_handle_t *) &qrt->stop); + /* If we are running the main interpreter, run the entrypoint. */ + ret = tjs__eval_text(qrt->ctx, tjs__code_run_main_data, tjs__code_run_main_size, "runMain.js"); + } + + if (ret != 0) + return ret; + uv__maybe_idle(qrt); uv_run(&qrt->loop, UV_RUN_DEFAULT); - int ret = 0; JSValue exc = JS_GetException(qrt->ctx); if (!JS_IsNull(exc)) { tjs_dump_error1(qrt->ctx, exc); @@ -439,10 +447,3 @@ JSValue TJS_EvalModule(JSContext *ctx, const char *filename, bool is_main) { dbuf_free(&dbuf); return ret; } - -int TJS_RunMain(TJSRuntime *qrt) { - JSContext *ctx = qrt->ctx; - - // TODO: consider moving this to TJS_Run, and enqueue it as a job. - return tjs__eval_text(ctx, tjs__code_run_main_data, tjs__code_run_main_size, "runMain.js"); -}; From db2a50bfe85e0d252d3af9c9598b7ef9670ab796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Tue, 29 Nov 2022 15:10:26 +0100 Subject: [PATCH 4/4] test: fixup mem limit test --- tests/test-mem-limit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-mem-limit.js b/tests/test-mem-limit.js index e0999a56..fe18bb84 100644 --- a/tests/test-mem-limit.js +++ b/tests/test-mem-limit.js @@ -14,5 +14,5 @@ import assert from './assert.js'; const stderrStr = new TextDecoder().decode(buf.subarray(0, nread)); const status = await proc.wait(); assert.ok(stderrStr.match(/InternalError: out of memory/) !== null, 'gives memory error'); - assert.ok(status.exit_status === 1 && status.term_signal === null, 'script fails') + assert.ok(status.exit_status !== 0 && status.term_signal === null, 'script fails') })();