2021-03-22 12:21:42 +01:00
|
|
|
use rhai::{Array, Engine, EvalAltResult, FLOAT};
|
2020-08-16 04:51:14 +02:00
|
|
|
|
|
|
|
pub mod one_fn_module_nested_attr {
|
|
|
|
use rhai::plugin::*;
|
|
|
|
|
|
|
|
#[export_module]
|
|
|
|
pub mod advanced_math {
|
|
|
|
use rhai::plugin::*;
|
|
|
|
use rhai::FLOAT;
|
|
|
|
|
|
|
|
#[rhai_fn(return_raw)]
|
2021-03-22 12:21:42 +01:00
|
|
|
pub fn get_mystic_number() -> Result<FLOAT, Box<EvalAltResult>> {
|
|
|
|
Ok(42.0)
|
2020-08-16 04:51:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn one_fn_module_nested_attr_test() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
let m = rhai::exported_module!(crate::one_fn_module_nested_attr::advanced_math);
|
2020-12-26 06:05:57 +01:00
|
|
|
engine.register_static_module("Math::Advanced", m.into());
|
2020-08-16 04:51:14 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
2020-12-26 06:05:57 +01:00
|
|
|
engine.eval::<FLOAT>(r#"let m = Math::Advanced::get_mystic_number(); m"#)?,
|
2020-08-16 04:51:14 +02:00
|
|
|
42.0
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-08-21 05:20:12 +02:00
|
|
|
|
2021-02-18 10:42:49 +01:00
|
|
|
pub mod one_fn_sub_module_nested_attr {
|
2020-08-21 05:20:12 +02:00
|
|
|
use rhai::plugin::*;
|
|
|
|
|
|
|
|
#[export_module]
|
|
|
|
pub mod advanced_math {
|
|
|
|
#[rhai_mod(name = "constants")]
|
|
|
|
pub mod my_module {
|
|
|
|
use rhai::plugin::*;
|
|
|
|
use rhai::FLOAT;
|
|
|
|
#[rhai_fn(return_raw)]
|
2021-03-22 12:21:42 +01:00
|
|
|
pub fn get_mystic_number() -> Result<FLOAT, Box<EvalAltResult>> {
|
|
|
|
Ok(42.0)
|
2020-08-21 05:20:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-18 10:42:49 +01:00
|
|
|
fn one_fn_sub_module_nested_attr_test() -> Result<(), Box<EvalAltResult>> {
|
2020-08-21 05:20:12 +02:00
|
|
|
let mut engine = Engine::new();
|
2021-02-18 10:42:49 +01:00
|
|
|
let m = rhai::exported_module!(crate::one_fn_sub_module_nested_attr::advanced_math);
|
2020-12-26 06:05:57 +01:00
|
|
|
engine.register_static_module("Math::Advanced", m.into());
|
2020-08-21 05:20:12 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
2022-07-29 05:01:00 +02:00
|
|
|
engine.eval::<FLOAT>(
|
|
|
|
"
|
|
|
|
let m = Math::Advanced::constants::get_mystic_number();
|
|
|
|
m
|
|
|
|
"
|
|
|
|
)?,
|
2020-08-21 05:20:12 +02:00
|
|
|
42.0
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-09-03 03:31:16 +02:00
|
|
|
|
|
|
|
mod export_nested_by_prefix {
|
|
|
|
use rhai::plugin::*;
|
|
|
|
|
2020-12-24 14:28:40 +01:00
|
|
|
#[export_module(export_prefix = "foo_")]
|
2020-09-03 03:31:16 +02:00
|
|
|
pub mod my_adds {
|
|
|
|
pub mod foo_first_adders {
|
|
|
|
use rhai::{FLOAT, INT};
|
|
|
|
|
|
|
|
pub fn add_float(f1: FLOAT, f2: FLOAT) -> FLOAT {
|
|
|
|
f1 + f2
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_int(i1: INT, i2: INT) -> INT {
|
|
|
|
i1 + i2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod foo_second_adders {
|
|
|
|
use rhai::{FLOAT, INT};
|
|
|
|
|
|
|
|
pub fn add_float(f1: FLOAT, f2: FLOAT) -> FLOAT {
|
|
|
|
f1 + f2
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_int(i1: INT, i2: INT) -> INT {
|
|
|
|
i1 + i2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 18:07:39 +01:00
|
|
|
#[allow(dead_code)] // used inside a `exported_module!`
|
2020-09-03 03:31:16 +02:00
|
|
|
#[rhai_mod(name = "foo_third_adders")]
|
|
|
|
pub mod baz_third_adders {
|
|
|
|
use rhai::{FLOAT, INT};
|
|
|
|
|
|
|
|
pub fn add_float(f1: FLOAT, f2: FLOAT) -> FLOAT {
|
|
|
|
f1 + f2
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_int(i1: INT, i2: INT) -> INT {
|
|
|
|
i1 + i2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 18:07:39 +01:00
|
|
|
#[allow(dead_code)] // used inside a `exported_module!`
|
2020-09-03 03:31:16 +02:00
|
|
|
pub mod bar_fourth_adders {
|
|
|
|
use rhai::{FLOAT, INT};
|
|
|
|
|
|
|
|
pub fn add_float(f1: FLOAT, f2: FLOAT) -> FLOAT {
|
|
|
|
f1 + f2
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_int(i1: INT, i2: INT) -> INT {
|
|
|
|
i1 + i2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn export_nested_by_prefix_test() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
let m = rhai::exported_module!(crate::export_nested_by_prefix::my_adds);
|
2020-12-26 06:05:57 +01:00
|
|
|
engine.register_static_module("Math::Advanced", m.into());
|
2020-09-03 03:31:16 +02:00
|
|
|
|
|
|
|
let output_array = engine.eval::<Array>(
|
2022-07-29 05:01:00 +02:00
|
|
|
"
|
|
|
|
let ex = 41.0;
|
|
|
|
let fx = Math::Advanced::foo_first_adders::add_float(ex, 1.0);
|
2020-09-03 03:31:16 +02:00
|
|
|
|
2022-07-29 05:01:00 +02:00
|
|
|
let ei = 41;
|
|
|
|
let fi = Math::Advanced::foo_first_adders::add_int(ei, 1);
|
2020-09-03 03:31:16 +02:00
|
|
|
|
2022-07-29 05:01:00 +02:00
|
|
|
let gx = 41.0;
|
|
|
|
let hx = Math::Advanced::foo_second_adders::add_float(gx, 1.0);
|
2020-09-03 03:31:16 +02:00
|
|
|
|
2022-07-29 05:01:00 +02:00
|
|
|
let gi = 41;
|
|
|
|
let hi = Math::Advanced::foo_second_adders::add_int(gi, 1);
|
2020-09-03 03:31:16 +02:00
|
|
|
|
2022-07-29 05:01:00 +02:00
|
|
|
[fx, hx, fi, hi]
|
|
|
|
",
|
2020-09-03 03:31:16 +02:00
|
|
|
)?;
|
|
|
|
assert_eq!(&output_array[0].as_float().unwrap(), &42.0);
|
|
|
|
assert_eq!(&output_array[1].as_float().unwrap(), &42.0);
|
|
|
|
assert_eq!(&output_array[2].as_int().unwrap(), &42);
|
|
|
|
assert_eq!(&output_array[3].as_int().unwrap(), &42);
|
|
|
|
|
|
|
|
assert!(matches!(*engine.eval::<FLOAT>(
|
2022-07-29 03:42:30 +02:00
|
|
|
"
|
|
|
|
let ex = 41.0;
|
|
|
|
let fx = Math::Advanced::foo_third_adders::add_float(ex, 1.0);
|
|
|
|
fx
|
|
|
|
").unwrap_err(),
|
2022-12-30 18:07:39 +01:00
|
|
|
EvalAltResult::ErrorFunctionNotFound(s, ..)
|
2022-07-29 05:01:00 +02:00
|
|
|
if s == "Math::Advanced::foo_third_adders::add_float (f64, f64)"));
|
2020-09-03 03:31:16 +02:00
|
|
|
|
|
|
|
assert!(matches!(*engine.eval::<FLOAT>(
|
2022-07-29 03:42:30 +02:00
|
|
|
"
|
|
|
|
let ex = 41;
|
|
|
|
let fx = Math::Advanced::foo_third_adders::add_int(ex, 1);
|
|
|
|
fx
|
|
|
|
").unwrap_err(),
|
2022-12-30 18:07:39 +01:00
|
|
|
EvalAltResult::ErrorFunctionNotFound(s, ..)
|
2022-07-29 05:01:00 +02:00
|
|
|
if s == "Math::Advanced::foo_third_adders::add_int (i64, i64)"));
|
2020-09-03 03:31:16 +02:00
|
|
|
|
|
|
|
assert!(matches!(*engine.eval::<FLOAT>(
|
2022-07-29 03:42:30 +02:00
|
|
|
"
|
|
|
|
let ex = 41;
|
|
|
|
let fx = Math::Advanced::bar_fourth_adders::add_int(ex, 1);
|
|
|
|
fx
|
|
|
|
").unwrap_err(),
|
2022-12-30 18:07:39 +01:00
|
|
|
EvalAltResult::ErrorFunctionNotFound(s, ..)
|
2022-07-29 05:01:00 +02:00
|
|
|
if s == "Math::Advanced::bar_fourth_adders::add_int (i64, i64)"));
|
2020-09-03 03:31:16 +02:00
|
|
|
|
|
|
|
assert!(matches!(*engine.eval::<FLOAT>(
|
2022-07-29 03:42:30 +02:00
|
|
|
"
|
|
|
|
let ex = 41.0;
|
|
|
|
let fx = Math::Advanced::bar_fourth_adders::add_float(ex, 1.0);
|
|
|
|
fx
|
|
|
|
").unwrap_err(),
|
2022-12-30 18:07:39 +01:00
|
|
|
EvalAltResult::ErrorFunctionNotFound(s, ..)
|
2022-07-29 05:01:00 +02:00
|
|
|
if s == "Math::Advanced::bar_fourth_adders::add_float (f64, f64)"));
|
2020-09-03 03:31:16 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|