go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit 5f04698d0a851950b404cc6821b6575a58ca5ff8
parent 0831a4ea539f46c5eeb8407ca71bc013dbdff873
Author: lash <dev@holbrook.no>
Date:   Mon, 10 Apr 2023 16:12:22 +0100

WIP implement high-level pagination test

Diffstat:
Mgo/dev/interactive.go | 4+++-
Mgo/engine/default.go | 12+++++++++++-
Mgo/engine/engine.go | 14++++++++++----
Mgo/engine/engine_test.go | 8+++++---
Mgo/engine/loop_test.go | 2+-
Mgo/render/size.go | 2--
Mgo/testdata/testdata.go | 31++++++++++++++++++++++++++++++-
Mgo/vm/runner.go | 1-
8 files changed, 60 insertions(+), 14 deletions(-)

diff --git a/go/dev/interactive.go b/go/dev/interactive.go @@ -12,13 +12,15 @@ import ( func main() { var dir string var root string + var size uint flag.StringVar(&dir, "d", ".", "resource dir to read from") + flag.UintVar(&size, "s", 0, "max size of output") flag.StringVar(&root, "root", "root", "entry point symbol") flag.Parse() fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir) ctx := context.Background() - en := engine.NewDefaultEngine(dir) + en := engine.NewSizedEngine(dir, uint32(size)) err := engine.Loop(&en, root, ctx, os.Stdin, os.Stdout) if err != nil { fmt.Fprintf(os.Stderr, "loop exited with error: %v", err) diff --git a/go/engine/default.go b/go/engine/default.go @@ -10,5 +10,15 @@ func NewDefaultEngine(dir string) Engine { st := state.NewState(0) rs := resource.NewFsResource(dir) ca := cache.NewCache() - return NewEngine(&st, &rs, ca) + return NewEngine(Config{}, &st, &rs, ca) +} + +func NewSizedEngine(dir string, size uint32) Engine { + st := state.NewState(0) + rs := resource.NewFsResource(dir) + ca := cache.NewCache() + cfg := Config{ + OutputSize: size, + } + return NewEngine(cfg, &st, &rs, ca) } diff --git a/go/engine/engine.go b/go/engine/engine.go @@ -7,15 +7,17 @@ import ( "log" "git.defalsify.org/festive/cache" + "git.defalsify.org/festive/render" "git.defalsify.org/festive/resource" "git.defalsify.org/festive/state" "git.defalsify.org/festive/vm" ) -//type Config struct { +type Config struct { + OutputSize uint32 // FlagCount uint32 // CacheSize uint32 -//} +} // Engine is an execution engine that handles top-level errors when running user inputs against currently exposed bytecode. type Engine struct { @@ -26,12 +28,16 @@ type Engine struct { } // NewEngine creates a new Engine -func NewEngine(st *state.State, rs resource.Resource, ca cache.Memory) Engine { +func NewEngine(cfg Config, st *state.State, rs resource.Resource, ca cache.Memory) Engine { + var szr *render.Sizer + if cfg.OutputSize > 0 { + szr = render.NewSizer(cfg.OutputSize) + } engine := Engine{ st: st, rs: rs, ca: ca, - vm: vm.NewVm(st, rs, ca, nil), + vm: vm.NewVm(st, rs, ca, szr), } return engine } diff --git a/go/engine/engine_test.go b/go/engine/engine_test.go @@ -75,7 +75,7 @@ func TestEngineInit(t *testing.T) { rs := NewFsWrapper(dataDir, &st) ca := cache.NewCache().WithCacheSize(1024) - en := NewEngine(&st, &rs, ca) + en := NewEngine(Config{}, &st, &rs, ca) err := en.Init("root", ctx) if err != nil { t.Fatal(err) @@ -114,7 +114,9 @@ func TestEngineInit(t *testing.T) { it has more lines 0:to foo -1:go bar` +1:go bar +2:see long` + if !bytes.Equal(b, []byte(expect)) { t.Fatalf("expected\n\t%s\ngot:\n\t%s\n", expect, b) } @@ -127,7 +129,7 @@ func TestEngineExecInvalidInput(t *testing.T) { rs := NewFsWrapper(dataDir, &st) ca := cache.NewCache().WithCacheSize(1024) - en := NewEngine(&st, &rs, ca) + en := NewEngine(Config{}, &st, &rs, ca) err := en.Init("root", ctx) if err != nil { t.Fatal(err) diff --git a/go/engine/loop_test.go b/go/engine/loop_test.go @@ -20,7 +20,7 @@ func TestLoopCorrect(t *testing.T) { rs := resource.NewFsResource(dataDir) ca := cache.NewCache().WithCacheSize(1024) - en := NewEngine(&st, &rs, ca) + en := NewEngine(Config{}, &st, &rs, ca) err := en.Init("root", ctx) if err != nil { t.Fatal(err) diff --git a/go/render/size.go b/go/render/size.go @@ -38,7 +38,6 @@ func(szr *Sizer) Set(key string, size uint16) error { } func(szr *Sizer) Check(s string) (uint32, bool) { - log.Printf("sizercheck %s", s) l := uint32(len(s)) if szr.outputSize > 0 { if l > szr.outputSize { @@ -84,7 +83,6 @@ func(szr *Sizer) GetAt(values map[string]string, idx uint16) (map[string]string, c := szr.crsrs[idx] v = v[c:] nl := strings.Index(v, "\n") - log.Printf("k %v v %v c %v nl %v", k, v, c, nl) if nl > 0 { v = v[:nl] } diff --git a/go/testdata/testdata.go b/go/testdata/testdata.go @@ -66,10 +66,12 @@ func foo() error { b := []byte{} b = vm.NewLine(b, vm.MOUT, []string{"0", "to foo"}, nil, nil) b = vm.NewLine(b, vm.MOUT, []string{"1", "go bar"}, nil, nil) + b = vm.NewLine(b, vm.MOUT, []string{"2", "see long"}, nil, nil) b = vm.NewLine(b, vm.LOAD, []string{"inky"}, []byte{20}, nil) b = vm.NewLine(b, vm.HALT, nil, nil, nil) b = vm.NewLine(b, vm.INCMP, []string{"0", "_"}, nil, nil) b = vm.NewLine(b, vm.INCMP, []string{"1", "baz"}, nil, nil) + b = vm.NewLine(b, vm.INCMP, []string{"2", "long"}, nil, nil) //b = vm.NewLine(b, vm.CATCH, []string{"_catch"}, []byte{1}, []uint8{1}) data := make(map[string]string) @@ -106,6 +108,33 @@ func baz() error { return out("baz", b, tpl, nil) } +func long() error { + b := []byte{} + b = vm.NewLine(b, vm.LOAD, []string{"longdata"}, []byte{0x00}, nil) + b = vm.NewLine(b, vm.MAP, []string{"longdata"}, nil, nil) + b = vm.NewLine(b, vm.MOUT, []string{"0", "back"}, nil, nil) + b = vm.NewLine(b, vm.MNEXT, []string{"00", "nexxt"}, nil, nil) + b = vm.NewLine(b, vm.MPREV, []string{"11", "prevv"}, nil, nil) + b = vm.NewLine(b, vm.HALT, nil, nil, nil) + b = vm.NewLine(b, vm.INCMP, []string{"0", "_"}, nil, nil) + + tpl := `data +{{.longdata}}` + + data := make(map[string]string) + data["longdata"] = `INKY 12 +PINKY 5555 +BLINKY 3t7 +CLYDE 11 +TINKYWINKY 22 +DIPSY 666 +LALA 111 +POO 222 +` + + return out("long", b, tpl, data) +} + func defaultCatch() error { b := []byte{} b = vm.NewLine(b, vm.MOUT, []string{"0", "back"}, nil, nil) @@ -123,7 +152,7 @@ func generate() error { return err } - fns := []genFunc{root, foo, bar, baz, defaultCatch} + fns := []genFunc{root, foo, bar, baz, long, defaultCatch} for _, fn := range fns { err = fn() if err != nil { diff --git a/go/vm/runner.go b/go/vm/runner.go @@ -209,7 +209,6 @@ func(vm *Vm) RunLoad(b []byte, ctx context.Context) ([]byte, error) { if err != nil { return b, err } - r, err := refresh(sym, vm.rs, ctx) if err != nil { return b, err