diff --git a/benches/eval_module.rs b/benches/eval_module.rs index 350877fe..de74ab24 100644 --- a/benches/eval_module.rs +++ b/benches/eval_module.rs @@ -3,7 +3,7 @@ ///! Test evaluating with scope extern crate test; -use rhai::{module_resolvers::StaticModuleResolver, Engine, Module, OptimizationLevel}; +use rhai::{Engine, Module, OptimizationLevel}; use test::Bencher; #[bench] @@ -20,9 +20,7 @@ fn bench_eval_module(bench: &mut Bencher) { let module = Module::eval_ast_as_new(Default::default(), &ast, &engine).unwrap(); - let mut resolver = StaticModuleResolver::new(); - resolver.insert("testing", module); - engine.set_module_resolver(Some(resolver)); + engine.register_module("testing", module); let ast = engine .compile( diff --git a/src/ast.rs b/src/ast.rs index 70e1425f..301e490f 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -31,12 +31,6 @@ use crate::stdlib::{ vec::Vec, }; -#[cfg(not(feature = "no_closure"))] -use crate::stdlib::collections::HashSet; - -#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] -use crate::stdlib::cmp::max; - /// A type representing the access mode of a scripted function. #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub enum FnAccess { @@ -98,7 +92,7 @@ pub struct ScriptFnDef { pub params: StaticVec, /// Access to external variables. #[cfg(not(feature = "no_closure"))] - pub externals: HashSet, + pub externals: crate::stdlib::collections::HashSet, } impl fmt::Display for ScriptFnDef { @@ -940,14 +934,20 @@ impl Expr { #[cfg(not(feature = "no_index"))] Self::Array(x, _) if self.is_constant() => { - let mut arr = Array::with_capacity(max(crate::engine::TYPICAL_ARRAY_SIZE, x.len())); + let mut arr = Array::with_capacity(crate::stdlib::cmp::max( + crate::engine::TYPICAL_ARRAY_SIZE, + x.len(), + )); arr.extend(x.iter().map(|v| v.get_constant_value().unwrap())); Dynamic(Union::Array(Box::new(arr))) } #[cfg(not(feature = "no_object"))] Self::Map(x, _) if self.is_constant() => { - let mut map = Map::with_capacity(max(crate::engine::TYPICAL_MAP_SIZE, x.len())); + let mut map = Map::with_capacity(crate::stdlib::cmp::max( + crate::engine::TYPICAL_MAP_SIZE, + x.len(), + )); map.extend( x.iter() .map(|(k, v)| (k.name.clone(), v.get_constant_value().unwrap())), diff --git a/src/engine.rs b/src/engine.rs index ec1aadb0..aaf460a7 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -28,20 +28,14 @@ use crate::stdlib::{ string::{String, ToString}, }; -/// Variable-sized array of `Dynamic` values. -/// -/// Not available under the `no_index` feature. #[cfg(not(feature = "no_index"))] -pub type Array = crate::stdlib::vec::Vec; +use crate::Array; #[cfg(not(feature = "no_index"))] pub const TYPICAL_ARRAY_SIZE: usize = 8; // Small arrays are typical -/// Hash map of `Dynamic` values with `ImmutableString` keys. -/// -/// Not available under the `no_object` feature. #[cfg(not(feature = "no_object"))] -pub type Map = HashMap; +use crate::Map; #[cfg(not(feature = "no_object"))] pub const TYPICAL_MAP_SIZE: usize = 8; // Small maps are typical diff --git a/src/engine_api.rs b/src/engine_api.rs index dcff03a4..9e16303f 100644 --- a/src/engine_api.rs +++ b/src/engine_api.rs @@ -761,7 +761,7 @@ impl Engine { self.register_indexer_get(getter) .register_indexer_set(setter) } - /// Register a `Module` as a sub-module with the `Engine`. + /// Register a `Module` as a fixed module namespace with the `Engine`. /// /// # Example /// @@ -775,7 +775,7 @@ impl Engine { /// let mut module = Module::new(); /// module.set_fn_1("calc", |x: i64| Ok(x + 1)); /// - /// // Register the module as a sub-module + /// // Register the module as a fixed sub-module /// engine.register_module("CalcService", module); /// /// assert_eq!(engine.eval::("CalcService::calc(41)")?, 42); diff --git a/src/fn_call.rs b/src/fn_call.rs index 2a1973f3..b22f0554 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -40,25 +40,25 @@ use crate::stdlib::{ use num_traits::float::Float; /// Extract the property name from a getter function name. +#[cfg(not(feature = "no_object"))] #[inline(always)] fn extract_prop_from_getter(_fn_name: &str) -> Option<&str> { - #[cfg(not(feature = "no_object"))] if _fn_name.starts_with(crate::engine::FN_GET) { - return Some(&_fn_name[crate::engine::FN_GET.len()..]); + Some(&_fn_name[crate::engine::FN_GET.len()..]) + } else { + None } - - None } /// Extract the property name from a setter function name. +#[cfg(not(feature = "no_object"))] #[inline(always)] fn extract_prop_from_setter(_fn_name: &str) -> Option<&str> { - #[cfg(not(feature = "no_object"))] if _fn_name.starts_with(crate::engine::FN_SET) { - return Some(&_fn_name[crate::engine::FN_SET.len()..]); + Some(&_fn_name[crate::engine::FN_SET.len()..]) + } else { + None } - - None } /// A type that temporarily stores a mutable reference to a `Dynamic`, @@ -244,6 +244,7 @@ impl Engine { } // Getter function not found? + #[cfg(not(feature = "no_object"))] if let Some(prop) = extract_prop_from_getter(fn_name) { return EvalAltResult::ErrorDotExpr( format!( @@ -257,6 +258,7 @@ impl Engine { } // Setter function not found? + #[cfg(not(feature = "no_object"))] if let Some(prop) = extract_prop_from_setter(fn_name) { return EvalAltResult::ErrorDotExpr( format!( diff --git a/src/fn_native.rs b/src/fn_native.rs index 6715d98d..9bf6c3ab 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -17,13 +17,6 @@ use crate::stdlib::rc::Rc; #[cfg(feature = "sync")] use crate::stdlib::sync::Arc; -#[cfg(any(not(feature = "no_closure"), not(feature = "no_module")))] -#[cfg(not(feature = "sync"))] -use crate::stdlib::cell::RefCell; -#[cfg(any(not(feature = "no_closure"), not(feature = "no_module")))] -#[cfg(feature = "sync")] -use crate::stdlib::sync::RwLock; - /// Trait that maps to `Send + Sync` only under the `sync` feature. #[cfg(feature = "sync")] pub trait SendSync: Send + Sync {} @@ -48,11 +41,11 @@ pub type Shared = Arc; /// Synchronized shared object. #[cfg(any(not(feature = "no_closure"), not(feature = "no_module")))] #[cfg(not(feature = "sync"))] -pub type Locked = RefCell; +pub type Locked = crate::stdlib::cell::RefCell; /// Synchronized shared object. #[cfg(any(not(feature = "no_closure"), not(feature = "no_module")))] #[cfg(feature = "sync")] -pub type Locked = RwLock; +pub type Locked = crate::stdlib::sync::RwLock; /// Context of native Rust function call. #[derive(Debug, Copy, Clone)] diff --git a/src/lib.rs b/src/lib.rs index 4ab6b3a5..496b60c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -141,11 +141,17 @@ pub use ast::FnAccess; #[cfg(not(feature = "no_function"))] pub use fn_func::Func; +/// Variable-sized array of `Dynamic` values. +/// +/// Not available under the `no_index` feature. #[cfg(not(feature = "no_index"))] -pub use engine::Array; +pub type Array = stdlib::vec::Vec; +/// Hash map of `Dynamic` values with `ImmutableString` keys. +/// +/// Not available under the `no_object` feature. #[cfg(not(feature = "no_object"))] -pub use engine::Map; +pub type Map = stdlib::collections::HashMap; #[cfg(not(feature = "no_module"))] pub use module::ModuleResolver; diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 6d077d7b..69c3b7d6 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -3,13 +3,13 @@ use crate::def_package; use crate::dynamic::Dynamic; -use crate::engine::{Array, OP_EQUALS, TYPICAL_ARRAY_SIZE}; +use crate::engine::{OP_EQUALS, TYPICAL_ARRAY_SIZE}; use crate::fn_native::{FnPtr, NativeCallContext}; use crate::plugin::*; use crate::result::EvalAltResult; use crate::token::NO_POS; use crate::utils::ImmutableString; -use crate::INT; +use crate::{Array, INT}; #[cfg(not(feature = "no_object"))] use crate::Map; diff --git a/src/packages/map_basic.rs b/src/packages/map_basic.rs index a46fb67d..d0102f12 100644 --- a/src/packages/map_basic.rs +++ b/src/packages/map_basic.rs @@ -2,10 +2,10 @@ use crate::def_package; use crate::dynamic::Dynamic; -use crate::engine::{Map, OP_EQUALS}; +use crate::engine::OP_EQUALS; use crate::plugin::*; use crate::utils::ImmutableString; -use crate::INT; +use crate::{Map, INT}; #[cfg(not(feature = "no_index"))] use crate::Array; diff --git a/src/parser.rs b/src/parser.rs index 59e4d4f8..136e798b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1418,7 +1418,9 @@ fn make_dot_expr( } // lhs.Fn() or lhs.eval() (_, Expr::FnCall(x, pos)) - if x.args.len() == 0 && [crate::engine::KEYWORD_FN_PTR, crate::engine::KEYWORD_EVAL].contains(&x.name.as_ref()) => + if x.args.len() == 0 + && [crate::engine::KEYWORD_FN_PTR, crate::engine::KEYWORD_EVAL] + .contains(&x.name.as_ref()) => { return Err(PERR::BadInput(LexError::ImproperSymbol(format!( "'{}' should not be called in method style. Try {}(...);", @@ -2664,11 +2666,13 @@ fn make_curry_from_externals(fn_expr: Expr, externals: StaticVec, pos: P args.push(Expr::Variable(Box::new((None, None, 0, x.clone().into())))); }); - let hash = calc_script_fn_hash(empty(), crate::engine::KEYWORD_FN_PTR_CURRY, num_externals + 1); + let curry_func = crate::engine::KEYWORD_FN_PTR_CURRY; + + let hash = calc_script_fn_hash(empty(), curry_func, num_externals + 1); let expr = Expr::FnCall( Box::new(FnCallExpr { - name: crate::engine::KEYWORD_FN_PTR_CURRY.into(), + name: curry_func.into(), hash, args, ..Default::default() diff --git a/src/utils.rs b/src/utils.rs index 672fb4ec..0ecf321c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -15,12 +15,6 @@ use crate::stdlib::{ string::{String, ToString}, }; -#[cfg(not(feature = "no_std"))] -use crate::stdlib::collections::hash_map::DefaultHasher; - -#[cfg(feature = "no_std")] -use ahash::AHasher; - /// A hasher that only takes one single `u64` and returns it as a hash key. /// /// # Panics @@ -95,9 +89,9 @@ pub fn calc_script_fn_hash<'a>( /// Create an instance of the default hasher. pub fn get_hasher() -> impl Hasher { #[cfg(feature = "no_std")] - let s: AHasher = Default::default(); + let s: ahash::AHasher = Default::default(); #[cfg(not(feature = "no_std"))] - let s = DefaultHasher::new(); + let s = crate::stdlib::collections::hash_map::DefaultHasher::new(); s } diff --git a/tests/plugins.rs b/tests/plugins.rs index 84da570c..e3e8d1a1 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -1,6 +1,5 @@ #![cfg(not(any(feature = "no_index", feature = "no_module")))] -use rhai::module_resolvers::StaticModuleResolver; use rhai::plugin::*; use rhai::{Engine, EvalAltResult, INT}; @@ -96,14 +95,9 @@ fn test_plugins_package() -> Result<(), Box> { "6 kitties" ); - let mut resolver = StaticModuleResolver::new(); - resolver.insert("test", exported_module!(test::special_array_package)); + engine.register_module("test", exported_module!(test::special_array_package)); - engine.set_module_resolver(Some(resolver)); - assert_eq!( - engine.eval::(r#"import "test" as test; test::MYSTIC_NUMBER"#)?, - 42 - ); + assert_eq!(engine.eval::("test::MYSTIC_NUMBER")?, 42); Ok(()) }