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

Using a function from an imported module consumes(?) arguments. #222

Closed DTibbs closed 4 years ago

DTibbs commented 4 years ago

I ran into this issue in a bit more complex environment, but was able to reproduce using the examples from the repo as of v0.18.1.

Steps to reproduce:

  1. Create a script import.rhai in the ./scripts dir
  2. Put the following in import.rhai:
    import "scripts/function_decl2" as util;
    let c = 5;
    print("c is " + c);
    let d = util::addme(c, 1);
    print("d is " + d);
    print("c is now: '" + c + "'");
    print("If c is not 5, util::addme is not a 'pure function' according to the docs?");
  3. Run the example script w/ the rhai_runner example with: cargo run --example rhai_runner -- scripts/import.rhai
  4. Observe output:
    addme(a, 4) should be 46:
    46
    a should still be 3:
    3
    c is 5
    d is 43
    c is now: ''
    If c is not 5, util::addme is not a 'pure function' according to the docs?

Expected result:

...
c is now: '5'
...

I checked previous versions, and this worked as expected in v0.16.1. v0.17.0 appears to be when this behavior was introduced.

DTibbs commented 4 years ago

Slightly more testing, and this appears to only happen to the first argument passed to an imported function: I tested the following in tests/modules.rs's test_module_resolver on master

    assert_eq!(
        engine.eval::<INT>(
            r#"
                import "hello" as h;
                let foo = 42;
                let bar = -1;
                //let foobar = h::sum(0, foo, bar, 1); // <-- this one works
                let foobar = h::sum(foo, 0, bar, 1); // <-- this one turns 'foo' into '()'
                foo
            "#
        )?,
        42
    );
schungx commented 4 years ago

Thanks for picking this up.

This seems to be a bug.

schungx commented 4 years ago

If you pull from this repo, the bug has been fixed. Please test.

Thanks for detecting this.

If you find no more problems, I'll push out a 0.18.2 to crates.io.

DTibbs commented 4 years ago

👍 Fix looks good.

Tested above mentioned steps, and in my project's use-case. No longer see the issue.

Thanks for the quick turnaround! Anxiously awaiting 0.18.2 on crates.io

schungx commented 4 years ago

0.18.2 is uploaded to crates.io.