diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index 10d33f8e..b9553cd8 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -197,17 +197,17 @@ macro_rules! gen_arithmetic_functions { } #[rhai_fn(name = "&")] #[inline(always)] - fn binary_and(x: $arg_type, y: $arg_type) -> $arg_type { + pub fn binary_and(x: $arg_type, y: $arg_type) -> $arg_type { x & y } #[rhai_fn(name = "|")] #[inline(always)] - fn binary_or(x: $arg_type, y: $arg_type) -> $arg_type { + pub fn binary_or(x: $arg_type, y: $arg_type) -> $arg_type { x | y } #[rhai_fn(name = "^")] #[inline(always)] - fn binary_xor(x: $arg_type, y: $arg_type) -> $arg_type { + pub fn binary_xor(x: $arg_type, y: $arg_type) -> $arg_type { x ^ y } } @@ -265,9 +265,9 @@ macro_rules! gen_signed_functions { } macro_rules! reg_functions { - ($mod_name:ident += $root:ident ; $($arg_type:ident),+ ) => { - $($mod_name.combine_flatten(exported_module!($root::$arg_type::functions));)* - } + ($mod_name:ident += $root:ident ; $($arg_type:ident),+ ) => { $( + $mod_name.combine_flatten(exported_module!($root::$arg_type::functions)); + )* } } def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { @@ -288,7 +288,10 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { // Basic arithmetic for floating-point #[cfg(not(feature = "no_float"))] - lib.combine_flatten(exported_module!(float_functions)); + { + lib.combine_flatten(exported_module!(f32_functions)); + lib.combine_flatten(exported_module!(f64_functions)); + } }); gen_arithmetic_functions!(arith_basic => INT); @@ -315,7 +318,7 @@ gen_signed_functions!(signed_num_128 => i128); #[cfg(not(feature = "no_float"))] #[export_module] -mod float_functions { +mod f32_functions { #[rhai_fn(name = "+")] #[inline(always)] pub fn add(x: f32, y: f32) -> f32 { @@ -343,38 +346,15 @@ mod float_functions { } #[rhai_fn(name = "-")] #[inline(always)] - pub fn neg_f32(x: f32) -> f32 { + pub fn neg(x: f32) -> f32 { -x } - #[rhai_fn(name = "-")] #[inline(always)] - pub fn neg_f64(x: f64) -> f64 { - -x - } - #[rhai_fn(name = "abs")] - #[inline(always)] - pub fn abs_f32(x: f32) -> f32 { + pub fn abs(x: f32) -> f32 { x.abs() } - #[rhai_fn(name = "abs")] - #[inline(always)] - pub fn abs_f64(x: f64) -> f64 { - x.abs() - } - #[rhai_fn(name = "sign")] #[inline] - pub fn sign_f32(x: f32) -> INT { - if x == 0.0 { - 0 - } else if x < 0.0 { - -1 - } else { - 1 - } - } - #[rhai_fn(name = "sign")] - #[inline] - pub fn sign_f64(x: f64) -> INT { + pub fn sign(x: f32) -> INT { if x == 0.0 { 0 } else if x < 0.0 { @@ -385,8 +365,45 @@ mod float_functions { } #[rhai_fn(name = "~", return_raw)] #[inline(always)] - pub fn pow_f_f(x: FLOAT, y: FLOAT) -> Result> { - Ok(x.powf(y).into()) + pub fn pow_f_f(x: f32, y: f32) -> Result> { + Ok(Dynamic::from(x.powf(y))) + } + #[rhai_fn(name = "~", return_raw)] + #[inline] + pub fn pow_f_i(x: f32, y: INT) -> Result> { + if cfg!(not(feature = "unchecked")) && y > (i32::MAX as INT) { + EvalAltResult::ErrorArithmetic( + format!("Number raised to too large an index: {} ~ {}", x, y), + Position::none(), + ) + .into() + } else { + Ok(Dynamic::from(x.powi(y as i32))) + } + } +} + +#[cfg(not(feature = "no_float"))] +#[export_module] +mod f64_functions { + #[rhai_fn(name = "-")] + #[inline(always)] + pub fn neg(x: f64) -> f64 { + -x + } + #[inline(always)] + pub fn abs(x: f64) -> f64 { + x.abs() + } + #[inline] + pub fn sign(x: f64) -> INT { + if x == 0.0 { + 0 + } else if x < 0.0 { + -1 + } else { + 1 + } } #[rhai_fn(name = "~", return_raw)] #[inline] diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 5b117731..a69372ae 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -11,6 +11,9 @@ use crate::plugin::*; #[cfg(not(feature = "unchecked"))] use crate::{result::EvalAltResult, token::Position}; +#[cfg(not(feature = "no_object"))] +use crate::engine::Map; + use crate::stdlib::{any::TypeId, boxed::Box}; #[cfg(not(feature = "unchecked"))] @@ -41,47 +44,35 @@ macro_rules! gen_array_functions { } macro_rules! reg_functions { - ($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { - $(set_exported_fn!($mod_name, "push", $root::$arg_type::push_func);)* - $(set_exported_fn!($mod_name, "insert", $root::$arg_type::insert_func);)* - } -} + ($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $( + set_exported_fn!($mod_name, "push", $root::$arg_type::push_func); + set_exported_fn!($mod_name, "insert", $root::$arg_type::insert_func); -macro_rules! reg_pad { - ($lib:expr, $($par:ty),*) => { - $({ - $lib.set_raw_fn("pad", - &[TypeId::of::(), TypeId::of::(), TypeId::of::<$par>()], - pad::<$par> - ); - })* - }; + $mod_name.set_raw_fn("pad", + &[TypeId::of::(), TypeId::of::(), TypeId::of::<$arg_type>()], + pad::<$arg_type>); + )* } } def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { lib.combine_flatten(exported_module!(array_functions)); reg_functions!(lib += basic; INT, bool, char, ImmutableString, FnPtr, Array, Unit); - reg_pad!(lib, INT, bool, char, ImmutableString, FnPtr, Array, Unit); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] { reg_functions!(lib += numbers; i8, u8, i16, u16, i32, i64, u32, u64); - reg_pad!(lib, u8, i16, u16, i32, u32, i64, u64); #[cfg(not(target_arch = "wasm32"))] - { - reg_functions!(lib += num_128; i128, u128); - reg_pad!(lib, i128, u128); - } + reg_functions!(lib += num_128; i128, u128); } #[cfg(not(feature = "no_float"))] - { - reg_functions!(lib += float; f32, f64); - reg_pad!(lib, f32, f64); - } + reg_functions!(lib += float; f32, f64); + + #[cfg(not(feature = "no_object"))] + reg_functions!(lib += map; Map); // Register array iterator lib.set_iter( @@ -207,3 +198,6 @@ gen_array_functions!(num_128 => i128, u128); #[cfg(not(feature = "no_float"))] gen_array_functions!(float => f32, f64); + +#[cfg(not(feature = "no_object"))] +gen_array_functions!(map => Map); diff --git a/src/packages/fn_basic.rs b/src/packages/fn_basic.rs index 274b85e1..bb3835d7 100644 --- a/src/packages/fn_basic.rs +++ b/src/packages/fn_basic.rs @@ -9,13 +9,13 @@ def_package!(crate:BasicFnPackage:"Basic Fn functions.", lib, { #[export_module] mod fn_ptr_functions { #[inline(always)] - fn name(f: &mut FnPtr) -> ImmutableString { + pub fn name(f: &mut FnPtr) -> ImmutableString { f.get_fn_name().clone() } #[rhai_fn(get = "name")] #[inline(always)] - fn name_prop(f: &mut FnPtr) -> ImmutableString { + pub fn name_prop(f: &mut FnPtr) -> ImmutableString { name(f) } } diff --git a/src/packages/logic.rs b/src/packages/logic.rs index 0c994dca..e05d8515 100644 --- a/src/packages/logic.rs +++ b/src/packages/logic.rs @@ -46,9 +46,9 @@ macro_rules! gen_cmp_functions { } macro_rules! reg_functions { - ($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { - $($mod_name.combine_flatten(exported_module!($root::$arg_type::functions));)* - } + ($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $( + $mod_name.combine_flatten(exported_module!($root::$arg_type::functions)); + )* } } def_package!(crate:LogicPackage:"Logical operators.", lib, { diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index 3f557124..f1de6c4f 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -41,9 +41,9 @@ macro_rules! gen_conversion_functions { } macro_rules! reg_functions { - ($mod_name:ident += $root:ident :: $func_name:ident ( $($arg_type:ident),+ ) ) => { - $(set_exported_fn!($mod_name, stringify!($func_name), $root::$arg_type::$func_name);)* - } + ($mod_name:ident += $root:ident :: $func_name:ident ( $($arg_type:ident),+ ) ) => { $( + set_exported_fn!($mod_name, stringify!($func_name), $root::$arg_type::$func_name); + )* } } def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { diff --git a/src/packages/string_basic.rs b/src/packages/string_basic.rs index 827ba183..ae5a84e0 100644 --- a/src/packages/string_basic.rs +++ b/src/packages/string_basic.rs @@ -37,16 +37,16 @@ macro_rules! gen_functions { } macro_rules! reg_print_functions { - ($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { - $(set_exported_fn!($mod_name, FN_TO_STRING, $root::$arg_type::to_string_func);)* - $(set_exported_fn!($mod_name, KEYWORD_PRINT, $root::$arg_type::to_string_func);)* - } + ($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $( + set_exported_fn!($mod_name, FN_TO_STRING, $root::$arg_type::to_string_func); + set_exported_fn!($mod_name, KEYWORD_PRINT, $root::$arg_type::to_string_func); + )* } } macro_rules! reg_debug_functions { - ($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { - $(set_exported_fn!($mod_name, KEYWORD_DEBUG, $root::$arg_type::to_string_func);)* - } + ($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $( + set_exported_fn!($mod_name, KEYWORD_DEBUG, $root::$arg_type::to_string_func); + )* } } def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, { diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 84126d86..883d4fec 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -38,10 +38,10 @@ macro_rules! gen_concat_functions { } macro_rules! reg_functions { - ($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { - $(set_exported_fn!($mod_name, "+", $root::$arg_type::append_func);)* - $(set_exported_fn!($mod_name, "+", $root::$arg_type::prepend_func);)* - } + ($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $( + set_exported_fn!($mod_name, "+", $root::$arg_type::append_func); + set_exported_fn!($mod_name, "+", $root::$arg_type::prepend_func); + )* } } def_package!(crate:MoreStringPackage:"Additional string utilities, including string building.", lib, { diff --git a/src/packages/time_basic.rs b/src/packages/time_basic.rs index d1401288..5b8e1f21 100644 --- a/src/packages/time_basic.rs +++ b/src/packages/time_basic.rs @@ -66,7 +66,7 @@ mod time_functions { } #[rhai_fn(return_raw, name = "-")] - fn time_diff(ts1: Instant, ts2: Instant) -> Result> { + pub fn time_diff(ts1: Instant, ts2: Instant) -> Result> { #[cfg(not(feature = "no_float"))] { Ok(if ts2 > ts1 {