diff --git a/Cargo.toml b/Cargo.toml index e39a0115..fe543553 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,5 @@ num-traits = "*" [features] debug_msgs = [] -no_stdlib = [] \ No newline at end of file +no_stdlib = [] +unchecked = [] diff --git a/README.md b/README.md index 9eaacd38..ebfc0909 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Rhai's current feature set: * Low compile-time overhead (~0.6 sec debug/~3 sec release for script runner app) * Easy-to-use language similar to JS+Rust * Support for overloaded functions -* No additional dependencies +* Very few additional dependencies (right now only `num-traits` to do checked arithmetic operations) **Note:** Currently, the version is 0.10.2, so the language and API's may change before they stabilize. @@ -43,6 +43,10 @@ Print debug messages to stdout (using `println!`) related to function registrati 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! + ## Related Other cool projects to check out: diff --git a/src/builtin.rs b/src/builtin.rs index 5a24db0a..e7cfb45b 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -3,15 +3,23 @@ use crate::any::Any; use crate::engine::{Array, Engine}; -use crate::fn_register::{RegisterFn, RegisterResultFn}; -use crate::parser::Position; -use crate::result::EvalAltResult; +use crate::fn_register::RegisterFn; +use std::fmt::{Debug, Display}; +use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Range, Rem, Sub}; + +#[cfg(feature = "unchecked")] +use std::ops::{Shl, Shr}; + +#[cfg(not(feature = "unchecked"))] +use crate::{parser::Position, result::EvalAltResult, RegisterResultFn}; + +#[cfg(not(feature = "unchecked"))] +use std::convert::TryFrom; + +#[cfg(not(feature = "unchecked"))] use num_traits::{ CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr, CheckedSub, }; -use std::convert::TryFrom; -use std::fmt::{Debug, Display}; -use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Range, Rem, Sub}; macro_rules! reg_op { ($self:expr, $x:expr, $op:expr, $( $y:ty ),*) => ( @@ -21,6 +29,7 @@ macro_rules! reg_op { ) } +#[cfg(not(feature = "unchecked"))] macro_rules! reg_op_result { ($self:expr, $x:expr, $op:expr, $( $y:ty ),*) => ( $( @@ -29,6 +38,7 @@ macro_rules! reg_op_result { ) } +#[cfg(not(feature = "unchecked"))] macro_rules! reg_op_result1 { ($self:expr, $x:expr, $op:expr, $v:ty, $( $y:ty ),*) => ( $( @@ -45,6 +55,7 @@ macro_rules! reg_un { ) } +#[cfg(not(feature = "unchecked"))] macro_rules! reg_un_result { ($self:expr, $x:expr, $op:expr, $( $y:ty ),*) => ( $( @@ -98,6 +109,7 @@ macro_rules! reg_func3 { impl Engine<'_> { /// Register the core built-in library. pub(crate) fn register_core_lib(&mut self) { + #[cfg(not(feature = "unchecked"))] fn add(x: T, y: T) -> Result { x.checked_add(&y).ok_or_else(|| { EvalAltResult::ErrorArithmetic( @@ -106,6 +118,7 @@ impl Engine<'_> { ) }) } + #[cfg(not(feature = "unchecked"))] fn sub(x: T, y: T) -> Result { x.checked_sub(&y).ok_or_else(|| { EvalAltResult::ErrorArithmetic( @@ -114,6 +127,7 @@ impl Engine<'_> { ) }) } + #[cfg(not(feature = "unchecked"))] fn mul(x: T, y: T) -> Result { x.checked_mul(&y).ok_or_else(|| { EvalAltResult::ErrorArithmetic( @@ -122,6 +136,7 @@ impl Engine<'_> { ) }) } + #[cfg(not(feature = "unchecked"))] fn div(x: T, y: T) -> Result where T: Display + CheckedDiv + PartialEq + TryFrom, @@ -143,6 +158,7 @@ impl Engine<'_> { ) }) } + #[cfg(not(feature = "unchecked"))] fn neg(x: T) -> Result { x.checked_neg().ok_or_else(|| { EvalAltResult::ErrorArithmetic( @@ -151,6 +167,7 @@ impl Engine<'_> { ) }) } + #[cfg(not(feature = "unchecked"))] fn abs>(x: T) -> Result { if x >= 0.into() { Ok(x) @@ -163,22 +180,22 @@ impl Engine<'_> { }) } } - fn add_unchecked(x: T, y: T) -> ::Output { + fn add_u(x: T, y: T) -> ::Output { x + y } - fn sub_unchecked(x: T, y: T) -> ::Output { + fn sub_u(x: T, y: T) -> ::Output { x - y } - fn mul_unchecked(x: T, y: T) -> ::Output { + fn mul_u(x: T, y: T) -> ::Output { x * y } - fn div_unchecked(x: T, y: T) -> ::Output { + fn div_u(x: T, y: T) -> ::Output { x / y } - fn neg_unchecked(x: T) -> ::Output { + fn neg_u(x: T) -> ::Output { -x } - fn abs_unchecked>(x: T) -> T + fn abs_u>(x: T) -> T where ::Output: Into, { @@ -224,7 +241,8 @@ impl Engine<'_> { fn binary_xor(x: T, y: T) -> ::Output { x ^ y } - fn left_shift(x: T, y: i64) -> Result { + #[cfg(not(feature = "unchecked"))] + fn shl(x: T, y: i64) -> Result { if y < 0 { return Err(EvalAltResult::ErrorArithmetic( format!("Left-shift by a negative number: {} << {}", x, y), @@ -239,7 +257,8 @@ impl Engine<'_> { ) }) } - fn right_shift(x: T, y: i64) -> Result { + #[cfg(not(feature = "unchecked"))] + fn shr(x: T, y: i64) -> Result { if y < 0 { return Err(EvalAltResult::ErrorArithmetic( format!("Right-shift by a negative number: {} >> {}", x, y), @@ -254,6 +273,15 @@ impl Engine<'_> { ) }) } + #[cfg(feature = "unchecked")] + fn shl_u>(x: T, y: T) -> >::Output { + x.shl(y) + } + #[cfg(feature = "unchecked")] + fn shr_u>(x: T, y: T) -> >::Output { + x.shr(y) + } + #[cfg(not(feature = "unchecked"))] fn modulo(x: T, y: T) -> Result { x.checked_rem(&y).ok_or_else(|| { EvalAltResult::ErrorArithmetic( @@ -262,7 +290,7 @@ impl Engine<'_> { ) }) } - fn modulo_unchecked(x: T, y: T) -> ::Output { + fn modulo_u(x: T, y: T) -> ::Output { x % y } fn pow_i64_i64(x: i64, y: i64) -> i64 { @@ -275,15 +303,26 @@ impl Engine<'_> { x.powi(y as i32) } - 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(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!(self, "+", add_unchecked, f32, f64); - reg_op!(self, "-", sub_unchecked, f32, f64); - reg_op!(self, "*", mul_unchecked, f32, f64); - reg_op!(self, "/", div_unchecked, f32, f64); + #[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, f32, f64); + reg_op!(self, "-", sub_u, f32, f64); + reg_op!(self, "*", mul_u, f32, f64); + reg_op!(self, "/", div_u, f32, f64); reg_cmp!(self, "<", lt, i8, u8, i16, u16, i32, i64, u32, u64, f32, f64, String, char); reg_cmp!(self, "<=", lte, i8, u8, i16, u16, i32, i64, u32, u64, f32, f64, String, char); @@ -303,19 +342,43 @@ impl Engine<'_> { 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_result1!(self, "<<", left_shift, i64, i8, u8, i16, u16, i32, i64, u32, u64); - reg_op_result1!(self, ">>", right_shift, i64, i8, u8, i16, u16); - reg_op_result1!(self, ">>", right_shift, i64, i32, i64, u32, u64); - reg_op_result!(self, "%", modulo, i8, u8, i16, u16, i32, i64, u32, u64); - reg_op!(self, "%", modulo_unchecked, f32, f64); + + #[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); + } + + #[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, "%", modulo_u, f32, f64); + self.register_fn("~", pow_i64_i64); self.register_fn("~", pow_f64_f64); self.register_fn("~", pow_f64_i64); - reg_un_result!(self, "-", neg, i8, i16, i32, i64); - reg_un!(self, "-", neg_unchecked, f32, f64); - reg_un_result!(self, "abs", abs, i8, i16, i32, i64); - reg_un!(self, "abs", abs_unchecked, f32, f64); + #[cfg(not(feature = "unchecked"))] + { + 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, f32, f64); + reg_un!(self, "abs", abs_u, f32, f64); reg_un!(self, "!", not, bool); self.register_fn("+", |x: String, y: String| x + &y); // String + String diff --git a/tests/math.rs b/tests/math.rs index bc79cbb0..98836037 100644 --- a/tests/math.rs +++ b/tests/math.rs @@ -15,25 +15,28 @@ fn test_math() -> Result<(), EvalAltResult> { ); // Overflow/underflow/division-by-zero errors - match engine.eval::("9223372036854775807 + 1") { - Err(EvalAltResult::ErrorArithmetic(_, _)) => (), - r => panic!("should return overflow error: {:?}", r), - } - match engine.eval::("(-9223372036854775807) - 2") { - 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(not(feature = "unchecked"))] + { + match engine.eval::("9223372036854775807 + 1") { + Err(EvalAltResult::ErrorArithmetic(_, _)) => (), + r => panic!("should return overflow error: {:?}", r), + } + match engine.eval::("(-9223372036854775807) - 2") { + 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), + } } Ok(())