Minor refactor.
This commit is contained in:
parent
d60f14ec27
commit
1d1e473ac4
@ -124,10 +124,9 @@ impl AST {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
pub fn source(&self) -> Option<&str> {
|
||||
if self.source.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(&self.source)
|
||||
match self.source.as_str() {
|
||||
"" => None,
|
||||
s => Some(s),
|
||||
}
|
||||
}
|
||||
/// Get a reference to the source.
|
||||
|
@ -890,10 +890,9 @@ impl<'x, 'px, 'pt> EvalContext<'_, 'x, 'px, '_, '_, '_, '_, 'pt> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
pub fn source(&self) -> Option<&str> {
|
||||
if self.global.source.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(&self.global.source)
|
||||
match self.global.source.as_str() {
|
||||
"" => None,
|
||||
s => Some(s),
|
||||
}
|
||||
}
|
||||
/// The current [`Scope`].
|
||||
@ -3241,10 +3240,9 @@ impl Engine {
|
||||
{
|
||||
use crate::ModuleResolver;
|
||||
|
||||
let source = if global.source.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(global.source.as_str())
|
||||
let source = match global.source.as_str() {
|
||||
"" => None,
|
||||
s => Some(s),
|
||||
};
|
||||
let path_pos = expr.position();
|
||||
|
||||
|
@ -155,7 +155,7 @@ impl Engine {
|
||||
) -> String {
|
||||
format!(
|
||||
"{}{}{} ({})",
|
||||
namespace.map_or(String::new(), |ns| ns.to_string()),
|
||||
namespace.map_or_else(|| String::new(), |ns| ns.to_string()),
|
||||
if namespace.is_some() {
|
||||
Token::DoubleColon.literal_syntax()
|
||||
} else {
|
||||
@ -372,14 +372,9 @@ impl Engine {
|
||||
}
|
||||
|
||||
// Run external function
|
||||
let source = if source.is_empty() {
|
||||
if parent_source.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(parent_source.as_str())
|
||||
}
|
||||
} else {
|
||||
Some(source.as_str())
|
||||
let source = match (source.as_str(), parent_source.as_str()) {
|
||||
("", "") => None,
|
||||
("", s) | (s, _) => Some(s),
|
||||
};
|
||||
|
||||
let context = (self, name, source, &*global, lib, pos).into();
|
||||
@ -424,10 +419,9 @@ impl Engine {
|
||||
pos,
|
||||
)
|
||||
})?;
|
||||
let source = if global.source.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(global.source.as_str())
|
||||
let source = match global.source.as_str() {
|
||||
"" => None,
|
||||
s => Some(s),
|
||||
};
|
||||
(debug(&text, source, pos).into(), false)
|
||||
} else {
|
||||
|
@ -50,7 +50,7 @@ impl fmt::Display for CallableFunction {
|
||||
Self::Plugin(_) => write!(f, "PluginFunction"),
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
CallableFunction::Script(s) => fmt::Display::fmt(s, f),
|
||||
Self::Script(s) => fmt::Display::fmt(s, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ use std::prelude::v1::*;
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
use num_traits::Float;
|
||||
|
||||
#[inline(never)]
|
||||
#[inline]
|
||||
pub fn make_err(msg: impl Into<String>) -> RhaiError {
|
||||
ERR::ErrorArithmetic(msg.into(), Position::NONE).into()
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ fn collect_fn_metadata(ctx: NativeCallContext) -> crate::Array {
|
||||
fn make_metadata(
|
||||
dict: &BTreeSet<Identifier>,
|
||||
namespace: Option<Identifier>,
|
||||
f: &ScriptFnDef,
|
||||
func: &ScriptFnDef,
|
||||
) -> Map {
|
||||
const DICT: &str = "key exists";
|
||||
|
||||
@ -53,10 +53,13 @@ fn collect_fn_metadata(ctx: NativeCallContext) -> crate::Array {
|
||||
if let Some(ns) = namespace {
|
||||
map.insert(dict.get("namespace").expect(DICT).clone(), ns.into());
|
||||
}
|
||||
map.insert(dict.get("name").expect(DICT).clone(), f.name.clone().into());
|
||||
map.insert(
|
||||
dict.get("name").expect(DICT).clone(),
|
||||
func.name.clone().into(),
|
||||
);
|
||||
map.insert(
|
||||
dict.get("access").expect(DICT).clone(),
|
||||
match f.access {
|
||||
match func.access {
|
||||
FnAccess::Public => dict.get("public").expect(DICT).clone(),
|
||||
FnAccess::Private => dict.get("private").expect(DICT).clone(),
|
||||
}
|
||||
@ -64,11 +67,11 @@ fn collect_fn_metadata(ctx: NativeCallContext) -> crate::Array {
|
||||
);
|
||||
map.insert(
|
||||
dict.get("is_anonymous").expect(DICT).clone(),
|
||||
f.name.starts_with(crate::engine::FN_ANONYMOUS).into(),
|
||||
func.name.starts_with(crate::engine::FN_ANONYMOUS).into(),
|
||||
);
|
||||
map.insert(
|
||||
dict.get("params").expect(DICT).clone(),
|
||||
f.params
|
||||
func.params
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(Into::into)
|
||||
|
@ -18,13 +18,6 @@ use rust_decimal::Decimal;
|
||||
#[cfg(feature = "decimal")]
|
||||
use super::arithmetic::make_err;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(feature = "only_i32")]
|
||||
pub const MAX_INT: INT = i32::MAX;
|
||||
#[allow(dead_code)]
|
||||
#[cfg(not(feature = "only_i32"))]
|
||||
pub const MAX_INT: INT = i64::MAX;
|
||||
|
||||
macro_rules! gen_conversion_as_functions {
|
||||
($root:ident => $func_name:ident ( $($arg_type:ident),+ ) -> $result_type:ty) => {
|
||||
pub mod $root { $(pub mod $arg_type {
|
||||
@ -264,7 +257,7 @@ mod float_functions {
|
||||
}
|
||||
#[rhai_fn(name = "to_int", return_raw)]
|
||||
pub fn f32_to_int(x: f32) -> RhaiResultOf<INT> {
|
||||
if cfg!(not(feature = "unchecked")) && x > (MAX_INT as f32) {
|
||||
if cfg!(not(feature = "unchecked")) && x > (INT::MAX as f32) {
|
||||
Err(
|
||||
ERR::ErrorArithmetic(format!("Integer overflow: to_int({})", x), Position::NONE)
|
||||
.into(),
|
||||
@ -275,7 +268,7 @@ mod float_functions {
|
||||
}
|
||||
#[rhai_fn(name = "to_int", return_raw)]
|
||||
pub fn f64_to_int(x: f64) -> RhaiResultOf<INT> {
|
||||
if cfg!(not(feature = "unchecked")) && x > (MAX_INT as f64) {
|
||||
if cfg!(not(feature = "unchecked")) && x > (INT::MAX as f64) {
|
||||
Err(
|
||||
ERR::ErrorArithmetic(format!("Integer overflow: to_int({})", x), Position::NONE)
|
||||
.into(),
|
||||
|
@ -1,6 +1,6 @@
|
||||
#![cfg(not(feature = "no_std"))]
|
||||
|
||||
use super::{arithmetic::make_err as make_arithmetic_err, math_basic::MAX_INT};
|
||||
use super::arithmetic::make_err as make_arithmetic_err;
|
||||
use crate::plugin::*;
|
||||
use crate::{def_package, Dynamic, EvalAltResult, RhaiResult, RhaiResultOf, INT};
|
||||
|
||||
@ -42,7 +42,7 @@ mod time_functions {
|
||||
{
|
||||
let seconds = timestamp.elapsed().as_secs();
|
||||
|
||||
if cfg!(not(feature = "unchecked")) && seconds > (MAX_INT as u64) {
|
||||
if cfg!(not(feature = "unchecked")) && seconds > (INT::MAX as u64) {
|
||||
Err(make_arithmetic_err(format!(
|
||||
"Integer overflow for timestamp.elapsed: {}",
|
||||
seconds
|
||||
@ -69,7 +69,7 @@ mod time_functions {
|
||||
if timestamp2 > timestamp1 {
|
||||
let seconds = (timestamp2 - timestamp1).as_secs();
|
||||
|
||||
if cfg!(not(feature = "unchecked")) && seconds > (MAX_INT as u64) {
|
||||
if cfg!(not(feature = "unchecked")) && seconds > (INT::MAX as u64) {
|
||||
Err(make_arithmetic_err(format!(
|
||||
"Integer overflow for timestamp duration: -{}",
|
||||
seconds
|
||||
@ -80,7 +80,7 @@ mod time_functions {
|
||||
} else {
|
||||
let seconds = (timestamp1 - timestamp2).as_secs();
|
||||
|
||||
if cfg!(not(feature = "unchecked")) && seconds > (MAX_INT as u64) {
|
||||
if cfg!(not(feature = "unchecked")) && seconds > (INT::MAX as u64) {
|
||||
Err(make_arithmetic_err(format!(
|
||||
"Integer overflow for timestamp duration: {}",
|
||||
seconds
|
||||
@ -97,7 +97,7 @@ mod time_functions {
|
||||
if seconds < 0.0 {
|
||||
subtract_impl(timestamp, -seconds)
|
||||
} else if cfg!(not(feature = "unchecked")) {
|
||||
if seconds > (MAX_INT as FLOAT) {
|
||||
if seconds > (INT::MAX as FLOAT) {
|
||||
Err(make_arithmetic_err(format!(
|
||||
"Integer overflow for timestamp add: {}",
|
||||
seconds
|
||||
@ -120,7 +120,7 @@ mod time_functions {
|
||||
if seconds < 0.0 {
|
||||
add_impl(timestamp, -seconds)
|
||||
} else if cfg!(not(feature = "unchecked")) {
|
||||
if seconds > (MAX_INT as FLOAT) {
|
||||
if seconds > (INT::MAX as FLOAT) {
|
||||
Err(make_arithmetic_err(format!(
|
||||
"Integer overflow for timestamp add: {}",
|
||||
seconds
|
||||
|
Loading…
Reference in New Issue
Block a user