Minor refactor.

This commit is contained in:
Stephen Chung 2022-01-14 21:49:38 +08:00
parent e2e0b8d083
commit ae77582028
4 changed files with 34 additions and 24 deletions

View File

@ -322,11 +322,10 @@ impl Engine {
if let Some(t) = table.get(&hash) { if let Some(t) = table.get(&hash) {
if let Some(ref c) = t.0 { if let Some(ref c) = t.0 {
if self if self
.eval_expr(scope, global, state, lib, this_ptr, &c, level) .eval_expr(scope, global, state, lib, this_ptr, &c, level)?
.and_then(|v| { .as_bool()
v.as_bool().map_err(|typ| { .map_err(|typ| {
self.make_type_mismatch_err::<bool>(typ, c.position()) self.make_type_mismatch_err::<bool>(typ, c.position())
})
})? })?
{ {
Some(&t.1) Some(&t.1)
@ -349,11 +348,10 @@ impl Engine {
{ {
if let Some(c) = condition { if let Some(c) = condition {
if !self if !self
.eval_expr(scope, global, state, lib, this_ptr, &c, level) .eval_expr(scope, global, state, lib, this_ptr, &c, level)?
.and_then(|v| { .as_bool()
v.as_bool().map_err(|typ| { .map_err(|typ| {
self.make_type_mismatch_err::<bool>(typ, c.position()) self.make_type_mismatch_err::<bool>(typ, c.position())
})
})? })?
{ {
continue; continue;

View File

@ -91,6 +91,7 @@ impl FuncInfo {
/// ///
/// `()` is cleared. /// `()` is cleared.
/// [`RhaiResult`][crate::RhaiResult] and [`RhaiResultOf<T>`] are expanded. /// [`RhaiResult`][crate::RhaiResult] and [`RhaiResultOf<T>`] are expanded.
#[cfg(feature = "metadata")]
pub fn format_return_type(typ: &str) -> std::borrow::Cow<str> { pub fn format_return_type(typ: &str) -> std::borrow::Cow<str> {
const RHAI_RESULT_TYPE: &str = "RhaiResult"; const RHAI_RESULT_TYPE: &str = "RhaiResult";
const RHAI_RESULT_TYPE_EXPAND: &str = "Result<Dynamic, Box<EvalAltResult>>"; const RHAI_RESULT_TYPE_EXPAND: &str = "Result<Dynamic, Box<EvalAltResult>>";

View File

@ -1,6 +1,6 @@
//! Module containing error definitions for the evaluation process. //! Module containing error definitions for the evaluation process.
use crate::{Dynamic, ImmutableString, ParseErrorType, Position, RhaiError, INT}; use crate::{Dynamic, ImmutableString, ParseErrorType, Position, INT};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use core_error::Error; use core_error::Error;
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
@ -34,22 +34,28 @@ pub enum EvalAltResult {
ErrorVariableNotFound(String, Position), ErrorVariableNotFound(String, Position),
/// Call to an unknown function. Wrapped value is the function signature. /// Call to an unknown function. Wrapped value is the function signature.
ErrorFunctionNotFound(String, Position), ErrorFunctionNotFound(String, Position),
/// An error has occurred inside a called function.
/// Wrapped values are the function name, function source, and the interior error.
ErrorInFunctionCall(String, String, RhaiError, Position),
/// Usage of an unknown [module][crate::Module]. Wrapped value is the [module][crate::Module] name. /// Usage of an unknown [module][crate::Module]. Wrapped value is the [module][crate::Module] name.
ErrorModuleNotFound(String, Position), ErrorModuleNotFound(String, Position),
/// An error has occurred inside a called function.
/// Wrapped values are the function name, function source, and the interior error.
ErrorInFunctionCall(String, String, Box<Self>, Position),
/// An error has occurred while loading a [module][crate::Module]. /// An error has occurred while loading a [module][crate::Module].
/// Wrapped value are the [module][crate::Module] name and the interior error. /// Wrapped value are the [module][crate::Module] name and the interior error.
ErrorInModule(String, RhaiError, Position), ErrorInModule(String, Box<Self>, Position),
/// Access to `this` that is not bound. /// Access to `this` that is not bound.
ErrorUnboundThis(Position), ErrorUnboundThis(Position),
/// Data is not of the required type. /// Data is not of the required type.
/// Wrapped values are the type requested and type of the actual result. /// Wrapped values are the type requested and type of the actual result.
ErrorMismatchDataType(String, String, Position), ErrorMismatchDataType(String, String, Position),
/// Returned type is not the same as the required output type. /// Returned type is not the same as the required output type.
/// Wrapped values are the type requested and type of the actual result. /// Wrapped values are the type requested and type of the actual result.
ErrorMismatchOutputType(String, String, Position), ErrorMismatchOutputType(String, String, Position),
/// Trying to index into a type that has no indexer function defined. Wrapped value is the type name.
ErrorIndexingType(String, Position),
/// Array access out-of-bounds. /// Array access out-of-bounds.
/// Wrapped values are the current number of elements in the array and the index number. /// Wrapped values are the current number of elements in the array and the index number.
ErrorArrayBounds(usize, INT, Position), ErrorArrayBounds(usize, INT, Position),
@ -59,10 +65,10 @@ pub enum EvalAltResult {
/// Bit-field indexing out-of-bounds. /// Bit-field indexing out-of-bounds.
/// Wrapped values are the current number of bits in the bit-field and the index number. /// Wrapped values are the current number of bits in the bit-field and the index number.
ErrorBitFieldBounds(usize, INT, Position), ErrorBitFieldBounds(usize, INT, Position),
/// Trying to index into a type that has no indexer function defined. Wrapped value is the type name.
ErrorIndexingType(String, Position), /// The `for` statement encounters a type that is not iterable.
/// The `for` statement encounters a type that is not an iterator.
ErrorFor(Position), ErrorFor(Position),
/// Data race detected when accessing a variable. Wrapped value is the variable name. /// Data race detected when accessing a variable. Wrapped value is the variable name.
ErrorDataRace(String, Position), ErrorDataRace(String, Position),
/// Assignment to a constant variable. Wrapped value is the variable name. /// Assignment to a constant variable. Wrapped value is the variable name.
@ -145,7 +151,7 @@ impl fmt::Display for EvalAltResult {
}?, }?,
Self::ErrorIndexingType(s, _) => write!(f, "Indexer not registered for {}", s)?, Self::ErrorIndexingType(s, _) => write!(f, "Indexer not registered for {}", s)?,
Self::ErrorUnboundThis(_) => f.write_str("'this' is not bound")?, Self::ErrorUnboundThis(_) => f.write_str("'this' is not bound")?,
Self::ErrorFor(_) => f.write_str("For loop expects a type with an iterator defined")?, Self::ErrorFor(_) => f.write_str("For loop expects a type that is iterable")?,
Self::ErrorTooManyOperations(_) => f.write_str("Too many operations")?, Self::ErrorTooManyOperations(_) => f.write_str("Too many operations")?,
Self::ErrorTooManyModules(_) => f.write_str("Too many modules imported")?, Self::ErrorTooManyModules(_) => f.write_str("Too many modules imported")?,
Self::ErrorStackOverflow(_) => f.write_str("Stack overflow")?, Self::ErrorStackOverflow(_) => f.write_str("Stack overflow")?,
@ -233,7 +239,7 @@ impl<T: AsRef<str>> From<T> for EvalAltResult {
} }
} }
impl<T: AsRef<str>> From<T> for RhaiError { impl<T: AsRef<str>> From<T> for Box<EvalAltResult> {
#[inline(never)] #[inline(never)]
fn from(err: T) -> Self { fn from(err: T) -> Self {
EvalAltResult::ErrorRuntime(err.as_ref().to_string().into(), Position::NONE).into() EvalAltResult::ErrorRuntime(err.as_ref().to_string().into(), Position::NONE).into()
@ -277,8 +283,13 @@ impl EvalAltResult {
| Self::ErrorArithmetic(_, _) | Self::ErrorArithmetic(_, _)
| Self::ErrorRuntime(_, _) => true, | Self::ErrorRuntime(_, _) => true,
Self::ErrorCustomSyntax(_, _, _) // Custom syntax raises errors only when they are compiled by one
| Self::ErrorTooManyOperations(_) // [`Engine`][crate::Engine] and run by another, causing a mismatch.
//
// Therefore, this error should not be catchable.
Self::ErrorCustomSyntax(_, _, _) => false,
Self::ErrorTooManyOperations(_)
| Self::ErrorTooManyModules(_) | Self::ErrorTooManyModules(_)
| Self::ErrorStackOverflow(_) | Self::ErrorStackOverflow(_)
| Self::ErrorDataTooLarge(_, _) | Self::ErrorDataTooLarge(_, _)

View File

@ -43,8 +43,8 @@ impl FnPtr {
/// Create a new function pointer without checking its parameters. /// Create a new function pointer without checking its parameters.
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub(crate) fn new_unchecked(name: Identifier, curry: StaticVec<Dynamic>) -> Self { pub(crate) fn new_unchecked(name: impl Into<Identifier>, curry: StaticVec<Dynamic>) -> Self {
Self(name, curry) Self(name.into(), curry)
} }
/// Get the name of the function. /// Get the name of the function.
#[inline(always)] #[inline(always)]