2020-08-01 18:52:26 +02:00
|
|
|
use rhai::module_resolvers::*;
|
2020-08-14 05:00:01 +02:00
|
|
|
use rhai::plugin::*;
|
2020-08-08 16:31:15 +02:00
|
|
|
use rhai::{Array, Engine, EvalAltResult, Module, RegisterFn, FLOAT};
|
2020-08-01 18:52:26 +02:00
|
|
|
|
|
|
|
pub mod raw_fn {
|
2020-08-03 02:27:19 +02:00
|
|
|
use rhai::plugin::*;
|
2020-08-02 09:39:08 +02:00
|
|
|
use rhai::FLOAT;
|
2020-08-01 18:52:26 +02:00
|
|
|
|
|
|
|
#[export_fn]
|
|
|
|
pub fn distance_function(x1: FLOAT, y1: FLOAT, x2: FLOAT, y2: FLOAT) -> FLOAT {
|
2020-08-02 09:39:08 +02:00
|
|
|
((y2 - y1).abs().powf(2.0) + (x2 - x1).abs().powf(2.0)).sqrt()
|
2020-08-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn raw_fn_test() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
2020-08-02 09:39:08 +02:00
|
|
|
engine.register_fn("get_mystic_number", || 42 as FLOAT);
|
2020-08-01 18:52:26 +02:00
|
|
|
let mut m = Module::new();
|
2020-08-13 06:34:53 +02:00
|
|
|
rhai::set_exported_fn!(
|
2020-08-02 09:39:08 +02:00
|
|
|
m,
|
|
|
|
"euclidean_distance".to_string(),
|
|
|
|
raw_fn::distance_function
|
|
|
|
);
|
2020-08-01 18:52:26 +02:00
|
|
|
let mut r = StaticModuleResolver::new();
|
|
|
|
r.insert("Math::Advanced".to_string(), m);
|
|
|
|
engine.set_module_resolver(Some(r));
|
|
|
|
|
2020-08-02 09:39:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<FLOAT>(
|
|
|
|
r#"import "Math::Advanced" as math;
|
|
|
|
let x = math::euclidean_distance(0.0, 1.0, 0.0, get_mystic_number()); x"#
|
|
|
|
)?,
|
|
|
|
41.0
|
|
|
|
);
|
2020-08-01 18:52:26 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
mod raw_fn_mut {
|
2020-08-03 02:27:19 +02:00
|
|
|
use rhai::plugin::*;
|
2020-08-02 09:39:08 +02:00
|
|
|
use rhai::FLOAT;
|
2020-08-01 18:52:26 +02:00
|
|
|
|
|
|
|
#[export_fn]
|
|
|
|
pub fn add_in_place(f1: &mut FLOAT, f2: FLOAT) {
|
|
|
|
*f1 += f2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn raw_fn_mut_test() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
2020-08-02 09:39:08 +02:00
|
|
|
engine.register_fn("get_mystic_number", || 42 as FLOAT);
|
2020-08-01 18:52:26 +02:00
|
|
|
let mut m = Module::new();
|
2020-08-13 06:34:53 +02:00
|
|
|
rhai::set_exported_fn!(m, "add_in_place", raw_fn_mut::add_in_place);
|
2020-08-01 18:52:26 +02:00
|
|
|
let mut r = StaticModuleResolver::new();
|
|
|
|
r.insert("Math::Advanced".to_string(), m);
|
|
|
|
engine.set_module_resolver(Some(r));
|
|
|
|
|
2020-08-02 09:39:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<FLOAT>(
|
|
|
|
r#"import "Math::Advanced" as math;
|
2020-08-01 18:52:26 +02:00
|
|
|
let x = get_mystic_number();
|
|
|
|
math::add_in_place(x, 1.0);
|
2020-08-02 09:39:08 +02:00
|
|
|
x"#
|
|
|
|
)?,
|
|
|
|
43.0
|
|
|
|
);
|
2020-08-01 18:52:26 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
mod raw_fn_str {
|
2020-08-03 02:27:19 +02:00
|
|
|
use rhai::plugin::*;
|
2020-08-01 18:52:26 +02:00
|
|
|
|
|
|
|
#[export_fn]
|
|
|
|
pub fn write_out_str(message: &str) -> bool {
|
|
|
|
eprintln!("{}", message);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn raw_fn_str_test() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
2020-08-02 09:39:08 +02:00
|
|
|
engine.register_fn("get_mystic_number", || 42 as FLOAT);
|
2020-08-01 18:52:26 +02:00
|
|
|
let mut m = Module::new();
|
2020-08-13 06:34:53 +02:00
|
|
|
rhai::set_exported_fn!(m, "write_out_str", raw_fn_str::write_out_str);
|
2020-08-01 18:52:26 +02:00
|
|
|
let mut r = StaticModuleResolver::new();
|
|
|
|
r.insert("Host::IO".to_string(), m);
|
|
|
|
engine.set_module_resolver(Some(r));
|
|
|
|
|
2020-08-02 09:39:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<bool>(
|
|
|
|
r#"import "Host::IO" as io;
|
2020-08-01 18:52:26 +02:00
|
|
|
let x = io::write_out_str("hello world!");
|
2020-08-02 09:39:08 +02:00
|
|
|
x"#
|
|
|
|
)?,
|
|
|
|
true
|
|
|
|
);
|
2020-08-01 18:52:26 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
mod mut_opaque_ref {
|
2020-08-03 02:27:19 +02:00
|
|
|
use rhai::plugin::*;
|
2020-08-02 09:39:08 +02:00
|
|
|
use rhai::INT;
|
2020-08-01 18:52:26 +02:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct StatusMessage {
|
|
|
|
os_code: Option<INT>,
|
|
|
|
message: String,
|
2020-08-02 09:39:08 +02:00
|
|
|
is_ok: bool,
|
2020-08-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[export_fn]
|
|
|
|
pub fn new_message(is_ok: bool, message: &str) -> StatusMessage {
|
|
|
|
StatusMessage {
|
|
|
|
is_ok,
|
|
|
|
os_code: None,
|
|
|
|
message: message.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[export_fn]
|
|
|
|
pub fn new_os_message(is_ok: bool, os_code: INT) -> StatusMessage {
|
|
|
|
StatusMessage {
|
|
|
|
is_ok,
|
|
|
|
os_code: Some(os_code),
|
|
|
|
message: format!("OS Code {}", os_code),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[export_fn]
|
|
|
|
pub fn write_out_message(message: &mut StatusMessage) -> bool {
|
|
|
|
eprintln!("{}", message.message);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_opaque_ref_test() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
let mut m = Module::new();
|
2020-08-13 06:34:53 +02:00
|
|
|
rhai::set_exported_fn!(m, "new_message", mut_opaque_ref::new_message);
|
|
|
|
rhai::set_exported_fn!(m, "new_os_message", mut_opaque_ref::new_os_message);
|
|
|
|
rhai::set_exported_fn!(m, "write_out_message", mut_opaque_ref::write_out_message);
|
2020-08-01 18:52:26 +02:00
|
|
|
let mut r = StaticModuleResolver::new();
|
|
|
|
r.insert("Host::Msg".to_string(), m);
|
|
|
|
engine.set_module_resolver(Some(r));
|
|
|
|
|
2020-08-02 09:39:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<bool>(
|
|
|
|
r#"import "Host::Msg" as msg;
|
2020-08-01 18:52:26 +02:00
|
|
|
let message1 = msg::new_message(true, "it worked");
|
|
|
|
let ok1 = msg::write_out_message(message1);
|
|
|
|
let message2 = msg::new_os_message(true, 0);
|
|
|
|
let ok2 = msg::write_out_message(message2);
|
2020-08-02 09:39:08 +02:00
|
|
|
ok1 && ok2"#
|
|
|
|
)?,
|
|
|
|
true
|
|
|
|
);
|
2020-08-01 18:52:26 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-08-08 04:19:17 +02:00
|
|
|
|
|
|
|
mod rename_fn {
|
|
|
|
use rhai::plugin::*;
|
|
|
|
use rhai::FLOAT;
|
|
|
|
|
|
|
|
#[export_fn(name = "add_float")]
|
|
|
|
pub fn add(f1: FLOAT, f2: FLOAT) -> FLOAT {
|
|
|
|
f1 + f2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rename_fn_test() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.register_fn("get_mystic_number", || 42 as FLOAT);
|
|
|
|
let mut m = Module::new();
|
2020-08-13 06:34:53 +02:00
|
|
|
rhai::set_exported_fn!(m, "add_two_floats", rename_fn::add_float);
|
2020-08-08 04:19:17 +02:00
|
|
|
let mut r = StaticModuleResolver::new();
|
|
|
|
r.insert("Math::Advanced".to_string(), m);
|
|
|
|
engine.set_module_resolver(Some(r));
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<FLOAT>(
|
|
|
|
r#"import "Math::Advanced" as math;
|
|
|
|
let x = get_mystic_number();
|
|
|
|
let y = math::add_two_floats(x, 1.0);
|
|
|
|
y"#
|
|
|
|
)?,
|
|
|
|
43.0
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-08-08 16:31:15 +02:00
|
|
|
|
|
|
|
mod duplicate_fn_rename {
|
|
|
|
use rhai::plugin::*;
|
|
|
|
use rhai::{FLOAT, INT};
|
|
|
|
|
|
|
|
#[export_fn(name = "add_float")]
|
|
|
|
pub fn add(f1: FLOAT, f2: FLOAT) -> FLOAT {
|
|
|
|
f1 + f2
|
|
|
|
}
|
|
|
|
|
|
|
|
#[export_fn(name = "add_int")]
|
|
|
|
pub fn add(i1: INT, i2: INT) -> INT {
|
|
|
|
i1 + i2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn duplicate_fn_rename_test() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.register_fn("get_mystic_number", || 42 as FLOAT);
|
|
|
|
let mut m = Module::new();
|
2020-08-13 06:34:53 +02:00
|
|
|
rhai::set_exported_fn!(m, "add_two_floats", duplicate_fn_rename::add_float);
|
|
|
|
rhai::set_exported_fn!(m, "add_two_ints", duplicate_fn_rename::add_int);
|
2020-08-08 16:31:15 +02:00
|
|
|
let mut r = StaticModuleResolver::new();
|
|
|
|
r.insert("Math::Advanced".to_string(), m);
|
|
|
|
engine.set_module_resolver(Some(r));
|
|
|
|
|
|
|
|
let output_array = engine.eval::<Array>(
|
|
|
|
r#"import "Math::Advanced" as math;
|
|
|
|
let fx = get_mystic_number();
|
|
|
|
let fy = math::add_two_floats(fx, 1.0);
|
|
|
|
let ix = 42;
|
|
|
|
let iy = math::add_two_ints(ix, 1);
|
|
|
|
[fy, iy]
|
2020-08-13 06:34:53 +02:00
|
|
|
"#,
|
2020-08-08 16:31:15 +02:00
|
|
|
)?;
|
|
|
|
assert_eq!(&output_array[0].as_float().unwrap(), &43.0);
|
|
|
|
assert_eq!(&output_array[1].as_int().unwrap(), &43);
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-08-09 21:19:39 +02:00
|
|
|
|
|
|
|
pub mod raw_returning_fn {
|
|
|
|
use rhai::plugin::*;
|
|
|
|
use rhai::FLOAT;
|
|
|
|
|
|
|
|
#[export_fn(return_raw)]
|
|
|
|
pub fn distance_function(
|
|
|
|
x1: FLOAT,
|
|
|
|
y1: FLOAT,
|
|
|
|
x2: FLOAT,
|
|
|
|
y2: FLOAT,
|
|
|
|
) -> Result<rhai::Dynamic, Box<rhai::EvalAltResult>> {
|
|
|
|
Ok(Dynamic::from(
|
|
|
|
((y2 - y1).abs().powf(2.0) + (x2 - x1).abs().powf(2.0)).sqrt(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn raw_returning_fn_test() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.register_fn("get_mystic_number", || 42 as FLOAT);
|
|
|
|
let mut m = Module::new();
|
2020-08-13 06:34:53 +02:00
|
|
|
rhai::set_exported_fn!(
|
2020-08-09 21:19:39 +02:00
|
|
|
m,
|
|
|
|
"euclidean_distance".to_string(),
|
|
|
|
raw_returning_fn::distance_function
|
|
|
|
);
|
|
|
|
let mut r = StaticModuleResolver::new();
|
|
|
|
r.insert("Math::Advanced".to_string(), m);
|
|
|
|
engine.set_module_resolver(Some(r));
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<FLOAT>(
|
|
|
|
r#"import "Math::Advanced" as math;
|
|
|
|
let x = math::euclidean_distance(0.0, 1.0, 0.0, get_mystic_number()); x"#
|
|
|
|
)?,
|
|
|
|
41.0
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|