Fully rehabilitate all modules after cache, page refactor

This commit is contained in:
lash 2023-04-09 10:16:41 +01:00
parent d183ec1824
commit 45de1f5c7a
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 48 additions and 25 deletions

View File

@ -1,12 +1,14 @@
package engine
import (
"git.defalsify.org/festive/state"
"git.defalsify.org/festive/cache"
"git.defalsify.org/festive/resource"
"git.defalsify.org/festive/state"
)
func NewDefaultEngine(dir string) Engine {
st := state.NewState(0)
rs := resource.NewFsResource(dir)
return NewEngine(&st, &rs)
ca := cache.NewCache()
return NewEngine(&st, &rs, ca)
}

View File

@ -6,6 +6,8 @@ import (
"io"
"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"
@ -20,11 +22,17 @@ import (
type Engine struct {
st *state.State
rs resource.Resource
ca cache.Memory
pg render.Renderer
}
// NewEngine creates a new Engine
func NewEngine(st *state.State, rs resource.Resource) Engine {
engine := Engine{st, rs}
func NewEngine(st *state.State, rs resource.Resource, ca cache.Memory) Engine {
engine := Engine{
st: st,
rs: rs,
ca: ca,
}
return engine
}
@ -32,12 +40,15 @@ func NewEngine(st *state.State, rs resource.Resource) Engine {
//
// It loads and executes code for the start node.
func(en *Engine) Init(sym string, ctx context.Context) error {
mn := render.NewMenu()
en.pg = render.NewPage(en.ca, en.rs).WithMenu(mn)
vmi := vm.NewVm(en.st, en.rs, en.ca, mn, nil)
err := en.st.SetInput([]byte{})
if err != nil {
return err
}
b := vm.NewLine(nil, vm.MOVE, []string{sym}, nil, nil)
b, err = vm.Run(b, en.st, en.rs, ctx)
b, err = vmi.Run(b, ctx)
if err != nil {
return err
}
@ -64,6 +75,9 @@ func (en *Engine) Exec(input []byte, ctx context.Context) (bool, error) {
if err != nil {
return false, err
}
mn := render.NewMenu()
en.pg = render.NewPage(en.ca, en.rs).WithMenu(mn)
vmi := vm.NewVm(en.st, en.rs, en.ca, mn, en.pg)
log.Printf("new execution with input '%s' (0x%x)", input, input)
code, err := en.st.GetCode()
@ -73,7 +87,7 @@ func (en *Engine) Exec(input []byte, ctx context.Context) (bool, error) {
if len(code) == 0 {
return false, fmt.Errorf("no code to execute")
}
code, err = vm.Run(code, en.st, en.rs, ctx)
code, err = vmi.Run(code, ctx)
if err != nil {
return false, err
}
@ -106,22 +120,27 @@ func (en *Engine) Exec(input []byte, ctx context.Context) (bool, error) {
// - the supplied writer fails to process the writes.
func(en *Engine) WriteResult(w io.Writer) error {
location, idx := en.st.Where()
v, err := en.st.Get()
v, err := en.ca.Get()
if err != nil {
return err
}
r, err := en.rs.RenderTemplate(location, v, idx, nil)
// r, err := en.rs.RenderTemplate(location, v, idx, nil)
// if err != nil {
// return err
// }
// m, err := en.rs.RenderMenu(idx)
// if err != nil {
// return err
// }
// if len(m) > 0 {
// r += "\n" + m
// }
r, err := en.pg.Render(location, v, idx)
if err != nil {
return err
}
m, err := en.rs.RenderMenu(idx)
if err != nil {
return err
}
if len(m) > 0 {
r += "\n" + m
}
c, err := io.WriteString(w, r)
log.Printf("%v bytes written as result for %v", c, location)
en.pg = nil
return err
}

View File

@ -8,6 +8,7 @@ import (
"path"
"testing"
"git.defalsify.org/festive/cache"
"git.defalsify.org/festive/resource"
"git.defalsify.org/festive/state"
"git.defalsify.org/festive/testdata"
@ -31,10 +32,6 @@ func NewFsWrapper(path string, st *state.State) FsWrapper {
}
}
func (r FsWrapper) RenderTemplate(sym string, values map[string]string, idx uint16, sizer *resource.Sizer) (string, error) {
return resource.DefaultRenderTemplate(r, sym, values, idx, sizer)
}
func(fs FsWrapper) one(ctx context.Context) (string, error) {
return "one", nil
}
@ -72,11 +69,13 @@ func generateTestData(t *testing.T) {
}
func TestEngineInit(t *testing.T) {
st := state.NewState(17).WithCacheSize(1024)
generateTestData(t)
ctx := context.TODO()
st := state.NewState(17)
rs := NewFsWrapper(dataDir, &st)
en := NewEngine(&st, &rs)
ca := cache.NewCache().WithCacheSize(1024)
en := NewEngine(&st, &rs, ca)
err := en.Init("root", ctx)
if err != nil {
t.Fatal(err)
@ -122,11 +121,13 @@ it has more lines
}
func TestEngineExecInvalidInput(t *testing.T) {
st := state.NewState(17).WithCacheSize(1024)
generateTestData(t)
ctx := context.TODO()
st := state.NewState(17)
rs := NewFsWrapper(dataDir, &st)
en := NewEngine(&st, &rs)
ca := cache.NewCache().WithCacheSize(1024)
en := NewEngine(&st, &rs, ca)
err := en.Init("root", ctx)
if err != nil {
t.Fatal(err)

View File

@ -154,6 +154,7 @@ func(m *Menu) reset() {
}
}
func(m *Menu) ReservedSize() uint16 {
return m.outputSize
}

View File

@ -13,7 +13,7 @@ import (
type Page struct {
cacheMap map[string]string // Mapped
cache *cache.Cache
cache cache.Memory
resource resource.Resource
menu *Menu
sink *string
@ -22,7 +22,7 @@ type Page struct {
sinkProcessed bool
}
func NewPage(cache *cache.Cache, rs resource.Resource) *Page {
func NewPage(cache cache.Memory, rs resource.Resource) *Page {
return &Page{
cache: cache,
cacheMap: make(map[string]string),