diff --git a/Cargo.toml b/Cargo.toml index 3bbd25ea..8fa20102 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,8 @@ unchecked = [] no_stdlib = [] no_index = [] no_float = [] +only_i32 = [] +only_i64 = [] [profile.release] lto = "fat" diff --git a/README.md b/README.md index 6f4d7ded..8d7724ea 100644 --- a/README.md +++ b/README.md @@ -38,13 +38,18 @@ Beware that in order to use pre-releases (alpha and beta) you need to specify th Optional features ----------------- -| Feature | Description | -| ------------ | ----------------------------------------------------------------------------------------------------------------------- | -| `debug_msgs` | Print debug messages to stdout (using `println!`) related to function registrations and function calls. | -| `no_stdlib` | Exclude the standard library of utility functions in the build, and only include the minimum necessary functionalities. | -| `unchecked` | Exclude arithmetic checking in the standard library. Beware that a bad script may panic the entire system! | -| `no_index` | Disable arrays and indexing features if you don't need them. | -| `no_float` | Disable floating-point numbers and math if you don't need them. | +| Feature | Description | +| ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `debug_msgs` | Print debug messages to stdout related to function registrations and calls. | +| `no_stdlib` | Exclude the standard library of utility functions in the build, and only include the minimum necessary functionalities. Standard types are not affected. | +| `unchecked` | Exclude arithmetic checking (such as overflows and division by zero). Beware that a bad script may panic the entire system! | +| `no_index` | Disable arrays and indexing features if you don't need them. | +| `no_float` | Disable floating-point numbers and math if you don't need them. | +| `only_i32` | Set the system integer type to `i32` and disable all other integer types. | +| `only_i64` | Set the system integer type to `i64` and disable all other integer types. | + +By default, Rhai includes all the standard functionalities in a small, tight package. Most features are here for you to opt-**out** of certain functionalities that you do not need. +Excluding unneeded functionalities can result in smaller, faster builds as well as less bugs due to a more restricted language. Related ------- @@ -190,20 +195,28 @@ Values and types The following primitive types are supported natively: -| Category | Types | -| --------------------------------------------------------------- | -------------------------------------- | -| Integer | `i32`, `u32`, `i64` _(default)_, `u64` | -| Floating-point (disabled with [`no_float`](#optional-features)) | `f32`, `f64` _(default)_ | -| Character | `char` | -| Boolean | `bool` | -| Array (disabled with [`no_index`](#optional-features)) | `rhai::Array` | -| Dynamic (i.e. can be anything) | `rhai::Dynamic` | +| Category | Types | +| ------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ | +| **Integer** | `u8`, `i8`, `u16`, `i16`,
`u32`, `i32` (default for [`only_i32`](#optional-features)),
`u64`, `i64` _(default)_ | +| **Floating-point** (disabled with [`no_float`](#optional-features)) | `f32`, `f64` _(default)_ | +| **Character** | `char` | +| **Boolean** | `bool` | +| **Array** (disabled with [`no_index`](#optional-features)) | `rhai::Array` | +| **Dynamic** (i.e. can be anything) | `rhai::Dynamic` | +| **System** (current configuration) | `rhai::INT` (`i32` or `i64`),
`rhai::FLOAT` (`f32` or `f64`) | + +All types are treated strictly separate by Rhai, meaning that `i32` and `i64` and `u32` are completely different; you cannot even add them together. + +The default integer type is `i64`. If you do not need any other integer type, you can enable the [`only_i64`](#optional-features) feature. + +If you only need 32-bit integers, you can enable the [`only_i32`](#optional-features) feature and remove support for all integer types other than `i32` including `i64`. +This is useful on 32-bit systems where using 64-bit integers incurs a performance penalty. + +If you do not need floating-point, enable the [`no_float`](#optional-features) feature to remove support. Value conversions ----------------- -All types are treated strictly separate by Rhai, meaning that `i32` and `i64` and `u32` are completely different; you cannot even add them together. - There is a `to_float` function to convert a supported number to an `f64`, and a `to_int` function to convert a supported number to `i64` and that's about it. For other conversions you can register your own conversion functions. There is also a `type_of` function to detect the type of a value. diff --git a/src/builtin.rs b/src/builtin.rs index 16a8acb1..0e8ab757 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -4,26 +4,38 @@ use crate::any::Any; use crate::engine::Engine; use crate::fn_register::RegisterFn; +use crate::parser::INT; + +#[cfg(not(feature = "unchecked"))] +use crate::{parser::Position, result::EvalAltResult, RegisterResultFn}; #[cfg(not(feature = "no_index"))] use crate::engine::Array; +#[cfg(not(feature = "no_float"))] +use crate::FLOAT; + use std::{ fmt::{Debug, Display}, - i32, i64, ops::{BitAnd, BitOr, BitXor, Range}, - u32, }; #[cfg(feature = "unchecked")] use std::ops::{Shl, Shr}; +#[cfg(not(feature = "unchecked"))] +#[cfg(not(feature = "no_float"))] +use std::{i32, i64}; + +#[cfg(not(feature = "unchecked"))] +#[cfg(not(feature = "only_i32"))] +use std::u32; + #[cfg(any(feature = "unchecked", not(feature = "no_float")))] use std::ops::{Add, Div, Mul, Neg, Rem, Sub}; #[cfg(not(feature = "unchecked"))] use { - crate::{parser::Position, result::EvalAltResult, RegisterResultFn}, num_traits::{ CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr, CheckedSub, @@ -259,7 +271,7 @@ impl Engine<'_> { x ^ y } #[cfg(not(feature = "unchecked"))] - fn shl(x: T, y: i64) -> Result { + fn shl(x: T, y: INT) -> Result { if y < 0 { return Err(EvalAltResult::ErrorArithmetic( format!("Left-shift by a negative number: {} << {}", x, y), @@ -275,7 +287,7 @@ impl Engine<'_> { }) } #[cfg(not(feature = "unchecked"))] - fn shr(x: T, y: i64) -> Result { + fn shr(x: T, y: INT) -> Result { if y < 0 { return Err(EvalAltResult::ErrorArithmetic( format!("Right-shift by a negative number: {} >> {}", x, y), @@ -312,33 +324,58 @@ impl Engine<'_> { x % y } #[cfg(not(feature = "unchecked"))] - fn pow_i64_i64_u(x: i64, y: i64) -> Result { - if y > (u32::MAX as i64) { - return Err(EvalAltResult::ErrorArithmetic( - format!("Power overflow: {} ~ {}", x, y), - Position::none(), - )); + fn pow_i_i_u(x: INT, y: INT) -> Result { + #[cfg(not(feature = "only_i32"))] + { + if y > (u32::MAX as INT) { + Err(EvalAltResult::ErrorArithmetic( + format!("Power overflow: {} ~ {}", x, y), + Position::none(), + )) + } else if y < 0 { + Err(EvalAltResult::ErrorArithmetic( + format!("Power underflow: {} ~ {}", x, y), + Position::none(), + )) + } else { + x.checked_pow(y as u32).ok_or_else(|| { + EvalAltResult::ErrorArithmetic( + format!("Power overflow: {} ~ {}", x, y), + Position::none(), + ) + }) + } } - x.checked_pow(y as u32).ok_or_else(|| { - EvalAltResult::ErrorArithmetic( - format!("Power overflow: {} ~ {}", x, y), - Position::none(), - ) - }) + #[cfg(feature = "only_i32")] + { + if y < 0 { + Err(EvalAltResult::ErrorArithmetic( + format!("Power underflow: {} ~ {}", x, y), + Position::none(), + )) + } else { + x.checked_pow(y as u32).ok_or_else(|| { + EvalAltResult::ErrorArithmetic( + format!("Power overflow: {} ~ {}", x, y), + Position::none(), + ) + }) + } + } } #[cfg(feature = "unchecked")] - fn pow_i64_i64(x: i64, y: i64) -> i64 { + fn pow_i_i(x: INT, y: INT) -> INT { x.pow(y as u32) } #[cfg(not(feature = "no_float"))] - fn pow_f64_f64(x: f64, y: f64) -> f64 { + fn pow_f_f(x: FLOAT, y: FLOAT) -> FLOAT { x.powf(y) } #[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "no_float"))] - fn pow_f64_i64_u(x: f64, y: i64) -> Result { - if y > (i32::MAX as i64) { + fn pow_f_i_u(x: FLOAT, y: INT) -> Result { + if y > (i32::MAX as INT) { return Err(EvalAltResult::ErrorArithmetic( format!("Power overflow: {} ~ {}", x, y), Position::none(), @@ -349,24 +386,42 @@ impl Engine<'_> { } #[cfg(feature = "unchecked")] #[cfg(not(feature = "no_float"))] - fn pow_f64_i64(x: f64, y: i64) -> f64 { + fn pow_f_i(x: FLOAT, y: INT) -> FLOAT { x.powi(y as i32) } #[cfg(not(feature = "unchecked"))] { - reg_op_result!(self, "+", add, i8, u8, i16, u16, i32, i64, u32, u64); - reg_op_result!(self, "-", sub, i8, u8, i16, u16, i32, i64, u32, u64); - reg_op_result!(self, "*", mul, i8, u8, i16, u16, i32, i64, u32, u64); - reg_op_result!(self, "/", div, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op_result!(self, "+", add, INT); + reg_op_result!(self, "-", sub, INT); + reg_op_result!(self, "*", mul, INT); + reg_op_result!(self, "/", div, INT); + + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_op_result!(self, "+", add, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op_result!(self, "-", sub, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op_result!(self, "*", mul, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op_result!(self, "/", div, i8, u8, i16, u16, i32, i64, u32, u64); + } } #[cfg(feature = "unchecked")] { - reg_op!(self, "+", add_u, i8, u8, i16, u16, i32, i64, u32, u64); - reg_op!(self, "-", sub_u, i8, u8, i16, u16, i32, i64, u32, u64); - reg_op!(self, "*", mul_u, i8, u8, i16, u16, i32, i64, u32, u64); - reg_op!(self, "/", div_u, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op!(self, "+", add_u, INT); + reg_op!(self, "-", sub_u, INT); + reg_op!(self, "*", mul_u, INT); + reg_op!(self, "/", div_u, INT); + + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_op!(self, "+", add_u, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op!(self, "-", sub_u, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op!(self, "*", mul_u, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op!(self, "/", div_u, i8, u8, i16, u16, i32, i64, u32, u64); + } } #[cfg(not(feature = "no_float"))] @@ -377,12 +432,23 @@ impl Engine<'_> { reg_op!(self, "/", div_u, f32, f64); } - reg_cmp!(self, "<", lt, i8, u8, i16, u16, i32, i64, u32, u64, String, char); - reg_cmp!(self, "<=", lte, i8, u8, i16, u16, i32, i64, u32, u64, String, char); - reg_cmp!(self, ">", gt, i8, u8, i16, u16, i32, i64, u32, u64, String, char); - reg_cmp!(self, ">=", gte, i8, u8, i16, u16, i32, i64, u32, u64, String, char); - reg_cmp!(self, "==", eq, i8, u8, i16, u16, i32, i64, u32, u64, bool, String, char); - reg_cmp!(self, "!=", ne, i8, u8, i16, u16, i32, i64, u32, u64, bool, String, char); + reg_cmp!(self, "<", lt, INT, String, char); + reg_cmp!(self, "<=", lte, INT, String, char); + reg_cmp!(self, ">", gt, INT, String, char); + reg_cmp!(self, ">=", gte, INT, String, char); + reg_cmp!(self, "==", eq, INT, String, char, bool); + reg_cmp!(self, "!=", ne, INT, String, char, bool); + + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_cmp!(self, "<", lt, i8, u8, i16, u16, i32, i64, u32, u64); + reg_cmp!(self, "<=", lte, i8, u8, i16, u16, i32, i64, u32, u64); + reg_cmp!(self, ">", gt, i8, u8, i16, u16, i32, i64, u32, u64); + reg_cmp!(self, ">=", gte, i8, u8, i16, u16, i32, i64, u32, u64); + reg_cmp!(self, "==", eq, i8, u8, i16, u16, i32, i64, u32, u64); + reg_cmp!(self, "!=", ne, i8, u8, i16, u16, i32, i64, u32, u64); + } #[cfg(not(feature = "no_float"))] { @@ -396,60 +462,97 @@ impl Engine<'_> { //reg_op!(self, "||", or, bool); //reg_op!(self, "&&", and, bool); - reg_op!(self, "|", binary_or, i8, u8, i16, u16, i32, i64, u32, u64); reg_op!(self, "|", or, bool); - reg_op!(self, "&", binary_and, i8, u8, i16, u16, i32, i64, u32, u64); reg_op!(self, "&", and, bool); - reg_op!(self, "^", binary_xor, i8, u8, i16, u16, i32, i64, u32, u64); + + reg_op!(self, "|", binary_or, INT); + reg_op!(self, "&", binary_and, INT); + reg_op!(self, "^", binary_xor, INT); + + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_op!(self, "|", binary_or, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op!(self, "&", binary_and, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op!(self, "^", binary_xor, i8, u8, i16, u16, i32, i64, u32, u64); + } #[cfg(not(feature = "unchecked"))] { - reg_op_result1!(self, "<<", shl, i64, i8, u8, i16, u16, i32, i64, u32, u64); - reg_op_result1!(self, ">>", shr, i64, i8, u8, i16, u16); - reg_op_result1!(self, ">>", shr, i64, i32, i64, u32, u64); - reg_op_result!(self, "%", modulo, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op_result1!(self, "<<", shl, INT, INT); + reg_op_result1!(self, ">>", shr, INT, INT); + reg_op_result!(self, "%", modulo, INT); + + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_op_result1!(self, "<<", shl, i64, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op_result1!(self, ">>", shr, i64, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op_result!(self, "%", modulo, i8, u8, i16, u16, i32, i64, u32, u64); + } } #[cfg(feature = "unchecked")] { - reg_op!(self, "<<", shl_u, i64, i8, u8, i16, u16, i32, i64, u32, u64); - reg_op!(self, ">>", shr_u, i64, i8, u8, i16, u16); - reg_op!(self, ">>", shr_u, i64, i32, i64, u32, u64); - reg_op!(self, "%", modulo_u, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op!(self, "<<", shl_u, INT, INT); + reg_op!(self, ">>", shr_u, INT, INT); + reg_op!(self, "%", modulo_u, INT); + + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_op!(self, "<<", shl_u, i64, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op!(self, ">>", shr_u, i64, i8, u8, i16, u16, i32, i64, u32, u64); + reg_op!(self, "%", modulo_u, i8, u8, i16, u16, i32, i64, u32, u64); + } } #[cfg(not(feature = "no_float"))] { reg_op!(self, "%", modulo_u, f32, f64); - self.register_fn("~", pow_f64_f64); + self.register_fn("~", pow_f_f); } #[cfg(not(feature = "unchecked"))] { - self.register_result_fn("~", pow_i64_i64_u); + self.register_result_fn("~", pow_i_i_u); #[cfg(not(feature = "no_float"))] - self.register_result_fn("~", pow_f64_i64_u); + self.register_result_fn("~", pow_f_i_u); } #[cfg(feature = "unchecked")] { - self.register_fn("~", pow_i64_i64); + self.register_fn("~", pow_i_i); #[cfg(not(feature = "no_float"))] - self.register_fn("~", pow_f64_i64); + self.register_fn("~", pow_f_i); } #[cfg(not(feature = "unchecked"))] { - reg_un_result!(self, "-", neg, i8, i16, i32, i64); - reg_un_result!(self, "abs", abs, i8, i16, i32, i64); + reg_un_result!(self, "-", neg, INT); + reg_un_result!(self, "abs", abs, INT); + + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_un_result!(self, "-", neg, i8, i16, i32, i64); + reg_un_result!(self, "abs", abs, i8, i16, i32, i64); + } } #[cfg(feature = "unchecked")] { - reg_un!(self, "-", neg_u, i8, i16, i32, i64); - reg_un!(self, "abs", abs_u, i8, i16, i32, i64); + reg_un!(self, "-", neg_u, INT); + reg_un!(self, "abs", abs_u, INT); + + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_un!(self, "-", neg_u, i8, i16, i32, i64); + reg_un!(self, "abs", abs_u, i8, i16, i32, i64); + } } #[cfg(not(feature = "no_float"))] @@ -464,33 +567,37 @@ impl Engine<'_> { self.register_fn("==", |_: (), _: ()| true); // () == () // Register print and debug - fn print_debug(x: T) -> String { + fn debug(x: T) -> String { format!("{:?}", x) } fn print(x: T) -> String { format!("{}", x) } - reg_func1!(self, "print", print, String, i8, u8, i16, u16); - reg_func1!(self, "print", print, String, i32, i64, u32, u64); - reg_func1!(self, "print", print, String, bool, char, String); + reg_func1!(self, "print", print, String, INT, bool, char, String); self.register_fn("print", || "".to_string()); self.register_fn("print", |_: ()| "".to_string()); + reg_func1!(self, "debug", debug, String, INT, bool, char, String, ()); - reg_func1!(self, "debug", print_debug, String, i8, u8, i16, u16); - reg_func1!(self, "debug", print_debug, String, i32, i64, u32, u64); - reg_func1!(self, "debug", print_debug, String, bool, char, String, ()); + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_func1!(self, "print", print, String, i8, u8, i16, u16); + reg_func1!(self, "print", print, String, i32, i64, u32, u64); + reg_func1!(self, "debug", debug, String, i8, u8, i16, u16); + reg_func1!(self, "debug", debug, String, i32, i64, u32, u64); + } #[cfg(not(feature = "no_float"))] { reg_func1!(self, "print", print, String, f32, f64); - reg_func1!(self, "debug", print_debug, String, f32, f64); + reg_func1!(self, "debug", debug, String, f32, f64); } #[cfg(not(feature = "no_index"))] { - reg_func1!(self, "print", print_debug, String, Array); - reg_func1!(self, "debug", print_debug, String, Array); + reg_func1!(self, "print", debug, String, Array); + reg_func1!(self, "debug", debug, String, Array); // Register array iterator self.register_iterator::(|a| { @@ -499,16 +606,16 @@ impl Engine<'_> { } // Register range function - self.register_iterator::, _>(|a| { + self.register_iterator::, _>(|a| { Box::new( - a.downcast_ref::>() + a.downcast_ref::>() .unwrap() .clone() .map(|n| n.into_dynamic()), ) }); - self.register_fn("range", |i1: i64, i2: i64| (i1..i2)); + self.register_fn("range", |i1: INT, i2: INT| (i1..i2)); } /// Register the built-in library. @@ -520,52 +627,69 @@ impl Engine<'_> { #[cfg(not(feature = "no_float"))] { // Advanced math functions - self.register_fn("sin", |x: f64| x.to_radians().sin()); - self.register_fn("cos", |x: f64| x.to_radians().cos()); - self.register_fn("tan", |x: f64| x.to_radians().tan()); - self.register_fn("sinh", |x: f64| x.to_radians().sinh()); - self.register_fn("cosh", |x: f64| x.to_radians().cosh()); - self.register_fn("tanh", |x: f64| x.to_radians().tanh()); - self.register_fn("asin", |x: f64| x.asin().to_degrees()); - self.register_fn("acos", |x: f64| x.acos().to_degrees()); - self.register_fn("atan", |x: f64| x.atan().to_degrees()); - self.register_fn("asinh", |x: f64| x.asinh().to_degrees()); - self.register_fn("acosh", |x: f64| x.acosh().to_degrees()); - self.register_fn("atanh", |x: f64| x.atanh().to_degrees()); - self.register_fn("sqrt", |x: f64| x.sqrt()); - self.register_fn("exp", |x: f64| x.exp()); - self.register_fn("ln", |x: f64| x.ln()); - self.register_fn("log", |x: f64, base: f64| x.log(base)); - self.register_fn("log10", |x: f64| x.log10()); - self.register_fn("floor", |x: f64| x.floor()); - self.register_fn("ceiling", |x: f64| x.ceil()); - self.register_fn("round", |x: f64| x.ceil()); - self.register_fn("int", |x: f64| x.trunc()); - self.register_fn("fraction", |x: f64| x.fract()); - self.register_fn("is_nan", |x: f64| x.is_nan()); - self.register_fn("is_finite", |x: f64| x.is_finite()); - self.register_fn("is_infinite", |x: f64| x.is_infinite()); + self.register_fn("sin", |x: FLOAT| x.to_radians().sin()); + self.register_fn("cos", |x: FLOAT| x.to_radians().cos()); + self.register_fn("tan", |x: FLOAT| x.to_radians().tan()); + self.register_fn("sinh", |x: FLOAT| x.to_radians().sinh()); + self.register_fn("cosh", |x: FLOAT| x.to_radians().cosh()); + self.register_fn("tanh", |x: FLOAT| x.to_radians().tanh()); + self.register_fn("asin", |x: FLOAT| x.asin().to_degrees()); + self.register_fn("acos", |x: FLOAT| x.acos().to_degrees()); + self.register_fn("atan", |x: FLOAT| x.atan().to_degrees()); + self.register_fn("asinh", |x: FLOAT| x.asinh().to_degrees()); + self.register_fn("acosh", |x: FLOAT| x.acosh().to_degrees()); + self.register_fn("atanh", |x: FLOAT| x.atanh().to_degrees()); + self.register_fn("sqrt", |x: FLOAT| x.sqrt()); + self.register_fn("exp", |x: FLOAT| x.exp()); + self.register_fn("ln", |x: FLOAT| x.ln()); + self.register_fn("log", |x: FLOAT, base: FLOAT| x.log(base)); + self.register_fn("log10", |x: FLOAT| x.log10()); + self.register_fn("floor", |x: FLOAT| x.floor()); + self.register_fn("ceiling", |x: FLOAT| x.ceil()); + self.register_fn("round", |x: FLOAT| x.ceil()); + self.register_fn("int", |x: FLOAT| x.trunc()); + self.register_fn("fraction", |x: FLOAT| x.fract()); + self.register_fn("is_nan", |x: FLOAT| x.is_nan()); + self.register_fn("is_finite", |x: FLOAT| x.is_finite()); + self.register_fn("is_infinite", |x: FLOAT| x.is_infinite()); // Register conversion functions - self.register_fn("to_float", |x: i8| x as f64); - self.register_fn("to_float", |x: u8| x as f64); - self.register_fn("to_float", |x: i16| x as f64); - self.register_fn("to_float", |x: u16| x as f64); - self.register_fn("to_float", |x: i32| x as f64); - self.register_fn("to_float", |x: u32| x as f64); - self.register_fn("to_float", |x: i64| x as f64); - self.register_fn("to_float", |x: u64| x as f64); - self.register_fn("to_float", |x: f32| x as f64); + self.register_fn("to_float", |x: INT| x as FLOAT); + self.register_fn("to_float", |x: f32| x as FLOAT); + + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + self.register_fn("to_float", |x: i8| x as FLOAT); + self.register_fn("to_float", |x: u8| x as FLOAT); + self.register_fn("to_float", |x: i16| x as FLOAT); + self.register_fn("to_float", |x: u16| x as FLOAT); + self.register_fn("to_float", |x: i32| x as FLOAT); + self.register_fn("to_float", |x: u32| x as FLOAT); + self.register_fn("to_float", |x: i64| x as FLOAT); + self.register_fn("to_float", |x: u64| x as FLOAT); + } } - self.register_fn("to_int", |x: i8| x as i64); - self.register_fn("to_int", |x: u8| x as i64); - self.register_fn("to_int", |x: i16| x as i64); - self.register_fn("to_int", |x: u16| x as i64); - self.register_fn("to_int", |x: i32| x as i64); - self.register_fn("to_int", |x: u32| x as i64); - self.register_fn("to_int", |x: u64| x as i64); - self.register_fn("to_int", |ch: char| ch as i64); + self.register_fn("to_int", |ch: char| ch as INT); + + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + self.register_fn("to_int", |x: i8| x as INT); + self.register_fn("to_int", |x: u8| x as INT); + self.register_fn("to_int", |x: i16| x as INT); + self.register_fn("to_int", |x: u16| x as INT); + } + + #[cfg(not(feature = "only_i32"))] + { + self.register_fn("to_int", |x: i32| x as INT); + self.register_fn("to_int", |x: u64| x as INT); + + #[cfg(feature = "only_i64")] + self.register_fn("to_int", |x: u32| x as INT); + } #[cfg(not(feature = "no_float"))] { @@ -579,24 +703,24 @@ impl Engine<'_> { )); } - Ok(x.trunc() as i64) + Ok(x.trunc() as INT) }); - self.register_result_fn("to_int", |x: f64| { - if x > (i64::MAX as f64) { + self.register_result_fn("to_int", |x: FLOAT| { + if x > (i64::MAX as FLOAT) { return Err(EvalAltResult::ErrorArithmetic( format!("Integer overflow: to_int({})", x), Position::none(), )); } - Ok(x.trunc() as i64) + Ok(x.trunc() as INT) }); } #[cfg(feature = "unchecked")] { - self.register_fn("to_int", |x: f32| x as i64); - self.register_fn("to_int", |x: f64| x as i64); + self.register_fn("to_int", |x: f32| x as INT); + self.register_fn("to_int", |x: f64| x as INT); } } @@ -606,7 +730,7 @@ impl Engine<'_> { fn push(list: &mut Array, item: T) { list.push(Box::new(item)); } - fn pad(list: &mut Array, len: i64, item: T) { + fn pad(list: &mut Array, len: INT, item: T) { if len >= 0 { while list.len() < len as usize { push(list, item.clone()); @@ -614,19 +738,24 @@ impl Engine<'_> { } } - reg_func2x!(self, "push", push, &mut Array, (), i8, u8, i16, u16); - reg_func2x!(self, "push", push, &mut Array, (), i32, i64, u32, u64); - reg_func2x!(self, "push", push, &mut Array, (), bool, char); + reg_func2x!(self, "push", push, &mut Array, (), INT, bool, char); reg_func2x!(self, "push", push, &mut Array, (), String, Array, ()); - reg_func3!(self, "pad", pad, &mut Array, i64, (), i8, u8, i16, u16); - reg_func3!(self, "pad", pad, &mut Array, i64, (), i32, u32, i64, u64); - reg_func3!(self, "pad", pad, &mut Array, i64, (), bool, char); - reg_func3!(self, "pad", pad, &mut Array, i64, (), String, Array, ()); + reg_func3!(self, "pad", pad, &mut Array, INT, (), INT, bool, char); + reg_func3!(self, "pad", pad, &mut Array, INT, (), String, Array, ()); + + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_func2x!(self, "push", push, &mut Array, (), i8, u8, i16, u16); + reg_func2x!(self, "push", push, &mut Array, (), i32, i64, u32, u64); + reg_func3!(self, "pad", pad, &mut Array, INT, (), i8, u8, i16, u16); + reg_func3!(self, "pad", pad, &mut Array, INT, (), i32, u32, i64, u64); + } #[cfg(not(feature = "no_float"))] { reg_func2x!(self, "push", push, &mut Array, (), f32, f64); - reg_func3!(self, "pad", pad, &mut Array, i64, (), f32, f64); + reg_func3!(self, "pad", pad, &mut Array, INT, (), f32, f64); } self.register_dynamic_fn("pop", |list: &mut Array| { @@ -636,9 +765,9 @@ impl Engine<'_> { 0 => ().into_dynamic(), _ => list.remove(0), }); - self.register_fn("len", |list: &mut Array| list.len() as i64); + self.register_fn("len", |list: &mut Array| list.len() as INT); self.register_fn("clear", |list: &mut Array| list.clear()); - self.register_fn("truncate", |list: &mut Array, len: i64| { + self.register_fn("truncate", |list: &mut Array, len: INT| { if len >= 0 { list.truncate(len as usize); } @@ -653,16 +782,19 @@ impl Engine<'_> { format!("{}{}", x, y) } - reg_func2x!( - self, "+", append, String, String, i8, u8, i16, u16, i32, i64, u32, u64, bool, char - ); + reg_func2x!(self, "+", append, String, String, INT, bool, char); self.register_fn("+", |x: String, _: ()| format!("{}", x)); - reg_func2y!( - self, "+", prepend, String, String, i8, u8, i16, u16, i32, i64, u32, u64, bool, char - ); + reg_func2y!(self, "+", prepend, String, String, INT, bool, char); self.register_fn("+", |_: (), y: String| format!("{}", y)); + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_func2x!(self, "+", append, String, String, i8, u8, i16, u16, i32, i64, u32, u64); + reg_func2y!(self, "+", prepend, String, String, i8, u8, i16, u16, i32, i64, u32, u64); + } + #[cfg(not(feature = "no_float"))] { reg_func2x!(self, "+", append, String, String, f32, f64); @@ -676,13 +808,13 @@ impl Engine<'_> { } // Register string utility functions - self.register_fn("len", |s: &mut String| s.chars().count() as i64); + self.register_fn("len", |s: &mut String| s.chars().count() as INT); self.register_fn("contains", |s: &mut String, ch: char| s.contains(ch)); self.register_fn("contains", |s: &mut String, find: String| s.contains(&find)); self.register_fn("clear", |s: &mut String| s.clear()); self.register_fn("append", |s: &mut String, ch: char| s.push(ch)); self.register_fn("append", |s: &mut String, add: String| s.push_str(&add)); - self.register_fn("truncate", |s: &mut String, len: i64| { + self.register_fn("truncate", |s: &mut String, len: INT| { if len >= 0 { let chars: Vec<_> = s.chars().take(len as usize).collect(); s.clear(); @@ -691,7 +823,7 @@ impl Engine<'_> { s.clear(); } }); - self.register_fn("pad", |s: &mut String, len: i64, ch: char| { + self.register_fn("pad", |s: &mut String, len: INT, ch: char| { for _ in 0..s.chars().count() - len as usize { s.push(ch); } diff --git a/src/engine.rs b/src/engine.rs index d13d45d3..ebbf4ec7 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -4,6 +4,10 @@ use crate::any::{Any, AnyExt, Dynamic, Variant}; use crate::parser::{Expr, FnDef, Position, Stmt}; use crate::result::EvalAltResult; use crate::scope::Scope; + +#[cfg(not(feature = "no_index"))] +use crate::INT; + use std::{ any::{type_name, TypeId}, borrow::Cow, @@ -419,15 +423,15 @@ impl Engine<'_> { .and_then(move |(idx, _, val)| map(val).map(|v| (idx, v))) } - /// Evaluate the value of an index (must evaluate to i64) + /// Evaluate the value of an index (must evaluate to INT) #[cfg(not(feature = "no_index"))] fn eval_index_value( &mut self, scope: &mut Scope, idx_expr: &Expr, - ) -> Result { + ) -> Result { self.eval_expr(scope, idx_expr)? - .downcast::() + .downcast::() .map(|v| *v) .map_err(|_| EvalAltResult::ErrorIndexExpr(idx_expr.position())) } @@ -437,7 +441,7 @@ impl Engine<'_> { fn get_indexed_value( &self, val: Dynamic, - idx: i64, + idx: INT, val_pos: Position, idx_pos: Position, ) -> Result<(Dynamic, IndexSourceType), EvalAltResult> { diff --git a/src/lib.rs b/src/lib.rs index fb23e27c..cc043e7e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,7 +78,7 @@ pub use call::FuncArgs; pub use engine::Engine; pub use error::{ParseError, ParseErrorType}; pub use fn_register::{RegisterDynamicFn, RegisterFn, RegisterResultFn}; -pub use parser::{Position, AST}; +pub use parser::{Position, AST, FLOAT, INT}; pub use result::EvalAltResult; pub use scope::Scope; diff --git a/src/parser.rs b/src/parser.rs index 9943a7f7..fcbe7e20 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -5,6 +5,17 @@ use crate::error::{LexError, ParseError, ParseErrorType}; use crate::optimize::optimize; use std::{borrow::Cow, char, fmt, iter::Peekable, str::Chars, str::FromStr, usize}; +/// The system integer type +#[cfg(not(feature = "only_i32"))] +pub type INT = i64; + +/// The system integer type +#[cfg(feature = "only_i32")] +pub type INT = i32; + +/// The system floating-point type +pub type FLOAT = f64; + type LERR = LexError; type PERR = ParseErrorType; @@ -158,9 +169,9 @@ impl Stmt { #[derive(Debug, Clone)] pub enum Expr { - IntegerConstant(i64, Position), + IntegerConstant(INT, Position), #[cfg(not(feature = "no_float"))] - FloatConstant(f64, Position), + FloatConstant(FLOAT, Position), Identifier(String, Position), CharConstant(char, Position), StringConstant(String, Position), @@ -233,9 +244,9 @@ impl Expr { #[derive(Debug, PartialEq, Clone)] pub enum Token { - IntegerConstant(i64), + IntegerConstant(INT), #[cfg(not(feature = "no_float"))] - FloatConstant(f64), + FloatConstant(FLOAT), Identifier(String), CharConstant(char), StringConst(String), @@ -711,7 +722,7 @@ impl<'a> TokenIterator<'a> { let out: String = result.iter().skip(2).filter(|&&c| c != '_').collect(); return Some(( - i64::from_str_radix(&out, radix) + INT::from_str_radix(&out, radix) .map(Token::IntegerConstant) .unwrap_or_else(|_| { Token::LexError(LERR::MalformedNumber(result.iter().collect())) @@ -723,7 +734,7 @@ impl<'a> TokenIterator<'a> { #[cfg(feature = "no_float")] return Some(( - i64::from_str(&out) + INT::from_str(&out) .map(Token::IntegerConstant) .unwrap_or_else(|_| { Token::LexError(LERR::MalformedNumber(result.iter().collect())) @@ -733,9 +744,9 @@ impl<'a> TokenIterator<'a> { #[cfg(not(feature = "no_float"))] return Some(( - i64::from_str(&out) + INT::from_str(&out) .map(Token::IntegerConstant) - .or_else(|_| f64::from_str(&out).map(Token::FloatConstant)) + .or_else(|_| FLOAT::from_str(&out).map(Token::FloatConstant)) .unwrap_or_else(|_| { Token::LexError(LERR::MalformedNumber(result.iter().collect())) }), @@ -1415,7 +1426,7 @@ fn parse_unary<'a>(input: &mut Peekable>) -> Result Ok(i .checked_neg() .map(|x| Expr::IntegerConstant(x, pos)) - .unwrap_or_else(|| Expr::FloatConstant(-(i as f64), pos))), + .unwrap_or_else(|| Expr::FloatConstant(-(i as FLOAT), pos))), // Negative integer #[cfg(feature = "no_float")] diff --git a/src/result.rs b/src/result.rs index f95e4b6a..8034329c 100644 --- a/src/result.rs +++ b/src/result.rs @@ -2,7 +2,7 @@ use crate::any::Dynamic; use crate::error::ParseError; -use crate::parser::Position; +use crate::parser::{Position, INT}; use std::{error::Error, fmt}; /// Evaluation result. @@ -24,10 +24,10 @@ pub enum EvalAltResult { ErrorCharMismatch(Position), /// Array access out-of-bounds. /// Wrapped values are the current number of elements in the array and the index number. - ErrorArrayBounds(usize, i64, Position), + ErrorArrayBounds(usize, INT, Position), /// String indexing out-of-bounds. /// Wrapped values are the current number of characters in the string and the index number. - ErrorStringBounds(usize, i64, Position), + ErrorStringBounds(usize, INT, Position), /// Trying to index into a type that is not an array and not a string. ErrorIndexingType(String, Position), /// Trying to index into an array or string with an index that is not `i64`. diff --git a/tests/arrays.rs b/tests/arrays.rs index 2cf9309f..4bee090e 100644 --- a/tests/arrays.rs +++ b/tests/arrays.rs @@ -1,12 +1,12 @@ #![cfg(not(feature = "no_index"))] -use rhai::{Engine, EvalAltResult, RegisterFn}; +use rhai::{Engine, EvalAltResult, RegisterFn, INT}; #[test] fn test_arrays() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = [1, 2, 3]; x[1]")?, 2); - assert_eq!(engine.eval::("let y = [1, 2, 3]; y[1] = 5; y[1]")?, 5); + assert_eq!(engine.eval::("let x = [1, 2, 3]; x[1]")?, 2); + assert_eq!(engine.eval::("let y = [1, 2, 3]; y[1] = 5; y[1]")?, 5); Ok(()) } @@ -15,7 +15,7 @@ fn test_arrays() -> Result<(), EvalAltResult> { fn test_array_with_structs() -> Result<(), EvalAltResult> { #[derive(Clone)] struct TestStruct { - x: i64, + x: INT, } impl TestStruct { @@ -23,11 +23,11 @@ fn test_array_with_structs() -> Result<(), EvalAltResult> { self.x += 1000; } - fn get_x(&mut self) -> i64 { + fn get_x(&mut self) -> INT { self.x } - fn set_x(&mut self, new_x: i64) { + fn set_x(&mut self, new_x: INT) { self.x = new_x; } @@ -44,10 +44,10 @@ fn test_array_with_structs() -> Result<(), EvalAltResult> { engine.register_fn("update", TestStruct::update); engine.register_fn("new_ts", TestStruct::new); - assert_eq!(engine.eval::("let a = [new_ts()]; a[0].x")?, 1); + assert_eq!(engine.eval::("let a = [new_ts()]; a[0].x")?, 1); assert_eq!( - engine.eval::( + engine.eval::( "let a = [new_ts()]; \ a[0].x = 100; \ a[0].update(); \ diff --git a/tests/binary_ops.rs b/tests/binary_ops.rs index edf560aa..3e59e8fd 100644 --- a/tests/binary_ops.rs +++ b/tests/binary_ops.rs @@ -1,15 +1,15 @@ -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_binary_ops() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("10 % 4")?, 2); - assert_eq!(engine.eval::("10 << 4")?, 160); - assert_eq!(engine.eval::("10 >> 4")?, 0); - assert_eq!(engine.eval::("10 & 4")?, 0); - assert_eq!(engine.eval::("10 | 4")?, 14); - assert_eq!(engine.eval::("10 ^ 4")?, 14); + assert_eq!(engine.eval::("10 % 4")?, 2); + assert_eq!(engine.eval::("10 << 4")?, 160); + assert_eq!(engine.eval::("10 >> 4")?, 0); + assert_eq!(engine.eval::("10 & 4")?, 0); + assert_eq!(engine.eval::("10 | 4")?, 14); + assert_eq!(engine.eval::("10 ^ 4")?, 14); assert_eq!(engine.eval::("42 == 42")?, true); assert_eq!(engine.eval::("42 > 42")?, false); diff --git a/tests/bit_shift.rs b/tests/bit_shift.rs index 7b0a4ab5..7dd3aa76 100644 --- a/tests/bit_shift.rs +++ b/tests/bit_shift.rs @@ -1,15 +1,15 @@ -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_left_shift() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("4 << 2")?, 16); + assert_eq!(engine.eval::("4 << 2")?, 16); Ok(()) } #[test] fn test_right_shift() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("9 >> 1")?, 4); + assert_eq!(engine.eval::("9 >> 1")?, 4); Ok(()) } diff --git a/tests/comments.rs b/tests/comments.rs index 0b871944..8c071f07 100644 --- a/tests/comments.rs +++ b/tests/comments.rs @@ -1,14 +1,14 @@ -use rhai::Engine; +use rhai::{Engine, INT}; #[test] fn test_comments() { let mut engine = Engine::new(); assert!(engine - .eval::("let x = 5; x // I am a single line comment, yay!") + .eval::("let x = 5; x // I am a single line comment, yay!") .is_ok()); assert!(engine - .eval::("let /* I am a multiline comment, yay! */ x = 5; x") + .eval::("let /* I am a multiline comment, yay! */ x = 5; x") .is_ok()); } diff --git a/tests/compound_equality.rs b/tests/compound_equality.rs index 65b3b3e4..569cfd94 100644 --- a/tests/compound_equality.rs +++ b/tests/compound_equality.rs @@ -1,10 +1,10 @@ -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_or_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 16; x |= 74; x")?, 90); + assert_eq!(engine.eval::("let x = 16; x |= 74; x")?, 90); assert_eq!(engine.eval::("let x = true; x |= false; x")?, true); assert_eq!(engine.eval::("let x = false; x |= true; x")?, true); @@ -15,7 +15,7 @@ fn test_or_equals() -> Result<(), EvalAltResult> { fn test_and_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 16; x &= 31; x")?, 16); + assert_eq!(engine.eval::("let x = 16; x &= 31; x")?, 16); assert_eq!(engine.eval::("let x = true; x &= false; x")?, false); assert_eq!(engine.eval::("let x = false; x &= true; x")?, false); assert_eq!(engine.eval::("let x = true; x &= true; x")?, true); @@ -26,41 +26,41 @@ fn test_and_equals() -> Result<(), EvalAltResult> { #[test] fn test_xor_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 90; x ^= 12; x")?, 86); + assert_eq!(engine.eval::("let x = 90; x ^= 12; x")?, 86); Ok(()) } #[test] fn test_multiply_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 2; x *= 3; x")?, 6); + assert_eq!(engine.eval::("let x = 2; x *= 3; x")?, 6); Ok(()) } #[test] fn test_divide_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 6; x /= 2; x")?, 3); + assert_eq!(engine.eval::("let x = 6; x /= 2; x")?, 3); Ok(()) } #[test] fn test_left_shift_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 9; x >>=1; x")?, 4); + assert_eq!(engine.eval::("let x = 9; x >>=1; x")?, 4); Ok(()) } #[test] fn test_right_shift_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 4; x<<= 2; x")?, 16); + assert_eq!(engine.eval::("let x = 4; x<<= 2; x")?, 16); Ok(()) } #[test] fn test_modulo_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 10; x %= 4; x")?, 2); + assert_eq!(engine.eval::("let x = 10; x %= 4; x")?, 2); Ok(()) } diff --git a/tests/decrement.rs b/tests/decrement.rs index 75d85076..3547adc4 100644 --- a/tests/decrement.rs +++ b/tests/decrement.rs @@ -1,10 +1,10 @@ -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_decrement() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 10; x -= 7; x")?, 3); + assert_eq!(engine.eval::("let x = 10; x -= 7; x")?, 3); let r = engine.eval::("let s = \"test\"; s -= \"ing\"; s"); diff --git a/tests/engine.rs b/tests/engine.rs index 17b83288..a01ae916 100644 --- a/tests/engine.rs +++ b/tests/engine.rs @@ -1,5 +1,5 @@ #![cfg(not(feature = "no_stdlib"))] -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_engine_call_fn() -> Result<(), EvalAltResult> { @@ -13,7 +13,7 @@ fn test_engine_call_fn() -> Result<(), EvalAltResult> { ", )?; - let result: i64 = engine.call_fn("hello", &ast, (String::from("abc"), 123_i64))?; + let result: INT = engine.call_fn("hello", &ast, (String::from("abc"), 123 as INT))?; assert_eq!(result, 126); diff --git a/tests/for.rs b/tests/for.rs index f67b95f2..be476328 100644 --- a/tests/for.rs +++ b/tests/for.rs @@ -1,5 +1,5 @@ #![cfg(not(feature = "no_index"))] -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_for() -> Result<(), EvalAltResult> { @@ -21,7 +21,7 @@ fn test_for() -> Result<(), EvalAltResult> { sum1 + sum2 "; - assert_eq!(engine.eval::(script)?, 30); + assert_eq!(engine.eval::(script)?, 30); Ok(()) } diff --git a/tests/get_set.rs b/tests/get_set.rs index 0a9fdd16..71886d2d 100644 --- a/tests/get_set.rs +++ b/tests/get_set.rs @@ -1,18 +1,18 @@ -use rhai::{Engine, EvalAltResult, RegisterFn}; +use rhai::{Engine, EvalAltResult, RegisterFn, INT}; #[test] fn test_get_set() -> Result<(), EvalAltResult> { #[derive(Clone)] struct TestStruct { - x: i64, + x: INT, } impl TestStruct { - fn get_x(&mut self) -> i64 { + fn get_x(&mut self) -> INT { self.x } - fn set_x(&mut self, new_x: i64) { + fn set_x(&mut self, new_x: INT) { self.x = new_x; } @@ -28,7 +28,7 @@ fn test_get_set() -> Result<(), EvalAltResult> { engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x); engine.register_fn("new_ts", TestStruct::new); - assert_eq!(engine.eval::("let a = new_ts(); a.x = 500; a.x")?, 500); + assert_eq!(engine.eval::("let a = new_ts(); a.x = 500; a.x")?, 500); Ok(()) } @@ -37,15 +37,15 @@ fn test_get_set() -> Result<(), EvalAltResult> { fn test_big_get_set() -> Result<(), EvalAltResult> { #[derive(Clone)] struct TestChild { - x: i64, + x: INT, } impl TestChild { - fn get_x(&mut self) -> i64 { + fn get_x(&mut self) -> INT { self.x } - fn set_x(&mut self, new_x: i64) { + fn set_x(&mut self, new_x: INT) { self.x = new_x; } @@ -86,7 +86,7 @@ fn test_big_get_set() -> Result<(), EvalAltResult> { engine.register_fn("new_tp", TestParent::new); assert_eq!( - engine.eval::("let a = new_tp(); a.child.x = 500; a.child.x")?, + engine.eval::("let a = new_tp(); a.child.x = 500; a.child.x")?, 500 ); diff --git a/tests/if_block.rs b/tests/if_block.rs index 286aa7fd..e8ddb25b 100644 --- a/tests/if_block.rs +++ b/tests/if_block.rs @@ -1,18 +1,18 @@ -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_if() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("if true { 55 }")?, 55); - assert_eq!(engine.eval::("if false { 55 } else { 44 }")?, 44); - assert_eq!(engine.eval::("if true { 55 } else { 44 }")?, 55); + assert_eq!(engine.eval::("if true { 55 }")?, 55); + assert_eq!(engine.eval::("if false { 55 } else { 44 }")?, 44); + assert_eq!(engine.eval::("if true { 55 } else { 44 }")?, 55); assert_eq!( - engine.eval::("if false { 55 } else if true { 33 } else { 44 }")?, + engine.eval::("if false { 55 } else if true { 33 } else { 44 }")?, 33 ); assert_eq!( - engine.eval::( + engine.eval::( r" if false { 55 } else if false { 33 } diff --git a/tests/increment.rs b/tests/increment.rs index 62b1b36c..980dda56 100644 --- a/tests/increment.rs +++ b/tests/increment.rs @@ -1,10 +1,10 @@ -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_increment() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 1; x += 2; x")?, 3); + assert_eq!(engine.eval::("let x = 1; x += 2; x")?, 3); assert_eq!( engine.eval::("let s = \"test\"; s += \"ing\"; s")?, "testing".to_string() diff --git a/tests/internal_fn.rs b/tests/internal_fn.rs index 95f584a5..09bb357e 100644 --- a/tests/internal_fn.rs +++ b/tests/internal_fn.rs @@ -1,11 +1,11 @@ -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_internal_fn() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("fn addme(a, b) { a+b } addme(3, 4)")?, 7); - assert_eq!(engine.eval::("fn bob() { return 4; 5 } bob()")?, 4); + assert_eq!(engine.eval::("fn addme(a, b) { a+b } addme(3, 4)")?, 7); + assert_eq!(engine.eval::("fn bob() { return 4; 5 } bob()")?, 4); Ok(()) } @@ -15,7 +15,7 @@ fn test_big_internal_fn() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); assert_eq!( - engine.eval::( + engine.eval::( r" fn mathme(a, b, c, d, e, f) { a - b * c + d * e - f diff --git a/tests/math.rs b/tests/math.rs index 9b92f1fb..4dd7fecc 100644 --- a/tests/math.rs +++ b/tests/math.rs @@ -1,41 +1,73 @@ -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_math() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("1 + 2")?, 3); - assert_eq!(engine.eval::("1 - 2")?, -1); - assert_eq!(engine.eval::("2 * 3")?, 6); - assert_eq!(engine.eval::("1 / 2")?, 0); - assert_eq!(engine.eval::("3 % 2")?, 1); + assert_eq!(engine.eval::("1 + 2")?, 3); + assert_eq!(engine.eval::("1 - 2")?, -1); + assert_eq!(engine.eval::("2 * 3")?, 6); + assert_eq!(engine.eval::("1 / 2")?, 0); + assert_eq!(engine.eval::("3 % 2")?, 1); + + #[cfg(not(feature = "only_i32"))] assert_eq!( - engine.eval::("(-9223372036854775807).abs()")?, + engine.eval::("(-9223372036854775807).abs()")?, 9223372036854775807 ); + #[cfg(feature = "only_i32")] + assert_eq!(engine.eval::("(-2147483647).abs()")?, 2147483647); + // Overflow/underflow/division-by-zero errors #[cfg(not(feature = "unchecked"))] { - match engine.eval::("9223372036854775807 + 1") { - Err(EvalAltResult::ErrorArithmetic(_, _)) => (), - r => panic!("should return overflow error: {:?}", r), + #[cfg(not(feature = "only_i32"))] + { + match engine.eval::("9223372036854775807 + 1") { + Err(EvalAltResult::ErrorArithmetic(_, _)) => (), + r => panic!("should return overflow error: {:?}", r), + } + match engine.eval::("-9223372036854775808 - 1") { + Err(EvalAltResult::ErrorArithmetic(_, _)) => (), + r => panic!("should return underflow error: {:?}", r), + } + match engine.eval::("9223372036854775807 * 9223372036854775807") { + Err(EvalAltResult::ErrorArithmetic(_, _)) => (), + r => panic!("should return overflow error: {:?}", r), + } + match engine.eval::("9223372036854775807 / 0") { + Err(EvalAltResult::ErrorArithmetic(_, _)) => (), + r => panic!("should return division by zero error: {:?}", r), + } + match engine.eval::("9223372036854775807 % 0") { + Err(EvalAltResult::ErrorArithmetic(_, _)) => (), + r => panic!("should return division by zero error: {:?}", r), + } } - match engine.eval::("-9223372036854775808 - 1") { - Err(EvalAltResult::ErrorArithmetic(_, _)) => (), - r => panic!("should return underflow error: {:?}", r), - } - match engine.eval::("9223372036854775807 * 9223372036854775807") { - Err(EvalAltResult::ErrorArithmetic(_, _)) => (), - r => panic!("should return overflow error: {:?}", r), - } - match engine.eval::("9223372036854775807 / 0") { - Err(EvalAltResult::ErrorArithmetic(_, _)) => (), - r => panic!("should return division by zero error: {:?}", r), - } - match engine.eval::("9223372036854775807 % 0") { - Err(EvalAltResult::ErrorArithmetic(_, _)) => (), - r => panic!("should return division by zero error: {:?}", r), + + #[cfg(feature = "only_i32")] + { + match engine.eval::("2147483647 + 1") { + Err(EvalAltResult::ErrorArithmetic(_, _)) => (), + r => panic!("should return overflow error: {:?}", r), + } + match engine.eval::("-2147483648 - 1") { + Err(EvalAltResult::ErrorArithmetic(_, _)) => (), + r => panic!("should return underflow error: {:?}", r), + } + match engine.eval::("2147483647 * 2147483647") { + Err(EvalAltResult::ErrorArithmetic(_, _)) => (), + r => panic!("should return overflow error: {:?}", r), + } + match engine.eval::("2147483647 / 0") { + Err(EvalAltResult::ErrorArithmetic(_, _)) => (), + r => panic!("should return division by zero error: {:?}", r), + } + match engine.eval::("2147483647 % 0") { + Err(EvalAltResult::ErrorArithmetic(_, _)) => (), + r => panic!("should return division by zero error: {:?}", r), + } } } diff --git a/tests/method_call.rs b/tests/method_call.rs index f9f09652..e1825094 100644 --- a/tests/method_call.rs +++ b/tests/method_call.rs @@ -1,10 +1,10 @@ -use rhai::{Engine, EvalAltResult, RegisterFn}; +use rhai::{Engine, EvalAltResult, RegisterFn, INT}; #[test] fn test_method_call() -> Result<(), EvalAltResult> { #[derive(Clone)] struct TestStruct { - x: i64, + x: INT, } impl TestStruct { diff --git a/tests/mismatched_op.rs b/tests/mismatched_op.rs index 235062d5..1abb3cc1 100644 --- a/tests/mismatched_op.rs +++ b/tests/mismatched_op.rs @@ -1,11 +1,11 @@ -use rhai::{Engine, EvalAltResult, RegisterFn}; +use rhai::{Engine, EvalAltResult, RegisterFn, INT}; #[test] #[cfg(not(feature = "no_stdlib"))] fn test_mismatched_op() { let mut engine = Engine::new(); - let r = engine.eval::("60 + \"hello\""); + let r = engine.eval::("60 + \"hello\""); match r { Err(EvalAltResult::ErrorMismatchOutputType(err, _)) if err == "string" => (), @@ -17,7 +17,7 @@ fn test_mismatched_op() { fn test_mismatched_op_custom_type() { #[derive(Clone)] struct TestStruct { - x: i64, + x: INT, } impl TestStruct { @@ -30,10 +30,15 @@ fn test_mismatched_op_custom_type() { engine.register_type_with_name::("TestStruct"); engine.register_fn("new_ts", TestStruct::new); - let r = engine.eval::("60 + new_ts()"); + let r = engine.eval::("60 + new_ts()"); match r { + #[cfg(feature = "only_i32")] + Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err == "+ (i32, TestStruct)" => (), + + #[cfg(not(feature = "only_i32"))] Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err == "+ (i64, TestStruct)" => (), + _ => panic!(), } } diff --git a/tests/number_literals.rs b/tests/number_literals.rs index 16e12719..f6466967 100644 --- a/tests/number_literals.rs +++ b/tests/number_literals.rs @@ -1,10 +1,10 @@ -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_number_literal() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("65")?, 65); + assert_eq!(engine.eval::("65")?, 65); Ok(()) } @@ -13,8 +13,8 @@ fn test_number_literal() -> Result<(), EvalAltResult> { fn test_hex_literal() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 0xf; x")?, 15); - assert_eq!(engine.eval::("let x = 0xff; x")?, 255); + assert_eq!(engine.eval::("let x = 0xf; x")?, 15); + assert_eq!(engine.eval::("let x = 0xff; x")?, 255); Ok(()) } @@ -23,8 +23,8 @@ fn test_hex_literal() -> Result<(), EvalAltResult> { fn test_octal_literal() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 0o77; x")?, 63); - assert_eq!(engine.eval::("let x = 0o1234; x")?, 668); + assert_eq!(engine.eval::("let x = 0o77; x")?, 63); + assert_eq!(engine.eval::("let x = 0o1234; x")?, 668); Ok(()) } @@ -33,9 +33,9 @@ fn test_octal_literal() -> Result<(), EvalAltResult> { fn test_binary_literal() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 0b1111; x")?, 15); + assert_eq!(engine.eval::("let x = 0b1111; x")?, 15); assert_eq!( - engine.eval::("let x = 0b0011_1100_1010_0101; x")?, + engine.eval::("let x = 0b0011_1100_1010_0101; x")?, 15525 ); diff --git a/tests/ops.rs b/tests/ops.rs index d1464dfb..aaddea8a 100644 --- a/tests/ops.rs +++ b/tests/ops.rs @@ -1,11 +1,11 @@ -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_ops() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("60 + 5")?, 65); - assert_eq!(engine.eval::("(1 + 2) * (6 - 4) / 2")?, 3); + assert_eq!(engine.eval::("60 + 5")?, 65); + assert_eq!(engine.eval::("(1 + 2) * (6 - 4) / 2")?, 3); Ok(()) } @@ -15,7 +15,7 @@ fn test_op_prec() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); assert_eq!( - engine.eval::("let x = 0; if x == 10 || true { x = 1} x")?, + engine.eval::("let x = 0; if x == 10 || true { x = 1} x")?, 1 ); diff --git a/tests/power_of.rs b/tests/power_of.rs index 78abcb5d..8e1ed522 100644 --- a/tests/power_of.rs +++ b/tests/power_of.rs @@ -1,19 +1,22 @@ -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, FLOAT, INT}; #[test] fn test_power_of() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("2 ~ 3")?, 8); - assert_eq!(engine.eval::("(-2 ~ 3)")?, -8); + assert_eq!(engine.eval::("2 ~ 3")?, 8); + assert_eq!(engine.eval::("(-2 ~ 3)")?, -8); #[cfg(not(feature = "no_float"))] { - assert_eq!(engine.eval::("2.2 ~ 3.3")?, 13.489468760533386_f64); - assert_eq!(engine.eval::("2.0~-2.0")?, 0.25_f64); - assert_eq!(engine.eval::("(-2.0~-2.0)")?, 0.25_f64); - assert_eq!(engine.eval::("(-2.0~-2)")?, 0.25_f64); - assert_eq!(engine.eval::("4~3")?, 64); + assert_eq!( + engine.eval::("2.2 ~ 3.3")?, + 13.489468760533386 as FLOAT + ); + assert_eq!(engine.eval::("2.0~-2.0")?, 0.25 as FLOAT); + assert_eq!(engine.eval::("(-2.0~-2.0)")?, 0.25 as FLOAT); + assert_eq!(engine.eval::("(-2.0~-2)")?, 0.25 as FLOAT); + assert_eq!(engine.eval::("4~3")?, 64); } Ok(()) @@ -23,19 +26,28 @@ fn test_power_of() -> Result<(), EvalAltResult> { fn test_power_of_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 2; x ~= 3; x")?, 8); - assert_eq!(engine.eval::("let x = -2; x ~= 3; x")?, -8); + assert_eq!(engine.eval::("let x = 2; x ~= 3; x")?, 8); + assert_eq!(engine.eval::("let x = -2; x ~= 3; x")?, -8); #[cfg(not(feature = "no_float"))] { assert_eq!( - engine.eval::("let x = 2.2; x ~= 3.3; x")?, - 13.489468760533386_f64 + engine.eval::("let x = 2.2; x ~= 3.3; x")?, + 13.489468760533386 as FLOAT ); - assert_eq!(engine.eval::("let x = 2.0; x ~= -2.0; x")?, 0.25_f64); - assert_eq!(engine.eval::("let x = -2.0; x ~= -2.0; x")?, 0.25_f64); - assert_eq!(engine.eval::("let x = -2.0; x ~= -2; x")?, 0.25_f64); - assert_eq!(engine.eval::("let x =4; x ~= 3; x")?, 64); + assert_eq!( + engine.eval::("let x = 2.0; x ~= -2.0; x")?, + 0.25 as FLOAT + ); + assert_eq!( + engine.eval::("let x = -2.0; x ~= -2.0; x")?, + 0.25 as FLOAT + ); + assert_eq!( + engine.eval::("let x = -2.0; x ~= -2; x")?, + 0.25 as FLOAT + ); + assert_eq!(engine.eval::("let x =4; x ~= 3; x")?, 64); } Ok(()) diff --git a/tests/throw.rs b/tests/throw.rs index ca14cb3f..5edecedc 100644 --- a/tests/throw.rs +++ b/tests/throw.rs @@ -1,16 +1,16 @@ -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_throw() { let mut engine = Engine::new(); - match engine.eval::(r#"if true { throw "hello" }"#) { + match engine.eval::(r#"if true { throw "hello" }"#) { Ok(_) => panic!("not an error"), Err(EvalAltResult::ErrorRuntime(s, _)) if s == "hello" => (), Err(err) => panic!("wrong error: {}", err), } - match engine.eval::(r#"throw;"#) { + match engine.eval::(r#"throw;"#) { Ok(_) => panic!("not an error"), Err(EvalAltResult::ErrorRuntime(s, _)) if s == "" => (), Err(err) => panic!("wrong error: {}", err), diff --git a/tests/types.rs b/tests/types.rs index ed3ff2ea..fd9cda32 100644 --- a/tests/types.rs +++ b/tests/types.rs @@ -4,8 +4,12 @@ use rhai::{Engine, EvalAltResult}; fn test_type_of() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); + #[cfg(not(feature = "only_i32"))] assert_eq!(engine.eval::("type_of(60 + 5)")?, "i64"); + #[cfg(feature = "only_i32")] + assert_eq!(engine.eval::("type_of(60 + 5)")?, "i32"); + #[cfg(not(feature = "no_float"))] assert_eq!(engine.eval::("type_of(1.0 + 2.0)")?, "f64"); @@ -17,7 +21,12 @@ fn test_type_of() -> Result<(), EvalAltResult> { ); assert_eq!(engine.eval::(r#"type_of("hello")"#)?, "string"); + + #[cfg(not(feature = "only_i32"))] assert_eq!(engine.eval::("let x = 123; x.type_of()")?, "i64"); + #[cfg(feature = "only_i32")] + assert_eq!(engine.eval::("let x = 123; x.type_of()")?, "i32"); + Ok(()) } diff --git a/tests/unary_after_binary.rs b/tests/unary_after_binary.rs index f44111d4..16d4730a 100644 --- a/tests/unary_after_binary.rs +++ b/tests/unary_after_binary.rs @@ -1,4 +1,4 @@ -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] // TODO also add test case for unary after compound @@ -6,12 +6,12 @@ use rhai::{Engine, EvalAltResult}; fn test_unary_after_binary() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("10 % +4")?, 2); - assert_eq!(engine.eval::("10 << +4")?, 160); - assert_eq!(engine.eval::("10 >> +4")?, 0); - assert_eq!(engine.eval::("10 & +4")?, 0); - assert_eq!(engine.eval::("10 | +4")?, 14); - assert_eq!(engine.eval::("10 ^ +4")?, 14); + assert_eq!(engine.eval::("10 % +4")?, 2); + assert_eq!(engine.eval::("10 << +4")?, 160); + assert_eq!(engine.eval::("10 >> +4")?, 0); + assert_eq!(engine.eval::("10 & +4")?, 0); + assert_eq!(engine.eval::("10 | +4")?, 14); + assert_eq!(engine.eval::("10 ^ +4")?, 14); Ok(()) } diff --git a/tests/unary_minus.rs b/tests/unary_minus.rs index 37718326..8f5f6013 100644 --- a/tests/unary_minus.rs +++ b/tests/unary_minus.rs @@ -1,12 +1,12 @@ -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_unary_minus() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = -5; x")?, -5); - assert_eq!(engine.eval::("fn neg(x) { -x } neg(5)")?, -5); - assert_eq!(engine.eval::("5 - -+++--+-5")?, 0); + assert_eq!(engine.eval::("let x = -5; x")?, -5); + assert_eq!(engine.eval::("fn neg(x) { -x } neg(5)")?, -5); + assert_eq!(engine.eval::("5 - -+++--+-5")?, 0); Ok(()) } diff --git a/tests/var_scope.rs b/tests/var_scope.rs index f8371bb3..0ad6c618 100644 --- a/tests/var_scope.rs +++ b/tests/var_scope.rs @@ -1,4 +1,4 @@ -use rhai::{Engine, EvalAltResult, Scope}; +use rhai::{Engine, EvalAltResult, Scope, INT}; #[test] fn test_var_scope() -> Result<(), EvalAltResult> { @@ -6,14 +6,14 @@ fn test_var_scope() -> Result<(), EvalAltResult> { let mut scope = Scope::new(); engine.eval_with_scope::<()>(&mut scope, false, "let x = 4 + 5")?; - assert_eq!(engine.eval_with_scope::(&mut scope, false, "x")?, 9); + assert_eq!(engine.eval_with_scope::(&mut scope, false, "x")?, 9); engine.eval_with_scope::<()>(&mut scope, false, "x = x + 1; x = x + 2;")?; - assert_eq!(engine.eval_with_scope::(&mut scope, false, "x")?, 12); + assert_eq!(engine.eval_with_scope::(&mut scope, false, "x")?, 12); assert_eq!( engine.eval_with_scope::<()>(&mut scope, false, "{let x = 3}")?, () ); - assert_eq!(engine.eval_with_scope::(&mut scope, false, "x")?, 12); + assert_eq!(engine.eval_with_scope::(&mut scope, false, "x")?, 12); Ok(()) } @@ -26,10 +26,10 @@ fn test_scope_eval() -> Result<(), EvalAltResult> { let mut scope = Scope::new(); // Then push some initialized variables into the state - // NOTE: Remember the default numbers used by Rhai are i64 and f64. + // NOTE: Remember the default numbers used by Rhai are INT and f64. // Better stick to them or it gets hard to work with other variables in the script. - scope.push("y", 42_i64); - scope.push("z", 999_i64); + scope.push("y", 42 as INT); + scope.push("z", 999 as INT); // First invocation engine @@ -37,12 +37,12 @@ fn test_scope_eval() -> Result<(), EvalAltResult> { .expect("y and z not found?"); // Second invocation using the same state - let result = engine.eval_with_scope::(&mut scope, false, "x")?; + let result = engine.eval_with_scope::(&mut scope, false, "x")?; println!("result: {}", result); // should print 966 // Variable y is changed in the script - assert_eq!(scope.get_value::("y").unwrap(), 1); + assert_eq!(scope.get_value::("y").unwrap(), 1); Ok(()) } diff --git a/tests/while_loop.rs b/tests/while_loop.rs index 8dc1ae85..fa76ea17 100644 --- a/tests/while_loop.rs +++ b/tests/while_loop.rs @@ -1,11 +1,11 @@ -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_while() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); assert_eq!( - engine.eval::( + engine.eval::( "let x = 0; while x < 10 { x = x + 1; if x > 5 { \ break } } x", )?,