rhai: Frame before call to rust a function missing

HEAVY EDIT after I understood a crucial detail.

The frame before calling to a rust function is missing. I can inject the frame in the rust function called, but there are two problems:

  • Source is not set in NativeCallContext
  • When I have a method of a struct it is difficult to get NativeCallContext

(I was able to get a NativeCallContext for methods using a wrapper written in rhai.)

My wish: To have the call to my_err automatically in the source chain, so I could avoid both problems. However, I suspect I am misunderstanding something.

use rhai::{Engine, EvalAltResult, FuncRegistration, NativeCallContext, Position};

fn my_err(ctx: NativeCallContext) -> Result<(), Box<EvalAltResult>> {
    // Name is set
    println!("fn_name: {:?}", ctx.fn_name());
    // Source is missing
    println!("source: {:?}", ctx.source());
    // Position set
    println!("postiion: {:?}", ctx.position());
    // I'd like to emulate the rhai error pattern for the rust function, so users can send me traces,
    // of course here we did not call rust_function
    let src = EvalAltResult::ErrorInFunctionCall(
        "rust_function".into(),
        file!().into(),
        "a rust error happened".into(),
        Position::new(line!() as u16, 0),
    );
    EvalAltResult::ErrorInFunctionCall(
        ctx.fn_name().into(),
        ctx.source().unwrap_or("").into(),
        src.into(),
        ctx.position(),
    )
    .into()
}

fn main() {
    let mut engine = Engine::new();
    engine.set_optimization_level(rhai::OptimizationLevel::None);
    FuncRegistration::new("my_err")
        .in_global_namespace()
        .register_into_engine(&mut engine, my_err);
    let mut ast = engine
        .compile(
            r#"
fn test_inner() {
    my_err();
}

fn test_other() {
    test_inner();
}

test_other();
    "#,
        )
        .unwrap();
    ast.set_source("inline");
    let err = engine.run_ast(&ast);
    println!("err: {}", err.err().unwrap());
}

Output

fn_name: "my_err"
source: None
postiion: 3:5
err: Runtime error: a rust error happened
in call to function 'rust_function' @ 'src/main.rs' (line 17, position 0)
in call to function 'my_err' (line 3, position 5)
in call to function 'test_inner' @ 'inline' (line 7, position 5)
in call to function 'test_other' @ 'inline' (line 10, position 1)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19

Most upvoted comments

in call to function ‘second_call’ (from ‘second.rhai’) @ ‘first.rhai’ (line 3, position 13)

The error message is clear and it adds even more helpful information than I wished for.

About the API change. I think for developers it is enough to have both source fields and clear documentation what the fields contain. So the breaking change is not needed. Of course in the future when you make a breaking release: call_position, call_source, fn_name, fn_source would be nice. fn_source just because the fn_name field already exists.

I am very sorry for the chaos, I stop polluting that other issue. I only wanted to make that other person aware that there is a discussion already.

We have three separate issues and you have to think about all of them separately. I discovered them all at once. Only one issue is really problematic, the other issues are semantic and could be seen different.

Issue one: source and position not matching in NativeCallContext and ErrorInFunctionCall (problematic)

Traces produced by rhai or if one inspects NativeCallContext or ErrorInFunctionCall do not match source/position: If you go to source at that position, you are at the wrong place, because source is wrong, position is correct. You need to have multiple files for this to be a problem. With a single file the problem is not visible. It has nothing to do with rust functions. You just need multiple modules/files.

Issue two: The ErrorInFunctionCall chain does not include calls to a rust function

By default in a trace you do not get a:

in call to function 'rust_fucntion' (line ?, position ?)

line by default. I solved that by inserting a ErrorInFunctionCall in error-handling in the rust function. This is the issue that you tend to refer, but I learned that is not a big problem, when I understood issue one. I even think, it might be perfectly reasonable that the rust function has to return a correct: ErrorInFunctionCall. This might be a documentation issue. The only problem: since currently source is not correct, I cannot do “the workaround” without the internal-feature.

Issue three: The initial source is empty

Since source is always one step behind, of course the initial source has to be empty. This issue would be fixed, by either fixing source or adding call_source.

About all issues

Depending on how issue one is fixed, some of the issues might go away. They are separate but influence each other. That is why I wanted to make you aware of all these issues, while only issue one is problematic.