Fix errors.

This commit is contained in:
Stephen Chung 2022-02-06 23:02:59 +08:00
parent 1ccbda1050
commit becbfa8930
5 changed files with 49 additions and 33 deletions

View File

@ -6,12 +6,12 @@ use crate::parser::ParseResult;
use crate::tokenizer::{is_valid_identifier, Token}; use crate::tokenizer::{is_valid_identifier, Token};
use crate::types::dynamic::Variant; use crate::types::dynamic::Variant;
use crate::{ use crate::{
Engine, EvalContext, Identifier, ImmutableString, LexError, Position, RhaiResult, Shared, reify, Engine, EvalContext, Identifier, ImmutableString, LexError, Position, RhaiResult,
StaticVec, reify, Shared, StaticVec,
}; };
use std::ops::Deref;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::ops::Deref;
/// Collection of special markers for custom syntax definition. /// Collection of special markers for custom syntax definition.
pub mod markers { pub mod markers {
@ -94,18 +94,18 @@ impl Expression<'_> {
pub fn get_literal_value<T: Variant>(&self) -> Option<T> { pub fn get_literal_value<T: Variant>(&self) -> Option<T> {
// Coded this way in order to maximally leverage potentials for dead-code removal. // Coded this way in order to maximally leverage potentials for dead-code removal.
match self.0 { match self.0 {
Expr::IntegerConstant(x, _) => reify!(x, |x: T| Some(x), || None), Expr::IntegerConstant(x, _) => reify!(*x, |x: T| Some(x), || None),
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
Expr::FloatConstant(x, _) => reify!(x, |x: T| Some(x), || None), Expr::FloatConstant(x, _) => reify!(*x, |x: T| Some(x), || None),
Expr::CharConstant(x, _) => reify!(x, |x: T| Some(x), || None), Expr::CharConstant(x, _) => reify!(*x, |x: T| Some(x), || None),
Expr::StringConstant(x, _) => reify!(x.clone(), |x: T| Some(x), || None), Expr::StringConstant(x, _) => reify!(x.clone(), |x: T| Some(x), || None),
Expr::Variable(_, _, x) => { Expr::Variable(_, _, x) => {
let x = Into::<ImmutableString>::into(&x.2); let x: ImmutableString = x.2.clone().into();
reify!(x, |x: T| Some(x), || None) reify!(x, |x: T| Some(x), || None)
} }
Expr::BoolConstant(x, _) => reify!(x, |x: T| Some(x), || None), Expr::BoolConstant(x, _) => reify!(*x, |x: T| Some(x), || None),
Expr::Unit(_) => reify!((), |x: T| Some(x), || None), Expr::Unit(_) => reify!((), |x: T| Some(x), || None),
_ => None, _ => None,

View File

@ -7,7 +7,7 @@ use super::callable_function::CallableFunction;
use super::native::{FnAny, SendSync}; use super::native::{FnAny, SendSync};
use crate::tokenizer::Position; use crate::tokenizer::Position;
use crate::types::dynamic::{DynamicWriteLock, Variant}; use crate::types::dynamic::{DynamicWriteLock, Variant};
use crate::{Dynamic, NativeCallContext, RhaiResultOf, ERR, reify}; use crate::{reify, Dynamic, NativeCallContext, RhaiResultOf, ERR};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{any::TypeId, mem}; use std::{any::TypeId, mem};
@ -45,7 +45,8 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
// If T is `&str`, data must be `ImmutableString`, so map directly to it // If T is `&str`, data must be `ImmutableString`, so map directly to it
data.flatten_in_place(); data.flatten_in_place();
let ref_str = data.as_str_ref().expect("&str"); let ref_str = data.as_str_ref().expect("&str");
let ref_t = reify!(ref_str, |ref_t: &T| ref_t, || unreachable!()); //let ref_t = reify!(&ref_str, |ref_t: &T| ref_t, || unreachable!());
let ref_t = unsafe { mem::transmute::<_, &T>(&ref_str) };
ref_t.clone() ref_t.clone()
} else if TypeId::of::<T>() == TypeId::of::<String>() { } else if TypeId::of::<T>() == TypeId::of::<String>() {
// If T is `String`, data must be `ImmutableString`, so map directly to it // If T is `String`, data must be `ImmutableString`, so map directly to it

View File

@ -68,8 +68,6 @@ extern crate no_std_compat as std;
use std::prelude::v1::*; use std::prelude::v1::*;
// Internal modules // Internal modules
mod reify;
mod api; mod api;
mod ast; mod ast;
mod engine; mod engine;
@ -79,6 +77,7 @@ mod module;
mod optimizer; mod optimizer;
pub mod packages; pub mod packages;
mod parser; mod parser;
mod reify;
mod tests; mod tests;
mod tokenizer; mod tokenizer;
mod types; mod types;

View File

@ -1,13 +1,16 @@
/// Runs `$code` if `$old` is of type `$t`. /// Runs `$code` if `$old` is of type `$t`.
///
/// This macro is primarily used for type casting between known types.
#[macro_export] #[macro_export]
macro_rules! reify { macro_rules! reify {
($old:ident, |$new:ident : $t:ty| $code:expr, || $fallback:expr) => {{ ($old:ident, |$new:ident : $t:ty| $code:expr, || $fallback:expr) => {{
#[allow(unused_imports)] #[allow(unused_imports)]
use ::std::{any::{Any, TypeId}, mem::{ManuallyDrop, transmute_copy}}; use std::any::Any;
if TypeId::of::<$t>() == $old.type_id() {
if std::any::TypeId::of::<$t>() == $old.type_id() {
// SAFETY: This is safe because we check to make sure the two types are // SAFETY: This is safe because we check to make sure the two types are
// actually the same type. // actually the same type.
let $new: $t = unsafe { transmute_copy(&ManuallyDrop::new($old)) }; let $new: $t = unsafe { std::mem::transmute_copy(&std::mem::ManuallyDrop::new($old)) };
$code $code
} else { } else {
$fallback $fallback
@ -24,3 +27,20 @@ macro_rules! reify {
reify!($old, |$new: $t| $code, || ()) reify!($old, |$new: $t| $code, || ())
}; };
} }
#[macro_export]
macro_rules! reify_dynamic {
($old:ident, |$new:ident : $t:ty| $code:expr) => {{
#[allow(unused_imports)]
use ::std::{
any::{Any, TypeId},
mem::{transmute_copy, ManuallyDrop},
};
if TypeId::of::<$t>() == TypeId::of::<crate::Dynamic>() {
// SAFETY: This is safe because we check to make sure the two types are
// actually the same type.
let $new: $t = unsafe { transmute_copy(&ManuallyDrop::new($old)) };
$code
}
}};
}

View File

@ -1,7 +1,7 @@
//! Helper module which defines the [`Any`] trait to to allow dynamic value handling. //! Helper module which defines the [`Any`] trait to to allow dynamic value handling.
use crate::func::native::SendSync; use crate::func::native::SendSync;
use crate::{ExclusiveRange, FnPtr, ImmutableString, InclusiveRange, INT, reify}; use crate::{reify, reify_dynamic, ExclusiveRange, FnPtr, ImmutableString, InclusiveRange, INT};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{ use std::{
@ -1182,10 +1182,10 @@ impl Dynamic {
reify!(value, |v: bool| return v.into()); reify!(value, |v: bool| return v.into());
reify!(value, |v: char| return v.into()); reify!(value, |v: char| return v.into());
reify!(value, |v: ImmutableString| return v.into()); reify!(value, |v: ImmutableString| return v.into());
reify!(value, |v: String| return v.into());
reify!(value, |v: &str| return v.into()); reify!(value, |v: &str| return v.into());
reify!(value, |v: ()| return v.into()); reify!(value, |v: ()| return v.into());
reify!(value, |v: String| return v.into());
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
reify!(value, |v: crate::Array| return v.into()); reify!(value, |v: crate::Array| return v.into());
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
@ -1200,7 +1200,8 @@ impl Dynamic {
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
reify!(value, |v: Instant| return v.into()); reify!(value, |v: Instant| return v.into());
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
reify!(value, |v: crate::Shared<crate::Locked<Dynamic>>| return v.into()); reify!(value, |v: crate::Shared<crate::Locked<Dynamic>>| return v
.into());
Self(Union::Variant( Self(Union::Variant(
Box::new(Box::new(value)), Box::new(Box::new(value)),
@ -1269,20 +1270,20 @@ impl Dynamic {
return self.flatten().try_cast::<T>(); return self.flatten().try_cast::<T>();
} }
reify!(self, |v: T| return Some(v)); reify_dynamic!(self, |v: T| return Some(v));
match self.0 { match self.0 {
Union::Int(v, ..) => reify!(v, |v: T| Some(v), || None), Union::Int(v, ..) => reify!(v, |v: T| Some(v), || None),
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
Union::Float(v, ..) => reify!(v, |v: T| Some(v), || None), Union::Float(v, ..) => reify!(*v, |v: T| Some(v), || None),
#[cfg(feature = "decimal")] #[cfg(feature = "decimal")]
Union::Decimal(v, ..) => reify!(v, |v: T| Some(v), || None), Union::Decimal(v, ..) => reify!(*v, |v: T| Some(v), || None),
Union::Bool(v, ..) => reify!(v, |v: T| Some(v), || None), Union::Bool(v, ..) => reify!(v, |v: T| Some(v), || None),
Union::Str(v, ..) => { Union::Str(v, ..) => {
reify!(v, |v: T| Some(v), || { reify!(v, |v: T| Some(v), || {
reify!(v.to_string(), |v: T| Some(v), || None) reify!(v.to_string(), |v: T| Some(v), || None)
}) })
}, }
Union::Char(v, ..) => reify!(v, |v: T| Some(v), || None), Union::Char(v, ..) => reify!(v, |v: T| Some(v), || None),
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Union::Array(v, ..) => reify!(v, |v: Box<T>| Some(*v), || None), Union::Array(v, ..) => reify!(v, |v: Box<T>| Some(*v), || None),
@ -1293,7 +1294,7 @@ impl Dynamic {
Union::FnPtr(v, ..) => reify!(v, |v: Box<T>| Some(*v), || None), Union::FnPtr(v, ..) => reify!(v, |v: Box<T>| Some(*v), || None),
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
Union::TimeStamp(v, ..) => reify!(v, |v: Box<T>| Some(*v), || None), Union::TimeStamp(v, ..) => reify!(v, |v: Box<T>| Some(*v), || None),
Union::Unit(v, ..) => reify!(v, |v: T| Some(v), || None), Union::Unit(_, ..) => reify!((), |v: T| Some(v), || None),
Union::Variant(v, _, _) => (*v).as_boxed_any().downcast().ok().map(|x| *x), Union::Variant(v, _, _) => (*v).as_boxed_any().downcast().ok().map(|x| *x),
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
Union::Shared(_, _, _) => unreachable!("Union::Shared case should be already handled"), Union::Shared(_, _, _) => unreachable!("Union::Shared case should be already handled"),
@ -1335,13 +1336,8 @@ impl Dynamic {
#[cfg(feature = "no_closure")] #[cfg(feature = "no_closure")]
let self_type_name = self.type_name(); let self_type_name = self.type_name();
self.try_cast::<T>().unwrap_or_else(|| { self.try_cast::<T>()
panic!( .unwrap_or_else(|| panic!("cannot cast {} to {}", self_type_name, type_name::<T>()))
"cannot cast {} value and to {}",
self_type_name,
type_name::<T>()
)
})
} }
/// Clone the [`Dynamic`] value and convert it into a specific type. /// Clone the [`Dynamic`] value and convert it into a specific type.
/// ///