Make sure the global namespace is always searched first.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use rhai::{Dynamic, Engine, EvalAltResult, NativeCallContext, INT};
|
||||
use rhai::{Dynamic, Engine, EvalAltResult, ImmutableString, NativeCallContext, INT};
|
||||
use std::any::TypeId;
|
||||
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
@@ -49,3 +49,41 @@ fn test_native_context_fn_name() -> Result<(), Box<EvalAltResult>> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_native_overload() -> Result<(), Box<EvalAltResult>> {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<String>(r#"let x = "hello, "; let y = "world"; x + y"#)?,
|
||||
"hello, world"
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<String>(r#"let x = "hello"; let y = (); x + y"#)?,
|
||||
"hello"
|
||||
);
|
||||
|
||||
// Overload the `+` operator for strings
|
||||
|
||||
engine
|
||||
.register_fn(
|
||||
"+",
|
||||
|s1: ImmutableString, s2: ImmutableString| -> ImmutableString {
|
||||
format!("{}***{}", s1, s2).into()
|
||||
},
|
||||
)
|
||||
.register_fn("+", |s1: ImmutableString, _: ()| -> ImmutableString {
|
||||
format!("{} Foo!", s1).into()
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<String>(r#"let x = "hello"; let y = "world"; x + y"#)?,
|
||||
"hello***world"
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<String>(r#"let x = "hello"; let y = (); x + y"#)?,
|
||||
"hello Foo!"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user