Minor refactor.

This commit is contained in:
Stephen Chung
2022-01-03 23:16:47 +08:00
parent d60f14ec27
commit 1d1e473ac4
8 changed files with 34 additions and 47 deletions

View File

@@ -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()
}

View File

@@ -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)

View File

@@ -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(),

View File

@@ -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