2024-04-26 20:12:56 +02:00
|
|
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
|
|
// All rights reserved.
|
|
|
|
|
|
|
|
// context-helpers.go
|
|
|
|
package expr
|
|
|
|
|
|
|
|
func cloneContext(sourceCtx ExprContext) (clonedCtx ExprContext) {
|
|
|
|
if sourceCtx != nil {
|
|
|
|
clonedCtx = sourceCtx.Clone()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-27 06:16:11 +02:00
|
|
|
func exportVar(ctx ExprContext, name string, value any) {
|
|
|
|
if name[0] == '@' {
|
|
|
|
name = name[1:]
|
|
|
|
}
|
2024-05-19 01:27:44 +02:00
|
|
|
ctx.UnsafeSetVar(name, value)
|
2024-04-27 06:16:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func exportFunc(ctx ExprContext, name string, info ExprFunc) {
|
|
|
|
if name[0] == '@' {
|
|
|
|
name = name[1:]
|
|
|
|
}
|
2024-05-22 20:52:44 +02:00
|
|
|
// ctx.RegisterFunc(name, info.Functor(), info.MinArgs(), info.MaxArgs())
|
|
|
|
// ctx.RegisterFuncInfo(name, info)
|
2024-05-24 06:28:48 +02:00
|
|
|
ctx.RegisterFunc(name, info.Functor(), info.ReturnType(), info.Params())
|
2024-04-27 06:16:11 +02:00
|
|
|
}
|
|
|
|
|
2024-04-26 20:12:56 +02:00
|
|
|
func exportObjects(destCtx, sourceCtx ExprContext) {
|
|
|
|
exportAll := isEnabled(sourceCtx, control_export_all)
|
2024-05-01 05:57:08 +02:00
|
|
|
// fmt.Printf("Exporting from sourceCtx [%p] to destCtx [%p] -- exportAll=%t\n", sourceCtx, destCtx, exportAll)
|
2024-04-26 20:12:56 +02:00
|
|
|
// Export variables
|
|
|
|
for _, refName := range sourceCtx.EnumVars(func(name string) bool { return exportAll || name[0] == '@' }) {
|
2024-05-01 05:57:08 +02:00
|
|
|
// fmt.Printf("\tExporting %q\n", refName)
|
2024-04-26 20:12:56 +02:00
|
|
|
refValue, _ := sourceCtx.GetVar(refName)
|
|
|
|
exportVar(destCtx, refName, refValue)
|
|
|
|
}
|
|
|
|
// Export functions
|
|
|
|
for _, refName := range sourceCtx.EnumFuncs(func(name string) bool { return exportAll || name[0] == '@' }) {
|
|
|
|
if info, _ := sourceCtx.GetFuncInfo(refName); info != nil {
|
|
|
|
exportFunc(destCtx, refName, info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|