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

`ExprTooDeep` error #678

Closed SeokminHong closed 1 year ago

SeokminHong commented 1 year ago

Hi again.

I tried a simple Rhai code on Playground:

let values = [1, 2, 3, 4, 5];

fn template(a, b) {
    `${a}-${b.name}`
}

values
    .map(|v| `${template("simple", #{name: v})}`)

The above code returns ["simple-1", "simple-2", "simple-3", "simple-4", "simple-5"].

However, when I tried to run on my code, it returns compile error:

use rhai::Engine;

fn main() {
    let engine = Engine::new();
    let c = engine.compile(
        r#"
let values = [1, 2, 3, 4, 5];

fn template(a, b) {
    `${a}-${b.name}`
}

values.map(|v| `${template("simple", #{name: v})}`)
"#,
    );
    println!("{:?}", c);
}
Err(ParseError(ExprTooDeep, 8:38))

This is my environment:

Could you check this error?

schungx commented 1 year ago

check out: https://rhai.rs/book/safety/max-stmt-depth.html

I suppose you're running on debug build?

SeokminHong commented 1 year ago

Yes, I think that's the problem. Thank you!

However, why does the closure inside the string interpolation takes too many depths? When I tested, it took 19 depth, even though it looked very simple.

schungx commented 1 year ago

However, why does the closure inside the string interpolation takes too many depths? When I tested, it took 19 depth, even though it looked very simple.

That's a good question... I'll look into it...

schungx commented 1 year ago

Right now, it is simplistically counting one function call level in Rhai's Rust code base to be one level, so it is quite arbitrary...