Merge branch 'v1.1-fixes'

This commit is contained in:
Stephen Chung
2021-11-05 23:26:00 +08:00
21 changed files with 144 additions and 43 deletions

View File

@@ -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(())
}