rhaiscript / rhai

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

on_debug callback not presenting source #884

Closed markeel closed 3 months ago

markeel commented 4 months ago

I have started integrating rhai into my application and all is going well, but I wanted to catch debug statements, but the second parameter should be the source I specified after compiling the AST with "set_source()"

I've checked and a call to the AST after with ".source()" returns what I set, but the on_debug() call does not include the source parameter. It is always None.

This feels like a bug, but it's possible I'm not setting something up properly. The "Rhai Book" says the only thing necessary is to call set_source(), and because ".source()" is returning the expected value, I think I've done that.

Here is the registering of the on_debug statement

        engine.on_debug(|msg, src, pos| {
            eprintln!("{src:?}:{pos}:{msg}");
        });

Here is where I create it.

    ...
       match self.rhai.engine.compile(script) {
            Ok(mut ast) => {
                let ast_src = src.as_ref().unwrap();
                let ast_id = format!("{}:{}", ast_src.source, ast_src.source_position);
                ast.set_source(ast_id.as_str());
                println!("get_ast source: {:?}", ast.source());
                let shared = rhai::Shared::new(ast);
                println!("get shared source: {:?}", shared.source());
    ...

Here is where I invoke it.

    ...
        let ast = &func.unwrap().ast;
        println!("source of ast: {:?}", ast.source());
        let name = &func.unwrap().name;

        match self.rhai.engine.call_fn_with_options::<rhai::Dynamic>(options, &mut scope, ast, name, args) {
        ...

Here is the rhai script I am running:

            fn call_me(p) 
                debug("called call_me");
                return `${p.value}_id`
            }

What I get when I run it (it gets run twice in my test suite):

get_ast source: Some("test:4")
get shared source: Some("test:4")
source of ast: Some("test:4")
None:line 3, position 5:"called call_me"
source of ast: Some("test:4")
None:line 3, position 5:"called call_me"
schungx commented 4 months ago

Hhmmm... May be an oversight.

The souce is primarily used when loading separate modules for an easy way to track where functions come from.

It probably should be set also when running an AST. I'll take a look.

schungx commented 4 months ago

Found something strange. The test here:

https://github.com/rhaiscript/rhai/blob/main/tests/print.rs#L38

should test against setting source in the AST and getting it back in debug statements.

EDIT: Ah, I see you're using call_fn.

schungx commented 4 months ago

You can check out the new drop https://github.com/rhaiscript/rhai/pull/885

This should fix the issue.

markeel commented 4 months ago

Awesome, I'll check it out.

schungx commented 3 months ago

Closing this for now. Feel free to reopen if there are further issues.