You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Request/Question] Support proxies for expr.Env #777
I've a question - does expr support having proxies as expr.Env (something similar to Proxy feature in JS)?
I have a situation where I've to deal with a chain of objects as env that have prototype inheritance (also like in JS).
As env itself is dynamic (might depend on context, environment vars, etc), in order to build expr.Env each time I need to run an expression - it's necessary to traverse a whole prototype chain and create a new map.
Feature Benefits
This feature will allow to handle complex cases such as:
Cases when value have to be pulled dynamically in runtime.
To abstract away env when it has a complex structure, such as prototype chain.
Avoid allocating additional maps or structs for env.
Proposed solutions
Global & Nested Proxy
Ideally, to handle such cases, expr.Env could consume a ProxyEnv interface that will be called to get a variable:
// feel free to provide a better nametypeProxyEnvinterface {
GetValue(pathstring) (bool, any)
}
If returned value implements ProxyEnv, it also should be treated as proxy:
Expr patcher designed to point to original code location! Actually a lot of optimizations is done via patcher. But errors always return original code allocation.
Activity
antonmedv commentedon Mar 30, 2025
I think we can add those features.
What about this case:
x1unix commentedon Mar 30, 2025
@antonmedv good question!
Please check updated proposal.
antonmedv commentedon Mar 31, 2025
What about something like this:
And on an expression:
Proxy will be called with
x1unix commentedon Mar 31, 2025
@antonmedv as far as I know, expr supports variables.
It might be complex to handle such cases:
imho it might be easier to support nested
ProxyEnvinstead.Also user might want to have a proxy only for specific fields (e.g. only for
foo.barbut not forfoo.bar.baz)x1unix commentedon Mar 31, 2025
@antonmedv if you're interested, I can try to make a PR after the proposal will be finalized.
antonmedv commentedon Mar 31, 2025
I think implementation should be done via patcher which will replace property access with function cause.
x1unix commentedon Apr 7, 2025
@antonmedv if patched code will throw an error, would error position point to an original (pre-patched) code?
Let's imagine a code like this:
and patched version would look like this:
Ideally returned
EvalErrorshould be transparently mapped to original code:Imho native object proxy support will make implementation much more simple.
antonmedv commentedon Apr 7, 2025
Expr patcher designed to point to original code location! Actually a lot of optimizations is done via patcher. But errors always return original code allocation.