From 111f5931b390f3c756f40bf9a9ced6921a1749db Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 18 Aug 2020 23:06:48 +0800 Subject: [PATCH 1/8] Add multiple instantiation. --- doc/src/SUMMARY.md | 3 +- doc/src/patterns/multiple.md | 89 ++++++++++++++++++++++++++++++++++++ doc/src/start/features.md | 12 +++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 doc/src/patterns/multiple.md diff --git a/doc/src/SUMMARY.md b/doc/src/SUMMARY.md index 2ef05a0f..7a4fd884 100644 --- a/doc/src/SUMMARY.md +++ b/doc/src/SUMMARY.md @@ -88,6 +88,7 @@ The Rhai Scripting Language 4. [Create from AST](language/modules/ast.md) 5. [Module Resolvers](rust/modules/resolvers.md) 1. [Custom Implementation](rust/modules/imp-resolver.md) + 18. [Eval Statement](language/eval.md) 6. [Safety and Protection](safety/index.md) 1. [Checked Arithmetic](safety/checked.md) 2. [Sand-Boxing](safety/sandbox.md) @@ -119,7 +120,7 @@ The Rhai Scripting Language 1. [Disable Keywords and/or Operators](engine/disable.md) 2. [Custom Operators](engine/custom-op.md) 3. [Extending with Custom Syntax](engine/custom-syntax.md) - 7. [Eval Statement](language/eval.md) + 7. [Multiple Instantiation](patterns/multiple.md) 8. [Appendix](appendix/index.md) 1. [Keywords](appendix/keywords.md) 2. [Operators and Symbols](appendix/operators.md) diff --git a/doc/src/patterns/multiple.md b/doc/src/patterns/multiple.md new file mode 100644 index 00000000..1abbe942 --- /dev/null +++ b/doc/src/patterns/multiple.md @@ -0,0 +1,89 @@ +Multiple Instantiation +====================== + +{{#include ../links.md}} + + +Background +---------- + +Rhai's [features] are not strictly additive. This is easily deduced from the [`no_std`] feature +which prepares the crate for `no-std` builds. Obviously, turning on this feature has a material +impact on how Rhai behaves. + +Many crates resolve this by going the opposite direction: build for `no-std` in default, +but add a `std` feature, included by default, which builds for the `stdlib`. + + +Rhai Language Features Are Not Additive +-------------------------------------- + +Rhai, however, is more complex. Language features cannot be easily made _additive_. + +That is because the _lack_ of a language feature is a feature by itself. + +For example, by including [`no_float`], a project sets the Rhai language to ignore floating-point math. +Floating-point numbers do not even parse under this case and will generate syntax errors. +Assume that the project expects this behavior (why? perhaps integers are all that make sense +within the project domain). + +Now, assume that a dependent crate also depends on Rhai. Under such circumstances, +unless _exact_ versioning is used and the dependent crate depends on a _different_ version +of Rhai, Cargo automatically _merges_ both dependencies, with the [`no_float`] feature turned on +because Cargo features are _additive_. + +This will break the dependent crate, which does not by itself specify [`no_float`] +and expects floating-point numbers and math to work normally. + +There is no way out of this dilemma. Reversing the [features] set with a `float` feature +causes the project to break because floating-point numbers are not rejected as expected. + + +Multiple Instantiations of Rhai Within The Same Project +------------------------------------------------------ + +The trick is to differentiate between multiple identical copies of Rhai, each having +a different [features] set, by their _sources_: + +* Different versions from [`crates.io`](https://crates.io/crates/rhai/) - The official crate. + +* Different releases from [`GitHub`](https://github.com/jonathandturner/rhai) - Crate source on GitHub. + +* Forked copy of [https://github.com/jonathandturner/rhai](https://github.com/jonathandturner/rhai) on GitHub. + +* Local copy of [https://github.com/jonathandturner/rhai](https://github.com/jonathandturner/rhai) downloaded form GitHub. + +Use the following configuration in `Cargo.toml` to pull in multiple copies of Rhai within the same project: + +```toml +[dependencies] +rhai = { version = "{{version}}", features = [ "no_float" ] } +rhai_github = { git = "https://github.com/jonathandturner/rhai", features = [ "unchecked" ] } +rhai_my_github = { git = "https://github.com/my_github/rhai", branch = "variation1", features = [ "serde", "no_closure" ] } +rhai_local = { path = "../rhai_copy" } +``` + +The example above creates four different modules: `rhai`, `rhai_github`, `rhai_my_github` and +`rhai_local`, each referring to a different Rhai copy with the appropriate [features] set. + +Only one crate of any particular version can be used from each source, because Cargo merges +all candidate cases within the same source, adding all [features] together. + +If more than four different instantiations of Rhai is necessary (why?), create more local repositories +or GitHub forks or branches. + + +Caveat - No Way To Avoid Dependency Conflicts +-------------------------------------------- + +Unfortunately, pulling in Rhai from different sources do not resolve the problem of +[features] conflict between dependencies. Even overriding `crates.io` via the `[patch]` manifest +section doesn't work - all dependencies will eventually find the only one copy. + +What is necessary - multiple copies of Rhai, one for each dependent crate that requires it, +together with their _unique_ [features] set intact. In other words, turning off Cargo's +crate merging feature _just for Rhai_. + +Unfortunately, as of this writing, there is no known method to achieve it. + +Therefore, moral of the story: avoid pulling in multiple crates that depend on Rhai. diff --git a/doc/src/start/features.md b/doc/src/start/features.md index 3201aa07..3e8067fa 100644 --- a/doc/src/start/features.md +++ b/doc/src/start/features.md @@ -52,3 +52,15 @@ no floating-point, is `Send + Sync` (so it can be safely used across threads), a nor loading external [modules]. This configuration is perfect for an expression parser in a 32-bit embedded system without floating-point hardware. + + +Caveat - Features Are Not Additive +--------------------------------- + +Rhai features are not strictly _additive_ - i.e. they do not only add optional functionalities. + +In fact, most features are _subtractive_ - i.e. they _remove_ functionalities. + +There is a reason for this design, because the _lack_ of a language feature by itself is a feature. + +See [here]({{rootUrl}}/patterns/multiple.md) for more details. From c5360db1856ab59fda50c5db161cdaca7f0d16c2 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 18 Aug 2020 23:07:17 +0800 Subject: [PATCH 2/8] Handle #{ in Engine::parse_json, restrict to object hashes only. --- RELEASES.md | 1 + doc/src/language/json.md | 42 ++++++++++++++++++++++++++++++++------ src/api.rs | 44 ++++++++++++++++++++++++++++++++-------- tests/maps.rs | 10 ++++++++- 4 files changed, 82 insertions(+), 15 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index b753bba3..89b05c36 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -9,6 +9,7 @@ New features * Adds `Engine::register_get_result`, `Engine::register_set_result`, `Engine::register_indexer_get_result`, `Engine::register_indexer_set_result` API. * Adds `Module::combine` to combine two modules. +* `Engine::parse_json` now also accepts a JSON object starting with `#{`. Version 0.18.1 diff --git a/doc/src/language/json.md b/doc/src/language/json.md index a5aaba2a..d76877c9 100644 --- a/doc/src/language/json.md +++ b/doc/src/language/json.md @@ -3,11 +3,14 @@ Parse an Object Map from JSON {{#include ../links.md}} -The syntax for an [object map] is extremely similar to JSON, with the exception of `null` values which can -technically be mapped to [`()`]. A valid JSON string does not start with a hash character `#` while a -Rhai [object map] does - that's the major difference! +The syntax for an [object map] is extremely similar to the JSON representation of a object hash, +with the exception of `null` values which can technically be mapped to [`()`]. -Use the `Engine::parse_json` method to parse a piece of JSON into an object map: +A valid JSON string does not start with a hash character `#` while a Rhai [object map] does - that's the major difference! + +Use the `Engine::parse_json` method to parse a piece of JSON into an object map. +The JSON text must represent a single object hash (i.e. must be wrapped within "`{ .. }`") +otherwise it returns a syntax error. ```rust // JSON string - notice that JSON property names are always quoted @@ -26,7 +29,7 @@ let json = r#"{ // Set the second boolean parameter to true in order to map 'null' to '()' let map = engine.parse_json(json, true)?; -map.len() == 6; // 'map' contains all properties in the JSON string +map.len() == 6; // 'map' contains all properties in the JSON string // Put the object map into a 'Scope' let mut scope = Scope::new(); @@ -34,7 +37,7 @@ scope.push("map", map); let result = engine.eval_with_scope::(r#"map["^^^!!!"].len()"#)?; -result == 3; // the object map is successfully used in the script +result == 3; // the object map is successfully used in the script ``` Representation of Numbers @@ -45,3 +48,30 @@ the [`no_float`] feature is not used. Most common generators of JSON data disti integer and floating-point values by always serializing a floating-point number with a decimal point (i.e. `123.0` instead of `123` which is assumed to be an integer). This style can be used successfully with Rhai [object maps]. + + +Parse JSON with Sub-Objects +-------------------------- + +`Engine::parse_json` depends on the fact that the [object map] literal syntax in Rhai is _almost_ +the same as a JSON object. However, it is _almost_ because the syntax for a sub-object in JSON +(i.e. "`{ ... }`") is different from a Rhai [object map] literal (i.e. "`#{ ... }`"). + +When `Engine::parse_json` encounters JSON with sub-objects, it fails with a syntax error. + +If it is certain that no text string in the JSON will ever contain the character '`{`', +then it is possible to parse it by first replacing all occupance of '`{`' with "`#{`". + +A JSON object hash starting with `#{` is handled transparently by `Engine::parse_json`. + +```rust +// JSON with sub-object 'b'. +let json = r#"{"a":1, "b":{"x":true, "y":false}}"#; + +let new_json = json.replace("{" "#{"); + +// The leading '{' will also be replaced to '#{', but parse_json can handle this. +let map = engine.parse_json(&new_json, false)?; + +map.len() == 2; // 'map' contains two properties: 'a' and 'b' +``` diff --git a/src/api.rs b/src/api.rs index 8487370f..8b91cd2a 100644 --- a/src/api.rs +++ b/src/api.rs @@ -2,7 +2,7 @@ use crate::any::{Dynamic, Variant}; use crate::engine::{Engine, Imports, State}; -use crate::error::ParseError; +use crate::error::{ParseError, ParseErrorType}; use crate::fn_native::{IteratorFn, SendSync}; use crate::module::{FuncReturn, Module}; use crate::optimize::OptimizationLevel; @@ -895,24 +895,39 @@ impl Engine { /// Parse a JSON string into a map. /// + /// The JSON string must be an object hash. It cannot be a simple JavaScript primitive. + /// /// Set `has_null` to `true` in order to map `null` values to `()`. /// Setting it to `false` will cause a _variable not found_ error during parsing. /// + /// # JSON With Sub-Objects + /// + /// This method assumes no sub-objects in the JSON string. That is because the syntax + /// of a JSON sub-object (or object hash), `{ .. }`, is different from Rhai's syntax, `#{ .. }`. + /// Parsing a JSON string with sub-objects will cause a syntax error. + /// + /// If it is certain that the character `{` never appears in any text string within the JSON object, + /// then globally replace `{` with `#{` before calling this method. + /// /// # Example /// /// ``` /// # fn main() -> Result<(), Box> { - /// use rhai::Engine; + /// use rhai::{Engine, Map}; /// /// let engine = Engine::new(); /// - /// let map = engine.parse_json(r#"{"a":123, "b":42, "c":false, "d":null}"#, true)?; + /// let map = engine.parse_json( + /// r#"{"a":123, "b":42, "c":{"x":false, "y":true}, "d":null}"# + /// .replace("{", "#{").as_str(), true)?; /// /// assert_eq!(map.len(), 4); - /// assert_eq!(map.get("a").cloned().unwrap().cast::(), 123); - /// assert_eq!(map.get("b").cloned().unwrap().cast::(), 42); - /// assert_eq!(map.get("c").cloned().unwrap().cast::(), false); - /// assert_eq!(map.get("d").cloned().unwrap().cast::<()>(), ()); + /// assert_eq!(map["a"].as_int().unwrap(), 123); + /// assert_eq!(map["b"].as_int().unwrap(), 42); + /// assert!(map["d"].is::<()>()); + /// + /// let c = map["c"].read_lock::().unwrap(); + /// assert_eq!(c["x"].as_bool().unwrap(), false); /// # Ok(()) /// # } /// ``` @@ -921,7 +936,20 @@ impl Engine { let mut scope = Scope::new(); // Trims the JSON string and add a '#' in front - let scripts = ["#", json.trim()]; + let json_text = json.trim_start(); + let scripts = if json_text.starts_with(Token::MapStart.syntax().as_ref()) { + [json_text, ""] + } else if json_text.starts_with(Token::LeftBrace.syntax().as_ref()) { + ["#", json_text] + } else { + return Err(ParseErrorType::MissingToken( + Token::LeftBrace.syntax().to_string(), + "to start a JSON object hash".to_string(), + ) + .into_err(Position::new(1, (json.len() - json_text.len() + 1) as u16)) + .into()); + }; + let stream = lex( &scripts, if has_null { diff --git a/tests/maps.rs b/tests/maps.rs index c57deaa6..988c51a3 100644 --- a/tests/maps.rs +++ b/tests/maps.rs @@ -1,6 +1,6 @@ #![cfg(not(feature = "no_object"))] -use rhai::{Engine, EvalAltResult, Map, Scope, INT}; +use rhai::{Engine, EvalAltResult, Map, ParseErrorType, Scope, INT}; #[test] fn test_map_indexing() -> Result<(), Box> { @@ -182,6 +182,14 @@ fn test_map_json() -> Result<(), Box> { ); } + engine.parse_json(&format!("#{}", json), true)?; + + assert!(matches!( + *engine.parse_json(" 123", true).expect_err("should error"), + EvalAltResult::ErrorParsing(ParseErrorType::MissingToken(token, _), pos) + if token == "{" && pos.position() == Some(4) + )); + Ok(()) } From 6a3e123306b3e27b34b6bb1a311b388afa2f27c0 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 18 Aug 2020 23:19:26 +0800 Subject: [PATCH 3/8] Use split_first_mut instead of split_at_mut. --- doc/src/rust/register-raw.md | 8 ++++---- src/fn_call.rs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/src/rust/register-raw.md b/doc/src/rust/register-raw.md index 5a73cae7..210f419f 100644 --- a/doc/src/rust/register-raw.md +++ b/doc/src/rust/register-raw.md @@ -33,7 +33,7 @@ engine.register_raw_fn( // But remember this is Rust, so you can keep only one mutable reference at any one time! // Therefore, get a '&mut' reference to the first argument _last_. - // Alternatively, use `args.split_at_mut(1)` etc. to split the slice first. + // Alternatively, use `args.split_first_mut()` etc. to split the slice first. let y: i64 = *args[1].read_lock::() // get a reference to the second argument .unwrap(); // then copying it because it is a primary type @@ -163,15 +163,15 @@ Hold Multiple References ------------------------ In order to access a value argument that is expensive to clone _while_ holding a mutable reference -to the first argument, either _consume_ that argument via `mem::take` as above, or use `args.split_at` +to the first argument, either _consume_ that argument via `mem::take` as above, or use `args.split_first` to partition the slice: ```rust // Partition the slice -let (first, rest) = args.split_at_mut(1); +let (first, rest) = args.split_first_mut().unwrap(); // Mutable reference to the first parameter -let this_ptr: &mut A = &mut *first[0].write_lock::().unwrap(); +let this_ptr: &mut A = &mut *first.write_lock::().unwrap(); // Immutable reference to the second value parameter // This can be mutable but there is no point because the parameter is passed by value diff --git a/src/fn_call.rs b/src/fn_call.rs index 7d5e313a..c7300f15 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -521,13 +521,13 @@ impl Engine { let result = if _is_method { // Method call of script function - map first argument to `this` - let (first, rest) = args.split_at_mut(1); + let (first, rest) = args.split_first_mut().unwrap(); self.call_script_fn( scope, mods, state, lib, - &mut Some(first[0]), + &mut Some(*first), fn_name, func, rest, From ac6d519d28052906f7e08928be3ce137c20c0aeb Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 20 Aug 2020 16:26:10 +0800 Subject: [PATCH 4/8] Fix bug that consumes first argument in module-qualified call. --- RELEASES.md | 6 ++++++ src/fn_call.rs | 39 +++++++++++++++++++++++++++++++++++---- tests/modules.rs | 25 +++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index 89b05c36..d42f41e2 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -4,6 +4,12 @@ Rhai Release Notes Version 0.19.0 ============== +Bug fixes +--------- + +* Fixes bug that prevents calling functions in closures. +* Fixes bug that erroneously consumes the first argument to a module-qualified function call. + New features ------------ diff --git a/src/fn_call.rs b/src/fn_call.rs index c7300f15..494fe1a0 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -974,6 +974,7 @@ impl Engine { ) -> Result> { let modules = modules.as_ref().unwrap(); let mut arg_values: StaticVec<_>; + let mut first_arg_value = None; let mut args: StaticVec<_>; if args_expr.is_empty() { @@ -988,17 +989,28 @@ impl Engine { Expr::Variable(x) if x.1.is_none() => { arg_values = args_expr .iter() - .skip(1) - .map(|expr| self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)) + .enumerate() + .map(|(i, expr)| { + // Skip the first argument + if i == 0 { + Ok(Default::default()) + } else { + self.eval_expr(scope, mods, state, lib, this_ptr, expr, level) + } + }) .collect::>()?; + // Get target reference to first argument let (target, _, _, pos) = search_scope_only(scope, state, this_ptr, args_expr.get(0).unwrap())?; self.inc_operations(state) .map_err(|err| err.new_position(pos))?; - args = once(target).chain(arg_values.iter_mut()).collect(); + let (first, rest) = arg_values.split_first_mut().unwrap(); + first_arg_value = Some(first); + + args = once(target).chain(rest.iter_mut()).collect(); } // func(..., ...) or func(mod::x, ...) _ => { @@ -1037,6 +1049,13 @@ impl Engine { match func { #[cfg(not(feature = "no_function"))] Some(f) if f.is_script() => { + // Clone first argument + if let Some(first) = first_arg_value { + let first_val = args[0].clone(); + args[0] = first; + *args[0] = first_val; + } + let args = args.as_mut(); let func = f.get_fn_def(); @@ -1055,7 +1074,19 @@ impl Engine { self.call_script_fn(scope, mods, state, lib, &mut None, name, func, args, level) } - Some(f) => f.get_native_fn()(self, lib, args.as_mut()), + Some(f) if f.is_native() => { + if !f.is_method() { + // Clone first argument + if let Some(first) = first_arg_value { + let first_val = args[0].clone(); + args[0] = first; + *args[0] = first_val; + } + } + + f.get_native_fn()(self, lib, args.as_mut()) + } + Some(_) => unreachable!(), None if def_val.is_some() => Ok(def_val.unwrap().into()), None => EvalAltResult::ErrorFunctionNotFound( format!( diff --git a/tests/modules.rs b/tests/modules.rs index 3b00ab17..06f5e37e 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -94,6 +94,31 @@ fn test_module_resolver() -> Result<(), Box> { 42 ); + assert_eq!( + engine.eval::( + r#" + import "hello" as h1; + import "hello" as h2; + let x = 42; + h1::sum(x, -10, 3, 7) + "# + )?, + 42 + ); + + assert_eq!( + engine.eval::( + r#" + import "hello" as h1; + import "hello" as h2; + let x = 42; + h1::sum(x, 0, 0, 0); + x + "# + )?, + 42 + ); + assert_eq!( engine.eval::( r#" From 24610688d30d4467b02b8c247bde6405f1b13577 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 20 Aug 2020 17:02:25 +0800 Subject: [PATCH 5/8] Fix no_std build. --- src/api.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/api.rs b/src/api.rs index 8b91cd2a..852fd2a7 100644 --- a/src/api.rs +++ b/src/api.rs @@ -34,6 +34,7 @@ use crate::optimize::optimize_into_ast; use crate::stdlib::{ any::{type_name, TypeId}, boxed::Box, + string::ToString, }; #[cfg(not(feature = "no_optimize"))] From 0b04d05afe1be17aa09b165a41b1936f72517704 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 22 Aug 2020 11:08:27 +0800 Subject: [PATCH 6/8] Do not parse closures when allow_anonymous_fn is false. --- RELEASES.md | 9 +++++ doc/src/engine/expressions.md | 4 +- src/parser.rs | 4 +- tests/closures.rs | 70 ++++++++++++++++++++++++++++++++++- 4 files changed, 84 insertions(+), 3 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index d42f41e2..6ed51c4a 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -7,6 +7,15 @@ Version 0.19.0 Bug fixes --------- +* `Engine::compile_expression`, `Engine::eval_expression` etc. no longer parse anonymous functions and closures. + + +Version 0.18.2 +============== + +Bug fixes +--------- + * Fixes bug that prevents calling functions in closures. * Fixes bug that erroneously consumes the first argument to a module-qualified function call. diff --git a/doc/src/engine/expressions.md b/doc/src/engine/expressions.md index e9e890bf..a87a901c 100644 --- a/doc/src/engine/expressions.md +++ b/doc/src/engine/expressions.md @@ -11,9 +11,11 @@ In these cases, use the `Engine::compile_expression` and `Engine::eval_expressio let result = engine.eval_expression::("2 + (10 + 10) * 2")?; ``` -When evaluating _expressions_, no full-blown statement (e.g. `if`, `while`, `for`) - not even variable assignment - +When evaluating _expressions_, no full-blown statement (e.g. `if`, `while`, `for`, `fn`) - not even variable assignment - is supported and will be considered parse errors when encountered. +[Closures] and [anonymous functions] are also not supported because in the background they compile to functions. + ```rust // The following are all syntax errors because the script is not an expression. diff --git a/src/parser.rs b/src/parser.rs index b8bb9cd8..39ffaada 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1862,7 +1862,7 @@ fn parse_unary( } // | ... #[cfg(not(feature = "no_function"))] - Token::Pipe | Token::Or => { + Token::Pipe | Token::Or if settings.allow_anonymous_fn => { let mut new_state = ParseState::new( state.engine, #[cfg(not(feature = "unchecked"))] @@ -3353,6 +3353,8 @@ impl Engine { }; let expr = parse_expr(input, &mut state, &mut functions, settings)?; + assert!(functions.is_empty()); + match input.peek().unwrap() { (Token::EOF, _) => (), // Return error if the expression doesn't end diff --git a/tests/closures.rs b/tests/closures.rs index 94b8a9ca..4eb8d81b 100644 --- a/tests/closures.rs +++ b/tests/closures.rs @@ -1,7 +1,11 @@ #![cfg(not(feature = "no_function"))] -use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, RegisterFn, INT}; +use rhai::{ + Dynamic, Engine, EvalAltResult, FnPtr, Map, Module, ParseErrorType, RegisterFn, Scope, INT, +}; use std::any::TypeId; +use std::cell::RefCell; use std::mem::take; +use std::rc::Rc; #[test] fn test_fn_ptr_curry_call() -> Result<(), Box> { @@ -39,6 +43,14 @@ fn test_fn_ptr_curry_call() -> Result<(), Box> { fn test_closures() -> Result<(), Box> { let mut engine = Engine::new(); + assert!(matches!( + *engine + .compile_expression("let f = |x| {};") + .expect_err("should error") + .0, + ParseErrorType::BadInput(_) + )); + assert_eq!( engine.eval::( r#" @@ -181,3 +193,59 @@ fn test_closures_data_race() -> Result<(), Box> { Ok(()) } + +type MyType = Rc>; + +#[test] +#[cfg(not(feature = "no_object"))] +#[cfg(not(feature = "sync"))] +fn test_closure_shared_obj() -> Result<(), Box> { + let mut engine = Engine::new(); + + // Register API on MyType + engine + .register_type_with_name::("MyType") + .register_get_set( + "data", + |p: &mut MyType| *p.borrow(), + |p: &mut MyType, value: INT| *p.borrow_mut() = value, + ) + .register_fn("+=", |p1: &mut MyType, p2: MyType| { + *p1.borrow_mut() += *p2.borrow() + }) + .register_fn("-=", |p1: &mut MyType, p2: MyType| { + *p1.borrow_mut() -= *p2.borrow() + }); + + let engine = engine; // Make engine immutable + + let code = r#" + #{ + name: "A", + description: "B", + cost: 1, + health_added: 0, + action: |p1, p2| { p1 += p2 } + } + "#; + + let ast = engine.compile(code)?; + let res = engine.eval_ast::(&ast)?; + + // Make closure + let f = move |p1: MyType, p2: MyType| -> Result<(), Box> { + let action_ptr = res["action"].clone().cast::(); + let name = action_ptr.fn_name(); + engine.call_fn::<_, ()>(&mut Scope::new(), &ast, name, (p1, p2)) + }; + + // Test closure + let p1 = Rc::new(RefCell::new(41)); + let p2 = Rc::new(RefCell::new(1)); + + f(p1.clone(), p2.clone())?; + + assert_eq!(*p1.borrow(), 42); + + Ok(()) +} From 0ece75aba364226d531c2f4f6409c598f01b2f30 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 22 Aug 2020 22:44:24 +0800 Subject: [PATCH 7/8] Allow module access in closures. --- Cargo.toml | 2 +- RELEASES.md | 3 ++- src/parser.rs | 15 +++++++++++++++ tests/call_fn.rs | 14 ++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index df785e3e..25305837 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rhai" -version = "0.19.0" +version = "0.18.3" edition = "2018" authors = ["Jonathan Turner", "Lukáš Hozda", "Stephen Chung"] description = "Embedded scripting for Rust" diff --git a/RELEASES.md b/RELEASES.md index 6ed51c4a..db26c661 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,13 +1,14 @@ Rhai Release Notes ================== -Version 0.19.0 +Version 0.18.3 ============== Bug fixes --------- * `Engine::compile_expression`, `Engine::eval_expression` etc. no longer parse anonymous functions and closures. +* Imported modules now work inside closures. Version 0.18.2 diff --git a/src/parser.rs b/src/parser.rs index 39ffaada..01be7715 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1645,11 +1645,22 @@ fn parse_primary( } Expr::Variable(Box::new(((s, settings.pos), None, 0, None))) } + // Module qualification + #[cfg(not(feature = "no_module"))] + Token::Identifier(s) if *next_token == Token::DoubleColon => { + // Once the identifier consumed we must enable next variables capturing + #[cfg(not(feature = "no_closure"))] + { + state.allow_capture = true; + } + Expr::Variable(Box::new(((s, settings.pos), None, 0, None))) + } // Normal variable access Token::Identifier(s) => { let index = state.access_var(&s, settings.pos); Expr::Variable(Box::new(((s, settings.pos), None, 0, index))) } + // Function call is allowed to have reserved keyword Token::Reserved(s) if *next_token == Token::LeftParen || *next_token == Token::Bang => { if is_keyword_function(&s) { @@ -1658,6 +1669,7 @@ fn parse_primary( return Err(PERR::Reserved(s).into_err(settings.pos)); } } + // Access to `this` as a variable is OK Token::Reserved(s) if s == KEYWORD_THIS && *next_token != Token::LeftParen => { if !settings.is_function_scope { @@ -1669,9 +1681,11 @@ fn parse_primary( Expr::Variable(Box::new(((s, settings.pos), None, 0, None))) } } + Token::Reserved(s) if is_valid_identifier(s.chars()) => { return Err(PERR::Reserved(s).into_err(settings.pos)); } + Token::LeftParen => parse_paren_expr(input, state, lib, settings.level_up())?, #[cfg(not(feature = "no_index"))] Token::LeftBracket => parse_array_literal(input, state, lib, settings.level_up())?, @@ -1680,6 +1694,7 @@ fn parse_primary( Token::True => Expr::True(settings.pos), Token::False => Expr::False(settings.pos), Token::LexError(err) => return Err(err.into_err(settings.pos)), + _ => { return Err( PERR::BadInput(format!("Unexpected '{}'", token.syntax())).into_err(settings.pos) diff --git a/tests/call_fn.rs b/tests/call_fn.rs index a7a5d514..5d22abd1 100644 --- a/tests/call_fn.rs +++ b/tests/call_fn.rs @@ -120,6 +120,20 @@ fn test_fn_ptr_raw() -> Result<(), Box> { 42 ); + assert_eq!( + engine.eval::( + r#" + fn foo(x, y) { this += x + y; } + + let x = 40; + let v = 1; + x.bar(Fn("foo").curry(v), 1); + x + "# + )?, + 42 + ); + assert!(matches!( *engine.eval::( r#" From 177a0de23cd44f0145af68f7ffe6b8b1cb26521b Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 22 Aug 2020 23:01:25 +0800 Subject: [PATCH 8/8] Fix closure test. --- src/api.rs | 8 ++++---- tests/closures.rs | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/api.rs b/src/api.rs index 852fd2a7..899b1ab6 100644 --- a/src/api.rs +++ b/src/api.rs @@ -2,7 +2,7 @@ use crate::any::{Dynamic, Variant}; use crate::engine::{Engine, Imports, State}; -use crate::error::{ParseError, ParseErrorType}; +use crate::error::ParseError; use crate::fn_native::{IteratorFn, SendSync}; use crate::module::{FuncReturn, Module}; use crate::optimize::OptimizationLevel; @@ -18,6 +18,7 @@ use crate::engine::{FN_IDX_GET, FN_IDX_SET}; #[cfg(not(feature = "no_object"))] use crate::{ engine::{make_getter, make_setter, Map}, + error::ParseErrorType, fn_register::{RegisterFn, RegisterResultFn}, token::Token, }; @@ -34,7 +35,6 @@ use crate::optimize::optimize_into_ast; use crate::stdlib::{ any::{type_name, TypeId}, boxed::Box, - string::ToString, }; #[cfg(not(feature = "no_optimize"))] @@ -944,8 +944,8 @@ impl Engine { ["#", json_text] } else { return Err(ParseErrorType::MissingToken( - Token::LeftBrace.syntax().to_string(), - "to start a JSON object hash".to_string(), + Token::LeftBrace.syntax().into(), + "to start a JSON object hash".into(), ) .into_err(Position::new(1, (json.len() - json_text.len() + 1) as u16)) .into()); diff --git a/tests/closures.rs b/tests/closures.rs index 4eb8d81b..348aa162 100644 --- a/tests/closures.rs +++ b/tests/closures.rs @@ -1,12 +1,13 @@ #![cfg(not(feature = "no_function"))] -use rhai::{ - Dynamic, Engine, EvalAltResult, FnPtr, Map, Module, ParseErrorType, RegisterFn, Scope, INT, -}; +use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, ParseErrorType, RegisterFn, Scope, INT}; use std::any::TypeId; use std::cell::RefCell; use std::mem::take; use std::rc::Rc; +#[cfg(not(feature = "no_object"))] +use rhai::Map; + #[test] fn test_fn_ptr_curry_call() -> Result<(), Box> { let mut engine = Engine::new();