Fix max call depth and add test.

This commit is contained in:
Stephen Chung
2020-04-07 23:13:47 +08:00
parent 9f3646d9ec
commit b74c85f04c
2 changed files with 42 additions and 1 deletions

29
tests/stack.rs Normal file
View File

@@ -0,0 +1,29 @@
use rhai::{Engine, EvalAltResult};
#[test]
fn test_stack_overflow() -> Result<(), EvalAltResult> {
let engine = Engine::new();
assert_eq!(
engine.eval::<i64>(
r"
fn foo(n) { if n == 0 { 0 } else { n + foo(n-1) } }
foo(38)
",
)?,
741
);
match engine.eval::<()>(
r"
fn foo(n) { if n == 0 { 0 } else { n + foo(n-1) } }
foo(1000)
",
) {
Ok(_) => panic!("should be stack overflow"),
Err(EvalAltResult::ErrorStackOverflow(_)) => (),
Err(_) => panic!("should be stack overflow"),
}
Ok(())
}