2020-08-09 18:21:26 +02:00
|
|
|
#![cfg(not(any(feature = "no_index", feature = "no_module")))]
|
|
|
|
|
|
|
|
use rhai::plugin::*;
|
|
|
|
use rhai::{Engine, EvalAltResult, INT, Module};
|
|
|
|
|
2020-08-09 19:13:02 +02:00
|
|
|
pub fn add_generic<T: std::ops::Add<Output = T>>(x: T, y: T) -> T {
|
|
|
|
x + y
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mul_generic<T: std::ops::Mul<Output = T>>(x: T, y: T) -> T {
|
|
|
|
x * y
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! generate_ops {
|
|
|
|
($op_name:ident, $op_fn:ident, $($type_names:ident),+) => {
|
|
|
|
pub mod $op_name {
|
|
|
|
$(
|
|
|
|
pub mod $type_names {
|
|
|
|
use rhai::plugin::*;
|
|
|
|
use super::super::$op_fn;
|
|
|
|
#[export_fn]
|
|
|
|
pub fn op(x: $type_names, y: $type_names) -> $type_names {
|
|
|
|
$op_fn(x, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
2020-08-09 18:21:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 19:13:02 +02:00
|
|
|
macro_rules! register_in_bulk {
|
|
|
|
($mod_name:ident, $op_name:ident, $($type_names:ident),+) => {
|
2020-08-09 18:21:26 +02:00
|
|
|
$(
|
|
|
|
{
|
|
|
|
let type_str = stringify!($type_names);
|
2020-08-13 06:40:50 +02:00
|
|
|
set_exported_fn!($mod_name,
|
|
|
|
format!(concat!(stringify!($op_name), "_{}"), type_str),
|
|
|
|
crate::$op_name::$type_names::op);
|
2020-08-09 18:21:26 +02:00
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 19:13:02 +02:00
|
|
|
generate_ops!(add, add_generic, i8, i16, i32, i64);
|
|
|
|
generate_ops!(mul, mul_generic, i8, i16, i32, i64);
|
2020-08-09 18:21:26 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-08-09 19:13:02 +02:00
|
|
|
fn test_generated_ops() -> Result<(), Box<EvalAltResult>> {
|
2020-08-09 18:21:26 +02:00
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
let mut m = Module::new();
|
2020-08-09 19:13:02 +02:00
|
|
|
register_in_bulk!(m, add, i8, i16, i32, i64);
|
|
|
|
register_in_bulk!(m, mul, i8, i16, i32, i64);
|
2020-08-09 18:21:26 +02:00
|
|
|
|
|
|
|
engine.load_package(m);
|
|
|
|
|
2020-08-09 18:47:11 +02:00
|
|
|
#[cfg(feature = "only_i32")]
|
|
|
|
assert_eq!(engine.eval::<INT>("let a = 0; add_i32(a, 1)")?, 1);
|
|
|
|
#[cfg(not(feature = "only_i32"))]
|
2020-08-09 18:21:26 +02:00
|
|
|
assert_eq!(engine.eval::<INT>("let a = 0; add_i64(a, 1)")?, 1);
|
|
|
|
|
2020-08-09 19:13:02 +02:00
|
|
|
#[cfg(feature = "only_i32")]
|
|
|
|
assert_eq!(engine.eval::<INT>("let a = 1; mul_i32(a, 2)")?, 2);
|
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
assert_eq!(engine.eval::<INT>("let a = 1; mul_i64(a, 2)")?, 2);
|
|
|
|
|
2020-08-09 18:21:26 +02:00
|
|
|
Ok(())
|
|
|
|
}
|