diff --git a/src/core/stuff.c b/src/core/stuff.c index fa5cb4a8..f9e4fd47 100644 --- a/src/core/stuff.c +++ b/src/core/stuff.c @@ -7,6 +7,7 @@ bool please_tail_call_err = true; +void before_exit(void); bool inErr; NORETURN NOINLINE void err(char* s) { if (inErr) { @@ -21,6 +22,7 @@ NORETURN NOINLINE void err(char* s) { vm_pstLive(); fflush(stderr); fflush(stdout); print_vmStack(); fflush(stderr); fputs("CBQN interpreter entered unexpected state, exiting.\n", stderr); + before_exit(); #ifdef DEBUG __builtin_trap(); #endif diff --git a/src/load.c b/src/load.c index d6cad43a..843103df 100644 --- a/src/load.c +++ b/src/load.c @@ -513,6 +513,7 @@ B bqn_execFile(B path, B args) { // consumes both return bqn_exec(path_chars(inc(path)), path, args); } +void before_exit(void); void bqn_exit(i32 code) { #ifdef DUMP_ON_EXIT cbqn_heapDump(); @@ -520,6 +521,7 @@ void bqn_exit(i32 code) { rtWrap_print(); CTR_FOR(CTR_PRINT) print_allocStats(); + before_exit(); exit(code); } diff --git a/src/main.c b/src/main.c index ef28e3fb..ef330544 100644 --- a/src/main.c +++ b/src/main.c @@ -226,6 +226,16 @@ static NOINLINE void repl_init() { completion_impl(inp, res, true, dist); } + static Replxx* global_replxx; + static char* global_histfile; + void before_exit() { + if (global_replxx!=NULL && global_histfile!=NULL) { + replxx_history_save(global_replxx, global_histfile); + replxx_end(global_replxx); + global_replxx = NULL; + } + } + static void cbqn_init_replxx() { build_theme(theme0_built, theme0); sysvalNames = emptyHVec(); @@ -239,6 +249,8 @@ static NOINLINE void repl_init() { gc_add(sysvalNames); gc_add(sysvalNamesNorm); } +#else + void before_exit() { } #endif @@ -656,6 +668,8 @@ int main(int argc, char* argv[]) { dec(p2); gc_add(tag(TOBJ(histfile), OBJ_TAG)); replxx_history_load(replxx, histfile); + global_replxx = replxx; + global_histfile = histfile; replxx_set_ignore_case(replxx, true); replxx_set_highlighter_callback(replxx, highlighter_replxx, NULL); @@ -668,8 +682,8 @@ int main(int argc, char* argv[]) { if (errno==0) printf("\n"); break; } - cbqn_runLine((char*)ln, strlen(ln)); replxx_history_add(replxx, ln); + cbqn_runLine((char*)ln, strlen(ln)); replxx_history_save(replxx, histfile); } } diff --git a/src/vm.c b/src/vm.c index df44c207..2a7b134f 100644 --- a/src/vm.c +++ b/src/vm.c @@ -1594,6 +1594,7 @@ NOINLINE void printErrMsg(B msg) { } +void before_exit(void); NOINLINE NORETURN void throwImpl(bool rethrow) { // printf("gStack %p-%p:\n", gStackStart, gStack); B* c = gStack; // while (c>gStackStart) { print(*--c); putchar('\n'); } printf("gStack printed\n"); @@ -1619,6 +1620,7 @@ NOINLINE NORETURN void throwImpl(bool rethrow) { Env* envEnd = envStart+envPrevHeight; unwindEnv(envStart-1); vm_pst(envCurr+1, envEnd); + before_exit(); #ifdef DEBUG __builtin_trap(); #else