2020-08-03 02:49:24 +02:00
|
|
|
use rhai::plugin::*;
|
2020-08-02 09:39:16 +02:00
|
|
|
use rhai::{Engine, EvalAltResult, INT};
|
|
|
|
|
|
|
|
#[export_module]
|
2020-08-02 12:53:25 +02:00
|
|
|
mod special_array_package {
|
2020-08-02 09:39:16 +02:00
|
|
|
use rhai::{Array, INT};
|
|
|
|
|
|
|
|
pub fn len(array: &mut Array, mul: INT) -> INT {
|
|
|
|
(array.len() as INT) * mul
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-02 12:53:25 +02:00
|
|
|
#[export_fn]
|
|
|
|
fn make_greeting(n: INT) -> String {
|
|
|
|
format!("{} {}", n, if n > 1 { "kitties" } else { "kitty" }).into()
|
|
|
|
}
|
|
|
|
|
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-02 12:53:25 +02:00
|
|
|
let mut m = exported_module!(special_array_package);
|
|
|
|
register_exported_fn!(m, "greet", make_greeting);
|
2020-08-02 09:39:16 +02:00
|
|
|
|
2020-08-02 12:53:25 +02:00
|
|
|
engine.load_package(m);
|
2020-08-02 09:39:16 +02:00
|
|
|
|
2020-08-02 12:53:25 +02:00
|
|
|
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; len(a, 2)")?, 6);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<String>("let a = [1, 2, 3]; greet(len(a, 2))")?,
|
|
|
|
"6 kitties"
|
|
|
|
);
|
2020-08-02 09:39:16 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|