quad-compat-rhai 1.4.0

Embedded scripting for Rust
Documentation
#![cfg(not(feature = "unchecked"))]
use quad_compat_rhai::{Engine, EvalAltResult, ParseErrorType, INT};

#[test]
#[cfg(not(feature = "no_function"))]
fn test_stack_overflow_fn_calls() -> Result<(), Box<EvalAltResult>> {
    let engine = Engine::new();

    assert_eq!(
        engine.eval::<INT>(
            "
                fn foo(n) { if n <= 1 { 0 } else { n + foo(n-1) } }
                foo(6)
            ",
        )?,
        20
    );

    let max = engine.max_call_levels();

    #[cfg(not(feature = "unchecked"))]
    assert!(matches!(
        *engine
            .eval::<()>(&format!(
                "
                    fn foo(n) {{ if n == 0 {{ 0 }} else {{ n + foo(n-1) }} }}
                    foo({})
                ",
                max + 1
            ))
            .expect_err("should error"),
        EvalAltResult::ErrorStackOverflow(_)
    ));

    Ok(())
}

#[test]
fn test_stack_overflow_parsing() -> Result<(), Box<EvalAltResult>> {
    let mut engine = Engine::new();

    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+(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+(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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+(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+(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+(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+(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))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
        ").expect_err("should error").0,
         ParseErrorType::ExprTooDeep
    );

    engine.compile("1 + 2")?;

    #[cfg(debug_assertions)]
    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
        ",
    )?;

    #[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 +
                    1
                "
            )
            .expect_err("should error")
            .0,
        ParseErrorType::ExprTooDeep
    );

    engine.set_max_expr_depths(
        100,
        #[cfg(not(feature = "no_function"))]
        6,
    );

    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 +
            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 +
            1 + 2 + 3 + 4 + 5 + 6 + 7 + 8
        ",
    )?;

    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 +
                    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
                "
            )
            .expect_err("should error")
            .0,
        ParseErrorType::ExprTooDeep
    );

    #[cfg(not(feature = "no_function"))]
    engine.compile("fn abc(x) { x + 1 }")?;

    Ok(())
}