diff --git a/src/any.rs b/src/any.rs index a7983030..7990ff10 100644 --- a/src/any.rs +++ b/src/any.rs @@ -18,7 +18,7 @@ use crate::stdlib::{ boxed::Box, fmt, string::String, - ops::{Deref, DerefMut} + ops::{DerefMut, Deref} }; #[cfg(not(feature = "sync"))] @@ -259,11 +259,13 @@ impl Dynamic { /// If the `Dynamic` is a Shared variant checking is performed on /// top of it's internal value. pub fn is(&self) -> bool { - self.type_id() == TypeId::of::() - || match self.0 { - Union::Str(_) => TypeId::of::() == TypeId::of::(), - _ => false, - } + let mut target_type_id = TypeId::of::(); + + if target_type_id == TypeId::of::() { + target_type_id = TypeId::of::(); + } + + self.type_id() == target_type_id } /// Get the TypeId of the value held by this `Dynamic`. @@ -606,6 +608,7 @@ impl Dynamic { _ => None, }; } + #[cfg(not(feature = "no_float"))] if type_id == TypeId::of::() { return match self.0 { @@ -613,30 +616,35 @@ impl Dynamic { _ => None, }; } + if type_id == TypeId::of::() { return match self.0 { Union::Bool(value) => unsafe_try_cast(value), _ => None, }; } + if type_id == TypeId::of::() { return match self.0 { Union::Str(value) => unsafe_try_cast(value), _ => None, }; } + if type_id == TypeId::of::() { return match self.0 { Union::Str(value) => unsafe_try_cast(value.into_owned()), _ => None, }; } + if type_id == TypeId::of::() { return match self.0 { Union::Char(value) => unsafe_try_cast(value), _ => None, }; } + #[cfg(not(feature = "no_index"))] if type_id == TypeId::of::() { return match self.0 { @@ -644,6 +652,7 @@ impl Dynamic { _ => None, }; } + #[cfg(not(feature = "no_object"))] if type_id == TypeId::of::() { return match self.0 { @@ -651,12 +660,14 @@ impl Dynamic { _ => None, }; } + if type_id == TypeId::of::() { return match self.0 { Union::FnPtr(value) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v), _ => None, }; } + if type_id == TypeId::of::<()>() { return match self.0 { Union::Unit(value) => unsafe_try_cast(value), @@ -951,6 +962,7 @@ impl Dynamic { pub fn as_int(&self) -> Result { match self.0 { Union::Int(n) => Ok(n), + Union::Shared(_) => self.read::().ok_or_else(|| self.type_name()), _ => Err(self.type_name()), } } @@ -961,6 +973,7 @@ impl Dynamic { pub fn as_float(&self) -> Result { match self.0 { Union::Float(n) => Ok(n), + Union::Shared(_) => self.read::().ok_or_else(|| self.type_name()), _ => Err(self.type_name()), } } @@ -970,6 +983,7 @@ impl Dynamic { pub fn as_bool(&self) -> Result { match self.0 { Union::Bool(b) => Ok(b), + Union::Shared(_) => self.read::().ok_or_else(|| self.type_name()), _ => Err(self.type_name()), } } @@ -979,12 +993,15 @@ impl Dynamic { pub fn as_char(&self) -> Result { match self.0 { Union::Char(n) => Ok(n), + Union::Shared(_) => self.read::().ok_or_else(|| self.type_name()), _ => Err(self.type_name()), } } /// Cast the `Dynamic` as a string and return the string slice. /// Returns the name of the actual type if the cast fails. + /// + /// Cast is failing if `self` is Shared Dynamic pub fn as_str(&self) -> Result<&str, &'static str> { match &self.0 { Union::Str(s) => Ok(s), @@ -1006,6 +1023,20 @@ impl Dynamic { match self.0 { Union::Str(s) => Ok(s), Union::FnPtr(f) => Ok(f.take_data().0), + Union::Shared(cell) => { + #[cfg(not(feature = "sync"))] + match &cell.container.borrow().deref().0 { + Union::Str(s) => Ok(s.clone()), + Union::FnPtr(f) => Ok(f.clone().take_data().0), + _ => Err(cell.value_type_name), + } + #[cfg(feature = "sync")] + match &cell.container.read().deref().0 { + Union::Str(s) => Ok(s.clone()), + Union::FnPtr(f) => Ok(f.clone().take_data().0), + _ => Err(cell.value_type_name), + } + } _ => Err(self.type_name()), } } diff --git a/src/engine.rs b/src/engine.rs index 06ff8d7f..8f727211 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1,6 +1,6 @@ //! Main module defining the script evaluation `Engine`. -use crate::any::{map_std_type_name, Dynamic, Union}; +use crate::any::{map_std_type_name, Dynamic, Union, DynamicWriteLock}; use crate::calc_fn_hash; use crate::fn_call::run_builtin_op_assignment; use crate::fn_native::{CallableFunction, Callback, FnPtr}; @@ -39,6 +39,7 @@ use crate::stdlib::{ iter::{empty, once}, string::{String, ToString}, vec::Vec, + ops::DerefMut, }; #[cfg(not(feature = "no_index"))] @@ -123,6 +124,9 @@ pub enum ChainType { pub enum Target<'a> { /// The target is a mutable reference to a `Dynamic` value somewhere. Ref(&'a mut Dynamic), + /// The target is a mutable reference to a Shared `Dynamic` value. + /// It holds the access guard and the original container both for cloning purposes + LockGuard((DynamicWriteLock<'a, Dynamic>, Dynamic)), /// The target is a temporary `Dynamic` value (i.e. the mutation can cause no side effects). Value(Dynamic), /// The target is a character inside a String. @@ -137,6 +141,7 @@ impl Target<'_> { pub fn is_ref(&self) -> bool { match self { Self::Ref(_) => true, + Self::LockGuard(_) => true, Self::Value(_) => false, #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, _) => false, @@ -146,6 +151,7 @@ impl Target<'_> { pub fn is_value(&self) -> bool { match self { Self::Ref(_) => false, + Self::LockGuard(_) => false, Self::Value(_) => true, #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, _) => false, @@ -156,6 +162,7 @@ impl Target<'_> { pub fn is(&self) -> bool { match self { Target::Ref(r) => r.is::(), + Target::LockGuard((r, _)) => r.is::(), Target::Value(r) => r.is::(), #[cfg(not(feature = "no_index"))] Target::StringChar(_, _, _) => TypeId::of::() == TypeId::of::(), @@ -165,6 +172,7 @@ impl Target<'_> { pub fn clone_into_dynamic(self) -> Dynamic { match self { Self::Ref(r) => r.clone(), // Referenced value is cloned + Self::LockGuard((_, orig)) => orig, // Return original container of the Shared Dynamic Self::Value(v) => v, // Owned value is simply taken #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, ch) => ch, // Character is taken @@ -174,6 +182,7 @@ impl Target<'_> { pub fn as_mut(&mut self) -> &mut Dynamic { match self { Self::Ref(r) => *r, + Self::LockGuard((r, _)) => r.deref_mut(), Self::Value(ref mut r) => r, #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, ref mut r) => r, @@ -184,13 +193,16 @@ impl Target<'_> { pub fn set_value(&mut self, new_val: Dynamic) -> Result<(), Box> { match self { Self::Ref(r) => **r = new_val, + Self::LockGuard((r, _)) => **r = new_val, Self::Value(_) => { return Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS( Position::none(), ))) } #[cfg(not(feature = "no_index"))] - Self::StringChar(Dynamic(Union::Str(ref mut s)), index, _) => { + Self::StringChar(string, index, _) if string.is::() => { + let mut s = string.write_lock::().unwrap(); + // Replace the character at the specified index position let new_ch = new_val .as_char() @@ -216,7 +228,13 @@ impl Target<'_> { #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] impl<'a> From<&'a mut Dynamic> for Target<'a> { fn from(value: &'a mut Dynamic) -> Self { - Self::Ref(value) + if value.is_shared() { + // clone is cheap since it holds Arc/Rw under the hood + let container = value.clone(); + Self::LockGuard((value.write_lock::().unwrap(), container)) + } else { + Self::Ref(value) + } } } #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] @@ -676,15 +694,46 @@ impl Engine { } // xxx[rhs] = new_val _ if _new_val.is_some() => { - let mut new_val = _new_val.unwrap(); let mut idx_val2 = idx_val.clone(); + // `next` is introduced to bypass double mutable borrowing of target + #[cfg(not(feature = "no_index"))] + let mut next: Option<(u8, Dynamic)>; + match self.get_indexed_mut(state, lib, target, idx_val, pos, true, level) { // Indexed value is an owned value - the only possibility is an indexer // Try to call an index setter #[cfg(not(feature = "no_index"))] Ok(obj_ptr) if obj_ptr.is_value() => { - let args = &mut [target.as_mut(), &mut idx_val2, &mut new_val]; + next = Some((1, _new_val.unwrap())); + } + // Indexed value is a reference - update directly + Ok(ref mut obj_ptr) => { + obj_ptr + .set_value(_new_val.unwrap()) + .map_err(|err| err.new_position(rhs.position()))?; + + #[cfg(not(feature = "no_index"))] + { + next = None; + } + } + Err(err) => match *err { + // No index getter - try to call an index setter + #[cfg(not(feature = "no_index"))] + EvalAltResult::ErrorIndexingType(_, _) => { + next = Some((2, _new_val.unwrap())); + } + // Error + err => return Err(Box::new(err)), + }, + }; + + #[cfg(not(feature = "no_index"))] + match &mut next { + // next step is custom index setter call + Some((1, _new_val)) => { + let args = &mut [target.as_mut(), &mut idx_val2, _new_val]; self.exec_fn_call( state, lib, FN_IDX_SET, true, 0, args, is_ref, true, false, @@ -698,27 +747,20 @@ impl Engine { _ => Err(err), })?; } - // Indexed value is a reference - update directly - Ok(ref mut obj_ptr) => { - obj_ptr - .set_value(new_val) - .map_err(|err| err.new_position(rhs.position()))?; - } - Err(err) => match *err { - // No index getter - try to call an index setter - #[cfg(not(feature = "no_index"))] - EvalAltResult::ErrorIndexingType(_, _) => { - let args = &mut [target.as_mut(), &mut idx_val2, &mut new_val]; - self.exec_fn_call( - state, lib, FN_IDX_SET, true, 0, args, is_ref, true, false, - None, level, - )?; - } - // Error - err => return Err(Box::new(err)), - }, + // next step is custom index setter call in case of error + Some((2, _new_val)) => { + let args = &mut [target.as_mut(), &mut idx_val2, _new_val]; + + self.exec_fn_call( + state, lib, FN_IDX_SET, true, 0, args, is_ref, true, false, + None, level, + )?; + } + None => (), + _ => unreachable!() } + Ok(Default::default()) } // xxx[rhs] @@ -835,11 +877,10 @@ impl Engine { .map_err(|err| err.new_position(*pos))?; let val = &mut val; - let target = &mut val.into(); let (result, may_be_changed) = self .eval_dot_index_chain_helper( - state, lib, this_ptr, target, expr, idx_values, next_chain, + state, lib, this_ptr, &mut val.into(), expr, idx_values, next_chain, level, _new_val, ) .map_err(|err| err.new_position(*pos))?; @@ -1069,18 +1110,6 @@ impl Engine { let val = target.as_mut(); - // if val.is_shared() { - // return self.get_indexed_mut( - // state, - // lib, - // &mut Target::Value(val.read::().unwrap()), - // idx, - // idx_pos, - // create, - // level, - // ); - // } - match val { #[cfg(not(feature = "no_index"))] Dynamic(Union::Array(arr)) => { diff --git a/src/fn_call.rs b/src/fn_call.rs index 6ca46046..19f3fd33 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -699,7 +699,6 @@ impl Engine { } // Handle shared() - #[cfg(not(feature = "no_shared"))] if name == KEYWORD_SHARED && args_expr.len() == 1 { let expr = args_expr.get(0).unwrap(); let value = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?; diff --git a/src/fn_native.rs b/src/fn_native.rs index e51bf71b..54f3da08 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -18,7 +18,7 @@ use crate::stdlib::{boxed::Box, convert::TryFrom, fmt, iter::empty, string::Stri use crate::stdlib::mem; #[cfg(not(feature = "sync"))] -use crate::stdlib::{rc::Rc, cell::{RefCell, Ref, RefMut}}; +use crate::stdlib::{rc::Rc, cell::RefCell}; #[cfg(feature = "sync")] use crate::stdlib::sync::{Arc, RwLock}; diff --git a/src/fn_register.rs b/src/fn_register.rs index ddcddff3..eef4e66b 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -114,7 +114,7 @@ pub fn by_value(data: &mut Dynamic) -> T { ref_T.clone() } else if TypeId::of::() == TypeId::of::() { // If T is String, data must be ImmutableString, so map directly to it - *unsafe_cast_box(Box::new(data.as_str().unwrap().to_string())).unwrap() + *unsafe_cast_box(Box::new(data.clone().take_string().unwrap())).unwrap() } else { // We consume the argument and then replace it with () - the argument is not supposed to be used again. // This way, we avoid having to clone the argument again, because it is already a clone when passed here. diff --git a/src/parser.rs b/src/parser.rs index 3068bad0..2128f38c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -411,6 +411,11 @@ struct ParseState<'e> { /// Tracks a list of external variables (variables that are not explicitly declared in the scope). #[cfg(not(feature = "no_capture"))] externals: HashMap, + /// An indicator that prevents variables capturing into externals one time. + /// If set to true the next call of `access_var` will not capture the variable. + /// All consequent calls to `access_var` will not be affected + #[cfg(not(feature = "no_capture"))] + capture: bool, /// Encapsulates a local stack with variable names to simulate an actual runtime scope. modules: Vec, /// Maximum levels of expression nesting. @@ -436,6 +441,8 @@ impl<'e> ParseState<'e> { max_function_expr_depth, #[cfg(not(feature = "no_capture"))] externals: Default::default(), + #[cfg(not(feature = "no_capture"))] + capture: true, stack: Default::default(), modules: Default::default(), } @@ -458,8 +465,12 @@ impl<'e> ParseState<'e> { .and_then(|(i, _)| NonZeroUsize::new(i + 1)); #[cfg(not(feature = "no_capture"))] - if index.is_none() && !self.externals.contains_key(name) { - self.externals.insert(name.to_string(), pos); + if self.capture { + if index.is_none() && !self.externals.contains_key(name) { + self.externals.insert(name.to_string(), pos); + } + } else { + self.capture = true } index @@ -2171,9 +2182,18 @@ fn parse_binary_op( let (op_token, pos) = input.next().unwrap(); - let rhs = parse_unary(input, state, lib, settings)?; + let next = input.peek().unwrap(); + let next_precedence = next.0.precedence(custom); - let next_precedence = input.peek().unwrap().0.precedence(custom); + #[cfg(any(not(feature = "no_object"), not(feature = "no_capture")))] + if op_token == Token::Period { + if let (Token::Identifier(_), _) = next { + // prevents capturing of the object properties as vars: xxx. + state.capture = false; + } + } + + let rhs = parse_unary(input, state, lib, settings)?; // Bind to right if the next operator has higher precedence // If same precedence, then check if the operator binds right diff --git a/src/token.rs b/src/token.rs index bd496afb..71ec09a6 100644 --- a/src/token.rs +++ b/src/token.rs @@ -1430,13 +1430,20 @@ fn get_identifier( /// Is this keyword allowed as a function? #[inline(always)] pub fn is_keyword_function(name: &str) -> bool { - name == KEYWORD_PRINT + let mut result = name == KEYWORD_PRINT || name == KEYWORD_DEBUG || name == KEYWORD_TYPE_OF || name == KEYWORD_EVAL || name == KEYWORD_FN_PTR || name == KEYWORD_FN_PTR_CALL - || name == KEYWORD_FN_PTR_CURRY + || name == KEYWORD_FN_PTR_CURRY; + + #[cfg(not(feature = "no-shared"))] + { + result = result || name == KEYWORD_SHARED; + } + + result } pub fn is_valid_identifier(name: impl Iterator) -> bool { diff --git a/tests/closures.rs b/tests/closures.rs index 3aec4865..ae0c6535 100644 --- a/tests/closures.rs +++ b/tests/closures.rs @@ -1,6 +1,6 @@ #![cfg(not(feature = "no_function"))] -use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, INT}; -use std::any::TypeId; +use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, INT, Array}; +use std::any::{TypeId, Any}; #[test] fn test_fn_ptr_curry_call() -> Result<(), Box> { @@ -58,3 +58,150 @@ fn test_closures() -> Result<(), Box> { Ok(()) } + +#[test] +#[cfg(not(feature = "no_shared"))] +fn test_shared() -> Result<(), Box> { + let engine = Engine::new(); + + // assert_eq!( + // engine.eval::( + // r#" + // shared(42) + // "# + // )?, + // 42 + // ); + // + // assert_eq!( + // engine.eval::( + // r#" + // shared(true) + // "# + // )?, + // true + // ); + // + // #[cfg(not(feature = "no_float"))] + // assert_eq!( + // engine.eval::( + // r#" + // shared(4.2) + // "# + // )?, + // 4.2 + // ); + // + // assert_eq!( + // engine.eval::( + // r#" + // shared("test") + // "# + // )?, + // "test" + // ); + // + // #[cfg(not(feature = "no_index"))] + // { + // assert_eq!( + // engine.eval::( + // r#" + // let x = shared([1, 2, 3]); + // let y = shared([4, 5]); + // x + y + // "# + // )?.len(), + // 5 + // ); + // + // assert_eq!( + // engine.eval::( + // r" + // let x = shared([2, 9]); + // x.insert(-1, 1); + // x.insert(999, 3); + // + // let r = x.remove(2); + // + // let y = shared([4, 5]); + // x.append(y); + // + // x.len + r + // " + // )?, + // 14 + // ); + // + // assert_eq!( + // engine.eval::( + // r#" + // let x = shared([1, 2, 3]); + // + // if x[0] + x[2] == 4 { + // true + // } else { + // false + // } + // "# + // )?, + // true + // ); + // } + // + // #[cfg(not(feature = "no_object"))] + // assert_eq!( + // engine.eval::(r#" + // let y = shared(#{a: 1, b: 2, c: 3}); + // y.c = shared(5); + // y.c + // "#)?, + // 5 + // ); + // + // #[cfg(not(feature = "no_object"))] + // assert_eq!( + // engine.eval::(r#" + // let y = shared(#{a: 1, b: 2, c: shared(3)}); + // let c = y.c; + // c = 5;// "c" still holds Dynamic Shared + // y.c + // "#)?, + // 5 + // ); + // + // #[cfg(not(feature = "no_capture"))] + // assert_eq!( + // engine.eval::(r#" + // let x = shared(1); + // (|| x = x + 41).call(); + // x + // "#)?, + // 42 + // ); + + #[cfg(all(not(feature = "no_object"), not(feature = "no_capture")))] + assert_eq!( + engine.eval::(r#" + // let x = shared(#{a: 1, b: shared(2), c: 3}); + // let a = x.a; + // let b = x.b; + // a = 100; + // b = 20; + // + // let f = |a| { + // x.c = x.a + x.b;// + a; + // }; + // + // f.call(20); + // + // x.c + + let x = #{a: 1, b: 2}; + + x.a + x.b + "#)?, + 42 + ); + + Ok(()) +}