Clone functions now filter ref variables to avoid to pass them to child context

This commit is contained in:
Celestino Amoroso 2024-05-01 05:53:56 +02:00
parent d657cbb51e
commit 924f5da725
2 changed files with 48 additions and 4 deletions

View File

@ -44,9 +44,11 @@ func NewSimpleFuncStore() *SimpleFuncStore {
}
func (ctx *SimpleFuncStore) Clone() ExprContext {
svs := ctx.SimpleVarStore
return &SimpleFuncStore{
SimpleVarStore: SimpleVarStore{varStore: CloneMap(ctx.varStore)},
funcStore: CloneMap(ctx.funcStore),
// SimpleVarStore: SimpleVarStore{varStore: CloneMap(ctx.varStore)},
SimpleVarStore: SimpleVarStore{varStore: svs.cloneVars()},
funcStore: CloneFilteredMap(ctx.funcStore, func(name string) bool { return name[0] != '@' }),
}
}

View File

@ -4,7 +4,10 @@
// simple-var-store.go
package expr
import "fmt"
import (
"fmt"
"strings"
)
type SimpleVarStore struct {
varStore map[string]any
@ -16,9 +19,14 @@ func NewSimpleVarStore() *SimpleVarStore {
}
}
func (ctx *SimpleVarStore) cloneVars() (vars map[string]any) {
return CloneFilteredMap(ctx.varStore, func(name string) bool { return name[0] != '@' })
}
func (ctx *SimpleVarStore) Clone() (clone ExprContext) {
// fmt.Println("*** Cloning context ***")
clone = &SimpleVarStore{
varStore: CloneMap(ctx.varStore),
varStore: ctx.cloneVars(),
}
return clone
}
@ -29,10 +37,12 @@ func (ctx *SimpleVarStore) GetVar(varName string) (v any, exists bool) {
}
func (ctx *SimpleVarStore) setVar(varName string, value any) {
// fmt.Printf("[%p] setVar(%v, %v)\n", ctx, varName, value)
ctx.varStore[varName] = value
}
func (ctx *SimpleVarStore) SetVar(varName string, value any) {
// fmt.Printf("[%p] SetVar(%v, %v)\n", ctx, varName, value)
if allowedValue, ok := fromGenericAny(value); ok {
ctx.varStore[varName] = allowedValue
} else {
@ -68,3 +78,35 @@ func (ctx *SimpleVarStore) RegisterFunc(name string, functor Functor, minArgs, m
func (ctx *SimpleVarStore) EnumFuncs(acceptor func(name string) (accept bool)) (funcNames []string) {
return
}
func CtxToBuilder(sb *strings.Builder, ctx ExprContext, indent int) {
sb.WriteString("{\n")
first := true
for _, name := range ctx.EnumVars(func(name string) bool { return name[0] != '_' }) {
value, _ := ctx.GetVar(name)
sb.WriteString(strings.Repeat("\t", indent+1))
sb.WriteString(name)
sb.WriteString("=")
if _, ok := value.(Functor); ok {
sb.WriteString(": func(){}")
} else if _, ok = value.(map[any]any); ok {
sb.WriteString("dict{}")
} else {
sb.WriteString(fmt.Sprintf("%v", value))
}
if first {
first = false
} else {
sb.WriteByte(',')
}
sb.WriteByte('\n')
}
sb.WriteString(strings.Repeat("\t", indent))
sb.WriteString("}\n")
}
func CtxToString(ctx ExprContext, indent int) string {
var sb strings.Builder
CtxToBuilder(&sb, ctx, indent)
return sb.String()
}