Complete move map code to page code in render module

This commit is contained in:
lash 2023-04-10 15:26:18 +01:00
parent 765bc2a269
commit 0831a4ea53
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
11 changed files with 61 additions and 36 deletions

11
go/cache/cache.go vendored
View File

@ -113,12 +113,13 @@ func(ca *Cache) Update(key string, value string) error {
return nil
}
// Get returns the full key-value mapping for all mapped keys at the current cache level.
func(ca *Cache) Get() (map[string]string, error) {
if len(ca.Cache) == 0 {
return nil, fmt.Errorf("get at top frame")
func(ca *Cache) Get(key string) (string, error) {
i := ca.frameOf(key)
r, ok := ca.Cache[i][key]
if !ok {
return "", fmt.Errorf("unknown key: %s", key)
}
return ca.Cache[len(ca.Cache)-1], nil
return r, nil
}
// Reset flushes all state contents below the top level.

2
go/cache/memory.go vendored
View File

@ -4,7 +4,7 @@ type Memory interface {
Add(key string, val string, sizeLimit uint16) error
Update(key string, val string) error
ReservedSize(key string) (uint16, error)
Get() (map[string]string, error)
Get(key string) (string, error)
Push() error
Pop() error
Reset()

View File

@ -113,12 +113,11 @@ func (en *Engine) Exec(input []byte, ctx context.Context) (bool, error) {
// - the template for the given node point is note available for retrieval using the resource.Resource implementer.
// - the supplied writer fails to process the writes.
func(en *Engine) WriteResult(w io.Writer) error {
location, idx := en.st.Where()
//location, idx := en.st.Where()
r, err := en.vm.Render()
if err != nil {
return err
}
c, err := io.WriteString(w, r)
log.Printf("%v bytes written as result for %v idx %v", c, location, idx)
_, err = io.WriteString(w, r)
return err
}

View File

@ -36,9 +36,11 @@ func Loop(en *Engine, startSym string, ctx context.Context, reader io.Reader, wr
if err != nil {
return fmt.Errorf("unexpected termination: %v\n", err)
}
b := bytes.NewBuffer(nil)
en.WriteResult(b)
writer.Write(b.Bytes())
//b := bytes.NewBuffer(nil)
err = en.WriteResult(writer)
if err != nil {
return err
}
writer.Write([]byte{0x0a})
}
return nil

View File

@ -19,7 +19,6 @@ type Page struct {
sink *string
sinkSize uint16
sizer *Sizer
sinkProcessed bool
}
func NewPage(cache cache.Memory, rs resource.Resource) *Page {
@ -57,12 +56,16 @@ func(pg *Page) Usage() (uint32, uint32, error) {
return 0, 0, err
}
c += sz
log.Printf("v %x %v %v %v %v", []byte(v), len(v), l, sz, c)
}
r := uint32(l)
rsv := uint32(c)-r
log.Printf("size before %v %v", r, c)
if pg.menu != nil {
r += uint32(pg.menu.ReservedSize())
}
return r, uint32(c)-r, nil
log.Printf("size after %v %v", r, c)
return r, rsv, nil
}
// Map marks the given key for retrieval.
@ -71,7 +74,7 @@ func(pg *Page) Usage() (uint32, uint32, error) {
//
// Only one symbol with no size limitation may be mapped at the current level.
func(pg *Page) Map(key string) error {
m, err := pg.cache.Get()
v, err := pg.cache.Get(key)
if err != nil {
return err
}
@ -85,13 +88,14 @@ func(pg *Page) Map(key string) error {
}
pg.sink = &key
}
pg.cacheMap[key] = m[key]
pg.cacheMap[key] = v
if pg.sizer != nil {
err := pg.sizer.Set(key, l)
if err != nil {
return err
}
}
log.Printf("page map is now: %v", pg.cacheMap)
return nil
}
@ -277,12 +281,7 @@ func(pg *Page) render(sym string, values map[string]string, idx uint16) (string,
func(pg *Page) Render(sym string, idx uint16) (string, error) {
var err error
values, err := pg.cache.Get()
if err != nil {
return "", err
}
values, err = pg.prepare(sym, values, idx)
values, err := pg.prepare(sym, pg.cacheMap, idx)
if err != nil {
return "", err
}
@ -294,8 +293,5 @@ func(pg *Page) Render(sym string, idx uint16) (string, error) {
func(pg *Page) Reset() {
pg.sink = nil
pg.sinkSize = 0
pg.sinkProcessed = false
pg.cacheMap = make(map[string]string)
//pg.cacheMap = make(map[string]string)
}

View File

@ -8,6 +8,7 @@ import (
func TestPageCurrentSize(t *testing.T) {
t.Skip("usage is not in use, and it is unclear how it should be calculated")
ca := cache.NewCache()
pg := NewPage(ca, nil)
err := ca.Push()

View File

@ -316,8 +316,8 @@ func(st *State) SetInput(input []byte) error {
return nil
}
// Reset to initial state (start navigation over).
func(st *State) Reset() {
func(st *State) Reset() error {
return nil
}
func(st State) String() string {

View File

@ -88,7 +88,7 @@ func bar() error {
b = vm.NewLine(b, vm.HALT, nil, nil, nil)
b = vm.NewLine(b, vm.INCMP, []string{"*", "^"}, nil, nil)
tpl := "this is bar - an end node"
tpl := "this is bar - any input will return to top"
data := make(map[string]string)
data["pinky"] = "two"

View File

@ -103,10 +103,14 @@ func applyTarget(target []byte, st *state.State, ca cache.Memory, ctx context.Co
switch target[0] {
case '_':
sym, err = st.Up()
ca.Pop()
if err != nil {
return sym, idx, err
}
err = ca.Pop()
if err != nil {
return sym, idx, err
}
case '>':
idx, err = st.Next()
if err != nil {
@ -118,14 +122,31 @@ func applyTarget(target []byte, st *state.State, ca cache.Memory, ctx context.Co
return sym, idx, err
}
case '^':
_, err := st.SetFlag(state.FLAG_TERMINATE)
notTop := true
for notTop {
notTop, err := st.Top()
if notTop {
break
}
sym, err = st.Up()
if err != nil {
return sym, idx, err
}
err = ca.Pop()
if err != nil {
return sym, idx, err
}
}
default:
sym = string(target)
st.Down(sym)
ca.Push()
err := st.Down(sym)
if err != nil {
return sym, idx, err
}
err = ca.Push()
if err != nil {
return sym, idx, err
}
idx = 0
}
return sym, idx, nil

View File

@ -26,6 +26,7 @@ func NewVm(st *state.State, rs resource.Resource, ca cache.Memory, sizer *render
st: st,
rs: rs,
ca: ca,
pg: render.NewPage(ca, rs),
sizer: sizer,
}
vmi.Reset()
@ -34,7 +35,8 @@ func NewVm(st *state.State, rs resource.Resource, ca cache.Memory, sizer *render
func(vmi *Vm) Reset() {
vmi.mn = render.NewMenu()
vmi.pg = render.NewPage(vmi.ca, vmi.rs).WithMenu(vmi.mn)
vmi.pg.Reset()
vmi.pg = vmi.pg.WithMenu(vmi.mn) //render.NewPage(vmi.ca, vmi.rs).WithMenu(vmi.mn)
if vmi.sizer != nil {
vmi.pg = vmi.pg.WithSizer(vmi.sizer)
}

View File

@ -113,7 +113,9 @@ func TestRunLoadRender(t *testing.T) {
var err error
b := NewLine(nil, LOAD, []string{"one"}, []byte{0x0a}, nil)
b = NewLine(b, MAP, []string{"one"}, nil, nil)
b = NewLine(b, LOAD, []string{"two"}, []byte{0x0a}, nil)
b = NewLine(b, MAP, []string{"two"}, nil, nil)
b = NewLine(b, HALT, nil, nil, nil)
b, err = vm.Run(b, context.TODO())
if err != nil {
@ -129,6 +131,7 @@ func TestRunLoadRender(t *testing.T) {
}
b = NewLine(nil, LOAD, []string{"two"}, []byte{0x0a}, nil)
b = NewLine(b, MAP, []string{"two"}, nil, nil)
b = NewLine(b, HALT, nil, nil, nil)
b, err = vm.Run(b, context.TODO())
if err != nil {