diff --git a/src/api/register.rs b/src/api/register.rs index 42882d8fa..be335d2af 100644 --- a/src/api/register.rs +++ b/src/api/register.rs @@ -75,10 +75,7 @@ impl Engine { name: impl AsRef + Into, func: FUNC, ) -> &mut Self { - let mut reg = FuncRegistration::new(name.into()) - .with_namespace(FnNamespace::Global) - .with_purity(true) - .with_volatility(false); + let mut reg = FuncRegistration::new(name.into()).with_namespace(FnNamespace::Global); #[cfg(feature = "metadata")] { diff --git a/src/module/mod.rs b/src/module/mod.rs index 860bc29e8..918b9bfc9 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -445,7 +445,9 @@ impl FuncRegistration { f.num_params = arg_types.as_ref().len(); f.param_types.extend(arg_types.as_ref().iter().copied()); - if f.name == crate::engine::FN_IDX_GET || f.name == crate::engine::FN_IDX_SET { + if (f.name == crate::engine::FN_IDX_GET && f.num_params == 2) + || (f.name == crate::engine::FN_IDX_SET && f.num_params == 3) + { if let Some(&type_id) = f.param_types.first() { #[cfg(not(feature = "no_index"))] assert!( @@ -1657,18 +1659,25 @@ impl Module { /// /// # Panics /// - /// Panics if the type is [`Array`][crate::Array] or [`Map`][crate::Map]. - /// Indexers for arrays, object maps and strings cannot be registered. + /// Panics if the type is [`Array`][crate::Array], [`Map`][crate::Map], [`String`], + /// [`ImmutableString`][crate::ImmutableString], `&str` or [`INT`][crate::INT]. + /// + /// Indexers for arrays, object maps, strings and integers cannot be registered. /// /// # Example /// /// ``` /// use rhai::{Module, ImmutableString}; /// + /// #[derive(Clone)] + /// struct TestStruct(i64); + /// /// let mut module = Module::new(); + /// /// let hash = module.set_indexer_get_fn( - /// |x: &mut i64, y: ImmutableString| Ok(*x + y.len() as i64) + /// |x: &mut TestStruct, y: ImmutableString| Ok(x.0 + y.len() as i64) /// ); + /// /// assert!(module.contains_fn(hash)); /// ``` #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] @@ -1680,24 +1689,6 @@ impl Module { R: Variant + Clone, FUNC: RhaiNativeFunc<(Mut, B), 2, X, R, true> + SendSync + 'static, { - #[cfg(not(feature = "no_index"))] - assert!( - TypeId::of::() != TypeId::of::(), - "Cannot register indexer for arrays." - ); - #[cfg(not(feature = "no_object"))] - assert!( - TypeId::of::() != TypeId::of::(), - "Cannot register indexer for object maps." - ); - - assert!( - TypeId::of::() != TypeId::of::() - && TypeId::of::() != TypeId::of::<&str>() - && TypeId::of::() != TypeId::of::(), - "Cannot register indexer for strings." - ); - FuncRegistration::new(crate::engine::FN_IDX_GET) .with_namespace(FnNamespace::Global) .with_purity(true) @@ -1722,24 +1713,26 @@ impl Module { /// /// # Panics /// - /// Panics if the type is [`Array`][crate::Array] or [`Map`][crate::Map]. - /// Indexers for arrays, object maps and strings cannot be registered. - /// - /// # Panics + /// Panics if the type is [`Array`][crate::Array], [`Map`][crate::Map], [`String`], + /// [`ImmutableString`][crate::ImmutableString], `&str` or [`INT`][crate::INT]. /// - /// Panics if the type is [`Array`][crate::Array] or [`Map`][crate::Map]. - /// Indexers for arrays, object maps and strings cannot be registered. + /// Indexers for arrays, object maps, strings and integers cannot be registered. /// /// # Example /// /// ``` /// use rhai::{Module, ImmutableString}; /// + /// #[derive(Clone)] + /// struct TestStruct(i64); + /// /// let mut module = Module::new(); - /// let hash = module.set_indexer_set_fn(|x: &mut i64, y: ImmutableString, value: i64| { - /// *x = y.len() as i64 + value; + /// + /// let hash = module.set_indexer_set_fn(|x: &mut TestStruct, y: ImmutableString, value: i64| { + /// *x = TestStruct(y.len() as i64 + value); /// Ok(()) - /// }); + /// }); + /// /// assert!(module.contains_fn(hash)); /// ``` #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] @@ -1751,24 +1744,6 @@ impl Module { R: Variant + Clone, FUNC: RhaiNativeFunc<(Mut, B, R), 3, X, (), true> + SendSync + 'static, { - #[cfg(not(feature = "no_index"))] - assert!( - TypeId::of::() != TypeId::of::(), - "Cannot register indexer for arrays." - ); - #[cfg(not(feature = "no_object"))] - assert!( - TypeId::of::() != TypeId::of::(), - "Cannot register indexer for object maps." - ); - - assert!( - TypeId::of::() != TypeId::of::() - && TypeId::of::() != TypeId::of::<&str>() - && TypeId::of::() != TypeId::of::(), - "Cannot register indexer for strings." - ); - FuncRegistration::new(crate::engine::FN_IDX_SET) .with_namespace(FnNamespace::Global) .with_purity(false) @@ -1787,19 +1762,26 @@ impl Module { /// /// # Panics /// - /// Panics if the type is [`Array`][crate::Array] or [`Map`][crate::Map]. - /// Indexers for arrays, object maps and strings cannot be registered. + /// Panics if the type is [`Array`][crate::Array], [`Map`][crate::Map], [`String`], + /// [`ImmutableString`][crate::ImmutableString], `&str` or [`INT`][crate::INT]. + /// + /// Indexers for arrays, object maps, strings and integers cannot be registered. /// /// # Example /// /// ``` /// use rhai::{Module, ImmutableString}; /// + /// #[derive(Clone)] + /// struct TestStruct(i64); + /// /// let mut module = Module::new(); + /// /// let (hash_get, hash_set) = module.set_indexer_get_set_fn( - /// |x: &mut i64, y: ImmutableString| Ok(*x + y.len() as i64), - /// |x: &mut i64, y: ImmutableString, value: i64| { *x = y.len() as i64 + value; Ok(()) } + /// |x: &mut TestStruct, y: ImmutableString| Ok(x.0 + y.len() as i64), + /// |x: &mut TestStruct, y: ImmutableString, value: i64| { *x = TestStruct(y.len() as i64 + value); Ok(()) } /// ); + /// /// assert!(module.contains_fn(hash_get)); /// assert!(module.contains_fn(hash_set)); /// ``` diff --git a/tests/optimizer.rs b/tests/optimizer.rs index 8aa27478d..97accf07e 100644 --- a/tests/optimizer.rs +++ b/tests/optimizer.rs @@ -1,5 +1,5 @@ #![cfg(not(feature = "no_optimize"))] -use rhai::{CustomType, Engine, FuncRegistration, Module, OptimizationLevel, Scope, TypeBuilder, INT}; +use rhai::{Engine, FuncRegistration, Module, OptimizationLevel, Scope, INT}; #[test] fn test_optimizer() { @@ -147,7 +147,7 @@ fn test_optimizer_re_optimize() { #[test] fn test_optimizer_full() { - #[derive(Debug, Clone, CustomType)] + #[derive(Debug, Clone)] struct TestStruct(INT); let mut engine = Engine::new(); @@ -170,15 +170,16 @@ fn test_optimizer_full() { ); engine - .build_type::() + .register_type_with_name::("TestStruct") .register_fn("ts", |n: INT| TestStruct(n)) + .register_fn("value", |ts: &mut TestStruct| ts.0) .register_fn("+", |ts1: &mut TestStruct, ts2: TestStruct| TestStruct(ts1.0 + ts2.0)); let ast = engine .compile( " const FOO = ts(40) + ts(2); - field0(FOO) + value(FOO) ", ) .unwrap();