likebike / fasteval

Fast and safe evaluation of algebraic expressions
https://crates.io/crates/fasteval/
MIT License
267 stars 26 forks source link

Disabling functions like `print` #12

Open kotx opened 3 years ago

kotx commented 3 years ago

It might not be good to allow anybody who can eval expressions to print to console. I would like a way to do this in fasteval rather than checking for "print(" in the input or something.

likebike commented 3 years ago

Great idea. One way this might be implemented is by enabling the specification of the output stream, during the 'eval' step. (Right now, it always uses stderr.) This would allow the program to capture the output, or send it to /dev/null, etc.

likebike commented 3 years ago

I want to also mention that 'print' is a bit different than other functions. It has a special syntax and it is the only function that can receive String arguments. So there is probably no way to create a general "override" ability that works for all functions.

Originally, I did allow overriding of functions -- I would check the custom-defined namespace first and only use the built-in functions (like 'print' or 'cos') as a last resort. But as you can imagine, it was very slow to always be doing this kind of check. When I changed the priority of this library to focus on speed, I had to drop that feature.

One other possible way to achieve this would be a Cargo.toml "feature", so the developer could choose whether or not to enable 'print'.

kotx commented 3 years ago

One other possible way to achieve this would be a Cargo.toml "feature", so the developer could choose whether or not to enable 'print'.

This sounds good. It won't be as customizable, but seems easier to implement.