diff --git a/src/ast.rs b/src/ast.rs index d7b8a116..fdd7d0c9 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1,6 +1,5 @@ //! Module defining the AST (abstract syntax tree). -use crate::dynamic::{AccessMode, Union}; use crate::fn_native::shared_make_mut; use crate::module::NamespaceRef; use crate::token::Token; @@ -1761,10 +1760,7 @@ impl Expr { Self::FloatConstant(x, _) => (*x).into(), Self::CharConstant(x, _) => (*x).into(), Self::StringConstant(x, _) => x.clone().into(), - Self::FnPointer(x, _) => Dynamic(Union::FnPtr( - Box::new(FnPtr::new_unchecked(x.clone(), Default::default())), - AccessMode::ReadOnly, - )), + Self::FnPointer(x, _) => FnPtr::new_unchecked(x.clone(), Default::default()).into(), Self::BoolConstant(x, _) => (*x).into(), Self::Unit(_) => Dynamic::UNIT, @@ -1772,7 +1768,7 @@ impl Expr { Self::Array(x, _) if self.is_constant() => { let mut arr = Array::with_capacity(x.len()); arr.extend(x.iter().map(|v| v.get_constant_value().unwrap())); - arr.into() + Dynamic::from_array(arr) } #[cfg(not(feature = "no_object"))] @@ -1781,7 +1777,7 @@ impl Expr { x.0.iter().for_each(|(k, v)| { *map.get_mut(k.name.as_str()).unwrap() = v.get_constant_value().unwrap() }); - map.into() + Dynamic::from_map(map) } _ => return None, diff --git a/src/dynamic.rs b/src/dynamic.rs index 5b29bb0e..c17db24e 100644 --- a/src/dynamic.rs +++ b/src/dynamic.rs @@ -1721,6 +1721,14 @@ impl From<&crate::Identifier> for Dynamic { } } #[cfg(not(feature = "no_index"))] +impl Dynamic { + /// Create a [`Dynamc`] from an [`Array`]. + #[inline(always)] + pub(crate) fn from_array(array: Array) -> Self { + Self(Union::Array(Box::new(array), AccessMode::ReadWrite)) + } +} +#[cfg(not(feature = "no_index"))] impl From> for Dynamic { #[inline(always)] fn from(value: Vec) -> Self { @@ -1751,6 +1759,14 @@ impl std::iter::FromIterator for Dynamic { } } #[cfg(not(feature = "no_object"))] +impl Dynamic { + /// Create a [`Dynamc`] from a [`Map`]. + #[inline(always)] + pub(crate) fn from_map(map: Map) -> Self { + Self(Union::Map(Box::new(map), AccessMode::ReadWrite)) + } +} +#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_std"))] impl, T: Variant + Clone> From> for Dynamic