diff --git a/.gitignore b/.gitignore index 1ec7ed7e..067f30fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ target/ Cargo.lock -.vscode/ \ No newline at end of file +.vscode/ +.cargo/ \ No newline at end of file diff --git a/README.md b/README.md index 5488ec97..6f4d7ded 100644 --- a/README.md +++ b/README.md @@ -43,8 +43,8 @@ Optional features | `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 | -| `no_float` | Disable floating-point numbers and math | +| `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. | Related ------- @@ -190,14 +190,14 @@ Values and types The following primitive types are supported natively: -| Category | Types | -| ------------------------------ | -------------------------------------- | -| Integer | `i32`, `u32`, `i64` _(default)_, `u64` | -| Floating-point | `f32`, `f64` _(default)_ | -| Character | `char` | -| Boolean | `bool` | -| Array | `rhai::Array` | -| Dynamic (i.e. can be anything) | `rhai::Dynamic` | +| 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` | Value conversions ----------------- diff --git a/src/api.rs b/src/api.rs index ac9914d1..9d309756 100644 --- a/src/api.rs +++ b/src/api.rs @@ -274,6 +274,8 @@ impl<'e> Engine<'e> { /// /// ```rust /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # #[cfg(not(feature = "no_stdlib"))] + /// # { /// use rhai::Engine; /// /// let mut engine = Engine::new(); @@ -283,6 +285,7 @@ impl<'e> Engine<'e> { /// let result: i64 = engine.call_fn("add", &ast, (String::from("abc"), 123_i64))?; /// /// assert_eq!(result, 126); + /// # } /// # Ok(()) /// # } /// ``` diff --git a/src/builtin.rs b/src/builtin.rs index 106d647b..16a8acb1 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -479,7 +479,7 @@ impl Engine<'_> { 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); + reg_func1!(self, "debug", print_debug, String, bool, char, String, ()); #[cfg(not(feature = "no_float"))] { @@ -490,7 +490,7 @@ impl Engine<'_> { #[cfg(not(feature = "no_index"))] { reg_func1!(self, "print", print_debug, String, Array); - reg_func1!(self, "debug", print_debug, String, String, Array, ()); + reg_func1!(self, "debug", print_debug, String, Array); // Register array iterator self.register_iterator::(|a| { diff --git a/src/optimize.rs b/src/optimize.rs index 29f226f6..085b09f9 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -61,7 +61,7 @@ fn optimize_stmt(stmt: Stmt, changed: &mut bool) -> Stmt { // Remove all raw expression statements that evaluate to constants // except for the very last statement result.retain(|stmt| match stmt { - Stmt::Expr(expr) if expr.is_constant() => false, + Stmt::Expr(expr) if expr.is_constant() || expr.is_identifier() => false, _ => true, }); diff --git a/tests/arrays.rs b/tests/arrays.rs index cfd74e6e..2cf9309f 100644 --- a/tests/arrays.rs +++ b/tests/arrays.rs @@ -1,3 +1,4 @@ +#![cfg(not(feature = "no_index"))] use rhai::{Engine, EvalAltResult, RegisterFn}; #[test] diff --git a/tests/chars.rs b/tests/chars.rs index 4a5c2d98..cf207e43 100644 --- a/tests/chars.rs +++ b/tests/chars.rs @@ -6,11 +6,15 @@ fn test_chars() -> Result<(), EvalAltResult> { assert_eq!(engine.eval::("'y'")?, 'y'); assert_eq!(engine.eval::("'\\u2764'")?, '❤'); - assert_eq!(engine.eval::(r#"let x="hello"; x[2]"#)?, 'l'); - assert_eq!( - engine.eval::(r#"let x="hello"; x[2]='$'; x"#)?, - "he$lo".to_string() - ); + + #[cfg(not(feature = "no_index"))] + { + assert_eq!(engine.eval::(r#"let x="hello"; x[2]"#)?, 'l'); + assert_eq!( + engine.eval::(r#"let x="hello"; x[2]='$'; x"#)?, + "he$lo".to_string() + ); + } assert!(engine.eval::("'\\uhello'").is_err()); assert!(engine.eval::("''").is_err()); diff --git a/tests/engine.rs b/tests/engine.rs index b049d107..17b83288 100644 --- a/tests/engine.rs +++ b/tests/engine.rs @@ -1,3 +1,4 @@ +#![cfg(not(feature = "no_stdlib"))] use rhai::{Engine, EvalAltResult}; #[test] diff --git a/tests/float.rs b/tests/float.rs index 8e291e9e..af33ee34 100644 --- a/tests/float.rs +++ b/tests/float.rs @@ -1,3 +1,4 @@ +#![cfg(not(feature = "no_float"))] use rhai::{Engine, EvalAltResult, RegisterFn}; #[test] diff --git a/tests/for.rs b/tests/for.rs index 63390b31..f67b95f2 100644 --- a/tests/for.rs +++ b/tests/for.rs @@ -1,3 +1,4 @@ +#![cfg(not(feature = "no_index"))] use rhai::{Engine, EvalAltResult}; #[test] diff --git a/tests/mismatched_op.rs b/tests/mismatched_op.rs index 9964189b..235062d5 100644 --- a/tests/mismatched_op.rs +++ b/tests/mismatched_op.rs @@ -1,6 +1,7 @@ use rhai::{Engine, EvalAltResult, RegisterFn}; #[test] +#[cfg(not(feature = "no_stdlib"))] fn test_mismatched_op() { let mut engine = Engine::new(); @@ -26,17 +27,13 @@ fn test_mismatched_op_custom_type() { } let mut engine = Engine::new(); - engine.register_type::(); + engine.register_type_with_name::("TestStruct"); engine.register_fn("new_ts", TestStruct::new); let r = engine.eval::("60 + new_ts()"); match r { - Err(EvalAltResult::ErrorFunctionNotFound(err, _)) - if err == "+ (i64, mismatched_op::test_mismatched_op_custom_type::TestStruct)" => - { - () - } + Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err == "+ (i64, TestStruct)" => (), _ => panic!(), } } diff --git a/tests/power_of.rs b/tests/power_of.rs index ac41a611..78abcb5d 100644 --- a/tests/power_of.rs +++ b/tests/power_of.rs @@ -6,11 +6,15 @@ fn test_power_of() -> Result<(), EvalAltResult> { assert_eq!(engine.eval::("2 ~ 3")?, 8); assert_eq!(engine.eval::("(-2 ~ 3)")?, -8); - 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); + + #[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); + } Ok(()) } @@ -21,14 +25,18 @@ fn test_power_of_equals() -> Result<(), EvalAltResult> { 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.2; x ~= 3.3; x")?, - 13.489468760533386_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.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); + + #[cfg(not(feature = "no_float"))] + { + assert_eq!( + engine.eval::("let x = 2.2; x ~= 3.3; x")?, + 13.489468760533386_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.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); + } Ok(()) } diff --git a/tests/string.rs b/tests/string.rs index 2032154b..5d63b88f 100644 --- a/tests/string.rs +++ b/tests/string.rs @@ -12,10 +12,20 @@ fn test_string() -> Result<(), EvalAltResult> { engine.eval::(r#""Test string: \x58""#)?, "Test string: X".to_string() ); + assert_eq!( engine.eval::(r#""foo" + "bar""#)?, "foobar".to_string() ); + + #[cfg(not(feature = "no_stdlib"))] + assert_eq!( + engine.eval::(r#""foo" + 123"#)?, + "foo123".to_string() + ); + + #[cfg(not(feature = "no_float"))] + #[cfg(not(feature = "no_stdlib"))] assert_eq!( engine.eval::(r#""foo" + 123.4556"#)?, "foo123.4556".to_string() diff --git a/tests/types.rs b/tests/types.rs index 1cf303ad..ed3ff2ea 100644 --- a/tests/types.rs +++ b/tests/types.rs @@ -5,11 +5,17 @@ fn test_type_of() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); assert_eq!(engine.eval::("type_of(60 + 5)")?, "i64"); + + #[cfg(not(feature = "no_float"))] assert_eq!(engine.eval::("type_of(1.0 + 2.0)")?, "f64"); + + #[cfg(not(feature = "no_index"))] + #[cfg(not(feature = "no_float"))] assert_eq!( engine.eval::(r#"type_of([1.0, 2, "hello"])"#)?, "array" ); + assert_eq!(engine.eval::(r#"type_of("hello")"#)?, "string"); assert_eq!(engine.eval::("let x = 123; x.type_of()")?, "i64");