Merge branch 'master' into plugins

This commit is contained in:
Stephen Chung
2020-08-08 23:01:48 +08:00
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.