go-vise

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

commit 13bb7bed9c26795eb8604cd807531d91691908c1
parent ee55f3e5fe7e37a18bef715e962495dd0d69884d
Author: lash <dev@holbrook.no>
Date:   Fri, 31 Mar 2023 15:24:29 +0100

Add multi-instruction test stub

Diffstat:
Mgo/state/state.go | 6+++++-
Mgo/state/state_test.go | 4++++
Mgo/vm/vm.go | 8++++++++
Mgo/vm/vm_test.go | 14++++++++++++++
4 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/go/state/state.go b/go/state/state.go @@ -24,11 +24,13 @@ func NewState(bitSize uint64) State { bitSize += (8 - n) } - return State{ + st := State{ Flags: make([]byte, bitSize / 8), CacheSize: 0, CacheUseSize: 0, } + st.Down("") + return st } func(st State) WithCacheSize(cacheSize uint32) State { @@ -40,6 +42,7 @@ func(st *State) Down(input string) { m := make(map[string]string) st.Cache = append(st.Cache, m) st.CacheMap = make(map[string]string) + st.ExecPath = append(st.ExecPath, input) } func(st *State) Add(key string, value string, sizeHint uint32) error { @@ -110,6 +113,7 @@ func(st *State) Up() error { log.Printf("free frame %v key %v value size %v", l, k, sz) } st.Cache = st.Cache[:l] + st.ExecPath = st.ExecPath[:l] return nil } diff --git a/go/state/state_test.go b/go/state/state_test.go @@ -69,6 +69,10 @@ func TestStateDownUp(t *testing.T) { t.Error(err) } err = st.Up() + if err != nil { + t.Error(err) + } + err = st.Up() if err == nil { t.Errorf("expected out of top frame error") } diff --git a/go/vm/vm.go b/go/vm/vm.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "fmt" "context" + "log" "git.defalsify.org/festive/state" "git.defalsify.org/festive/resource" @@ -14,6 +15,7 @@ type Runner func(instruction []byte, st state.State, rs resource.Fetcher, ctx co func Run(instruction []byte, st state.State, rs resource.Fetcher, ctx context.Context) (state.State, []byte, error) { var err error for len(instruction) > 0 { + log.Printf("instruction is now %v", instruction) op := binary.BigEndian.Uint16(instruction[:2]) if op > _MAX { return st, instruction, fmt.Errorf("opcode value %v out of range (%v)", op, _MAX) @@ -21,16 +23,22 @@ func Run(instruction []byte, st state.State, rs resource.Fetcher, ctx context.Co switch op { case CATCH: st, instruction, err = RunCatch(instruction[2:], st, rs, ctx) + break case CROAK: st, instruction, err = RunCroak(instruction[2:], st, rs, ctx) + break case LOAD: st, instruction, err = RunLoad(instruction[2:], st, rs, ctx) + break case RELOAD: st, instruction, err = RunReload(instruction[2:], st, rs, ctx) + break case MAP: st, instruction, err = RunMap(instruction[2:], st, rs, ctx) + break case SINK: st, instruction, err = RunSink(instruction[2:], st, rs, ctx) + break default: err = fmt.Errorf("Unhandled state: %v", op) } diff --git a/go/vm/vm_test.go b/go/vm/vm_test.go @@ -128,3 +128,17 @@ func TestRunLoad(t *testing.T) { t.Errorf("Expected %v, got %v", expect, r) } } + +func TestRunMultiple(t *testing.T) { + st := state.NewState(5) + rs := TestResource{} + b := []byte{0x00, LOAD, 0x03} + b = append(b, []byte("one")...) + b = append(b, []byte{0x00, 0x00, LOAD, 0x03}...) + b = append(b, []byte("two")...) + b = append(b, 0x00) + st, _, err := Run(b, st, &rs, context.TODO()) + if err != nil { + t.Error(err) + } +}