rhaiscript / rhai

Rhai - An embedded scripting language for Rust.
https://crates.io/crates/rhai
Apache License 2.0
3.73k stars 175 forks source link

A ExprTooDeep error is returned for what seems to be a simple function #447

Closed Hodkinson closed 3 years ago

Hodkinson commented 3 years ago

It works fine in the playground, but when I run this test locally I see

Err(ParseError(ExprTooDeep, 5:39))

#[test]
fn complexity_issue() {
    let script = r#"
        fn my_function(env,input){
            if input.arg == "SOMETHING" {
                let args = #{
                    "key": env.lookup("name")
                };
            }
        }
    "#;

    let mut engine = Engine::new();

    let result = engine.compile(script);
    println!("{:?}", result);
    assert!(result.is_ok());
}
schungx commented 3 years ago

Yes, in debug mode, Rust stack usage is heavy and functions cannot be nested deeply without overflowing the stack. That's why, under debug, the max expression nesting limit is set to a low number by default

You can relax that via Engine::set_max_expr_depths

See also #303

Hodkinson commented 3 years ago

I see, thank you, doing engine.set_max_expr_depths(0, 0); fixed it for me. Is that (effectively) what is used by default in release?

schungx commented 3 years ago

Release builds have a much larger limit, but by default there is a limit.

https://rhai.rs/book/safety/max-stmt-depth.html has the numbers.