aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-01-24 00:25:10 +0100
committerDimitri Sokolyuk <demon@dim13.org>2018-01-24 00:25:10 +0100
commit07584da73461b69043451ebe48fcac226202492a (patch)
tree1f65f42f61bd10079ff369b039ff8dc0282f816d
parentbc1a7a271ba5a3f971e9dbed774b01b43af2642e (diff)
extract console, extract context
-rw-r--r--cmd/eval/main.go5
-rw-r--r--cmd/j1e/main.go5
-rw-r--r--console/console.go (renamed from console.go)18
-rw-r--r--core.go5
4 files changed, 19 insertions, 14 deletions
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/console.go
index e59a765..4fe9680 100644
--- a/console.go
+++ b/console/console.go
@@ -1,4 +1,4 @@
-package j1
+package console
import (
"context"
@@ -7,14 +7,14 @@ import (
"os"
)
-type console struct {
+type Console struct {
r io.Reader
w io.Writer
ich, och chan uint16
}
-func NewConsole(ctx context.Context) *console {
- c := &console{
+func New(ctx context.Context) *Console {
+ c := &Console{
r: os.Stdin,
w: os.Stdout,
ich: make(chan uint16, 1),
@@ -25,7 +25,7 @@ func NewConsole(ctx context.Context) *console {
return c
}
-func (c *console) read(ctx context.Context) {
+func (c *Console) read(ctx context.Context) {
var v uint16
for {
fmt.Fscanf(c.r, "%c", &v)
@@ -37,7 +37,7 @@ func (c *console) read(ctx context.Context) {
}
}
-func (c *console) write(ctx context.Context) {
+func (c *Console) write(ctx context.Context) {
for {
select {
case <-ctx.Done():
@@ -48,6 +48,6 @@ func (c *console) write(ctx context.Context) {
}
}
-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)) }
+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():