Add no_function feature to disable script-defined functions.

This commit is contained in:
Stephen Chung
2020-03-11 13:28:12 +08:00
parent 047f064cd1
commit 7c4d22d98a
10 changed files with 140 additions and 86 deletions

View File

@@ -39,10 +39,9 @@ fn test_bool_op_short_circuit() -> Result<(), EvalAltResult> {
assert_eq!(
engine.eval::<bool>(
r"
fn this() { true }
fn that() { 9/0 }
let this = true;
this() || that();
this || { throw; };
"
)?,
true
@@ -51,10 +50,9 @@ fn test_bool_op_short_circuit() -> Result<(), EvalAltResult> {
assert_eq!(
engine.eval::<bool>(
r"
fn this() { false }
fn that() { 9/0 }
let this = false;
this() && that();
this && { throw; };
"
)?,
false
@@ -72,10 +70,9 @@ fn test_bool_op_no_short_circuit1() {
engine
.eval::<bool>(
r"
fn this() { false }
fn that() { 9/0 }
let this = true;
this() | that();
this | { throw; }
"
)
.unwrap(),
@@ -92,10 +89,9 @@ fn test_bool_op_no_short_circuit2() {
engine
.eval::<bool>(
r"
fn this() { false }
fn that() { 9/0 }
let this = false;
this() & that();
this & { throw; }
"
)
.unwrap(),

View File

@@ -1,4 +1,5 @@
#![cfg(not(feature = "no_stdlib"))]
#![cfg(not(feature = "no_function"))]
use rhai::{Engine, EvalAltResult, INT};
#[test]

View File

@@ -1,3 +1,5 @@
#![cfg(not(feature = "no_function"))]
use rhai::{Engine, EvalAltResult, INT};
#[test]

View File

@@ -9,6 +9,7 @@ fn test_not() -> Result<(), EvalAltResult> {
false
);
#[cfg(not(feature = "no_function"))]
assert_eq!(engine.eval::<bool>("fn not(x) { !x } not(false)")?, true);
// TODO - do we allow stacking unary operators directly? e.g '!!!!!!!true'

View File

@@ -5,7 +5,10 @@ fn test_unary_minus() -> Result<(), EvalAltResult> {
let mut engine = Engine::new();
assert_eq!(engine.eval::<INT>("let x = -5; x")?, -5);
#[cfg(not(feature = "no_function"))]
assert_eq!(engine.eval::<INT>("fn neg(x) { -x } neg(5)")?, -5);
assert_eq!(engine.eval::<INT>("5 - -+++--+-5")?, 0);
Ok(())