Skip to content

Commit

Permalink
Restore initial terminal state when exiting with exit() builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoNex committed Oct 26, 2023
1 parent 9745a92 commit 6dc1cf2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions internal/vm/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1210,3 +1210,7 @@ int vm_run(struct vm * restrict vm) {
TARGET_HALT:
return 0;
}

void set_exit() {
atexit(restore_term);
}
15 changes: 14 additions & 1 deletion internal/vm/vm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package vm

// #cgo CFLAGS: -Werror -Wall -g -Ofast -mtune=native -fopenmp
// #cgo CFLAGS: -Wall -g -Ofast -mtune=native -fopenmp
// #cgo LDFLAGS: -fopenmp
// #include <stdlib.h>
// #include <stdio.h>
Expand All @@ -25,6 +25,7 @@ import (
"github.com/NicoNex/tau/internal/compiler"
"github.com/NicoNex/tau/internal/obj"
"github.com/NicoNex/tau/internal/parser"
"golang.org/x/term"
)

type (
Expand All @@ -36,6 +37,7 @@ type (
var (
Consts []obj.Object
importTab = make(map[string]C.struct_object)
TermState *term.State
)

func NewState() State {
Expand Down Expand Up @@ -161,3 +163,14 @@ func vm_exec_load_module(vm *C.struct_vm, cpath *C.char) {
vm.stack[vm.sp] = mod
vm.sp++
}

//export restore_term
func restore_term() {
if TermState != nil {
term.Restore(int(os.Stdin.Fd()), TermState)
}
}

func init() {
C.set_exit()
}
1 change: 1 addition & 0 deletions internal/vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ void go_vm_errorf(struct vm * restrict vm, const char *fmt);
struct object vm_last_popped_stack_elem(struct vm * restrict vm);
void vm_dispose(struct vm *vm);
void state_dispose(struct state s);
void set_exit();

0 comments on commit 6dc1cf2

Please sign in to comment.