Clone functions now filter ref variables to avoid to pass them to child context
This commit is contained in:
parent
d657cbb51e
commit
924f5da725
@ -44,9 +44,11 @@ func NewSimpleFuncStore() *SimpleFuncStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *SimpleFuncStore) Clone() ExprContext {
|
func (ctx *SimpleFuncStore) Clone() ExprContext {
|
||||||
|
svs := ctx.SimpleVarStore
|
||||||
return &SimpleFuncStore{
|
return &SimpleFuncStore{
|
||||||
SimpleVarStore: SimpleVarStore{varStore: CloneMap(ctx.varStore)},
|
// SimpleVarStore: SimpleVarStore{varStore: CloneMap(ctx.varStore)},
|
||||||
funcStore: CloneMap(ctx.funcStore),
|
SimpleVarStore: SimpleVarStore{varStore: svs.cloneVars()},
|
||||||
|
funcStore: CloneFilteredMap(ctx.funcStore, func(name string) bool { return name[0] != '@' }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,10 @@
|
|||||||
// simple-var-store.go
|
// simple-var-store.go
|
||||||
package expr
|
package expr
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type SimpleVarStore struct {
|
type SimpleVarStore struct {
|
||||||
varStore map[string]any
|
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) {
|
func (ctx *SimpleVarStore) Clone() (clone ExprContext) {
|
||||||
|
// fmt.Println("*** Cloning context ***")
|
||||||
clone = &SimpleVarStore{
|
clone = &SimpleVarStore{
|
||||||
varStore: CloneMap(ctx.varStore),
|
varStore: ctx.cloneVars(),
|
||||||
}
|
}
|
||||||
return clone
|
return clone
|
||||||
}
|
}
|
||||||
@ -29,10 +37,12 @@ func (ctx *SimpleVarStore) GetVar(varName string) (v any, exists bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *SimpleVarStore) setVar(varName string, value any) {
|
func (ctx *SimpleVarStore) setVar(varName string, value any) {
|
||||||
|
// fmt.Printf("[%p] setVar(%v, %v)\n", ctx, varName, value)
|
||||||
ctx.varStore[varName] = value
|
ctx.varStore[varName] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *SimpleVarStore) SetVar(varName string, value any) {
|
func (ctx *SimpleVarStore) SetVar(varName string, value any) {
|
||||||
|
// fmt.Printf("[%p] SetVar(%v, %v)\n", ctx, varName, value)
|
||||||
if allowedValue, ok := fromGenericAny(value); ok {
|
if allowedValue, ok := fromGenericAny(value); ok {
|
||||||
ctx.varStore[varName] = allowedValue
|
ctx.varStore[varName] = allowedValue
|
||||||
} else {
|
} 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) {
|
func (ctx *SimpleVarStore) EnumFuncs(acceptor func(name string) (accept bool)) (funcNames []string) {
|
||||||
return
|
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()
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user