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

only_i32/i64: other integer types can still be received from function calls #361

Closed SWW13 closed 3 years ago

SWW13 commented 3 years ago

The documentation for only_i64 mentions that other integer type are disabled:

sets the system integer type (INT) to i64 and disable all other integer types

But they can still be received from Rust functions (and also passed into Rust functions). Not sure if this is unintended or just a documentation issue.

use rhai::RegisterFn;

const SCRIPT: &'static str = r###"
let val = get();
print("val = " + val);
set(val);
print("type_of(val) = " + type_of(val));
"###;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut engine = rhai::Engine::new();
    engine.register_fn("get", move || 42u32);
    engine.register_fn("set", move |arg: u32| println!("set({:?})", arg));

    engine.eval(SCRIPT)?;

    Ok(())
}

Output:

val = 42
set(42)
type_of(val) = u32
schungx commented 3 years ago

Probably documentation issue.

Any Rust type can be used under any feature flag combination. The Rhai language has no types.

only_i64 only means that operators and functions for other numeric types are not built-in. You may build them yourself, though.

For example:

not only_i64: u8 + u8 ==> u8 result

only_i64: u8 + u8 ==> error: function not found for + (u8, u8)

And don't forget it is type_of instead of typeof... You're missing an underscore here. Not JavaScript.

SWW13 commented 3 years ago

Probably documentation issue.

I suspected that. Just wanted to make sure and have an issue so the documentation can be extended eventually.

And don't forget it is type_of instead of typeof... You're missing an underscore here. Not JavaScript.

Good catch, fixed that.

schungx commented 3 years ago

I suspected that. Just wanted to make sure and have an issue so the documentation can be extended eventually.

I'll add the appropriate warning into the documentation.

schungx commented 3 years ago

Closing this for now. Please feel free to reopen if the issue is not resolved.