2021-11-05 16:22:05 +01:00
|
|
|
use rhai::{Dynamic, Engine, EvalAltResult, ImmutableString, NativeCallContext, INT};
|
2021-01-24 14:33:05 +01:00
|
|
|
use std::any::TypeId;
|
|
|
|
|
2021-04-03 05:12:35 +02:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2021-01-24 14:33:05 +01:00
|
|
|
#[test]
|
|
|
|
fn test_native_context() -> Result<(), Box<EvalAltResult>> {
|
2021-04-03 05:12:35 +02:00
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine.set_max_modules(40);
|
|
|
|
engine.register_fn("test", |context: NativeCallContext, x: INT| {
|
|
|
|
context.engine().max_modules() as INT + x
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(engine.eval::<INT>("test(2)")?, 42);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_native_context_fn_name() -> Result<(), Box<EvalAltResult>> {
|
2021-03-03 15:49:29 +01:00
|
|
|
fn add_double(
|
|
|
|
context: NativeCallContext,
|
|
|
|
args: &mut [&mut Dynamic],
|
|
|
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
2021-01-24 14:33:05 +01:00
|
|
|
let x = args[0].as_int().unwrap();
|
|
|
|
let y = args[1].as_int().unwrap();
|
|
|
|
Ok(format!("{}_{}", context.fn_name(), x + 2 * y).into())
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine
|
|
|
|
.register_raw_fn(
|
|
|
|
"add_double",
|
|
|
|
&[TypeId::of::<INT>(), TypeId::of::<INT>()],
|
|
|
|
add_double,
|
|
|
|
)
|
|
|
|
.register_raw_fn(
|
2021-04-03 05:12:35 +02:00
|
|
|
"append_x2",
|
2021-01-24 14:33:05 +01:00
|
|
|
&[TypeId::of::<INT>(), TypeId::of::<INT>()],
|
|
|
|
add_double,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(engine.eval::<String>("add_double(40, 1)")?, "add_double_42");
|
|
|
|
|
2021-04-03 05:12:35 +02:00
|
|
|
assert_eq!(engine.eval::<String>("append_x2(40, 1)")?, "append_x2_42");
|
2021-01-24 14:33:05 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-05 16:22:05 +01:00
|
|
|
|
|
|
|
#[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 {
|
2022-08-11 13:01:23 +02:00
|
|
|
format!("{s1}***{s2}").into()
|
2021-11-05 16:22:05 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.register_fn("+", |s1: ImmutableString, _: ()| -> ImmutableString {
|
2022-08-11 13:01:23 +02:00
|
|
|
format!("{s1} Foo!").into()
|
2021-11-05 16:22:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<String>(r#"let x = "hello"; let y = "world"; x + y"#)?,
|
2022-09-03 09:15:42 +02:00
|
|
|
"hello***world"
|
2021-11-05 16:22:05 +01:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<String>(r#"let x = "hello"; let y = (); x + y"#)?,
|
2022-09-03 09:15:42 +02:00
|
|
|
"hello Foo!"
|
|
|
|
);
|
|
|
|
|
|
|
|
engine.set_fast_operators(true);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<String>(r#"let x = "hello"; let y = "world"; x + y"#)?,
|
|
|
|
"helloworld"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<String>(r#"let x = "hello"; let y = (); x + y"#)?,
|
|
|
|
"hello"
|
2021-11-05 16:22:05 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|