This commit is contained in:
Stephen Chung
2020-07-31 16:39:38 +08:00
parent 871fcb38be
commit e70e0ff4e2
5 changed files with 310 additions and 321 deletions

View File

@@ -578,7 +578,9 @@ impl Dynamic {
}
/// Convert the `Dynamic` value into specific type.
/// Casting to a `Dynamic` just returns as is.
///
/// Casting to a `Dynamic` just returns as is, but if it contains a shared value,
/// it is cloned into a `Dynamic` with a normal value.
///
/// Returns `None` if types mismatched.
///
@@ -602,6 +604,19 @@ impl Dynamic {
pub fn try_cast<T: Variant>(self) -> Option<T> {
let type_id = TypeId::of::<T>();
match self.0 {
#[cfg(not(feature = "no_shared"))]
#[cfg(not(feature = "sync"))]
Union::Shared(cell) => return cell.container.borrow().deref().clone().try_cast(),
#[cfg(not(feature = "no_shared"))]
#[cfg(feature = "sync")]
Union::Shared(cell) => {
return cell.container.read().unwrap().deref().clone().try_cast()
}
_ => (),
}
if type_id == TypeId::of::<Dynamic>() {
return unsafe_cast_box::<_, T>(Box::new(self)).ok().map(|v| *v);
}
@@ -681,23 +696,15 @@ impl Dynamic {
match self.0 {
Union::Variant(value) => (*value).as_box_any().downcast().map(|x| *x).ok(),
#[cfg(not(feature = "no_shared"))]
#[cfg(not(feature = "sync"))]
Union::Shared(cell) => return cell.container.borrow().deref().clone().try_cast(),
#[cfg(not(feature = "no_shared"))]
#[cfg(feature = "sync")]
Union::Shared(cell) => {
return cell.container.read().unwrap().deref().clone().try_cast()
}
Union::Shared(_) => unreachable!(),
_ => None,
}
}
/// Convert the `Dynamic` value into a specific type.
/// Casting to a `Dynamic` just returns as is.
///
/// Casting to a `Dynamic` just returns as is, but if it contains a shared value,
/// it is cloned into a `Dynamic` with a normal value.
///
/// Returns `None` if types mismatched.
///

View File

@@ -1328,12 +1328,15 @@ impl Engine {
// Normal assignment
ScopeEntryType::Normal if op.is_empty() => {
#[cfg(not(feature = "no_shared"))]
let lhs_ptr = if lhs_ptr.is_shared() {
lhs_ptr.write_lock::<Dynamic>().unwrap();
if lhs_ptr.is_shared() {
*lhs_ptr.write_lock::<Dynamic>().unwrap() = rhs_val;
} else {
lhs_ptr
};
*lhs_ptr = rhs_val;
*lhs_ptr = rhs_val;
}
#[cfg(feature = "no_shared")]
{
*lhs_ptr = rhs_val;
}
Ok(Default::default())
}
// Op-assignment - in order of precedence:
@@ -1351,14 +1354,17 @@ impl Engine {
.get_fn(hash_fn, false)
.or_else(|| self.packages.get_fn(hash_fn, false))
{
// Overriding exact implementation
let mut lock_guard;
#[cfg(not(feature = "no_shared"))]
let lhs_ptr = if lhs_ptr.is_shared() {
&mut lhs_ptr.write_lock::<Dynamic>().unwrap()
lock_guard = Some(lhs_ptr.write_lock::<Dynamic>().unwrap());
lock_guard.as_deref_mut().unwrap()
} else {
lhs_ptr
};
// Overriding exact implementation
func(self, lib, &mut [lhs_ptr, &mut rhs_val])?;
} else if run_builtin_op_assignment(op, lhs_ptr, &rhs_val)?.is_none() {
// Not built in, map to `var = var op rhs`
@@ -1375,13 +1381,15 @@ impl Engine {
.map_err(|err| err.new_position(*op_pos))?;
#[cfg(not(feature = "no_shared"))]
let lhs_ptr = if lhs_ptr.is_shared() {
lhs_ptr.write_lock::<Dynamic>().unwrap()
if lhs_ptr.is_shared() {
*lhs_ptr.write_lock::<Dynamic>().unwrap() = value;
} else {
lhs_ptr
};
*lhs_ptr = value;
*lhs_ptr = value;
}
#[cfg(feature = "no_shared")]
{
*lhs_ptr = value;
}
}
Ok(Default::default())
}