rhaiscript / rhai

Rhai - An embedded scripting language for Rust.
https://crates.io/crates/rhai
Apache License 2.0
3.79k stars 177 forks source link

Option to eval without mutating Engine #99

Closed Imberflur closed 4 years ago

Imberflur commented 4 years ago

In some cases, I don't need the script to define any functions instead I want it to perform some calculations and return a value without modifying the engine. This could be accomplished by having the script define a function and then use engine.call_fn but this requires that initial mutation of the engine to define that function.

schungx commented 4 years ago

What is wrong with just eval? Pass in your function script and it gets evaluated...

The function can be defined within the script itself using the fn keyword.

If you want to run the function multiple times and don't want to keep parsing it, first compile the function into an AST then eval_ast it.

schungx commented 4 years ago

https://github.com/jonathandturner/rhai/pull/98

In this PR, Engine cleans up after itself.