rhai/tests/stack.rs

126 lines
8.8 KiB
Rust
Raw Normal View History

2020-05-23 18:29:06 +02:00
#![cfg(not(feature = "unchecked"))]
2020-08-08 10:03:14 +02:00
use rhai::{Engine, EvalAltResult, ParseErrorType, INT};
2020-04-07 17:13:47 +02:00
#[test]
2020-05-23 18:29:06 +02:00
#[cfg(not(feature = "no_function"))]
fn test_stack_overflow_fn_calls() -> Result<(), Box<EvalAltResult>> {
2020-04-07 17:13:47 +02:00
let engine = Engine::new();
assert_eq!(
2020-08-05 18:24:25 +02:00
engine.eval::<INT>(
2021-04-20 06:01:35 +02:00
"
fn foo(n) { if n <= 1 { 0 } else { n + foo(n-1) } }
2020-12-29 05:29:45 +01:00
foo(6)
",
2020-04-07 17:13:47 +02:00
)?,
2020-12-29 05:29:45 +01:00
20
2020-04-07 17:13:47 +02:00
);
2020-12-29 05:29:45 +01:00
let max = engine.max_call_levels();
#[cfg(not(feature = "unchecked"))]
2020-05-19 04:08:27 +02:00
assert!(matches!(
*engine
2022-01-10 06:26:33 +01:00
.run(&format!(
2021-04-20 06:01:35 +02:00
"
2020-12-29 05:29:45 +01:00
fn foo(n) {{ if n == 0 {{ 0 }} else {{ n + foo(n-1) }} }}
foo({})
",
max + 1
))
2023-04-10 17:23:59 +02:00
.unwrap_err(),
2022-02-08 02:46:14 +01:00
EvalAltResult::ErrorStackOverflow(..)
2020-05-19 04:08:27 +02:00
));
2020-04-07 17:13:47 +02:00
Ok(())
}
#[test]
fn test_stack_overflow_parsing() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
2020-08-08 10:03:14 +02:00
assert_eq!(
*engine.compile(
"
let a = (1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1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022-12-08 10:18:40 +01:00
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1
2021-10-18 10:40:54 +02:00
",
)?;
#[cfg(debug_assertions)]
assert_eq!(
*engine
.compile(
"
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
2022-12-08 10:18:40 +01:00
1 + 2
2021-10-18 10:40:54 +02:00
"
)
2023-04-10 17:23:59 +02:00
.unwrap_err()
.err_type(),
2021-10-18 10:40:54 +02:00
ParseErrorType::ExprTooDeep
);
2020-10-18 16:24:01 +02:00
engine.set_max_expr_depths(
100,
2020-10-18 16:38:15 +02:00
#[cfg(not(feature = "no_function"))]
2020-10-18 16:24:01 +02:00
6,
);
engine.compile(
2021-04-20 06:01:35 +02:00
"
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 0 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
2022-12-08 10:18:40 +01:00
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
2021-03-27 11:08:34 +01:00
",
)?;
2020-08-08 10:03:14 +02:00
assert_eq!(
*engine
.compile(
2021-04-20 06:01:35 +02:00
"
2021-03-27 11:08:34 +01:00
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 +
2022-12-08 10:18:40 +01:00
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0
2021-03-27 11:08:34 +01:00
"
2020-08-08 10:03:14 +02:00
)
2023-04-10 17:23:59 +02:00
.unwrap_err()
.err_type(),
2020-08-08 10:03:14 +02:00
ParseErrorType::ExprTooDeep
);
2020-05-23 18:29:06 +02:00
#[cfg(not(feature = "no_function"))]
engine.compile("fn abc(x) { x + 1 }")?;
Ok(())
}