Bring more functions into modules in plugins via rhai_fn(name) attribute.

This commit is contained in:
Stephen Chung
2020-08-16 23:41:59 +08:00
parent 31a05f8c48
commit e75d91e9bf
9 changed files with 210 additions and 244 deletions

View File

@@ -7,9 +7,14 @@ use rhai::{Engine, EvalAltResult, INT};
mod special_array_package {
use rhai::{Array, INT};
#[rhai_fn(name = "test")]
pub fn len(array: &mut Array, mul: INT) -> INT {
(array.len() as INT) * mul
}
#[rhai_fn(name = "+")]
pub fn funky_add(x: INT, y: INT) -> INT {
x / 2 + y * 2
}
}
macro_rules! gen_unary_functions {
@@ -18,7 +23,7 @@ macro_rules! gen_unary_functions {
pub mod $arg_type {
use super::super::*;
#[export_fn]
#[export_fn(name="test")]
pub fn single(x: $arg_type) -> $return_type {
super::super::$op_fn(x)
}
@@ -48,9 +53,10 @@ fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
reg_functions!(engine += greet::single(INT, bool, char));
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; len(a, 2)")?, 6);
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; test(a, 2)")?, 6);
assert_eq!(engine.eval::<INT>("2 + 2")?, 5);
assert_eq!(
engine.eval::<String>("let a = [1, 2, 3]; greet(len(a, 2))")?,
engine.eval::<String>("let a = [1, 2, 3]; greet(test(a, 2))")?,
"6 kitties"
);

View File

@@ -39,5 +39,14 @@ fn test_timestamp() -> Result<(), Box<EvalAltResult>> {
)? < 10
);
assert!(engine.eval::<bool>(
r"
let time1 = timestamp();
for x in range(0, 10000) {}
let time2 = timestamp();
time1 <= time2
"
)?);
Ok(())
}