From 5e4847849671846e31c82a625e2e4066cd8ab78e Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 26 Jul 2020 10:07:40 +0800 Subject: [PATCH 1/2] Improve writeup. --- README.md | 2 +- doc/src/SUMMARY.md | 21 ++++----- doc/src/about/index.md | 5 ++- doc/src/about/license.md | 3 +- doc/src/about/non-design.md | 23 ++++++++-- doc/src/advanced.md | 4 +- doc/src/engine/hello-world.md | 30 ++++++++----- doc/src/engine/raw.md | 9 ++-- doc/src/language/for.md | 3 +- doc/src/language/if.md | 3 ++ doc/src/language/logic.md | 39 ---------------- doc/src/language/method.md | 13 +++--- doc/src/language/other-op.md | 66 ++++++++++++++++++++++++++++ doc/src/rust/custom.md | 4 +- doc/src/rust/functions.md | 6 +-- doc/src/rust/modules/imp-resolver.md | 2 +- doc/src/rust/override.md | 4 +- doc/src/start/builds/performance.md | 4 +- doc/src/start/features.md | 4 +- 19 files changed, 156 insertions(+), 89 deletions(-) create mode 100644 doc/src/language/other-op.md diff --git a/README.md b/README.md index f5431f6e..1516abcc 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ Licensed under either: * [Apache License, Version 2.0](https://github.com/jonathandturner/rhai/blob/master/LICENSE-APACHE.txt), or * [MIT license](https://github.com/jonathandturner/rhai/blob/master/LICENSE-MIT.txt) -at your option. +at your choice. Unless explicitly stated otherwise, any contribution intentionally submitted for inclusion in this crate, as defined in the Apache-2.0 license, shall diff --git a/doc/src/SUMMARY.md b/doc/src/SUMMARY.md index b1e5a561..3592dbc5 100644 --- a/doc/src/SUMMARY.md +++ b/doc/src/SUMMARY.md @@ -21,7 +21,7 @@ The Rhai Scripting Language 2. [Scripts](start/examples/scripts.md) 3. [Using the `Engine`](engine/index.md) 1. [Hello World in Rhai - Evaluate a Script](engine/hello-world.md) - 2. [Compile a Script to AST for Repeated Evaluations](engine/compile.md) + 2. [Compile to AST for Repeated Evaluations](engine/compile.md) 3. [Call a Rhai Function from Rust](engine/call-fn.md) 4. [Create a Rust Anonymous Function from a Rhai Function](engine/func.md) 5. [Evaluate Expressions Only](engine/expressions.md) @@ -65,21 +65,22 @@ The Rhai Scripting Language 5. [Variables](language/variables.md) 6. [Constants](language/constants.md) 7. [Logic Operators](language/logic.md) - 8. [If Statement](language/if.md) - 9. [While Loop](language/while.md) - 10. [Loop Statement](language/loop.md) - 11. [For Loop](language/for.md) - 12. [Return Values](language/return.md) - 13. [Throw Exception on Error](language/throw.md) - 14. [Functions](language/functions.md) + 8. [Other Operators](language/other-op.md) + 9. [If Statement](language/if.md) + 10. [While Loop](language/while.md) + 11. [Loop Statement](language/loop.md) + 12. [For Loop](language/for.md) + 13. [Return Values](language/return.md) + 14. [Throw Exception on Error](language/throw.md) + 15. [Functions](language/functions.md) 1. [Call Method as Function](language/method.md) 2. [Overloading](language/overload.md) 3. [Namespaces](language/fn-namespaces.md) 4. [Function Pointers](language/fn-ptr.md) 5. [Anonymous Functions](language/fn-anon.md) 6. [Currying](language/fn-curry.md) - 15. [Print and Debug](language/print-debug.md) - 16. [Modules](language/modules/index.md) + 16. [Print and Debug](language/print-debug.md) + 17. [Modules](language/modules/index.md) 1. [Export Variables, Functions and Sub-Modules](language/modules/export.md) 2. [Import Modules](language/modules/import.md) 3. [Create from Rust](rust/modules/index.md) diff --git a/doc/src/about/index.md b/doc/src/about/index.md index 76603bde..cf021da5 100644 --- a/doc/src/about/index.md +++ b/doc/src/about/index.md @@ -7,6 +7,9 @@ Rhai is an embedded scripting language and evaluation engine for Rust that gives to add scripting to any application. -This Book is for version {{version}} of Rhai. +Versions +-------- + +This Book is for version **{{version}}** of Rhai. For the latest development version, see [here]({{rootUrl}}/vnext/). diff --git a/doc/src/about/license.md b/doc/src/about/license.md index a60ac1eb..a1f24339 100644 --- a/doc/src/about/license.md +++ b/doc/src/about/license.md @@ -6,9 +6,10 @@ Licensing Rhai is licensed under either: * [Apache License, Version 2.0]({{repoHome}}/LICENSE-APACHE.txt), or + * [MIT license]({{repoHome}}/LICENSE-MIT.txt) -at your option. +at your choice. Unless explicitly stated otherwise, any contribution intentionally submitted for inclusion in this crate, as defined in the Apache-2.0 license, shall be dual-licensed as above, diff --git a/doc/src/about/non-design.md b/doc/src/about/non-design.md index 52888164..7f40614e 100644 --- a/doc/src/about/non-design.md +++ b/doc/src/about/non-design.md @@ -18,21 +18,38 @@ It doesn't attempt to be a new language. For example: * No first-class functions - Code your functions in Rust instead, and register them with Rhai. - There is, however, support for simple [function pointers] allowing runtime dispatch by function name. + There is, however, support for simple [function pointers] to allow runtime dispatch by function name. * No garbage collection - this should be expected, so... * No closures - do your closure magic in Rust instead; [turn a Rhai scripted function into a Rust closure]({{rootUrl}}/engine/call-fn.md). + But you can [curry][currying] a [function pointer] with arguments to simulate it somewhat. + * No byte-codes/JIT - Rhai has an AST-walking interpreter which will not win any speed races. The purpose of Rhai is not to be extremely _fast_, but to make it as easy as possible to integrate with native Rust applications. + +Do Not Write The Next 4D VR Game in Rhai +--------------------------------------- + Due to this intended usage, Rhai deliberately keeps the language simple and small by omitting advanced language features such as classes, inheritance, first-class functions, closures, concurrency, byte-codes, JIT etc. Avoid the temptation to write full-fledge application logic entirely in Rhai - that use case is best fulfilled by more complete languages such as JavaScript or Lua. -Therefore, in actual practice, it is usually best to expose a Rust API into Rhai for scripts to call. -All your core functionalities should be in Rust. + +Thin Dynamic Wrapper Layer Over Rust Code +---------------------------------------- + +In actual practice, it is usually best to expose a Rust API into Rhai for scripts to call. + +All the core functionalities should be written in Rust, with Rhai being the dynamic _control_ layer. + This is similar to some dynamic languages where most of the core functionalities reside in a C/C++ standard library. + +Another similar scenario is a web front-end driving back-end services written in a systems language. +In this case, JavaScript takes the role of Rhai while the back-end language, well... it can actually also be Rust. +Except that Rhai integrates with Rust _much_ more tightly, removing the need for interfaces such +as XHR calls and payload encoding such as JSON. diff --git a/doc/src/advanced.md b/doc/src/advanced.md index b7d69e39..cd958c73 100644 --- a/doc/src/advanced.md +++ b/doc/src/advanced.md @@ -5,7 +5,7 @@ Advanced Topics This section covers advanced features such as: -* Simulated [Object Oriented Programming][OOP]. +* Simulated [Object Oriented Programming (OOP)][OOP]. * [`serde`] integration. @@ -13,4 +13,6 @@ This section covers advanced features such as: * [Domain-Specific Languages][DSL]. +* Low-level [function registration API]({{rootUrl}}/rust/register-raw.md) + * The dreaded (or beloved for those with twisted tastes) [`eval`] statement. diff --git a/doc/src/engine/hello-world.md b/doc/src/engine/hello-world.md index c4e868af..908a8cf8 100644 --- a/doc/src/engine/hello-world.md +++ b/doc/src/engine/hello-world.md @@ -22,19 +22,31 @@ fn main() -> Result<(), Box> } ``` -`rhai::EvalAltResult` is a Rust `enum` containing all errors encountered during the parsing or evaluation process. +Evaluate a script file directly: + +```rust +// 'eval_file' takes a 'PathBuf' +let result = engine.eval_file::("hello_world.rhai".into())?; +``` -Evaluate a Script ----------------- +Error Type +---------- -The type parameter is used to specify the type of the return value, which _must_ match the actual type or an error is returned. -Rhai is very strict here. +`rhai::EvalAltResult` is the standard Rhai error type, which is a Rust `enum` containing all errors encountered +during the parsing or evaluation process. -Use [`Dynamic`] for uncertain return types. + +Return Type +----------- + +The type parameter for `Engine::eval` is used to specify the type of the return value, +which _must_ match the actual type or an error is returned. Rhai is very strict here. There are two ways to specify the return type - _turbofish_ notation, or type inference. +Use [`Dynamic`] for uncertain return types. + ```rust let result = engine.eval::("40 + 2")?; // return type is i64, specified using 'turbofish' notation @@ -46,9 +58,3 @@ let result: Dynamic = engine.eval("boo()")?; // use 'Dynamic' if you're not s let result = engine.eval::("40 + 2")?; // returns an error because the actual return type is i64, not String ``` - -Evaluate a script file directly: - -```rust -let result = engine.eval_file::("hello_world.rhai".into())?; // 'eval_file' takes a 'PathBuf' -``` diff --git a/doc/src/engine/raw.md b/doc/src/engine/raw.md index ce61aa91..06e4ff33 100644 --- a/doc/src/engine/raw.md +++ b/doc/src/engine/raw.md @@ -10,7 +10,10 @@ In many controlled embedded environments, however, these may not be needed and u application code storage space. Use `Engine::new_raw` to create a _raw_ `Engine`, in which only a minimal set of -basic arithmetic and logical operators are supported. +basic arithmetic and logical operators are supported (see below). + +To add more functionalities to a _raw_ `Engine`, load [packages] into it. + Built-in Operators ------------------ @@ -20,7 +23,7 @@ Built-in Operators | `+`, | `+=` | `INT`, `FLOAT` (if not [`no_float`]), `ImmutableString` | | `-`, `*`, `/`, `%`, `~`, | `-=`, `*=`, `/=`, `%=`, `~=` | `INT`, `FLOAT` (if not [`no_float`]) | | `<<`, `>>`, `^`, | `<<=`, `>>=`, `^=` | `INT` | -| `&`, `|`, | `&=`, `|=` | `INT`, `bool` | -| `&&`, `||` | | `bool` | +| `&`, \|, | `&=`, \|= | `INT`, `bool` | +| `&&`, \|\| | | `bool` | | `==`, `!=` | | `INT`, `FLOAT` (if not [`no_float`]), `bool`, `char`, `()`, `ImmutableString` | | `>`, `>=`, `<`, `<=` | | `INT`, `FLOAT` (if not [`no_float`]), `char`, `()`, `ImmutableString` | diff --git a/doc/src/language/for.md b/doc/src/language/for.md index 6780a1cd..15ea22e2 100644 --- a/doc/src/language/for.md +++ b/doc/src/language/for.md @@ -3,7 +3,8 @@ {{#include ../links.md}} -Iterating through a range or an [array] is provided by the `for` ... `in` loop. +Iterating through a range or an [array], or any type with a registered _iterator_, +is provided by the `for` ... `in` loop. Like C, `continue` can be used to skip to the next iteration, by-passing all following statements; `break` can be used to break out of the loop unconditionally. diff --git a/doc/src/language/if.md b/doc/src/language/if.md index 3958a2e3..b9c63dab 100644 --- a/doc/src/language/if.md +++ b/doc/src/language/if.md @@ -19,6 +19,9 @@ if foo(x) { } ``` +Braces Are Mandatory +-------------------- + Unlike C, the condition expression does _not_ need to be enclosed in parentheses '`(`' .. '`)`', but all branches of the `if` statement must be enclosed within braces '`{`' .. '`}`', even when there is only one statement inside the branch. diff --git a/doc/src/language/logic.md b/doc/src/language/logic.md index 113641f6..699b9827 100644 --- a/doc/src/language/logic.md +++ b/doc/src/language/logic.md @@ -65,42 +65,3 @@ a() | b(); // both a() and b() are evaluated a() & b(); // both a() and b() are evaluated ``` - -Compound Assignment Operators ----------------------------- - -```rust -let number = 9; - -number += 8; // number = number + 8 - -number -= 7; // number = number - 7 - -number *= 6; // number = number * 6 - -number /= 5; // number = number / 5 - -number %= 4; // number = number % 4 - -number ~= 3; // number = number ~ 3 - -number <<= 2; // number = number << 2 - -number >>= 1; // number = number >> 1 - -number &= 0x00ff; // number = number & 0x00ff; - -number |= 0x00ff; // number = number | 0x00ff; - -number ^= 0x00ff; // number = number ^ 0x00ff; -``` - -The `+=` operator can also be used to build [strings]: - -```rust -let my_str = "abc"; -my_str += "ABC"; -my_str += 12345; - -my_str == "abcABC12345" -``` diff --git a/doc/src/language/method.md b/doc/src/language/method.md index 6b6b9314..c2e9e99e 100644 --- a/doc/src/language/method.md +++ b/doc/src/language/method.md @@ -4,8 +4,8 @@ Call Method as Function {{#include ../links.md}} -First `&mut` Reference Parameter -------------------------------- +First `&mut` Parameter +---------------------- Property [getters/setters] and [methods][custom types] in a Rust custom type registered with the [`Engine`] can be called just like a regular function. In fact, like Rust, property getters/setters and object methods @@ -37,19 +37,20 @@ array[0].update(); // <- call in method-call style will update 'a' ``` -Encouraged Usage ----------------- +`&mut` is Efficient +------------------ Using a `&mut` first parameter is highly encouraged when using types that are expensive to clone, even when the intention is not to mutate that argument, because it avoids cloning that argument value. -For primary types that are cheap to clone, including `ImmutableString`, this is not necessary. +For primary types that are cheap to clone (e.g. those that implement `Copy`), +including `ImmutableString`, this is not necessary. Avoid `&mut ImmutableString` --------------------------- -`ImmutableString`, Rhai internal [string] type, is an exception. +`ImmutableString`, Rhai's internal [string] type, is an exception. `ImmutableString` is cheap to clone, but expensive to take a mutable reference (because the underlying string must be cloned to make a private copy). diff --git a/doc/src/language/other-op.md b/doc/src/language/other-op.md new file mode 100644 index 00000000..bf5017d3 --- /dev/null +++ b/doc/src/language/other-op.md @@ -0,0 +1,66 @@ +Other Operators +=============== + +{{#include ../links.md}} + + +Compound Assignment Operators +---------------------------- + +```rust +let number = 9; + +number += 8; // number = number + 8 + +number -= 7; // number = number - 7 + +number *= 6; // number = number * 6 + +number /= 5; // number = number / 5 + +number %= 4; // number = number % 4 + +number ~= 3; // number = number ~ 3 + +number <<= 2; // number = number << 2 + +number >>= 1; // number = number >> 1 + +number &= 0x00ff; // number = number & 0x00ff; + +number |= 0x00ff; // number = number | 0x00ff; + +number ^= 0x00ff; // number = number ^ 0x00ff; +``` + + +The Flexible `+=` +---------------- + +The `+=` operator can also be used to build [strings]: + +```rust +let my_str = "abc"; +my_str += "ABC"; +my_str += 12345; + +my_str == "abcABC12345" +``` + +It may also be used to concatenate [arrays]: + +```rust +let my_array = [1, 2, 3]; +my_array += [4, 5]; + +my_array == [1, 2, 3, 4, 5]; +``` + +or mix two [object maps] together: + +```rust +let my_obj = #{a:1, b:2}; +my_obj += #{c:3, d:4, e:5}; + +my_obj.len() == 5; +``` diff --git a/doc/src/rust/custom.md b/doc/src/rust/custom.md index 7460f50e..3e6b5ca3 100644 --- a/doc/src/rust/custom.md +++ b/doc/src/rust/custom.md @@ -66,8 +66,8 @@ let mut engine = Engine::new(); engine.register_type::(); ``` -Methods on Custom Type ---------------------- +Methods on The Custom Type +------------------------- To use native custom types, methods and functions in Rhai scripts, simply register them using one of the `Engine::register_XXX` API. diff --git a/doc/src/rust/functions.md b/doc/src/rust/functions.md index a929484c..8d8666cf 100644 --- a/doc/src/rust/functions.md +++ b/doc/src/rust/functions.md @@ -11,8 +11,8 @@ see [fallible functions]({{rootUrl}}/rust/fallible.md)). ```rust use rhai::{Dynamic, Engine, EvalAltResult, ImmutableString}; -use rhai::RegisterFn; // use 'RegisterFn' trait for 'register_fn' -use rhai::RegisterResultFn; // use 'RegisterResultFn' trait for 'register_result_fn' +use rhai::RegisterFn; // use 'RegisterFn' trait for 'register_fn' +use rhai::RegisterResultFn; // use 'RegisterResultFn' trait for 'register_result_fn' // Normal function that returns a standard type // Remember to use 'ImmutableString' and not 'String' @@ -26,7 +26,7 @@ fn add_len_str(x: i64, s: &str) -> i64 { // Function that returns a 'Dynamic' value - must return a 'Result' fn get_any_value() -> Result> { - Ok((42_i64).into()) // standard types can use 'into()' + Ok((42_i64).into()) // standard types can use 'into()' } let mut engine = Engine::new(); diff --git a/doc/src/rust/modules/imp-resolver.md b/doc/src/rust/modules/imp-resolver.md index 415abf72..55d68315 100644 --- a/doc/src/rust/modules/imp-resolver.md +++ b/doc/src/rust/modules/imp-resolver.md @@ -30,7 +30,7 @@ impl ModuleResolver for MyModuleResolver { &self, engine: &Engine, // reference to the current 'Engine' path: &str, // the module path - pos: Position, // location of the 'import' statement + pos: Position, // position of the 'import' statement ) -> Result> { // Check module path. if is_valid_module_path(path) { diff --git a/doc/src/rust/override.md b/doc/src/rust/override.md index 576b5ad8..f6d4d656 100644 --- a/doc/src/rust/override.md +++ b/doc/src/rust/override.md @@ -12,7 +12,9 @@ fn to_int(num) { print("Ha! Gotcha! " + num); } -print(to_int(123)); // what happens? +let x = (123).to_int(); + +print(x); // what happens? ``` A registered native Rust function, in turn, overrides any built-in function of the diff --git a/doc/src/start/builds/performance.md b/doc/src/start/builds/performance.md index dc47a4cc..27747208 100644 --- a/doc/src/start/builds/performance.md +++ b/doc/src/start/builds/performance.md @@ -37,8 +37,8 @@ Use `ImmutableString` Internally, Rhai uses _immutable_ [strings] instead of the Rust `String` type. This is mainly to avoid excessive cloning when passing function arguments. -The encapsulated immutable string type is `ImmutableString`. It is cheap to clone (just an `Rc` or `Arc` reference -count increment depending on the [`sync`] feature). +Rhai's internal string type is `ImmutableString` (basically `Rc` or `Arc` depending on the [`sync`] feature). +It is cheap to clone, but expensive to modify (a new copy of the string must be made in order to change it). Therefore, functions taking `String` parameters should use `ImmutableString` or `&str` (which maps to `ImmutableString`) for the best performance with Rhai. diff --git a/doc/src/start/features.md b/doc/src/start/features.md index 023834fd..f2286a38 100644 --- a/doc/src/start/features.md +++ b/doc/src/start/features.md @@ -25,7 +25,7 @@ more control over what a script can (or cannot) do. | `no_module` | Disable loading external [modules]. | | `no_std` | Build for `no-std`. Notice that additional dependencies will be pulled in to replace `std` features. | | `serde` | Enable serialization/deserialization via [`serde`]. Notice that the [`serde`](https://crates.io/crates/serde) crate will be pulled in together with its dependencies. | -| `internals` | Expose internal data structures (e.g. [`AST`] nodes) and enable defining [custom syntax]. Beware that Rhai internals are volatile and may change from version to version. | +| `internals` | Expose internal data structures (e.g. [`AST`] nodes). Beware that Rhai internals are volatile and may change from version to version. | Example @@ -46,7 +46,7 @@ rhai = { version = "{{version}}", features = [ "sync", "unchecked", "only_i32", ``` The resulting scripting engine supports only the `i32` integer numeral type (and no others like `u32`, `i16` or `i64`), -no floating-point, is `Send + Sync` (so it can be safely used across threads), does not support defining [functions] +no floating-point, is `Send + Sync` (so it can be safely used across threads), and does not support defining [functions] nor loading external [modules]. This configuration is perfect for an expression parser in a 32-bit embedded system without floating-point hardware. From e7af008d744bc200515b66c4401a9613c2da7d98 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 26 Jul 2020 13:51:09 +0800 Subject: [PATCH 2/2] Avoid warnings. --- src/any.rs | 4 +- src/api.rs | 9 +- src/engine.rs | 199 ++++++++++++++++++++++-------------- src/fn_args.rs | 7 +- src/fn_call.rs | 34 +++--- src/fn_native.rs | 19 +++- src/fn_register.rs | 5 +- src/lib.rs | 2 +- src/module.rs | 56 ++++++---- src/optimize.rs | 32 ++++-- src/packages/arithmetic.rs | 37 +++++-- src/packages/array_basic.rs | 19 ++-- src/packages/map_basic.rs | 8 +- src/packages/math_basic.rs | 12 ++- src/packages/mod.rs | 1 - src/packages/string_more.rs | 16 +-- src/packages/time_basic.rs | 7 +- src/parser.rs | 118 ++++++++++++++++++--- src/scope.rs | 2 + src/serde/de.rs | 19 ++-- src/serde/ser.rs | 33 +++--- src/settings.rs | 41 ++++---- src/token.rs | 5 +- tests/call_fn.rs | 4 +- 24 files changed, 464 insertions(+), 225 deletions(-) diff --git a/src/any.rs b/src/any.rs index 1d478e62..ea959db7 100644 --- a/src/any.rs +++ b/src/any.rs @@ -19,9 +19,11 @@ use crate::stdlib::{ collections::HashMap, fmt, string::String, - vec::Vec, }; +#[cfg(not(feature = "no_index"))] +use crate::stdlib::vec::Vec; + #[cfg(not(feature = "no_std"))] #[cfg(not(target_arch = "wasm32"))] use crate::stdlib::time::Instant; diff --git a/src/api.rs b/src/api.rs index c4c91f38..0e8373e0 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,9 +1,8 @@ //! Module that defines the extern API of `Engine`. use crate::any::{Dynamic, Variant}; -use crate::engine::{make_getter, make_setter, Engine, Imports, State, FN_IDX_GET, FN_IDX_SET}; +use crate::engine::{make_getter, make_setter, Engine, Imports, State}; use crate::error::ParseError; -use crate::fn_args::FuncArgs; use crate::fn_native::{IteratorFn, SendSync}; use crate::fn_register::RegisterFn; use crate::module::{FuncReturn, Module}; @@ -12,13 +11,15 @@ use crate::parser::AST; use crate::result::EvalAltResult; use crate::scope::Scope; use crate::token::{lex, Position}; -use crate::utils::StaticVec; + +#[cfg(not(feature = "no_index"))] +use crate::engine::{FN_IDX_GET, FN_IDX_SET}; #[cfg(not(feature = "no_object"))] use crate::engine::Map; #[cfg(not(feature = "no_function"))] -use crate::engine::get_script_function_by_signature; +use crate::{engine::get_script_function_by_signature, fn_args::FuncArgs, utils::StaticVec}; use crate::stdlib::{ any::{type_name, TypeId}, diff --git a/src/engine.rs b/src/engine.rs index b7b73f2a..78c05030 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -4,10 +4,10 @@ use crate::any::{map_std_type_name, Dynamic, Union, Variant}; use crate::calc_fn_hash; use crate::fn_call::run_builtin_op_assignment; use crate::fn_native::{CallableFunction, Callback, FnPtr}; -use crate::module::{resolvers, Module, ModuleRef, ModuleResolver}; +use crate::module::{Module, ModuleRef}; use crate::optimize::OptimizationLevel; use crate::packages::{Package, PackagesCollection, StandardPackage}; -use crate::parser::{Expr, FnAccess, ImmutableString, ReturnType, ScriptFnDef, Stmt}; +use crate::parser::{Expr, ImmutableString, ReturnType, Stmt}; use crate::r#unsafe::unsafe_cast_var_name_to_lifetime; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; @@ -15,8 +15,13 @@ use crate::syntax::{CustomSyntax, EvalContext}; use crate::token::Position; use crate::utils::StaticVec; +#[cfg(not(feature = "no_function"))] +use crate::parser::{FnAccess, ScriptFnDef}; + +#[cfg(not(feature = "no_module"))] +use crate::module::{resolvers, ModuleResolver}; + use crate::stdlib::{ - any::TypeId, borrow::Cow, boxed::Box, collections::{HashMap, HashSet}, @@ -26,6 +31,9 @@ use crate::stdlib::{ vec::Vec, }; +#[cfg(not(feature = "no_index"))] +use crate::stdlib::any::TypeId; + /// Variable-sized array of `Dynamic` values. /// /// Not available under the `no_index` feature. @@ -66,13 +74,6 @@ pub const MAX_EXPR_DEPTH: usize = 128; #[cfg(not(debug_assertions))] pub const MAX_FUNCTION_EXPR_DEPTH: usize = 32; -#[cfg(feature = "unchecked")] -pub const MAX_CALL_STACK_DEPTH: usize = usize::MAX; -#[cfg(feature = "unchecked")] -pub const MAX_EXPR_DEPTH: usize = 0; -#[cfg(feature = "unchecked")] -pub const MAX_FUNCTION_EXPR_DEPTH: usize = 0; - pub const KEYWORD_PRINT: &str = "print"; pub const KEYWORD_DEBUG: &str = "debug"; pub const KEYWORD_TYPE_OF: &str = "type_of"; @@ -86,6 +87,7 @@ pub const FN_GET: &str = "get$"; pub const FN_SET: &str = "set$"; pub const FN_IDX_GET: &str = "index$get$"; pub const FN_IDX_SET: &str = "index$set$"; +#[cfg(not(feature = "no_function"))] pub const FN_ANONYMOUS: &str = "anon$"; pub const MARKER_EXPR: &str = "$expr$"; pub const MARKER_BLOCK: &str = "$block$"; @@ -108,6 +110,7 @@ pub enum Target<'a> { Value(Dynamic), /// The target is a character inside a String. /// This is necessary because directly pointing to a char inside a String is impossible. + #[cfg(not(feature = "no_index"))] StringChar(&'a mut Dynamic, usize, Dynamic), } @@ -116,7 +119,9 @@ impl Target<'_> { pub fn is_ref(&self) -> bool { match self { Self::Ref(_) => true, - Self::Value(_) | Self::StringChar(_, _, _) => false, + Self::Value(_) => false, + #[cfg(not(feature = "no_index"))] + Self::StringChar(_, _, _) => false, } } /// Is the `Target` an owned value? @@ -124,6 +129,7 @@ impl Target<'_> { match self { Self::Ref(_) => false, Self::Value(_) => true, + #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, _) => false, } } @@ -132,14 +138,16 @@ impl Target<'_> { match self { Target::Ref(r) => r.is::(), Target::Value(r) => r.is::(), + #[cfg(not(feature = "no_index"))] Target::StringChar(_, _, _) => TypeId::of::() == TypeId::of::(), } } /// Get the value of the `Target` as a `Dynamic`, cloning a referenced value if necessary. pub fn clone_into_dynamic(self) -> Dynamic { match self { - Self::Ref(r) => r.clone(), // Referenced value is cloned - Self::Value(v) => v, // Owned value is simply taken + Self::Ref(r) => r.clone(), // Referenced value is cloned + Self::Value(v) => v, // Owned value is simply taken + #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, ch) => ch, // Character is taken } } @@ -148,6 +156,7 @@ impl Target<'_> { match self { Self::Ref(r) => *r, Self::Value(ref mut r) => r, + #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, ref mut r) => r, } } @@ -161,6 +170,7 @@ impl Target<'_> { Position::none(), ))) } + #[cfg(not(feature = "no_index"))] Self::StringChar(Dynamic(Union::Str(ref mut s)), index, _) => { // Replace the character at the specified index position let new_ch = new_val @@ -176,7 +186,8 @@ impl Target<'_> { *s = chars.iter().collect::().into(); } } - _ => unreachable!(), + #[cfg(not(feature = "no_index"))] + Self::StringChar(_, _, _) => unreachable!(), } Ok(()) @@ -249,6 +260,34 @@ pub fn get_script_function_by_signature<'a>( } } +/// [INTERNALS] A type containing all the limits imposed by the `Engine`. +/// Exported under the `internals` feature only. +/// +/// ## WARNING +/// +/// This type is volatile and may change. +#[cfg(not(feature = "unchecked"))] +pub struct Limits { + /// Maximum levels of call-stack to prevent infinite recursion. + /// + /// Defaults to 16 for debug builds and 128 for non-debug builds. + pub max_call_stack_depth: usize, + /// Maximum depth of statements/expressions at global level. + pub max_expr_depth: usize, + /// Maximum depth of statements/expressions in functions. + pub max_function_expr_depth: usize, + /// Maximum number of operations allowed to run. + pub max_operations: u64, + /// Maximum number of modules allowed to load. + pub max_modules: usize, + /// Maximum length of a string. + pub max_string_size: usize, + /// Maximum length of an array. + pub max_array_size: usize, + /// Maximum number of properties in a map. + pub max_map_size: usize, +} + /// Rhai main scripting engine. /// /// ``` @@ -275,6 +314,7 @@ pub struct Engine { pub(crate) packages: PackagesCollection, /// A module resolution service. + #[cfg(not(feature = "no_module"))] pub(crate) module_resolver: Option>, /// A hashmap mapping type names to pretty-print names. @@ -296,24 +336,10 @@ pub struct Engine { /// Optimize the AST after compilation. pub(crate) optimization_level: OptimizationLevel, - /// Maximum levels of call-stack to prevent infinite recursion. - /// - /// Defaults to 16 for debug builds and 128 for non-debug builds. - pub(crate) max_call_stack_depth: usize, - /// Maximum depth of statements/expressions at global level. - pub(crate) max_expr_depth: usize, - /// Maximum depth of statements/expressions in functions. - pub(crate) max_function_expr_depth: usize, - /// Maximum number of operations allowed to run. - pub(crate) max_operations: u64, - /// Maximum number of modules allowed to load. - pub(crate) max_modules: usize, - /// Maximum length of a string. - pub(crate) max_string_size: usize, - /// Maximum length of an array. - pub(crate) max_array_size: usize, - /// Maximum number of properties in a map. - pub(crate) max_map_size: usize, + + /// Max limits. + #[cfg(not(feature = "unchecked"))] + pub(crate) limits: Limits, } impl fmt::Debug for Engine { @@ -338,7 +364,8 @@ impl Default for Engine { #[cfg(not(feature = "no_std"))] #[cfg(not(target_arch = "wasm32"))] module_resolver: Some(Box::new(resolvers::FileModuleResolver::new())), - #[cfg(any(feature = "no_module", feature = "no_std", target_arch = "wasm32",))] + #[cfg(not(feature = "no_module"))] + #[cfg(any(feature = "no_std", target_arch = "wasm32",))] module_resolver: None, type_names: None, @@ -360,14 +387,17 @@ impl Default for Engine { #[cfg(not(feature = "no_optimize"))] optimization_level: OptimizationLevel::Simple, - max_call_stack_depth: MAX_CALL_STACK_DEPTH, - max_expr_depth: MAX_EXPR_DEPTH, - max_function_expr_depth: MAX_FUNCTION_EXPR_DEPTH, - max_operations: 0, - max_modules: usize::MAX, - max_string_size: 0, - max_array_size: 0, - max_map_size: 0, + #[cfg(not(feature = "unchecked"))] + limits: Limits { + max_call_stack_depth: MAX_CALL_STACK_DEPTH, + max_expr_depth: MAX_EXPR_DEPTH, + max_function_expr_depth: MAX_FUNCTION_EXPR_DEPTH, + max_operations: 0, + max_modules: usize::MAX, + max_string_size: 0, + max_array_size: 0, + max_map_size: 0, + }, }; engine.load_package(StandardPackage::new().get()); @@ -546,6 +576,8 @@ impl Engine { packages: Default::default(), global_module: Default::default(), + + #[cfg(not(feature = "no_module"))] module_resolver: None, type_names: None, @@ -563,14 +595,17 @@ impl Engine { #[cfg(not(feature = "no_optimize"))] optimization_level: OptimizationLevel::Simple, - max_call_stack_depth: MAX_CALL_STACK_DEPTH, - max_expr_depth: MAX_EXPR_DEPTH, - max_function_expr_depth: MAX_FUNCTION_EXPR_DEPTH, - max_operations: 0, - max_modules: usize::MAX, - max_string_size: 0, - max_array_size: 0, - max_map_size: 0, + #[cfg(not(feature = "unchecked"))] + limits: Limits { + max_call_stack_depth: MAX_CALL_STACK_DEPTH, + max_expr_depth: MAX_EXPR_DEPTH, + max_function_expr_depth: MAX_FUNCTION_EXPR_DEPTH, + max_operations: 0, + max_modules: usize::MAX, + max_string_size: 0, + max_array_size: 0, + max_map_size: 0, + }, } } @@ -982,23 +1017,25 @@ impl Engine { fn get_indexed_mut<'a>( &self, state: &mut State, - lib: &Module, + _lib: &Module, target: &'a mut Target, - mut idx: Dynamic, + mut _idx: Dynamic, idx_pos: Position, create: bool, - level: usize, + _level: usize, ) -> Result, Box> { self.inc_operations(state)?; + #[cfg(not(feature = "no_index"))] let is_ref = target.is_ref(); + let val = target.as_mut(); match val { #[cfg(not(feature = "no_index"))] Dynamic(Union::Array(arr)) => { // val_array[idx] - let index = idx + let index = _idx .as_int() .map_err(|_| EvalAltResult::ErrorNumericIndexExpr(idx_pos))?; @@ -1021,13 +1058,13 @@ impl Engine { Dynamic(Union::Map(map)) => { // val_map[idx] Ok(if create { - let index = idx + let index = _idx .take_immutable_string() .map_err(|_| EvalAltResult::ErrorStringIndexExpr(idx_pos))?; map.entry(index).or_insert(Default::default()).into() } else { - let index = idx + let index = _idx .downcast_ref::() .ok_or_else(|| EvalAltResult::ErrorStringIndexExpr(idx_pos))?; @@ -1041,7 +1078,7 @@ impl Engine { Dynamic(Union::Str(s)) => { // val_string[idx] let chars_len = s.chars().count(); - let index = idx + let index = _idx .as_int() .map_err(|_| EvalAltResult::ErrorNumericIndexExpr(idx_pos))?; @@ -1062,9 +1099,9 @@ impl Engine { #[cfg(not(feature = "no_index"))] _ => { let type_name = val.type_name(); - let args = &mut [val, &mut idx]; + let args = &mut [val, &mut _idx]; self.exec_fn_call( - state, lib, FN_IDX_GET, true, 0, args, is_ref, true, None, level, + state, _lib, FN_IDX_GET, true, 0, args, is_ref, true, None, _level, ) .map(|(v, _)| v.into()) .map_err(|err| match *err { @@ -1639,19 +1676,20 @@ impl Engine { Stmt::Const(_) => unreachable!(), // Import statement + #[cfg(not(feature = "no_module"))] Stmt::Import(x) => { - let (expr, (name, pos)) = x.as_ref(); + let (expr, (name, _pos)) = x.as_ref(); // Guard against too many modules - if state.modules >= self.max_modules { - return Err(Box::new(EvalAltResult::ErrorTooManyModules(*pos))); + #[cfg(not(feature = "unchecked"))] + if state.modules >= self.limits.max_modules { + return Err(Box::new(EvalAltResult::ErrorTooManyModules(*_pos))); } if let Some(path) = self .eval_expr(scope, mods, state, lib, this_ptr, &expr, level)? .try_cast::() { - #[cfg(not(feature = "no_module"))] if let Some(resolver) = &self.module_resolver { let mut module = resolver.resolve(self, &path, expr.position())?; module.index_all_sub_modules(); @@ -1666,15 +1704,13 @@ impl Engine { expr.position(), ))) } - - #[cfg(feature = "no_module")] - Ok(Default::default()) } else { Err(Box::new(EvalAltResult::ErrorImportExpr(expr.position()))) } } // Export statement + #[cfg(not(feature = "no_module"))] Stmt::Export(list) => { for ((id, id_pos), rename) in list.iter() { // Mark scope variables as public @@ -1696,8 +1732,18 @@ impl Engine { .map_err(|err| err.new_position(stmt.position())) } + #[cfg(feature = "unchecked")] + #[inline(always)] + fn check_data_size( + &self, + result: Result>, + ) -> Result> { + return result; + } + /// Check a result to ensure that the data size is within allowable limit. /// Position in `EvalAltResult` may be None and should be set afterwards. + #[cfg(not(feature = "unchecked"))] fn check_data_size( &self, result: Result>, @@ -1706,7 +1752,8 @@ impl Engine { return result; // If no data size limits, just return - if self.max_string_size + self.max_array_size + self.max_map_size == 0 { + if self.limits.max_string_size + self.limits.max_array_size + self.limits.max_map_size == 0 + { return result; } @@ -1766,37 +1813,37 @@ impl Engine { // Simply return all errors Err(_) => return result, // String with limit - Ok(Dynamic(Union::Str(_))) if self.max_string_size > 0 => (), + Ok(Dynamic(Union::Str(_))) if self.limits.max_string_size > 0 => (), // Array with limit #[cfg(not(feature = "no_index"))] - Ok(Dynamic(Union::Array(_))) if self.max_array_size > 0 => (), + Ok(Dynamic(Union::Array(_))) if self.limits.max_array_size > 0 => (), // Map with limit #[cfg(not(feature = "no_object"))] - Ok(Dynamic(Union::Map(_))) if self.max_map_size > 0 => (), + Ok(Dynamic(Union::Map(_))) if self.limits.max_map_size > 0 => (), // Everything else is simply returned Ok(_) => return result, }; let (arr, map, s) = calc_size(result.as_ref().unwrap()); - if s > self.max_string_size { + if s > self.limits.max_string_size { Err(Box::new(EvalAltResult::ErrorDataTooLarge( "Length of string".to_string(), - self.max_string_size, + self.limits.max_string_size, s, Position::none(), ))) - } else if arr > self.max_array_size { + } else if arr > self.limits.max_array_size { Err(Box::new(EvalAltResult::ErrorDataTooLarge( "Size of array".to_string(), - self.max_array_size, + self.limits.max_array_size, arr, Position::none(), ))) - } else if map > self.max_map_size { + } else if map > self.limits.max_map_size { Err(Box::new(EvalAltResult::ErrorDataTooLarge( "Number of properties in object map".to_string(), - self.max_map_size, + self.limits.max_map_size, map, Position::none(), ))) @@ -1812,7 +1859,7 @@ impl Engine { #[cfg(not(feature = "unchecked"))] // Guard against too many operations - if self.max_operations > 0 && state.operations > self.max_operations { + if self.limits.max_operations > 0 && state.operations > self.limits.max_operations { return Err(Box::new(EvalAltResult::ErrorTooManyOperations( Position::none(), ))); diff --git a/src/fn_args.rs b/src/fn_args.rs index 833014bb..e5a22390 100644 --- a/src/fn_args.rs +++ b/src/fn_args.rs @@ -22,11 +22,10 @@ macro_rules! impl_args { fn into_vec(self) -> StaticVec { let ($($p,)*) = self; - #[allow(unused_mut)] - let mut v = StaticVec::new(); - $(v.push($p.into_dynamic());)* + let mut _v = StaticVec::new(); + $(_v.push($p.into_dynamic());)* - v + _v } } diff --git a/src/fn_call.rs b/src/fn_call.rs index 98228853..b2a06455 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -11,13 +11,18 @@ use crate::error::ParseErrorType; use crate::fn_native::{FnCallArgs, FnPtr}; use crate::module::{Module, ModuleRef}; use crate::optimize::OptimizationLevel; -use crate::parser::{Expr, ImmutableString, ScriptFnDef, AST, INT}; -use crate::r#unsafe::unsafe_cast_var_name_to_lifetime; +use crate::parser::{Expr, ImmutableString, AST, INT}; use crate::result::EvalAltResult; -use crate::scope::{EntryType as ScopeEntryType, Scope}; +use crate::scope::Scope; use crate::token::Position; use crate::utils::StaticVec; +#[cfg(not(feature = "no_function"))] +use crate::{ + parser::ScriptFnDef, r#unsafe::unsafe_cast_var_name_to_lifetime, + scope::EntryType as ScopeEntryType, +}; + #[cfg(not(feature = "no_float"))] use crate::parser::FLOAT; @@ -106,17 +111,17 @@ impl Engine { /// **DO NOT** reuse the argument values unless for the first `&mut` argument - all others are silently replaced by `()`! pub(crate) fn call_fn_raw( &self, - scope: &mut Scope, - mods: &mut Imports, + _scope: &mut Scope, + _mods: &mut Imports, state: &mut State, lib: &Module, fn_name: &str, (hash_fn, hash_script): (u64, u64), args: &mut FnCallArgs, is_ref: bool, - is_method: bool, + _is_method: bool, def_val: Option, - level: usize, + _level: usize, ) -> Result<(Dynamic, bool), Box> { self.inc_operations(state)?; @@ -125,7 +130,7 @@ impl Engine { // Check for stack overflow #[cfg(not(feature = "no_function"))] #[cfg(not(feature = "unchecked"))] - if level > self.max_call_stack_depth { + if _level > self.limits.max_call_stack_depth { return Err(Box::new( EvalAltResult::ErrorStackOverflow(Position::none()), )); @@ -151,7 +156,7 @@ impl Engine { if let Some(func) = func { #[cfg(not(feature = "no_function"))] - let need_normalize = is_ref && (func.is_pure() || (func.is_script() && !is_method)); + let need_normalize = is_ref && (func.is_pure() || (func.is_script() && !_is_method)); #[cfg(feature = "no_function")] let need_normalize = is_ref && func.is_pure(); @@ -164,25 +169,25 @@ impl Engine { let fn_def = func.get_fn_def(); // Method call of script function - map first argument to `this` - return if is_method { + return if _is_method { let (first, rest) = args.split_at_mut(1); Ok(( self.call_script_fn( - scope, - mods, + _scope, + _mods, state, lib, &mut Some(first[0]), fn_name, fn_def, rest, - level, + _level, )?, false, )) } else { let result = self.call_script_fn( - scope, mods, state, lib, &mut None, fn_name, fn_def, args, level, + _scope, _mods, state, lib, &mut None, fn_name, fn_def, args, _level, )?; // Restore the original reference @@ -305,6 +310,7 @@ impl Engine { /// Function call arguments may be _consumed_ when the function requires them to be passed by value. /// All function arguments not in the first position are always passed by value and thus consumed. /// **DO NOT** reuse the argument values unless for the first `&mut` argument - all others are silently replaced by `()`! + #[cfg(not(feature = "no_function"))] pub(crate) fn call_script_fn( &self, scope: &mut Scope, diff --git a/src/fn_native.rs b/src/fn_native.rs index 8fc60714..bfb8b956 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -2,14 +2,18 @@ use crate::any::Dynamic; use crate::engine::Engine; -use crate::module::{FuncReturn, Module}; -use crate::parser::ScriptFnDef; +use crate::module::Module; use crate::result::EvalAltResult; use crate::token::{is_valid_identifier, Position}; -use crate::utils::{ImmutableString, StaticVec}; -use crate::Scope; +use crate::utils::ImmutableString; -use crate::stdlib::{boxed::Box, convert::TryFrom, fmt, mem, string::String, vec::Vec}; +#[cfg(not(feature = "no_function"))] +use crate::{module::FuncReturn, parser::ScriptFnDef, scope::Scope, utils::StaticVec}; + +use crate::stdlib::{boxed::Box, convert::TryFrom, fmt, string::String, vec::Vec}; + +#[cfg(not(feature = "no_function"))] +use crate::stdlib::mem; #[cfg(not(feature = "sync"))] use crate::stdlib::rc::Rc; @@ -85,12 +89,17 @@ impl FnPtr { /// Call the function pointer with curried arguments (if any). /// + /// The function must be a script-defined function. It cannot be a Rust function. + /// + /// To call a Rust function, just call it directly in Rust! + /// /// ## WARNING /// /// All the arguments are _consumed_, meaning that they're replaced by `()`. /// This is to avoid unnecessarily cloning the arguments. /// Do not use the arguments after this call. If they are needed afterwards, /// clone them _before_ calling this function. + #[cfg(not(feature = "no_function"))] pub fn call_dynamic( &self, engine: &Engine, diff --git a/src/fn_register.rs b/src/fn_register.rs index 28ee084a..c2eb5534 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -133,12 +133,11 @@ macro_rules! make_func { Box::new(move |_: &Engine, _: &Module, args: &mut FnCallArgs| { // The arguments are assumed to be of the correct number and types! - #[allow(unused_variables, unused_mut)] - let mut drain = args.iter_mut(); + let mut _drain = args.iter_mut(); $( // Downcast every element, panic in case of a type mismatch (which shouldn't happen). // Call the user-supplied function using ($convert) to access it either by value or by reference. - let $par = ($convert)(drain.next().unwrap()); + let $par = ($convert)(_drain.next().unwrap()); )* // Call the function with each parameter value diff --git a/src/lib.rs b/src/lib.rs index 240d87d1..1194fd67 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -170,7 +170,7 @@ pub use parser::{CustomExpr, Expr, FloatWrapper, ReturnType, ScriptFnDef, Stmt}; #[cfg(feature = "internals")] #[deprecated(note = "this type is volatile and may change")] -pub use engine::{Imports, State as EvalState}; +pub use engine::{Imports, Limits, State as EvalState}; #[cfg(feature = "internals")] #[deprecated(note = "this type is volatile and may change")] diff --git a/src/module.rs b/src/module.rs index 22632c7d..c5002544 100644 --- a/src/module.rs +++ b/src/module.rs @@ -2,22 +2,29 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; -use crate::engine::{make_getter, make_setter, Engine, Imports, FN_IDX_GET, FN_IDX_SET}; -use crate::fn_native::{CallableFunction as Func, FnCallArgs, IteratorFn, SendSync, Shared}; -use crate::parser::{ - FnAccess, - FnAccess::{Private, Public}, - ScriptFnDef, AST, -}; +use crate::engine::{make_getter, make_setter, Engine}; +use crate::fn_native::{CallableFunction as Func, FnCallArgs, IteratorFn, SendSync}; +use crate::parser::{FnAccess, FnAccess::Public}; use crate::result::EvalAltResult; -use crate::scope::{Entry as ScopeEntry, Scope}; use crate::token::{Position, Token}; use crate::utils::{StaticVec, StraightHasherBuilder}; +#[cfg(not(feature = "no_function"))] +use crate::{fn_native::Shared, parser::ScriptFnDef}; + +#[cfg(not(feature = "no_module"))] +use crate::{ + engine::Imports, + parser::AST, + scope::{Entry as ScopeEntry, Scope}, +}; + +#[cfg(not(feature = "no_index"))] +use crate::engine::{FN_IDX_GET, FN_IDX_SET}; + use crate::stdlib::{ any::TypeId, boxed::Box, - cell::RefCell, collections::HashMap, fmt, format, iter::empty, @@ -25,10 +32,14 @@ use crate::stdlib::{ num::NonZeroUsize, ops::{Deref, DerefMut}, string::{String, ToString}, - vec, vec::Vec, }; +#[cfg(not(feature = "no_module"))] +#[cfg(not(feature = "sync"))] +use crate::stdlib::cell::RefCell; + +#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_std"))] #[cfg(feature = "sync")] use crate::stdlib::sync::RwLock; @@ -738,6 +749,8 @@ impl Module { /// }); /// assert!(module.contains_fn(hash)); /// ``` + #[cfg(not(feature = "no_object"))] + #[cfg(not(feature = "no_index"))] pub fn set_indexer_set_fn( &mut self, func: impl Fn(&mut A, B, C) -> FuncReturn<()> + SendSync + 'static, @@ -781,6 +794,8 @@ impl Module { /// assert!(module.contains_fn(hash_get)); /// assert!(module.contains_fn(hash_set)); /// ``` + #[cfg(not(feature = "no_object"))] + #[cfg(not(feature = "no_index"))] pub fn set_indexer_get_set_fn( &mut self, getter: impl Fn(&mut A, B) -> FuncReturn + SendSync + 'static, @@ -909,11 +924,11 @@ impl Module { self.merge_filtered(other, |_, _, _| true) } - /// Merge another module into this module, with only selected functions based on a filter predicate. + /// Merge another module into this module, with only selected script-defined functions based on a filter predicate. pub(crate) fn merge_filtered( &mut self, other: &Self, - filter: impl Fn(FnAccess, &str, usize) -> bool, + _filter: impl Fn(FnAccess, &str, usize) -> bool, ) -> &mut Self { self.variables .extend(other.variables.iter().map(|(k, v)| (k.clone(), v.clone()))); @@ -924,7 +939,7 @@ impl Module { .iter() .filter(|(_, (_, _, _, v))| match v { #[cfg(not(feature = "no_function"))] - Func::Script(ref f) => filter(f.access, f.name.as_str(), f.params.len()), + Func::Script(ref f) => _filter(f.access, f.name.as_str(), f.params.len()), _ => true, }) .map(|(&k, v)| (k, v.clone())), @@ -975,6 +990,7 @@ impl Module { } /// Get an iterator to the functions in the module. + #[cfg(not(feature = "no_function"))] pub(crate) fn iter_fn( &self, ) -> impl Iterator, Func)> { @@ -1038,6 +1054,7 @@ impl Module { /// Scan through all the sub-modules in the module build an index of all /// variables and external Rust functions via hashing. + #[cfg(not(feature = "no_module"))] pub(crate) fn index_all_sub_modules(&mut self) { // Collect a particular module. fn index_module<'a>( @@ -1063,8 +1080,8 @@ impl Module { for (name, access, params, func) in module.functions.values() { match access { // Private functions are not exported - Private => continue, - Public => (), + FnAccess::Private => continue, + FnAccess::Public => (), } #[cfg(not(feature = "no_function"))] @@ -1100,10 +1117,13 @@ impl Module { return; } - let mut variables = Vec::new(); - let mut functions = Vec::new(); + let mut qualifiers: Vec<_> = Default::default(); + let mut variables: Vec<_> = Default::default(); + let mut functions: Vec<_> = Default::default(); - index_module(self, &mut vec!["root"], &mut variables, &mut functions); + qualifiers.push("root"); + + index_module(self, &mut qualifiers, &mut variables, &mut functions); self.all_variables = variables.into_iter().collect(); self.all_functions = functions.into_iter().collect(); diff --git a/src/optimize.rs b/src/optimize.rs index 738ea300..0948fbd5 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -6,11 +6,14 @@ use crate::engine::{ Engine, Imports, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_PRINT, KEYWORD_TYPE_OF, }; use crate::module::Module; -use crate::parser::{map_dynamic_to_expr, Expr, ReturnType, ScriptFnDef, Stmt, AST}; +use crate::parser::{map_dynamic_to_expr, Expr, ScriptFnDef, Stmt, AST}; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; use crate::token::is_valid_identifier; use crate::utils::StaticVec; +#[cfg(not(feature = "no_function"))] +use crate::parser::ReturnType; + #[cfg(feature = "internals")] use crate::parser::CustomExpr; @@ -249,6 +252,7 @@ fn optimize_stmt(stmt: Stmt, state: &mut State, preserve_result: bool) -> Stmt { // let id; stmt @ Stmt::Let(_) => stmt, // import expr as id; + #[cfg(not(feature = "no_module"))] Stmt::Import(x) => Stmt::Import(Box::new((optimize_expr(x.0, state), x.1))), // { block } Stmt::Block(x) => { @@ -290,6 +294,7 @@ fn optimize_stmt(stmt: Stmt, state: &mut State, preserve_result: bool) -> Stmt { match expr { Stmt::Let(x) if x.1.is_none() => removed = true, Stmt::Let(x) if x.1.is_some() => removed = x.1.unwrap().is_pure(), + #[cfg(not(feature = "no_module"))] Stmt::Import(x) => removed = x.0.is_pure(), _ => { result.push(expr); @@ -345,8 +350,11 @@ fn optimize_stmt(stmt: Stmt, state: &mut State, preserve_result: bool) -> Stmt { state.set_dirty(); Stmt::Noop(pos) } - // Only one let/import statement - leave it alone - [Stmt::Let(_)] | [Stmt::Import(_)] => Stmt::Block(Box::new((result.into(), pos))), + // Only one let statement - leave it alone + [Stmt::Let(_)] => Stmt::Block(Box::new((result.into(), pos))), + // Only one import statement - leave it alone + #[cfg(not(feature = "no_module"))] + [Stmt::Import(_)] => Stmt::Block(Box::new((result.into(), pos))), // Only one statement - promote [_] => { state.set_dirty(); @@ -557,16 +565,16 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr { // First search in functions lib (can override built-in) // Cater for both normal function call style and method call style (one additional arguments) #[cfg(not(feature = "no_function"))] - let has_script_fn = state.lib.iter_fn().find(|(_, _, _, f)| { + let _has_script_fn = state.lib.iter_fn().find(|(_, _, _, f)| { if !f.is_script() { return false; } let fn_def = f.get_fn_def(); fn_def.name.as_str() == name && (args.len()..=args.len() + 1).contains(&fn_def.params.len()) }).is_some(); #[cfg(feature = "no_function")] - const has_script_fn: bool = false; + let _has_script_fn: bool = false; - if has_script_fn { + if _has_script_fn { // A script-defined function overrides the built-in function - do not make the call x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect(); return Expr::FnCall(x); @@ -686,7 +694,9 @@ fn optimize( // Keep all variable declarations at this level // and always keep the last return value let keep = match stmt { - Stmt::Let(_) | Stmt::Import(_) => true, + Stmt::Let(_) => true, + #[cfg(not(feature = "no_module"))] + Stmt::Import(_) => true, _ => i == num_statements - 1, }; optimize_stmt(stmt, &mut state, keep) @@ -721,7 +731,7 @@ pub fn optimize_into_ast( engine: &Engine, scope: &Scope, statements: Vec, - functions: Vec, + _functions: Vec, level: OptimizationLevel, ) -> AST { #[cfg(feature = "no_optimize")] @@ -735,7 +745,7 @@ pub fn optimize_into_ast( // We only need the script library's signatures for optimization purposes let mut lib2 = Module::new(); - functions + _functions .iter() .map(|fn_def| { ScriptFnDef { @@ -751,7 +761,7 @@ pub fn optimize_into_ast( lib2.set_script_fn(fn_def); }); - functions + _functions .into_iter() .map(|mut fn_def| { let pos = fn_def.body.position(); @@ -782,7 +792,7 @@ pub fn optimize_into_ast( module.set_script_fn(fn_def); }); } else { - functions.into_iter().for_each(|fn_def| { + _functions.into_iter().for_each(|fn_def| { module.set_script_fn(fn_def); }); } diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index 3e3852d6..61350996 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -1,8 +1,9 @@ use crate::def_package; use crate::module::FuncReturn; use crate::parser::INT; -use crate::result::EvalAltResult; -use crate::token::Position; + +#[cfg(not(feature = "unchecked"))] +use crate::{result::EvalAltResult, token::Position}; #[cfg(not(feature = "no_float"))] use crate::parser::FLOAT; @@ -11,22 +12,25 @@ use crate::parser::FLOAT; #[cfg(feature = "no_std")] use num_traits::*; +#[cfg(not(feature = "unchecked"))] use num_traits::{ identities::Zero, CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr, CheckedSub, }; -use crate::stdlib::{ - boxed::Box, - fmt::Display, - format, - ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Sub}, -}; +use crate::stdlib::ops::{BitAnd, BitOr, BitXor}; + +#[cfg(any(feature = "unchecked", not(feature = "no_float")))] +use crate::stdlib::ops::{Add, Div, Mul, Neg, Rem, Sub}; #[cfg(feature = "unchecked")] use crate::stdlib::ops::{Shl, Shr}; +#[cfg(not(feature = "unchecked"))] +use crate::stdlib::{boxed::Box, fmt::Display, format}; + // Checked add +#[cfg(not(feature = "unchecked"))] pub(crate) fn add(x: T, y: T) -> FuncReturn { x.checked_add(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( @@ -36,6 +40,7 @@ pub(crate) fn add(x: T, y: T) -> FuncReturn { }) } // Checked subtract +#[cfg(not(feature = "unchecked"))] pub(crate) fn sub(x: T, y: T) -> FuncReturn { x.checked_sub(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( @@ -45,6 +50,7 @@ pub(crate) fn sub(x: T, y: T) -> FuncReturn { }) } // Checked multiply +#[cfg(not(feature = "unchecked"))] pub(crate) fn mul(x: T, y: T) -> FuncReturn { x.checked_mul(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( @@ -54,6 +60,7 @@ pub(crate) fn mul(x: T, y: T) -> FuncReturn { }) } // Checked divide +#[cfg(not(feature = "unchecked"))] pub(crate) fn div(x: T, y: T) -> FuncReturn where T: Display + CheckedDiv + PartialEq + Zero, @@ -74,6 +81,7 @@ where }) } // Checked negative - e.g. -(i32::MIN) will overflow i32::MAX +#[cfg(not(feature = "unchecked"))] pub(crate) fn neg(x: T) -> FuncReturn { x.checked_neg().ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( @@ -83,6 +91,7 @@ pub(crate) fn neg(x: T) -> FuncReturn { }) } // Checked absolute +#[cfg(not(feature = "unchecked"))] pub(crate) fn abs(x: T) -> FuncReturn { // FIX - We don't use Signed::abs() here because, contrary to documentation, it panics // when the number is ::MIN instead of returning ::MIN itself. @@ -98,26 +107,32 @@ pub(crate) fn abs(x: T) -> FuncRetu } } // Unchecked add - may panic on overflow +#[cfg(any(feature = "unchecked", not(feature = "no_float")))] fn add_u(x: T, y: T) -> FuncReturn<::Output> { Ok(x + y) } // Unchecked subtract - may panic on underflow +#[cfg(any(feature = "unchecked", not(feature = "no_float")))] fn sub_u(x: T, y: T) -> FuncReturn<::Output> { Ok(x - y) } // Unchecked multiply - may panic on overflow +#[cfg(any(feature = "unchecked", not(feature = "no_float")))] fn mul_u(x: T, y: T) -> FuncReturn<::Output> { Ok(x * y) } // Unchecked divide - may panic when dividing by zero +#[cfg(any(feature = "unchecked", not(feature = "no_float")))] fn div_u(x: T, y: T) -> FuncReturn<::Output> { Ok(x / y) } // Unchecked negative - may panic on overflow +#[cfg(any(feature = "unchecked", not(feature = "no_float")))] fn neg_u(x: T) -> FuncReturn<::Output> { Ok(-x) } // Unchecked absolute - may panic on overflow +#[cfg(any(feature = "unchecked", not(feature = "no_float")))] fn abs_u(x: T) -> FuncReturn<::Output> where T: Neg + PartialOrd + Default + Into<::Output>, @@ -140,6 +155,7 @@ fn binary_xor(x: T, y: T) -> FuncReturn<::Output> { Ok(x ^ y) } // Checked left-shift +#[cfg(not(feature = "unchecked"))] pub(crate) fn shl(x: T, y: INT) -> FuncReturn { // Cannot shift by a negative number of bits if y < 0 { @@ -157,6 +173,7 @@ pub(crate) fn shl(x: T, y: INT) -> FuncReturn { }) } // Checked right-shift +#[cfg(not(feature = "unchecked"))] pub(crate) fn shr(x: T, y: INT) -> FuncReturn { // Cannot shift by a negative number of bits if y < 0 { @@ -184,6 +201,7 @@ pub(crate) fn shr_u>(x: T, y: T) -> FuncReturn<>::Output> Ok(x.shr(y)) } // Checked modulo +#[cfg(not(feature = "unchecked"))] pub(crate) fn modulo(x: T, y: T) -> FuncReturn { x.checked_rem(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( @@ -193,10 +211,12 @@ pub(crate) fn modulo(x: T, y: T) -> FuncReturn { }) } // Unchecked modulo - may panic if dividing by zero +#[cfg(any(feature = "unchecked", not(feature = "no_float")))] fn modulo_u(x: T, y: T) -> FuncReturn<::Output> { Ok(x % y) } // Checked power +#[cfg(not(feature = "unchecked"))] pub(crate) fn pow_i_i(x: INT, y: INT) -> FuncReturn { #[cfg(not(feature = "only_i32"))] if y > (u32::MAX as INT) { @@ -245,6 +265,7 @@ pub(crate) fn pow_f_f(x: FLOAT, y: FLOAT) -> FuncReturn { } // Checked power #[cfg(not(feature = "no_float"))] +#[cfg(not(feature = "unchecked"))] pub(crate) fn pow_f_i(x: FLOAT, y: INT) -> FuncReturn { // Raise to power that is larger than an i32 if y > (i32::MAX as INT) { diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 0a580933..c0bea7ee 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -5,10 +5,14 @@ use crate::def_package; use crate::engine::{Array, Engine}; use crate::module::{FuncReturn, Module}; use crate::parser::{ImmutableString, INT}; -use crate::result::EvalAltResult; -use crate::token::Position; -use crate::stdlib::{any::TypeId, boxed::Box, string::ToString}; +#[cfg(not(feature = "unchecked"))] +use crate::{result::EvalAltResult, token::Position}; + +use crate::stdlib::{any::TypeId, boxed::Box}; + +#[cfg(not(feature = "unchecked"))] +use crate::stdlib::string::ToString; // Register array utility functions fn push(list: &mut Array, item: T) -> FuncReturn<()> { @@ -26,7 +30,7 @@ fn ins(list: &mut Array, position: INT, item: T) -> FuncRetu Ok(()) } fn pad( - engine: &Engine, + _engine: &Engine, _: &Module, args: &mut [&mut Dynamic], ) -> FuncReturn<()> { @@ -34,10 +38,13 @@ fn pad( // Check if array will be over max size limit #[cfg(not(feature = "unchecked"))] - if engine.max_array_size > 0 && len > 0 && (len as usize) > engine.max_array_size { + if _engine.limits.max_array_size > 0 + && len > 0 + && (len as usize) > _engine.limits.max_array_size + { return Err(Box::new(EvalAltResult::ErrorDataTooLarge( "Size of array".to_string(), - engine.max_array_size, + _engine.limits.max_array_size, len as usize, Position::none(), ))); diff --git a/src/packages/map_basic.rs b/src/packages/map_basic.rs index a86e4665..cc1aa000 100644 --- a/src/packages/map_basic.rs +++ b/src/packages/map_basic.rs @@ -1,16 +1,20 @@ #![cfg(not(feature = "no_object"))] -use crate::any::Dynamic; use crate::def_package; use crate::engine::Map; -use crate::module::FuncReturn; use crate::parser::{ImmutableString, INT}; +#[cfg(not(feature = "no_index"))] +use crate::{any::Dynamic, module::FuncReturn}; + +#[cfg(not(feature = "no_index"))] use crate::stdlib::vec::Vec; +#[cfg(not(feature = "no_index"))] fn map_get_keys(map: &mut Map) -> FuncReturn> { Ok(map.iter().map(|(k, _)| k.clone().into()).collect()) } +#[cfg(not(feature = "no_index"))] fn map_get_values(map: &mut Map) -> FuncReturn> { Ok(map.iter().map(|(_, v)| v.clone()).collect()) } diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index aed7e5b7..55bb68a3 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -1,20 +1,26 @@ use crate::def_package; use crate::parser::INT; -use crate::result::EvalAltResult; -use crate::token::Position; #[cfg(not(feature = "no_float"))] use crate::parser::FLOAT; +#[cfg(not(feature = "no_float"))] +#[cfg(not(feature = "unchecked"))] +use crate::{result::EvalAltResult, token::Position}; + #[cfg(not(feature = "no_float"))] #[cfg(feature = "no_std")] use num_traits::*; -use crate::stdlib::{boxed::Box, format, i32, i64}; +#[cfg(not(feature = "no_float"))] +#[cfg(not(feature = "unchecked"))] +use crate::stdlib::{boxed::Box, format}; #[cfg(feature = "only_i32")] +#[cfg(not(feature = "unchecked"))] pub const MAX_INT: INT = i32::MAX; #[cfg(not(feature = "only_i32"))] +#[cfg(not(feature = "unchecked"))] pub const MAX_INT: INT = i64::MAX; def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 8207a966..ccd78add 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -24,7 +24,6 @@ pub use arithmetic::ArithmeticPackage; #[cfg(not(feature = "no_index"))] pub use array_basic::BasicArrayPackage; pub use eval::EvalPackage; -#[cfg(not(feature = "no_function"))] pub use fn_basic::BasicFnPackage; pub use iter_basic::BasicIteratorPackage; pub use logic::LogicPackage; diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 248b0e93..62a5fdb2 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -3,10 +3,11 @@ use crate::def_package; use crate::engine::Engine; use crate::module::{FuncReturn, Module}; use crate::parser::{ImmutableString, INT}; -use crate::result::EvalAltResult; -use crate::token::Position; use crate::utils::StaticVec; +#[cfg(not(feature = "unchecked"))] +use crate::{result::EvalAltResult, token::Position}; + #[cfg(not(feature = "no_index"))] use crate::engine::Array; @@ -226,15 +227,15 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str lib.set_raw_fn( "pad", &[TypeId::of::(), TypeId::of::(), TypeId::of::()], - |engine: &Engine, _: &Module, args: &mut [&mut Dynamic]| { + |_engine: &Engine, _: &Module, args: &mut [&mut Dynamic]| { let len = *args[1].downcast_ref::< INT>().unwrap(); // Check if string will be over max size limit #[cfg(not(feature = "unchecked"))] - if engine.max_string_size > 0 && len > 0 && (len as usize) > engine.max_string_size { + if _engine.limits.max_string_size > 0 && len > 0 && (len as usize) > _engine.limits.max_string_size { return Err(Box::new(EvalAltResult::ErrorDataTooLarge( "Length of string".to_string(), - engine.max_string_size, + _engine.limits.max_string_size, len as usize, Position::none(), ))); @@ -253,10 +254,11 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str p.push(ch); } - if engine.max_string_size > 0 && s.len() > engine.max_string_size { + #[cfg(not(feature = "unchecked"))] + if _engine.limits.max_string_size > 0 && s.len() > _engine.limits.max_string_size { return Err(Box::new(EvalAltResult::ErrorDataTooLarge( "Length of string".to_string(), - engine.max_string_size, + _engine.limits.max_string_size, s.len(), Position::none(), ))); diff --git a/src/packages/time_basic.rs b/src/packages/time_basic.rs index 7c166ab1..d554b68e 100644 --- a/src/packages/time_basic.rs +++ b/src/packages/time_basic.rs @@ -2,6 +2,7 @@ use super::logic::{eq, gt, gte, lt, lte, ne}; #[cfg(feature = "no_float")] +#[cfg(not(feature = "unchecked"))] use super::math_basic::MAX_INT; use crate::def_package; @@ -11,7 +12,11 @@ use crate::result::EvalAltResult; use crate::parser::FLOAT; #[cfg(feature = "no_float")] -use crate::{module::FuncReturn, parser::INT, token::Position}; +use crate::parser::INT; + +#[cfg(feature = "no_float")] +#[cfg(not(feature = "unchecked"))] +use crate::token::Position; #[cfg(not(target_arch = "wasm32"))] use crate::stdlib::time::Instant; diff --git a/src/parser.rs b/src/parser.rs index da0409db..99722611 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -3,8 +3,7 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::engine::{ - make_getter, make_setter, Engine, FN_ANONYMOUS, KEYWORD_THIS, MARKER_BLOCK, MARKER_EXPR, - MARKER_IDENT, + make_getter, make_setter, Engine, KEYWORD_THIS, MARKER_BLOCK, MARKER_EXPR, MARKER_IDENT, }; use crate::error::{LexError, ParseError, ParseErrorType}; use crate::fn_native::Shared; @@ -15,6 +14,9 @@ use crate::syntax::FnCustomSyntaxEval; use crate::token::{is_valid_identifier, Position, Token, TokenStream}; use crate::utils::{StaticVec, StraightHasherBuilder}; +#[cfg(not(feature = "no_function"))] +use crate::engine::FN_ANONYMOUS; + use crate::stdlib::{ borrow::Cow, boxed::Box, @@ -32,6 +34,7 @@ use crate::stdlib::{ }; #[cfg(not(feature = "no_std"))] +#[cfg(not(feature = "no_function"))] use crate::stdlib::collections::hash_map::DefaultHasher; #[cfg(feature = "no_std")] @@ -84,7 +87,7 @@ impl AST { &self.0 } - /// Get the statements. + /// [INTERNALS] Get the statements. #[cfg(feature = "internals")] #[deprecated(note = "this method is volatile and may change")] pub fn statements(&self) -> &[Stmt] { @@ -102,7 +105,7 @@ impl AST { &self.1 } - /// Get the internal `Module` containing all script-defined functions. + /// [INTERNALS] Get the internal `Module` containing all script-defined functions. #[cfg(feature = "internals")] #[deprecated(note = "this method is volatile and may change")] pub fn lib(&self) -> &Module { @@ -404,20 +407,28 @@ struct ParseState<'e> { /// Encapsulates a local stack with variable names to simulate an actual runtime scope. modules: Vec, /// Maximum levels of expression nesting. + #[cfg(not(feature = "unchecked"))] max_expr_depth: usize, /// Maximum levels of expression nesting in functions. + #[cfg(not(feature = "unchecked"))] max_function_expr_depth: usize, } impl<'e> ParseState<'e> { /// Create a new `ParseState`. - pub fn new(engine: &'e Engine, max_expr_depth: usize, max_function_expr_depth: usize) -> Self { + pub fn new( + engine: &'e Engine, + #[cfg(not(feature = "unchecked"))] max_expr_depth: usize, + #[cfg(not(feature = "unchecked"))] max_function_expr_depth: usize, + ) -> Self { Self { engine, - max_expr_depth, - max_function_expr_depth, stack: Default::default(), modules: Default::default(), + #[cfg(not(feature = "unchecked"))] + max_expr_depth, + #[cfg(not(feature = "unchecked"))] + max_function_expr_depth, } } /// Find a variable by name in the `ParseState`, searching in reverse. @@ -476,6 +487,7 @@ impl ParseSettings { } } /// Make sure that the current level of expression nesting is within the maximum limit. + #[cfg(not(feature = "unchecked"))] pub fn ensure_level_within_max_limit(&self, limit: usize) -> Result<(), ParseError> { if limit == 0 { Ok(()) @@ -519,8 +531,10 @@ pub enum Stmt { /// return/throw ReturnWithVal(Box<((ReturnType, Position), Option)>), /// import expr as module + #[cfg(not(feature = "no_module"))] Import(Box<(Expr, (String, Position))>), /// expr id as name, ... + #[cfg(not(feature = "no_module"))] Export(Box)>>), } @@ -544,7 +558,10 @@ impl Stmt { Stmt::While(x) => x.1.position(), Stmt::Loop(x) => x.position(), Stmt::For(x) => x.2.position(), + + #[cfg(not(feature = "no_module"))] Stmt::Import(x) => (x.1).1, + #[cfg(not(feature = "no_module"))] Stmt::Export(x) => (x.get(0).0).1, } } @@ -563,12 +580,13 @@ impl Stmt { Stmt::Let(_) | Stmt::Const(_) - | Stmt::Import(_) - | Stmt::Export(_) | Stmt::Expr(_) | Stmt::Continue(_) | Stmt::Break(_) | Stmt::ReturnWithVal(_) => false, + + #[cfg(not(feature = "no_module"))] + Stmt::Import(_) | Stmt::Export(_) => false, } } @@ -587,7 +605,10 @@ impl Stmt { Stmt::Let(_) | Stmt::Const(_) => false, Stmt::Block(x) => x.0.iter().all(Stmt::is_pure), Stmt::Continue(_) | Stmt::Break(_) | Stmt::ReturnWithVal(_) => false, + + #[cfg(not(feature = "no_module"))] Stmt::Import(_) => false, + #[cfg(not(feature = "no_module"))] Stmt::Export(_) => false, } } @@ -996,6 +1017,7 @@ fn parse_paren_expr( lib: &mut FunctionsLib, settings: ParseSettings, ) -> Result { + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; if match_token(input, Token::RightParen)? { @@ -1028,6 +1050,8 @@ fn parse_call_expr( settings: ParseSettings, ) -> Result { let (token, token_pos) = input.peek().unwrap(); + + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; let mut args = StaticVec::new(); @@ -1141,6 +1165,7 @@ fn parse_call_expr( /// Parse an indexing chain. /// Indexing binds to the right, so this call parses all possible levels of indexing following in the input. +#[cfg(not(feature = "no_index"))] fn parse_index_chain( input: &mut TokenStream, state: &mut ParseState, @@ -1148,6 +1173,7 @@ fn parse_index_chain( lhs: Expr, mut settings: ParseSettings, ) -> Result { + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; let idx_expr = parse_expr(input, state, lib, settings.level_up())?; @@ -1321,21 +1347,25 @@ fn parse_index_chain( } /// Parse an array literal. +#[cfg(not(feature = "no_index"))] fn parse_array_literal( input: &mut TokenStream, state: &mut ParseState, lib: &mut FunctionsLib, settings: ParseSettings, ) -> Result { + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; let mut arr = StaticVec::new(); while !input.peek().unwrap().0.is_eof() { - if state.engine.max_array_size > 0 && arr.len() >= state.engine.max_array_size { + #[cfg(not(feature = "unchecked"))] + if state.engine.limits.max_array_size > 0 && arr.len() >= state.engine.limits.max_array_size + { return Err(PERR::LiteralTooLarge( "Size of array literal".to_string(), - state.engine.max_array_size, + state.engine.limits.max_array_size, ) .into_err(input.peek().unwrap().1)); } @@ -1384,6 +1414,7 @@ fn parse_map_literal( lib: &mut FunctionsLib, settings: ParseSettings, ) -> Result { + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; let mut map = StaticVec::new(); @@ -1436,10 +1467,11 @@ fn parse_map_literal( } }; - if state.engine.max_map_size > 0 && map.len() >= state.engine.max_map_size { + #[cfg(not(feature = "unchecked"))] + if state.engine.limits.max_map_size > 0 && map.len() >= state.engine.limits.max_map_size { return Err(PERR::LiteralTooLarge( "Number of properties in object map literal".to_string(), - state.engine.max_map_size, + state.engine.limits.max_map_size, ) .into_err(input.peek().unwrap().1)); } @@ -1492,6 +1524,8 @@ fn parse_primary( ) -> Result { let (token, token_pos) = input.peek().unwrap(); settings.pos = *token_pos; + + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; let (token, _) = match token { @@ -1626,6 +1660,8 @@ fn parse_unary( ) -> Result { let (token, token_pos) = input.peek().unwrap(); settings.pos = *token_pos; + + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; match token { @@ -1708,7 +1744,9 @@ fn parse_unary( Token::Pipe | Token::Or => { let mut state = ParseState::new( state.engine, + #[cfg(not(feature = "unchecked"))] state.max_function_expr_depth, + #[cfg(not(feature = "unchecked"))] state.max_function_expr_depth, ); @@ -1809,6 +1847,8 @@ fn parse_op_assignment_stmt( ) -> Result { let (token, token_pos) = input.peek().unwrap(); settings.pos = *token_pos; + + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; let op = match token { @@ -2048,6 +2088,8 @@ fn parse_binary_op( mut settings: ParseSettings, ) -> Result { settings.pos = lhs.position(); + + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; let mut root = lhs; @@ -2081,6 +2123,8 @@ fn parse_binary_op( settings = settings.level_up(); settings.pos = pos; + + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; let cmp_def = Some(false); @@ -2164,6 +2208,8 @@ fn parse_expr( mut settings: ParseSettings, ) -> Result { settings.pos = input.peek().unwrap().1; + + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; // Check if it is a custom syntax. @@ -2291,6 +2337,8 @@ fn parse_if( ) -> Result { // if ... settings.pos = eat_token(input, Token::If); + + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; // if guard { if_body } @@ -2324,6 +2372,8 @@ fn parse_while( ) -> Result { // while ... settings.pos = eat_token(input, Token::While); + + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; // while guard { body } @@ -2346,6 +2396,8 @@ fn parse_loop( ) -> Result { // loop ... settings.pos = eat_token(input, Token::Loop); + + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; // loop { body } @@ -2364,6 +2416,8 @@ fn parse_for( ) -> Result { // for ... settings.pos = eat_token(input, Token::For); + + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; // for name ... @@ -2417,6 +2471,8 @@ fn parse_let( ) -> Result { // let/const... (specified in `var_type`) settings.pos = input.next().unwrap().1; + + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; // let name ... @@ -2475,6 +2531,8 @@ fn parse_import( ) -> Result { // import ... settings.pos = eat_token(input, Token::Import); + + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; // import expr ... @@ -2509,12 +2567,14 @@ fn parse_import( #[cfg(not(feature = "no_module"))] fn parse_export( input: &mut TokenStream, - state: &mut ParseState, + _state: &mut ParseState, _lib: &mut FunctionsLib, mut settings: ParseSettings, ) -> Result { settings.pos = eat_token(input, Token::Export); - settings.ensure_level_within_max_limit(state.max_expr_depth)?; + + #[cfg(not(feature = "unchecked"))] + settings.ensure_level_within_max_limit(_state.max_expr_depth)?; let mut exports = StaticVec::new(); @@ -2594,6 +2654,7 @@ fn parse_block( } }; + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; let mut statements = StaticVec::new(); @@ -2657,6 +2718,8 @@ fn parse_expr_stmt( mut settings: ParseSettings, ) -> Result { settings.pos = input.peek().unwrap().1; + + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; let expr = parse_expr(input, state, lib, settings.level_up())?; @@ -2678,6 +2741,8 @@ fn parse_stmt( x => x, }; settings.pos = *token_pos; + + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; match token { @@ -2703,7 +2768,9 @@ fn parse_stmt( (Token::Fn, pos) => { let mut state = ParseState::new( state.engine, + #[cfg(not(feature = "unchecked"))] state.max_function_expr_depth, + #[cfg(not(feature = "unchecked"))] state.max_function_expr_depth, ); @@ -2807,6 +2874,7 @@ fn parse_fn( access: FnAccess, mut settings: ParseSettings, ) -> Result { + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; let name = match input.next().unwrap() { @@ -2899,6 +2967,7 @@ fn parse_anon_fn( lib: &mut FunctionsLib, mut settings: ParseSettings, ) -> Result<(Expr, ScriptFnDef), ParseError> { + #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; let mut params = Vec::new(); @@ -2994,7 +3063,15 @@ impl Engine { optimization_level: OptimizationLevel, ) -> Result { let mut functions = Default::default(); - let mut state = ParseState::new(self, self.max_expr_depth, self.max_function_expr_depth); + + let mut state = ParseState::new( + self, + #[cfg(not(feature = "unchecked"))] + self.limits.max_expr_depth, + #[cfg(not(feature = "unchecked"))] + self.limits.max_function_expr_depth, + ); + let settings = ParseSettings { allow_if_expr: false, allow_stmt_expr: false, @@ -3032,7 +3109,14 @@ impl Engine { ) -> Result<(Vec, Vec), ParseError> { let mut statements: Vec = Default::default(); let mut functions = Default::default(); - let mut state = ParseState::new(self, self.max_expr_depth, self.max_function_expr_depth); + + let mut state = ParseState::new( + self, + #[cfg(not(feature = "unchecked"))] + self.limits.max_expr_depth, + #[cfg(not(feature = "unchecked"))] + self.limits.max_function_expr_depth, + ); while !input.peek().unwrap().0.is_eof() { let settings = ParseSettings { diff --git a/src/scope.rs b/src/scope.rs index 8342c792..5693554c 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -377,6 +377,7 @@ impl<'a> Scope<'a> { } /// Update the access type of an entry in the Scope. + #[cfg(not(feature = "no_module"))] pub(crate) fn set_entry_alias(&mut self, index: usize, alias: String) -> &mut Self { let entry = self.0.get_mut(index).expect("invalid index in Scope"); entry.alias = Some(Box::new(alias)); @@ -384,6 +385,7 @@ impl<'a> Scope<'a> { } /// Get an iterator to entries in the Scope. + #[cfg(not(feature = "no_module"))] pub(crate) fn into_iter(self) -> impl Iterator> { self.0.into_iter() } diff --git a/src/serde/de.rs b/src/serde/de.rs index 4ab9919a..6a7cbf6d 100644 --- a/src/serde/de.rs +++ b/src/serde/de.rs @@ -46,8 +46,12 @@ impl<'de> DynamicDeserializer<'de> { } /// Shortcut for a type conversion error. fn type_error(&self) -> Result> { + self.type_error_str(type_name::()) + } + /// Shortcut for a type conversion error. + fn type_error_str(&self, error: &str) -> Result> { Err(Box::new(EvalAltResult::ErrorMismatchOutputType( - type_name::().into(), + error.into(), self.value.type_name().into(), Position::none(), ))) @@ -283,23 +287,23 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> { } } - fn deserialize_f32>(self, visitor: V) -> Result> { + fn deserialize_f32>(self, _visitor: V) -> Result> { #[cfg(not(feature = "no_float"))] return self .value .downcast_ref::() - .map_or_else(|| self.type_error(), |&x| visitor.visit_f32(x)); + .map_or_else(|| self.type_error(), |&x| _visitor.visit_f32(x)); #[cfg(feature = "no_float")] return self.type_error_str("f32"); } - fn deserialize_f64>(self, visitor: V) -> Result> { + fn deserialize_f64>(self, _visitor: V) -> Result> { #[cfg(not(feature = "no_float"))] return self .value .downcast_ref::() - .map_or_else(|| self.type_error(), |&x| visitor.visit_f64(x)); + .map_or_else(|| self.type_error(), |&x| _visitor.visit_f64(x)); #[cfg(feature = "no_float")] return self.type_error_str("f64"); @@ -359,11 +363,11 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> { visitor.visit_newtype_struct(self) } - fn deserialize_seq>(self, visitor: V) -> Result> { + fn deserialize_seq>(self, _visitor: V) -> Result> { #[cfg(not(feature = "no_index"))] return self.value.downcast_ref::().map_or_else( || self.type_error(), - |arr| visitor.visit_seq(IterateArray::new(arr.iter())), + |arr| _visitor.visit_seq(IterateArray::new(arr.iter())), ); #[cfg(feature = "no_index")] @@ -461,6 +465,7 @@ where iter: ITER, } +#[cfg(not(feature = "no_index"))] impl<'a, ITER> IterateArray<'a, ITER> where ITER: Iterator, diff --git a/src/serde/ser.rs b/src/serde/ser.rs index 7c3f9321..50ee077d 100644 --- a/src/serde/ser.rs +++ b/src/serde/ser.rs @@ -11,11 +11,14 @@ use crate::engine::Map; use serde::ser::{ Error, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple, - SerializeTupleStruct, SerializeTupleVariant, Serializer, + SerializeTupleStruct, Serializer, }; use serde::Serialize; -use crate::stdlib::{any::type_name, fmt, mem}; +#[cfg(not(any(feature = "no_object", feature = "no_index")))] +use serde::ser::SerializeTupleVariant; + +use crate::stdlib::{fmt, mem}; /// Serializer for `Dynamic` which is kept as a reference. pub struct DynamicSerializer { @@ -323,13 +326,13 @@ impl Serializer for &mut DynamicSerializer { self, _name: &'static str, _variant_index: u32, - variant: &'static str, - len: usize, + _variant: &'static str, + _len: usize, ) -> Result> { #[cfg(not(any(feature = "no_object", feature = "no_index")))] return Ok(TupleVariantSerializer { - variant, - array: Array::with_capacity(len), + variant: _variant, + array: Array::with_capacity(_len), }); #[cfg(any(feature = "no_object", feature = "no_index"))] { @@ -391,13 +394,13 @@ impl SerializeSeq for DynamicSerializer { fn serialize_element( &mut self, - value: &T, + _value: &T, ) -> Result<(), Box> { #[cfg(not(feature = "no_index"))] { - let value = value.serialize(&mut *self)?; + let _value = _value.serialize(&mut *self)?; let arr = self.value.downcast_mut::().unwrap(); - arr.push(value); + arr.push(_value); Ok(()) } #[cfg(feature = "no_index")] @@ -419,13 +422,13 @@ impl SerializeTuple for DynamicSerializer { fn serialize_element( &mut self, - value: &T, + _value: &T, ) -> Result<(), Box> { #[cfg(not(feature = "no_index"))] { - let value = value.serialize(&mut *self)?; + let _value = _value.serialize(&mut *self)?; let arr = self.value.downcast_mut::().unwrap(); - arr.push(value); + arr.push(_value); Ok(()) } #[cfg(feature = "no_index")] @@ -446,13 +449,13 @@ impl SerializeTupleStruct for DynamicSerializer { fn serialize_field( &mut self, - value: &T, + _value: &T, ) -> Result<(), Box> { #[cfg(not(feature = "no_index"))] { - let value = value.serialize(&mut *self)?; + let _value = _value.serialize(&mut *self)?; let arr = self.value.downcast_mut::().unwrap(); - arr.push(value); + arr.push(_value); Ok(()) } #[cfg(feature = "no_index")] diff --git a/src/settings.rs b/src/settings.rs index 60c1a22a..d638499c 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,12 +1,17 @@ //! Configuration settings for `Engine`. use crate::engine::Engine; -use crate::module::ModuleResolver; use crate::optimize::OptimizationLevel; use crate::packages::PackageLibrary; use crate::token::{is_valid_identifier, Token}; -use crate::stdlib::{boxed::Box, format, string::String}; +#[cfg(not(feature = "no_module"))] +use crate::module::ModuleResolver; + +use crate::stdlib::{format, string::String}; + +#[cfg(not(feature = "no_module"))] +use crate::stdlib::boxed::Box; impl Engine { /// Load a new package into the `Engine`. @@ -41,21 +46,21 @@ impl Engine { /// infinite recursion and stack overflows. #[cfg(not(feature = "unchecked"))] pub fn set_max_call_levels(&mut self, levels: usize) -> &mut Self { - self.max_call_stack_depth = levels; + self.limits.max_call_stack_depth = levels; self } /// The maximum levels of function calls allowed for a script. #[cfg(not(feature = "unchecked"))] pub fn max_call_levels(&self) -> usize { - self.max_call_stack_depth + self.limits.max_call_stack_depth } /// Set the maximum number of operations allowed for a script to run to avoid /// consuming too much resources (0 for unlimited). #[cfg(not(feature = "unchecked"))] pub fn set_max_operations(&mut self, operations: u64) -> &mut Self { - self.max_operations = if operations == u64::MAX { + self.limits.max_operations = if operations == u64::MAX { 0 } else { operations @@ -66,20 +71,20 @@ impl Engine { /// The maximum number of operations allowed for a script to run (0 for unlimited). #[cfg(not(feature = "unchecked"))] pub fn max_operations(&self) -> u64 { - self.max_operations + self.limits.max_operations } /// Set the maximum number of imported modules allowed for a script. #[cfg(not(feature = "unchecked"))] pub fn set_max_modules(&mut self, modules: usize) -> &mut Self { - self.max_modules = modules; + self.limits.max_modules = modules; self } /// The maximum number of imported modules allowed for a script. #[cfg(not(feature = "unchecked"))] pub fn max_modules(&self) -> usize { - self.max_modules + self.limits.max_modules } /// Set the depth limits for expressions (0 for unlimited). @@ -89,12 +94,12 @@ impl Engine { max_expr_depth: usize, max_function_expr_depth: usize, ) -> &mut Self { - self.max_expr_depth = if max_expr_depth == usize::MAX { + self.limits.max_expr_depth = if max_expr_depth == usize::MAX { 0 } else { max_expr_depth }; - self.max_function_expr_depth = if max_function_expr_depth == usize::MAX { + self.limits.max_function_expr_depth = if max_function_expr_depth == usize::MAX { 0 } else { max_function_expr_depth @@ -105,33 +110,33 @@ impl Engine { /// The depth limit for expressions (0 for unlimited). #[cfg(not(feature = "unchecked"))] pub fn max_expr_depth(&self) -> usize { - self.max_expr_depth + self.limits.max_expr_depth } /// The depth limit for expressions in functions (0 for unlimited). #[cfg(not(feature = "unchecked"))] pub fn max_function_expr_depth(&self) -> usize { - self.max_function_expr_depth + self.limits.max_function_expr_depth } /// Set the maximum length of strings (0 for unlimited). #[cfg(not(feature = "unchecked"))] pub fn set_max_string_size(&mut self, max_size: usize) -> &mut Self { - self.max_string_size = if max_size == usize::MAX { 0 } else { max_size }; + self.limits.max_string_size = if max_size == usize::MAX { 0 } else { max_size }; self } /// The maximum length of strings (0 for unlimited). #[cfg(not(feature = "unchecked"))] pub fn max_string_size(&self) -> usize { - self.max_string_size + self.limits.max_string_size } /// Set the maximum length of arrays (0 for unlimited). #[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "no_index"))] pub fn set_max_array_size(&mut self, max_size: usize) -> &mut Self { - self.max_array_size = if max_size == usize::MAX { 0 } else { max_size }; + self.limits.max_array_size = if max_size == usize::MAX { 0 } else { max_size }; self } @@ -139,14 +144,14 @@ impl Engine { #[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "no_index"))] pub fn max_array_size(&self) -> usize { - self.max_array_size + self.limits.max_array_size } /// Set the maximum length of object maps (0 for unlimited). #[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "no_object"))] pub fn set_max_map_size(&mut self, max_size: usize) -> &mut Self { - self.max_map_size = if max_size == usize::MAX { 0 } else { max_size }; + self.limits.max_map_size = if max_size == usize::MAX { 0 } else { max_size }; self } @@ -154,7 +159,7 @@ impl Engine { #[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "no_object"))] pub fn max_map_size(&self) -> usize { - self.max_map_size + self.limits.max_map_size } /// Set the module resolution service used by the `Engine`. diff --git a/src/token.rs b/src/token.rs index 72e733d3..775e78db 100644 --- a/src/token.rs +++ b/src/token.rs @@ -1549,7 +1549,10 @@ pub fn lex<'a, 'e>(input: &'a [&'a str], engine: &'e Engine) -> TokenIterator<'a TokenIterator { engine, state: TokenizeState { - max_string_size: engine.max_string_size, + #[cfg(not(feature = "unchecked"))] + max_string_size: engine.limits.max_string_size, + #[cfg(feature = "unchecked")] + max_string_size: 0, non_unary: false, comment_level: 0, end_with_none: false, diff --git a/tests/call_fn.rs b/tests/call_fn.rs index e96ee63d..9808ff70 100644 --- a/tests/call_fn.rs +++ b/tests/call_fn.rs @@ -166,9 +166,9 @@ fn test_fn_ptr_curry_call() -> Result<(), Box> { module.set_raw_fn( "call_with_arg", &[TypeId::of::(), TypeId::of::()], - |engine: &Engine, module: &Module, args: &mut [&mut Dynamic]| { + |engine: &Engine, lib: &Module, args: &mut [&mut Dynamic]| { let fn_ptr = std::mem::take(args[0]).cast::(); - fn_ptr.call_dynamic(engine, module, None, [std::mem::take(args[1])]) + fn_ptr.call_dynamic(engine, lib, None, [std::mem::take(args[1])]) }, );