diff --git a/codegen/src/function.rs b/codegen/src/function.rs index f5bb6989..9c005554 100644 --- a/codegen/src/function.rs +++ b/codegen/src/function.rs @@ -228,7 +228,7 @@ impl ExportedFn { let keep = match (self.params.skip, parent_scope) { (true, _) => false, (_, ExportScope::PubOnly) => self.is_public, - (_, ExportScope::Prefix(s)) => self.exported_name().as_ref().starts_with(s), + (_, ExportScope::Prefix(s)) => self.name().to_string().starts_with(s), (_, ExportScope::All) => true, }; self.params.skip = !keep; diff --git a/codegen/tests/test_modules.rs b/codegen/tests/test_modules.rs index 3ce1bf37..f873e42b 100644 --- a/codegen/tests/test_modules.rs +++ b/codegen/tests/test_modules.rs @@ -229,16 +229,16 @@ mod export_by_prefix { use rhai::{FLOAT, INT}; #[rhai_fn(name = "foo_add_f")] - pub fn add_float(f1: FLOAT, f2: FLOAT) -> FLOAT { + pub fn foo_add1(f1: FLOAT, f2: FLOAT) -> FLOAT { f1 + f2 } - #[rhai_fn(name = "foo_add_i")] - fn add_int(i1: INT, i2: INT) -> INT { + #[rhai_fn(name = "bar_add_i")] + fn foo_add_int(i1: INT, i2: INT) -> INT { i1 + i2 } - #[rhai_fn(name = "bar_add")] + #[rhai_fn(name = "foo_add_float2")] pub fn add_float2(f1: FLOAT, f2: FLOAT) -> FLOAT { f1 + f2 } @@ -271,7 +271,7 @@ fn export_by_prefix_test() -> Result<(), Box> { let fx = math::foo_add_f(ex, 1.0); let gx = math::foo_m(41.0, 1.0); let ei = 41; - let fi = math::foo_add_i(ei, 1); + let fi = math::bar_add_i(ei, 1); let gi = math::foo_n(41, 1); [fx, gx, fi, gi] "#, @@ -284,21 +284,11 @@ fn export_by_prefix_test() -> Result<(), Box> { assert!(matches!(*engine.eval::( r#"import "Math::Advanced" as math; let ex = 41.0; - let fx = math::bar_add(ex, 1.0); + let fx = math::foo_add_float2(ex, 1.0); fx "#).unwrap_err(), EvalAltResult::ErrorFunctionNotFound(s, p) - if s == "math::bar_add (f64, f64)" - && p == rhai::Position::new(3, 23))); - - assert!(matches!(*engine.eval::( - r#"import "Math::Advanced" as math; - let ex = 41.0; - let fx = math::add_float2(ex, 1.0); - fx - "#).unwrap_err(), - EvalAltResult::ErrorFunctionNotFound(s, p) - if s == "math::add_float2 (f64, f64)" + if s == "math::foo_add_float2 (f64, f64)" && p == rhai::Position::new(3, 23))); assert!(matches!(*engine.eval::( diff --git a/codegen/tests/test_nested.rs b/codegen/tests/test_nested.rs index bf0c0030..f0144990 100644 --- a/codegen/tests/test_nested.rs +++ b/codegen/tests/test_nested.rs @@ -1,5 +1,5 @@ use rhai::module_resolvers::*; -use rhai::{Engine, EvalAltResult, RegisterFn, FLOAT, INT}; +use rhai::{Array, Engine, EvalAltResult, RegisterFn, FLOAT, INT}; pub mod one_fn_module_nested_attr { use rhai::plugin::*; @@ -70,3 +70,133 @@ fn one_fn_submodule_nested_attr_test() -> Result<(), Box> { ); Ok(()) } + +mod export_nested_by_prefix { + use rhai::plugin::*; + #[export_module(export_prefix = "foo_")] + + 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 + } + } + + #[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 + } + } + + 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> { + let mut engine = Engine::new(); + let m = rhai::exported_module!(crate::export_nested_by_prefix::my_adds); + let mut r = StaticModuleResolver::new(); + r.insert("Math::Advanced".to_string(), m); + engine.set_module_resolver(Some(r)); + + let output_array = engine.eval::( + r#"import "Math::Advanced" as math; + let ex = 41.0; + let fx = math::foo_first_adders::add_float(ex, 1.0); + + let ei = 41; + let fi = math::foo_first_adders::add_int(ei, 1); + + let gx = 41.0; + let hx = math::foo_second_adders::add_float(gx, 1.0); + + let gi = 41; + let hi = math::foo_second_adders::add_int(gi, 1); + + [fx, hx, fi, hi] + "#, + )?; + 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::( + r#"import "Math::Advanced" as math; + let ex = 41.0; + let fx = math::foo_third_adders::add_float(ex, 1.0); + fx + "#).unwrap_err(), + EvalAltResult::ErrorFunctionNotFound(s, p) + if s == "math::foo_third_adders::add_float (f64, f64)" + && p == rhai::Position::new(3, 41))); + + assert!(matches!(*engine.eval::( + r#"import "Math::Advanced" as math; + let ex = 41; + let fx = math::foo_third_adders::add_int(ex, 1); + fx + "#).unwrap_err(), + EvalAltResult::ErrorFunctionNotFound(s, p) + if s == "math::foo_third_adders::add_int (i64, i64)" + && p == rhai::Position::new(3, 41))); + + assert!(matches!(*engine.eval::( + r#"import "Math::Advanced" as math; + let ex = 41; + let fx = math::bar_fourth_adders::add_int(ex, 1); + fx + "#).unwrap_err(), + EvalAltResult::ErrorFunctionNotFound(s, p) + if s == "math::bar_fourth_adders::add_int (i64, i64)" + && p == rhai::Position::new(3, 42))); + + + assert!(matches!(*engine.eval::( + r#"import "Math::Advanced" as math; + let ex = 41.0; + let fx = math::bar_fourth_adders::add_float(ex, 1.0); + fx + "#).unwrap_err(), + EvalAltResult::ErrorFunctionNotFound(s, p) + if s == "math::bar_fourth_adders::add_float (f64, f64)" + && p == rhai::Position::new(3, 42))); + + Ok(()) +}