Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trying to understand expr lang more, is there anything that expression can do that goja / otto cant and vice versa? #669

Open
sprappcom opened this issue Jun 11, 2024 · 6 comments

Comments

@sprappcom
Copy link

as titled

@antonmedv
Copy link
Member

antonmedv commented Jun 12, 2024

Hello,

Let me explain the difference.

It may seem what expr and goja solves a similar problem, but they do it in a little bit different way. Also use cases can be different, goja targets more environments where JS may be needed, Expr on the other hand targets rule engines, filtering, and policy of different kinds.

A few differences between goja and expr:

  • Main difference is that Expr can be type checked. Expr comes with advance type checker which can use type defined directly in Go program. This is very useful, for example, if you want users to allow editing expressions in UI and on save button your program can type check the new expression and warn of any errors found.

  • goja/otto implement full JS, so you need to be aware of what users can write: infinite loops while(true);, self-recursions, an so on. Expr is non-Turing complete and has limited set of functionality available for users. Expr also has more expressive syntactic sugar, for example predicates all(tweets, .size <= 280). Also, in Expr you have full control other the language and what features available to users.

  • Patching. Another powerful tool in Expr is AST patching (expr-lang.org/docs/patch). Patching allows to modify expressions in any possible form before compilation. A lot of companies use this powerful feature in a lot of different ways.

  • Expr designed to be working with Go. Expr does not wrap values and uses them directly. As goja is JS vm, goja has to wrap all passed values into Value struct (sometimes with strange consequences) and unwrap ExportTo.

  • Speed. As Expr was designed specifically for Go and with business rule engines/expressions in mind, Expr is faster than goja/otto (bench). Expr uses own virtual machine, Expr has optimizing compiler, and Expr uses a few tweaks to speed up the Expr/Go communications.

  • Expr is dependencies free. Check out Expr's go.mod file. Expr has zero dependencies; this makes auditing process is much easier, easier to upgrade.

Both Expr and goja a very cool and powerful languages. And you need to choose what suits you best for the task you are working on.

@sprappcom
Copy link
Author

sprappcom commented Jun 13, 2024

thx so much for the comprehensive explanation.

just curious, i was looking at making my own cloudflare workers (which would prefer js than wasm), so was wondering if i can use expr as replacement?

i know expr is not using js but i was wondering will it be a good fit for FaaS kind of scenario,

also i'm hoping able to do "hot reload" like detecting file changes and reload expr lang script. <- any caveats? for example i wont use "golang plugin" reload as memory is not reclaimed for the old unused golang plugins.

will expr be a good fit for FaaS? what caveats do i have to look out for?

@antonmedv appreciate this last question. thx. will use expr in filtering too.

@antonmedv
Copy link
Member

will expr be a good fit for FaaS? what caveats do i have to look out for?

I don't have enough context to give meaningful advice here. ¯\_(ツ)_/¯

@PranavPeshwe
Copy link
Contributor

PranavPeshwe commented Aug 25, 2024

@sprappcom

will expr be a good fit for FaaS?

What aspects or use-cases (in a FaaS context) do you mean to refer to ? Or do you mean to ask whether expr (alone by itself) could serve as a full-blown FaaS runtime?
If it is the latter that you mean to ask, then the answer is no. Can answer in detail if you have any specific question(s).

@PranavPeshwe
Copy link
Contributor

@antonmedv

Expr is non-Turing complete

Never gave a thought to this until I read your comment. I think, expr is Turing complete. We have [practically] arbitrary memory addressing, some form or iteration/looping, and conditional code-flow, which should be enough to claim Turing completeness. If that is not enough, what definitely nails Turing completeness is the support for custom functions (i.e execution of arbitrary golang code).
WDYT?

@antonmedv
Copy link
Member

There is no doubt what you can turn expert into turning complete language with custom functions.

But only using builtin functions, not possible.

Here is an example: using only built-in functions produce Fibonacci sequence.

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

No branches or pull requests

3 participants