diff --git a/Cargo.toml b/Cargo.toml index 85b57974..0cc0e20d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,7 @@ no_index = [] # no arrays and indexing no_object = [] # no custom objects no_function = [] # no script-defined functions no_capture = [] # no automatic read/write binding of anonymous function's local variables to it's external context -no_shared = [] # no explicit shared variables in the script code +no_shared = [] # no explicit shared() and take() functions in the script code no_module = [] # no modules internals = [] # expose internal data structures unicode-xid-ident = ["unicode-xid"] # allow Unicode Standard Annex #31 for identifiers. diff --git a/src/any.rs b/src/any.rs index db2ada1b..1a82d237 100644 --- a/src/any.rs +++ b/src/any.rs @@ -568,7 +568,7 @@ impl Dynamic { /// /// ## Safety /// - /// Both situations normally shouldn't happen since all operations in Rhai + /// Both situations normally shouldn't happen since most operations in Rhai /// use pass-by-value data and the script executed in a single thread. /// /// # Example @@ -741,7 +741,9 @@ impl Dynamic { } /// Get a copy of a specific type to the `Dynamic`. - /// Casting to `Dynamic` just returns a reference to it. + /// Casting to `Dynamic` returns a clone of the value in case of NON-shared + /// Dynamic. In case of Shared Dynamic returns a clone of the inner data of + /// Shared Dynamic. /// Returns `None` if the cast fails. #[inline(always)] pub fn read(&self) -> Option { diff --git a/src/engine.rs b/src/engine.rs index 8f727211..4cc3b266 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -93,6 +93,7 @@ pub const KEYWORD_FN_PTR: &str = "Fn"; pub const KEYWORD_FN_PTR_CALL: &str = "call"; pub const KEYWORD_FN_PTR_CURRY: &str = "curry"; pub const KEYWORD_SHARED: &str = "shared"; +pub const KEYWORD_TAKE: &str = "take"; pub const KEYWORD_THIS: &str = "this"; pub const FN_TO_STRING: &str = "to_string"; #[cfg(not(feature = "no_object"))] diff --git a/src/fn_call.rs b/src/fn_call.rs index 19f3fd33..6f82df42 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -5,7 +5,7 @@ use crate::calc_fn_hash; use crate::engine::{ search_imports, search_namespace, search_scope_only, Engine, Imports, State, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_FN_PTR_CALL, KEYWORD_FN_PTR_CURRY, KEYWORD_PRINT, - KEYWORD_TYPE_OF, KEYWORD_SHARED + KEYWORD_TYPE_OF, KEYWORD_SHARED, KEYWORD_TAKE }; use crate::error::ParseErrorType; use crate::fn_native::{FnCallArgs, FnPtr}; @@ -593,6 +593,9 @@ impl Engine { .into(), false, )) + } else if _fn_name == KEYWORD_TAKE { + // take call + return Ok((obj.read::().unwrap(), false)); } else { #[cfg(not(feature = "no_object"))] let redirected; diff --git a/src/token.rs b/src/token.rs index 71ec09a6..b0c77e38 100644 --- a/src/token.rs +++ b/src/token.rs @@ -2,7 +2,7 @@ use crate::engine::{ Engine, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_FN_PTR_CALL, KEYWORD_FN_PTR_CURRY, - KEYWORD_SHARED, KEYWORD_PRINT, KEYWORD_THIS, KEYWORD_TYPE_OF, + KEYWORD_SHARED, KEYWORD_TAKE, KEYWORD_PRINT, KEYWORD_THIS, KEYWORD_TYPE_OF, }; use crate::error::LexError; @@ -503,11 +503,12 @@ impl Token { "===" | "!==" | "->" | "<-" | "=>" | ":=" | "::<" | "(*" | "*)" | "#" | "public" | "new" | "use" | "module" | "package" | "var" | "static" | "with" | "do" | "each" | "then" | "goto" | "exit" | "switch" | "match" | "case" | "try" | "catch" - | "default" | "void" | "null" | "nil" | "spawn" | "go" | "shared" | "sync" + | "default" | "void" | "null" | "nil" | "spawn" | "go" | "sync" | "async" | "await" | "yield" => Reserved(syntax.into()), KEYWORD_PRINT | KEYWORD_DEBUG | KEYWORD_TYPE_OF | KEYWORD_EVAL | KEYWORD_FN_PTR - | KEYWORD_FN_PTR_CALL | KEYWORD_FN_PTR_CURRY | KEYWORD_SHARED | KEYWORD_THIS => Reserved(syntax.into()), + | KEYWORD_FN_PTR_CALL | KEYWORD_FN_PTR_CURRY | KEYWORD_SHARED + | KEYWORD_TAKE |KEYWORD_THIS => Reserved(syntax.into()), _ => return None, }) @@ -1440,7 +1441,7 @@ pub fn is_keyword_function(name: &str) -> bool { #[cfg(not(feature = "no-shared"))] { - result = result || name == KEYWORD_SHARED; + result = result || name == KEYWORD_SHARED || name == KEYWORD_TAKE; } result diff --git a/tests/closures.rs b/tests/closures.rs index 25166ee1..6198bc26 100644 --- a/tests/closures.rs +++ b/tests/closures.rs @@ -116,8 +116,8 @@ fn test_shared() -> Result<(), Box> { let s = shared("test"); let i = shared(0); i = 2; - s[i] = 'S'; + s "# )?, @@ -283,6 +283,26 @@ fn test_shared() -> Result<(), Box> { engine.register_fn("update", TestStruct::update); engine.register_fn("merge", TestStruct::merge); engine.register_fn("new_ts", TestStruct::new); + engine. + register_raw_fn( + "mutate_with_cb", + &[ + TypeId::of::(), + TypeId::of::(), + TypeId::of::(), + ], + move |engine: &Engine, lib: &Module, args: &mut [&mut Dynamic]| { + let fp = std::mem::take(args[2]).cast::(); + let mut value = args[1].clone(); + { + let mut lock = value.write_lock::().unwrap(); + *lock = *lock + 1; + } + let this_ptr = args.get_mut(0).unwrap(); + + fp.call_dynamic(engine, lib, Some(this_ptr), [value]) + }, + ); assert_eq!( engine.eval::( @@ -291,11 +311,33 @@ fn test_shared() -> Result<(), Box> { a.x = 100; a.update(); - // a.merge(a); + a.merge(a.take()); // take is important to prevent a deadlock + a.x " )?, - 1100 + 2200 + ); + + assert_eq!( + engine.eval::( + r" + let a = shared(new_ts()); + let b = shared(100); + + a.mutate_with_cb(b, |param| { + this.x = param; + param = 50; + this.update(); + }); + + a.update(); + a.x += b; + + a.x + " + )?, + 2151 ); Ok(())