Skip to content

fix: support ConstExpr with functions defined via expr.Function#955

Open
Yanhu007 wants to merge 1 commit intoexpr-lang:masterfrom
Yanhu007:fix/constexpr-with-function
Open

fix: support ConstExpr with functions defined via expr.Function#955
Yanhu007 wants to merge 1 commit intoexpr-lang:masterfrom
Yanhu007:fix/constexpr-with-function

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #809

Problem

expr.ConstExpr("fn") panics when fn was defined using expr.Function() instead of being a method on the environment struct:

expr.Function("identity", func(params ...any) (any, error) {
    return params[0], nil
}, new(func(any) any))

expr.Compile(`identity(1)`, expr.ConstExpr("identity")) // panic!

This is because ConstExpr only looks in the environment object via runtime.Fetch, not in the Functions table populated by expr.Function().

Fix

Check c.Functions first and extract the appropriate function implementation (Func, Fast, or Safe) before falling back to the environment object lookup.

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 expr-lang#809
@antonmedv
Copy link
Copy Markdown
Member

Could you please also add a test to cover this API usage?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

expr.ConstExpr panics if function was defined with expr.Function instead of env

2 participants