commit 0526690e1e9f310f8ca7869b6cde16a1bcb25a49
parent b616752bedfae63a9fd5ae23de0328fe5960b9f2
Author: lash <dev@holbrook.no>
Date: Sat, 14 Sep 2024 16:36:48 +0100
Eliminate need for Init as separate step in engine interface
Diffstat:
5 files changed, 66 insertions(+), 27 deletions(-)
diff --git a/engine/db.go b/engine/db.go
@@ -245,6 +245,9 @@ func(en *DefaultEngine) setupVm() {
// prepare engine for Init run.
func(en *DefaultEngine) prepare() error {
+ if en.initd {
+ return nil
+ }
err := en.preparePersist()
if err != nil {
return err
@@ -331,6 +334,9 @@ func(en *DefaultEngine) Finish() error {
// change root to current state location if non-empty.
func(en *DefaultEngine) restore() {
+ if en.initd {
+ return
+ }
location, _ := en.st.Where()
if len(location) == 0 {
return
@@ -344,22 +350,23 @@ func(en *DefaultEngine) restore() {
// Init implements the Engine interface.
//
// It loads and executes code for the start node.
-func(en *DefaultEngine) Init(ctx context.Context) (bool, error) {
+func(en *DefaultEngine) init(ctx context.Context, input []byte) (bool, error) {
err := en.prepare()
if err != nil {
return false, err
}
en.restore()
+
+ if en.st.Language != nil {
+ logg.TraceCtxf(ctx, "set language on context", "lang", en.st.Language)
+ ctx = context.WithValue(ctx, "Language", *en.st.Language)
+ }
+
if en.initd {
logg.DebugCtxf(ctx, "already initialized")
return true, nil
}
- if en.cfg.SessionId != "" {
- ctx = context.WithValue(ctx, "SessionId", en.cfg.SessionId)
- }
- if en.st.Language != nil {
- ctx = context.WithValue(ctx, "Language", *en.st.Language)
- }
+
sym := en.cfg.Root
if sym == "" {
@@ -395,6 +402,7 @@ func(en *DefaultEngine) Init(ctx context.Context) (bool, error) {
if err != nil {
return false, err
}
+ en.initd = true
return len(b) > 0, nil
}
@@ -412,9 +420,21 @@ func(en *DefaultEngine) Init(ctx context.Context) (bool, error) {
// * input processing against bytcode failed
func (en *DefaultEngine) Exec(ctx context.Context, input []byte) (bool, error) {
var err error
+
if en.cfg.SessionId != "" {
ctx = context.WithValue(ctx, "SessionId", en.cfg.SessionId)
}
+
+ cont, err := en.init(ctx, input)
+ if err != nil {
+ return false, err
+ }
+ if !cont {
+ return cont, nil
+ } else if len(input) == 0 {
+ return true, nil
+ }
+
if en.st.Language != nil {
ctx = context.WithValue(ctx, "Language", *en.st.Language)
}
diff --git a/engine/db_test.go b/engine/db_test.go
@@ -56,7 +56,8 @@ func TestDbEngineMinimal(t *testing.T) {
cfg := Config{}
rs := resource.NewMenuResource()
en := NewEngine(cfg, rs)
- cont, err := en.Init(ctx)
+ //cont, err := en.Init(ctx)
+ cont, err := en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -186,7 +187,8 @@ func TestDbEngineRoot(t *testing.T) {
rs := resource.NewMenuResource()
rs.WithCodeGetter(codeGet)
en := NewEngine(cfg, rs)
- cont, err := en.Init(ctx)
+ //cont, err := en.Init(ctx)
+ cont, err := en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -230,7 +232,8 @@ func TestDbEnginePersist(t *testing.T) {
rs.AddLocalFunc("foo", flagSet)
en := NewEngine(cfg, rs)
en = en.WithPersister(pe)
- cont, err := en.Init(ctx)
+ //cont, err := en.Init(ctx)
+ cont, err := en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -265,7 +268,8 @@ func TestDbEngineDebug(t *testing.T) {
rs.AddLocalFunc("foo", flagSet)
dbg := NewSimpleDebug(w)
en := NewEngine(cfg, rs).WithDebug(dbg)
- c, err := en.Init(ctx)
+ //c, err := en.Init(ctx)
+ c, err := en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -296,7 +300,8 @@ func TestDbEnsure(t *testing.T) {
store.Connect(ctx, "")
pe := persist.NewPersister(store)
en := NewEngine(cfg, rs).WithPersister(pe)
- _, err = en.Init(ctx)
+ //_, err = en.Init(ctx)
+ _, err = en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -320,7 +325,8 @@ func TestDbKeepPersisterContent(t *testing.T) {
pe := persist.NewPersister(store)
pe = pe.WithContent(st, ca)
en := NewEngine(cfg, rs).WithPersister(pe)
- _, err = en.Init(ctx)
+ //_, err = en.Init(ctx)
+ _, err = en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -348,7 +354,8 @@ func TestDbKeepState(t *testing.T) {
en = en.WithState(st)
en = en.WithMemory(ca)
en = en.WithPersister(pe)
- _, err = en.Init(ctx)
+ //_, err = en.Init(ctx)
+ _, err = en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -378,7 +385,8 @@ func TestDbFirst(t *testing.T) {
en := NewEngine(cfg, rs)
en = en.WithState(st)
en = en.WithFirst(flagSet)
- _, err = en.Init(ctx)
+ //_, err = en.Init(ctx)
+ _, err = en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
diff --git a/engine/engine.go b/engine/engine.go
@@ -8,7 +8,7 @@ import (
// EngineIsh defines the interface for execution engines that handle vm initialization and execution, and rendering outputs.
type Engine interface {
// Init sets the engine up for vm execution. It must be called before Exec.
- Init(ctx context.Context) (bool, error)
+ //Init(ctx context.Context) (bool, error)
// Exec executes the pending bytecode.
Exec(ctx context.Context, input []byte) (bool, error)
// WriteResult renders output according to the state of VM execution
diff --git a/engine/engine_test.go b/engine/engine_test.go
@@ -128,7 +128,8 @@ func TestEngineInit(t *testing.T) {
en = en.WithState(st)
en = en.WithMemory(ca)
- _, err = en.Init(ctx)
+ //_, err = en.Init(ctx)
+ _, err = en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -188,7 +189,8 @@ func TestEngineExecInvalidInput(t *testing.T) {
en = en.WithState(st)
en = en.WithMemory(ca)
var err error
- _, err = en.Init(ctx)
+ //_, err = en.Init(ctx)
+ _, err = en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -212,7 +214,8 @@ func TestEngineResumeTerminated(t *testing.T) {
en = en.WithState(st)
en = en.WithMemory(ca)
var err error
- _, err = en.Init(ctx)
+ //_, err = en.Init(ctx)
+ _, err = en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -251,7 +254,8 @@ func TestLanguageSet(t *testing.T) {
en = en.WithMemory(ca)
var err error
- _, err = en.Init(ctx)
+ //_, err = en.Init(ctx)
+ _, err = en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -306,7 +310,8 @@ func TestLanguageRender(t *testing.T) {
en = en.WithMemory(ca)
var err error
- _, err = en.Init(ctx)
+ //_, err = en.Init(ctx)
+ _, err = en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -349,7 +354,8 @@ func TestConfigLanguageRender(t *testing.T) {
en = en.WithMemory(ca)
var err error
- _, err = en.Init(ctx)
+ //_, err = en.Init(ctx)
+ _, err = en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -406,7 +412,8 @@ func TestPreVm(t *testing.T) {
en = en.WithState(st)
en = en.WithMemory(ca)
en = en.WithFirst(preBlock)
- r, err := en.Init(ctx)
+ //r, err := en.Init(ctx)
+ r, err := en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -428,7 +435,8 @@ func TestPreVm(t *testing.T) {
en = en.WithState(st)
en = en.WithMemory(ca)
en = en.WithFirst(preAllow)
- r, err = en.Init(ctx)
+ //r, err = en.Init(ctx)
+ r, err = en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
diff --git a/engine/loop_test.go b/engine/loop_test.go
@@ -27,7 +27,8 @@ func TestLoopTop(t *testing.T) {
en = en.WithState(st)
en = en.WithMemory(ca)
- _, err = en.Init(ctx)
+ //_, err = en.Init(ctx)
+ _, err = en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -66,7 +67,8 @@ func TestLoopBackForth(t *testing.T) {
en := NewEngine(cfg, rs)
en = en.WithState(st)
- _, err = en.Init(ctx)
+ //_, err = en.Init(ctx)
+ _, err = en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
@@ -103,7 +105,8 @@ func TestLoopBrowse(t *testing.T) {
en := NewEngine(cfg, rs)
en = en.WithState(st)
- _, err = en.Init(ctx)
+ //_, err = en.Init(ctx)
+ _, err = en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}