From 6273296ad62a4518433f0fdb43a53e77b1a66944 Mon Sep 17 00:00:00 2001 From: Yanhu007 Date: Mon, 13 Apr 2026 06:06:08 +0800 Subject: [PATCH] fix: support ConstExpr with functions defined via expr.Function ConstExpr panics when the named function was defined using expr.Function() instead of being a method on the environment struct. This is because ConstExpr only looks in the environment object via runtime.Fetch, not in the Functions table. Check c.Functions first and extract the appropriate function implementation (Func, Fast, or Safe) before falling back to the environment object lookup. Fixes #809 --- conf/config.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/conf/config.go b/conf/config.go index f7c95d20..ffae53a5 100644 --- a/conf/config.go +++ b/conf/config.go @@ -74,6 +74,22 @@ func (c *Config) WithEnv(env any) { } func (c *Config) ConstExpr(name string) { + // Check if the function is defined via expr.Function first. + if fn, ok := c.Functions[name]; ok { + if fn.Func != nil { + c.ConstFns[name] = reflect.ValueOf(fn.Func) + return + } + if fn.Fast != nil { + c.ConstFns[name] = reflect.ValueOf(fn.Fast) + return + } + if fn.Safe != nil { + c.ConstFns[name] = reflect.ValueOf(fn.Safe) + return + } + panic(fmt.Errorf("const expression %q must have a function implementation", name)) + } if c.EnvObject == nil { panic("no environment is specified for ConstExpr()") }