Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify run #330

Merged
merged 4 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/js/runMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { getopts } from '@tjs/std';

(async () => {
(() => {
const {
evalFile,
evalScript,
Expand Down
1 change: 1 addition & 0 deletions src/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions src/tjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,5 @@ 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
19 changes: 10 additions & 9 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
};
2 changes: 1 addition & 1 deletion tests/test-mem-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})();