Merge branch 'master' into plugins

This commit is contained in:
Stephen Chung 2020-08-08 23:01:48 +08:00
commit 0e344ff3eb
6 changed files with 32 additions and 15 deletions

View File

@ -789,13 +789,13 @@ impl Dynamic {
self.try_cast::<T>().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,
}

View File

@ -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

View File

@ -53,7 +53,7 @@ pub fn shared_make_mut<T: Clone>(value: &mut Shared<T>) -> &mut T {
}
/// Consume a `Shared` resource if is unique (i.e. not shared).
pub fn shared_try_take<T: Clone>(value: Shared<T>) -> Result<T, Shared<T>> {
pub fn shared_try_take<T>(value: Shared<T>) -> Result<T, Shared<T>> {
#[cfg(not(feature = "sync"))]
return Rc::try_unwrap(value);
#[cfg(feature = "sync")]
@ -65,7 +65,7 @@ pub fn shared_try_take<T: Clone>(value: Shared<T>) -> Result<T, Shared<T>> {
/// # Panics
///
/// Panics if the resource is shared (i.e. has other outstanding references).
pub fn shared_take<T: Clone>(value: Shared<T>) -> T {
pub fn shared_take<T>(value: Shared<T>) -> T {
shared_try_take(value).map_err(|_| ()).unwrap()
}

View File

@ -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)))

View File

@ -338,7 +338,7 @@ impl<'a> Scope<'a> {
/// ```
pub fn get_value<T: Variant + Clone>(&self, name: &str) -> Option<T> {
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<Item = (&str, Dynamic)> {
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.

View File

@ -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<EvalAltResult>> {
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::<FnPtr>(), TypeId::of::<INT>()],
|engine: &Engine, lib: &Module, args: &mut [&mut Dynamic]| {
@ -15,9 +16,6 @@ fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
},
);
let mut engine = Engine::new();
engine.load_package(module);
#[cfg(not(feature = "no_object"))]
assert_eq!(
engine.eval::<INT>(
@ -38,7 +36,7 @@ fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
#[cfg(not(feature = "no_closure"))]
#[cfg(not(feature = "no_object"))]
fn test_closures() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
let mut engine = Engine::new();
assert_eq!(
engine.eval::<INT>(
@ -77,6 +75,19 @@ fn test_closures() -> Result<(), Box<EvalAltResult>> {
"#
)?);
engine.register_fn("plus_one", |x: INT| x + 1);
assert_eq!(
engine.eval::<INT>(
r#"
let a = 41;
let f = || plus_one(a);
f.call()
"#
)?,
42
);
Ok(())
}