rhaiscript / rhai

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

Fibonacci example script overflows #414

Closed Rdna123 closed 3 years ago

Rdna123 commented 3 years ago
cargo run --bin rhai-run .\scripts\fibonacci.rhai
    Finished dev [unoptimized + debuginfo] target(s) in 0.10s
     Running `target\debug\rhai-run.exe .\scripts\fibonacci.rhai`
Running Fibonacci(28) x 5 times...
Ready... Go!

thread 'main' has overflowed its stack
error: process didn't exit successfully: `target\debug\rhai-run.exe .\scripts\fibonacci.rhai` (exit code: 0xc00000fd, STATUS_STACK_OVERFLOW)
schungx commented 3 years ago

Different O/S and architecture have different setups for the stack, and it is hard to predict how many levels of function calls will overflow the stack. Rhai has a set of defaults (different for debug vs release builds) that are chosen to be reasonable, but may not be accurate for all systems.

Rust's debug build supports only a small number of recursive function calls before overflowing the stack.

You can try compiling with release to do much better. It definitely will not overflow with only 28 levels.

And you can use the Engine::set_max_call_levels API to restrict the number of recursive function calls. Choose a number that matches your system's stack behavior.

Again, as I mentioned, debug builds overflow much much quicker than release build, so you need to set this limit to a low number for debug builds.

Rdna123 commented 3 years ago

I did not know that, thank you!

schungx commented 3 years ago

Probably need to put something in the documentation...