diff --git a/src/any.rs b/src/any.rs index d03ea744..afbfc956 100644 --- a/src/any.rs +++ b/src/any.rs @@ -789,13 +789,13 @@ impl Dynamic { self.try_cast::().unwrap() } - /// Dereference the `Dynamic`. + /// Flatten the `Dynamic` and clone it. /// /// If the `Dynamic` is not a shared value, it returns a cloned copy. /// /// If the `Dynamic` is a shared value, it a cloned copy of the shared value. #[inline(always)] - pub fn get_inner_clone(&self) -> Self { + pub fn flatten_clone(&self) -> Self { match &self.0 { #[cfg(not(feature = "no_closure"))] Union::Shared(cell) => { @@ -826,7 +826,7 @@ impl Dynamic { #[cfg(feature = "sync")] return shared_try_take(cell) - .map_or_else(|c| c.read().unwrap().clone(), RwLock::into_inner); + .map_or_else(|c| c.read().unwrap().clone(), |v| v.into_inner().unwrap()); } _ => self, } diff --git a/src/fn_call.rs b/src/fn_call.rs index 16fbbd9a..65e2b8cb 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -930,7 +930,7 @@ impl Engine { .map_err(|err| err.new_position(pos))?; args = if target.is_shared() { - arg_values.insert(0, target.get_inner_clone()); + arg_values.insert(0, target.flatten_clone()); arg_values.iter_mut().collect() } else { // Turn it into a method call only if the object is not shared diff --git a/src/fn_native.rs b/src/fn_native.rs index a98294de..4d2cf19b 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -53,7 +53,7 @@ pub fn shared_make_mut(value: &mut Shared) -> &mut T { } /// Consume a `Shared` resource if is unique (i.e. not shared). -pub fn shared_try_take(value: Shared) -> Result> { +pub fn shared_try_take(value: Shared) -> Result> { #[cfg(not(feature = "sync"))] return Rc::try_unwrap(value); #[cfg(feature = "sync")] @@ -65,7 +65,7 @@ pub fn shared_try_take(value: Shared) -> Result> { /// # Panics /// /// Panics if the resource is shared (i.e. has other outstanding references). -pub fn shared_take(value: Shared) -> T { +pub fn shared_take(value: Shared) -> T { shared_try_take(value).map_err(|_| ()).unwrap() } diff --git a/src/parser.rs b/src/parser.rs index 8b3bf3ea..cc0b91c8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1634,6 +1634,12 @@ fn parse_primary( Token::FloatConstant(x) => Expr::FloatConstant(Box::new(FloatWrapper(x, settings.pos))), Token::CharConstant(c) => Expr::CharConstant(Box::new((c, settings.pos))), Token::StringConstant(s) => Expr::StringConstant(Box::new((s.into(), settings.pos))), + + // Function call + Token::Identifier(s) if *next_token == Token::LeftParen || *next_token == Token::Bang => { + 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))) diff --git a/src/scope.rs b/src/scope.rs index afc55e72..1e489f0a 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -338,7 +338,7 @@ impl<'a> Scope<'a> { /// ``` pub fn get_value(&self, name: &str) -> Option { self.get_entry(name) - .and_then(|Entry { value, .. }| value.get_inner_clone().try_cast()) + .and_then(|Entry { value, .. }| value.flatten_clone().try_cast()) } /// Update the value of the named entry. @@ -441,7 +441,7 @@ impl<'a> Scope<'a> { /// ``` pub fn iter(&self) -> impl Iterator { self.iter_raw() - .map(|(name, value)| (name, value.get_inner_clone())) + .map(|(name, value)| (name, value.flatten_clone())) } /// Get an iterator to entries in the Scope. diff --git a/tests/closures.rs b/tests/closures.rs index 3fc1cbd5..d7684540 100644 --- a/tests/closures.rs +++ b/tests/closures.rs @@ -1,12 +1,13 @@ #![cfg(not(feature = "no_function"))] -use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, INT}; +use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, RegisterFn, INT}; use std::any::TypeId; #[test] fn test_fn_ptr_curry_call() -> Result<(), Box> { - let mut module = Module::new(); + let mut engine = Engine::new(); - module.set_raw_fn( + #[allow(deprecated)] + engine.register_raw_fn( "call_with_arg", &[TypeId::of::(), TypeId::of::()], |engine: &Engine, lib: &Module, args: &mut [&mut Dynamic]| { @@ -15,9 +16,6 @@ fn test_fn_ptr_curry_call() -> Result<(), Box> { }, ); - let mut engine = Engine::new(); - engine.load_package(module); - #[cfg(not(feature = "no_object"))] assert_eq!( engine.eval::( @@ -38,7 +36,7 @@ fn test_fn_ptr_curry_call() -> Result<(), Box> { #[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_object"))] fn test_closures() -> Result<(), Box> { - let engine = Engine::new(); + let mut engine = Engine::new(); assert_eq!( engine.eval::( @@ -77,6 +75,19 @@ fn test_closures() -> Result<(), Box> { "# )?); + engine.register_fn("plus_one", |x: INT| x + 1); + + assert_eq!( + engine.eval::( + r#" + let a = 41; + let f = || plus_one(a); + f.call() + "# + )?, + 42 + ); + Ok(()) }