From 46288b240b1d8feaed9dd416800a3a7cb83869e3 Mon Sep 17 00:00:00 2001 From: lash Date: Thu, 13 Apr 2023 07:56:35 +0100 Subject: [PATCH] Make client input available for LOAD instruction execution --- engine/engine_test.go | 8 ++++---- render/size_test.go | 8 ++++---- resource/fs.go | 8 ++++---- resource/resource.go | 2 +- resource/resource_test.go | 4 ++-- vm/runner.go | 9 +++++---- vm/runner_test.go | 8 ++++---- 7 files changed, 24 insertions(+), 23 deletions(-) diff --git a/engine/engine_test.go b/engine/engine_test.go index f4463c5..4bc06fb 100644 --- a/engine/engine_test.go +++ b/engine/engine_test.go @@ -32,16 +32,16 @@ func NewFsWrapper(path string, st *state.State) FsWrapper { } } -func(fs FsWrapper) one(sym string, ctx context.Context) (string, error) { +func(fs FsWrapper) one(sym string, input []byte, ctx context.Context) (string, error) { return "one", nil } -func(fs FsWrapper) inky(sym string, ctx context.Context) (string, error) { +func(fs FsWrapper) inky(sym string, input []byte, ctx context.Context) (string, error) { return "tinkywinky", nil } -func(fs FsWrapper) pinky(sym string, ctx context.Context) (string, error) { - return "xyzzy", nil +func(fs FsWrapper) pinky(sym string, input []byte, ctx context.Context) (string, error) { + return fmt.Sprintf("xyzzy: %x", input), nil } func(fs FsWrapper) FuncFor(sym string) (resource.EntryFunc, error) { diff --git a/render/size_test.go b/render/size_test.go index 11ca518..e965775 100644 --- a/render/size_test.go +++ b/render/size_test.go @@ -41,19 +41,19 @@ func funcFor(sym string) (resource.EntryFunc, error) { return nil, fmt.Errorf("unknown func: %s", sym) } -func getFoo(sym string, ctx context.Context) (string, error) { +func getFoo(sym string, input []byte, ctx context.Context) (string, error) { return "inky", nil } -func getBar(sym string, ctx context.Context) (string, error) { +func getBar(sym string, input []byte, ctx context.Context) (string, error) { return "pinky", nil } -func getBaz(sym string, ctx context.Context) (string, error) { +func getBaz(sym string, input []byte, ctx context.Context) (string, error) { return "blinky", nil } -func getXyzzy(sym string, ctx context.Context) (string, error) { +func getXyzzy(sym string, input []byte, ctx context.Context) (string, error) { return "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve", nil } diff --git a/resource/fs.go b/resource/fs.go index 66514b1..1b6bda6 100644 --- a/resource/fs.go +++ b/resource/fs.go @@ -44,7 +44,7 @@ func(fs FsResource) FuncFor(sym string) (EntryFunc, error) { if ok { return fn, nil } - _, err := fs.getFuncNoCtx(sym) + _, err := fs.getFuncNoCtx(sym, nil) if err != nil { return nil, fmt.Errorf("unknown sym: %s", sym) } @@ -55,11 +55,11 @@ func(fs FsResource) String() string { return fmt.Sprintf("fs resource at path: %s", fs.Path) } -func(fs FsResource) getFunc(sym string, ctx context.Context) (string, error) { - return fs.getFuncNoCtx(sym) +func(fs FsResource) getFunc(sym string, input []byte, ctx context.Context) (string, error) { + return fs.getFuncNoCtx(sym, input) } -func(fs FsResource) getFuncNoCtx(sym string) (string, error) { +func(fs FsResource) getFuncNoCtx(sym string, input []byte) (string, error) { fb := sym + ".txt" fp := path.Join(fs.Path, fb) log.Printf("getfunc search dir %s %s for %s", fs.Path, fp, sym) diff --git a/resource/resource.go b/resource/resource.go index 3694e20..c406c84 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -5,7 +5,7 @@ import ( ) // EntryFunc is a function signature for retrieving value for a key -type EntryFunc func(sym string, ctx context.Context) (string, error) +type EntryFunc func(sym string, input []byte, ctx context.Context) (string, error) type CodeFunc func(sym string) ([]byte, error) type TemplateFunc func(sym string) (string, error) type FuncForFunc func(sym string) (EntryFunc, error) diff --git a/resource/resource_test.go b/resource/resource_test.go index 1a100c2..66f5a21 100644 --- a/resource/resource_test.go +++ b/resource/resource_test.go @@ -37,7 +37,7 @@ func funcFor(sym string) (EntryFunc, error) { return nil, fmt.Errorf("unknown func: %s", sym) } -func get(sym string, ctx context.Context) (string, error) { +func get(sym string, input []byte, ctx context.Context) (string, error) { switch sym { case "foo": return "inky", nil @@ -49,6 +49,6 @@ func get(sym string, ctx context.Context) (string, error) { return "", fmt.Errorf("unknown sym: %s", sym) } -func getXyzzy(sym string, ctx context.Context) (string, error) { +func getXyzzy(sym string, input []byte, ctx context.Context) (string, error) { return "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve", nil } diff --git a/vm/runner.go b/vm/runner.go index 1ddfd93..1fe00f4 100644 --- a/vm/runner.go +++ b/vm/runner.go @@ -211,7 +211,7 @@ func(vm *Vm) RunLoad(b []byte, ctx context.Context) ([]byte, error) { if err != nil { return b, err } - r, err := refresh(sym, vm.rs, ctx) + r, err := vm.refresh(sym, vm.rs, ctx) if err != nil { return b, err } @@ -226,7 +226,7 @@ func(vm *Vm) RunReload(b []byte, ctx context.Context) ([]byte, error) { return b, err } - r, err := refresh(sym, vm.rs, ctx) + r, err := vm.refresh(sym, vm.rs, ctx) if err != nil { return b, err } @@ -421,7 +421,7 @@ func(vm *Vm) Render(ctx context.Context) (string, error) { } // retrieve data for key -func refresh(key string, rs resource.Resource, ctx context.Context) (string, error) { +func(vm *Vm) refresh(key string, rs resource.Resource, ctx context.Context) (string, error) { fn, err := rs.FuncFor(key) if err != nil { return "", err @@ -429,6 +429,7 @@ func refresh(key string, rs resource.Resource, ctx context.Context) (string, err if fn == nil { return "", fmt.Errorf("no retrieve function for external symbol %v", key) } - return fn(key, ctx) + input, _ := vm.st.GetInput() + return fn(key, input, ctx) } diff --git a/vm/runner_test.go b/vm/runner_test.go index 72677bf..4f78f8d 100644 --- a/vm/runner_test.go +++ b/vm/runner_test.go @@ -20,15 +20,15 @@ type TestResource struct { state *state.State } -func getOne(sym string, ctx context.Context) (string, error) { +func getOne(sym string, input []byte, ctx context.Context) (string, error) { return "one", nil } -func getTwo(sym string, ctx context.Context) (string, error) { +func getTwo(sym string, input []byte, ctx context.Context) (string, error) { return "two", nil } -func getDyn(sym string, ctx context.Context) (string, error) { +func getDyn(sym string, input []byte, ctx context.Context) (string, error) { return dynVal, nil } @@ -69,7 +69,7 @@ func (r TestResource) FuncFor(sym string) (resource.EntryFunc, error) { return nil, fmt.Errorf("invalid function: '%s'", sym) } -func(r TestResource) getInput(sym string, ctx context.Context) (string, error) { +func(r TestResource) getInput(sym string, input []byte, ctx context.Context) (string, error) { v, err := r.state.GetInput() return string(v), err }