From c98633dd2bf3761a29719ede3b03efe85305dedd Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 20 May 2020 11:12:22 +0800 Subject: [PATCH] Add EvalPackage. --- README.md | 10 +++++++++- src/packages/eval.rs | 12 ++++++++++++ src/packages/mod.rs | 2 ++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/packages/eval.rs diff --git a/README.md b/README.md index b824d178..8673aba5 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ Disable script-defined functions (`no_function`) only when the feature is not ne [`Engine::new_raw`](#raw-engine) creates a _raw_ engine which does not register _any_ utility functions. This makes the scripting language quite useless as even basic arithmetic operators are not supported. -Selectively include the necessary functionalities by loading specific [packages](#packages) to minimize the footprint. +Selectively include the necessary functionalities by loading specific [packages] to minimize the footprint. Packages are sharable (even across threads via the [`sync`] feature), so they only have to be created once. Related @@ -380,6 +380,9 @@ Use `Engine::new_raw` to create a _raw_ `Engine`, in which _nothing_ is added, n ### Packages +[package]: #packages +[packages]: #packages + Rhai functional features are provided in different _packages_ that can be loaded via a call to `Engine::load_package`. Packages reside under `rhai::packages::*` and the trait `rhai::packages::Package` must be loaded in order for packages to be used. @@ -408,6 +411,7 @@ The follow packages are available: | `BasicMathPackage` | Basic math functions (e.g. `sin`, `sqrt`) | No | Yes | | `BasicArrayPackage` | Basic [array] functions | No | Yes | | `BasicMapPackage` | Basic [object map] functions | No | Yes | +| `EvalPackage` | Disable [`eval`] | No | No | | `CorePackage` | Basic essentials | | | | `StandardPackage` | Standard library | | | @@ -2669,6 +2673,8 @@ engine.set_optimization_level(rhai::OptimizationLevel::None); `eval` - or "How to Shoot Yourself in the Foot even Easier" --------------------------------------------------------- +[`eval`]: #eval---or-how-to-shoot-yourself-in-the-foot-even-easier + Saving the best for last: in addition to script optimizations, there is the ever-dreaded... `eval` function! ```rust @@ -2730,3 +2736,5 @@ fn alt_eval(script: String) -> Result<(), Box> { engine.register_result_fn("eval", alt_eval); ``` + +There is even a [package] named `EvalPackage` which implements the disabling override. diff --git a/src/packages/eval.rs b/src/packages/eval.rs new file mode 100644 index 00000000..a6756d96 --- /dev/null +++ b/src/packages/eval.rs @@ -0,0 +1,12 @@ +use crate::def_package; +use crate::module::FuncReturn; +use crate::stdlib::string::String; + +def_package!(crate:EvalPackage:"Disable 'eval'.", lib, { + lib.set_fn_1_mut( + "eval", + |_: &mut String| -> FuncReturn<()> { + Err("eval is evil!".into()) + }, + ); +}); diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 3e8a4f04..62df57a6 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -8,6 +8,7 @@ use crate::stdlib::{any::TypeId, boxed::Box, collections::HashMap, rc::Rc, sync: mod arithmetic; mod array_basic; +mod eval; mod iter_basic; mod logic; mod map_basic; @@ -21,6 +22,7 @@ mod time_basic; pub use arithmetic::ArithmeticPackage; #[cfg(not(feature = "no_index"))] pub use array_basic::BasicArrayPackage; +pub use eval::EvalPackage; pub use iter_basic::BasicIteratorPackage; pub use logic::LogicPackage; #[cfg(not(feature = "no_object"))]