2020-08-07 02:41:06 +02:00
|
|
|
#![cfg(not(any(feature = "no_index", feature = "no_module")))]
|
|
|
|
|
2020-09-13 16:12:11 +02:00
|
|
|
use rhai::module_resolvers::StaticModuleResolver;
|
2020-08-03 02:49:24 +02:00
|
|
|
use rhai::plugin::*;
|
2020-08-02 09:39:16 +02:00
|
|
|
use rhai::{Engine, EvalAltResult, INT};
|
|
|
|
|
2020-08-20 16:11:41 +02:00
|
|
|
mod test {
|
|
|
|
use rhai::plugin::*;
|
2020-08-02 09:39:16 +02:00
|
|
|
|
2020-08-20 16:11:41 +02:00
|
|
|
#[export_module]
|
|
|
|
pub mod special_array_package {
|
|
|
|
use rhai::{Array, INT};
|
|
|
|
|
2020-09-13 16:36:24 +02:00
|
|
|
pub const MYSTIC_NUMBER: INT = 42;
|
2020-09-13 16:12:11 +02:00
|
|
|
|
2020-08-21 15:48:45 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
pub mod feature {
|
2020-09-04 09:42:31 +02:00
|
|
|
use rhai::{Array, Dynamic, EvalAltResult};
|
|
|
|
|
2020-08-21 15:48:45 +02:00
|
|
|
#[rhai_fn(get = "foo", return_raw)]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn foo(array: &mut Array) -> Result<Dynamic, Box<EvalAltResult>> {
|
|
|
|
Ok(array[0].clone())
|
|
|
|
}
|
2020-08-20 16:11:41 +02:00
|
|
|
}
|
2020-08-21 15:48:45 +02:00
|
|
|
|
2020-09-19 12:18:40 +02:00
|
|
|
pub fn hash(_text: String) -> INT {
|
|
|
|
42
|
|
|
|
}
|
|
|
|
|
2020-09-04 05:57:40 +02:00
|
|
|
#[rhai_fn(name = "test", name = "hi")]
|
2020-08-20 16:11:41 +02:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn len(array: &mut Array, mul: INT) -> INT {
|
|
|
|
(array.len() as INT) * mul
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "+")]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn funky_add(x: INT, y: INT) -> INT {
|
|
|
|
x / 2 + y * 2
|
|
|
|
}
|
2020-08-16 17:41:59 +02:00
|
|
|
}
|
2020-08-02 09:39:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-14 07:43:26 +02:00
|
|
|
macro_rules! gen_unary_functions {
|
|
|
|
($op_name:ident = $op_fn:ident ( $($arg_type:ident),+ ) -> $return_type:ident) => {
|
|
|
|
mod $op_name { $(
|
|
|
|
pub mod $arg_type {
|
|
|
|
use super::super::*;
|
|
|
|
|
2020-08-16 17:41:59 +02:00
|
|
|
#[export_fn(name="test")]
|
2020-08-14 07:43:26 +02:00
|
|
|
pub fn single(x: $arg_type) -> $return_type {
|
|
|
|
super::super::$op_fn(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)* }
|
|
|
|
}
|
2020-08-02 12:53:25 +02:00
|
|
|
}
|
|
|
|
|
2020-08-14 07:43:26 +02:00
|
|
|
macro_rules! reg_functions {
|
|
|
|
($mod_name:ident += $op_name:ident :: $func:ident ( $($arg_type:ident),+ )) => {
|
|
|
|
$(register_exported_fn!($mod_name, stringify!($op_name), $op_name::$arg_type::$func);)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_greeting<T: std::fmt::Display>(n: T) -> String {
|
|
|
|
format!("{} kitties", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
gen_unary_functions!(greet = make_greeting(INT, bool, char) -> String);
|
|
|
|
|
2020-08-02 09:39:16 +02:00
|
|
|
#[test]
|
2020-08-02 12:53:25 +02:00
|
|
|
fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
|
2020-08-02 09:39:16 +02:00
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
2020-08-21 15:48:45 +02:00
|
|
|
let mut m = Module::new();
|
2020-09-13 16:12:11 +02:00
|
|
|
combine_with_exported_module!(&mut m, "test", test::special_array_package);
|
2020-08-02 12:53:25 +02:00
|
|
|
engine.load_package(m);
|
2020-08-02 09:39:16 +02:00
|
|
|
|
2020-08-14 07:43:26 +02:00
|
|
|
reg_functions!(engine += greet::single(INT, bool, char));
|
|
|
|
|
2020-08-19 07:39:20 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-08-18 03:25:43 +02:00
|
|
|
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; a.foo")?, 1);
|
2020-08-19 07:39:20 +02:00
|
|
|
|
2020-09-19 12:18:40 +02:00
|
|
|
assert_eq!(engine.eval::<INT>(r#"hash("hello")"#)?, 42);
|
2020-09-04 05:57:40 +02:00
|
|
|
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; test(a, 2)")?, 6);
|
|
|
|
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; hi(a, 2)")?, 6);
|
2020-08-16 17:41:59 +02:00
|
|
|
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; test(a, 2)")?, 6);
|
|
|
|
assert_eq!(engine.eval::<INT>("2 + 2")?, 5);
|
2020-08-02 12:53:25 +02:00
|
|
|
assert_eq!(
|
2020-08-16 17:41:59 +02:00
|
|
|
engine.eval::<String>("let a = [1, 2, 3]; greet(test(a, 2))")?,
|
2020-08-02 12:53:25 +02:00
|
|
|
"6 kitties"
|
|
|
|
);
|
2020-08-02 09:39:16 +02:00
|
|
|
|
2020-09-13 16:12:11 +02:00
|
|
|
let mut resolver = StaticModuleResolver::new();
|
|
|
|
resolver.insert("test", exported_module!(test::special_array_package));
|
|
|
|
|
|
|
|
engine.set_module_resolver(Some(resolver));
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(r#"import "test" as test; test::MYSTIC_NUMBER"#)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
2020-08-02 09:39:16 +02:00
|
|
|
Ok(())
|
|
|
|
}
|