From 07584da73461b69043451ebe48fcac226202492a Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 24 Jan 2018 00:25:10 +0100 Subject: extract console, extract context --- cmd/eval/main.go | 5 ++++- cmd/j1e/main.go | 5 ++++- console.go | 53 ----------------------------------------------------- console/console.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ core.go | 5 ++--- 5 files changed, 63 insertions(+), 58 deletions(-) delete mode 100644 console.go create mode 100644 console/console.go diff --git a/cmd/eval/main.go b/cmd/eval/main.go index a5fa686..152824c 100644 --- a/cmd/eval/main.go +++ b/cmd/eval/main.go @@ -4,6 +4,7 @@ import ( "context" "dim13.org/j1" + "dim13.org/j1/console" ) func main() { @@ -11,5 +12,7 @@ func main() { if err := vm.LoadFile("testdata/j1e.bin"); err != nil { panic(err) } - vm.Run(context.Background()) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + vm.Run(ctx, cancel, console.New(ctx)) } diff --git a/cmd/j1e/main.go b/cmd/j1e/main.go index 057e106..8f31aeb 100644 --- a/cmd/j1e/main.go +++ b/cmd/j1e/main.go @@ -6,10 +6,13 @@ import ( "context" "dim13.org/j1" + "dim13.org/j1/console" ) func main() { vm := j1.New() vm.LoadBytes(J1eBin) - vm.Run(context.Background()) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + vm.Run(ctx, cancel, console.New(ctx)) } diff --git a/console.go b/console.go deleted file mode 100644 index e59a765..0000000 --- a/console.go +++ /dev/null @@ -1,53 +0,0 @@ -package j1 - -import ( - "context" - "fmt" - "io" - "os" -) - -type console struct { - r io.Reader - w io.Writer - ich, och chan uint16 -} - -func NewConsole(ctx context.Context) *console { - c := &console{ - r: os.Stdin, - w: os.Stdout, - ich: make(chan uint16, 1), - och: make(chan uint16, 1), - } - go c.read(ctx) - go c.write(ctx) - return c -} - -func (c *console) read(ctx context.Context) { - var v uint16 - for { - fmt.Fscanf(c.r, "%c", &v) - select { - case <-ctx.Done(): - return - case c.ich <- v: - } - } -} - -func (c *console) write(ctx context.Context) { - for { - select { - case <-ctx.Done(): - return - case v := <-c.och: - fmt.Fprintf(c.w, "%c", v) - } - } -} - -func (c *console) Read() uint16 { return <-c.ich } -func (c *console) Write(v uint16) { c.och <- v } -func (c *console) Len() uint16 { return uint16(len(c.ich)) } diff --git a/console/console.go b/console/console.go new file mode 100644 index 0000000..4fe9680 --- /dev/null +++ b/console/console.go @@ -0,0 +1,53 @@ +package console + +import ( + "context" + "fmt" + "io" + "os" +) + +type Console struct { + r io.Reader + w io.Writer + ich, och chan uint16 +} + +func New(ctx context.Context) *Console { + c := &Console{ + r: os.Stdin, + w: os.Stdout, + ich: make(chan uint16, 1), + och: make(chan uint16, 1), + } + go c.read(ctx) + go c.write(ctx) + return c +} + +func (c *Console) read(ctx context.Context) { + var v uint16 + for { + fmt.Fscanf(c.r, "%c", &v) + select { + case <-ctx.Done(): + return + case c.ich <- v: + } + } +} + +func (c *Console) write(ctx context.Context) { + for { + select { + case <-ctx.Done(): + return + case v := <-c.och: + fmt.Fprintf(c.w, "%c", v) + } + } +} + +func (c *Console) Read() uint16 { return <-c.ich } +func (c *Console) Write(v uint16) { c.och <- v } +func (c *Console) Len() uint16 { return uint16(len(c.ich)) } diff --git a/core.go b/core.go index c3108b6..74355e5 100644 --- a/core.go +++ b/core.go @@ -55,10 +55,9 @@ func (c *Core) LoadFile(fname string) error { } // Run evaluates content of memory -func (c *Core) Run(ctx context.Context) { - ctx, cancel := context.WithCancel(ctx) - c.tty = NewConsole(ctx) +func (c *Core) Run(ctx context.Context, cancel context.CancelFunc, con Console) { c.stop = cancel + c.tty = con for { select { case <-ctx.Done(): -- cgit v1.2.3