diff --git a/CHANGELOG.md b/CHANGELOG.md index dd5f5ab6..a03e5fda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ Enhancements * `Engine::parse_json` now natively handles nested JSON inputs (using a token remap filter) without needing to replace `{` with `#{`. * `to_json` is added to object maps to cheaply convert it to JSON format (`()` is mapped to `null`, all other data types must be supported by JSON) * A global function `format_map_as_json` is provided which is the same as `to_json` for object maps. +* `FileModuleResolver` now accepts a custom `Scope` to provide constants for optimization. Version 1.6.1 diff --git a/src/module/resolvers/file.rs b/src/module/resolvers/file.rs index 561dfaae..6fe16b48 100644 --- a/src/module/resolvers/file.rs +++ b/src/module/resolvers/file.rs @@ -45,11 +45,12 @@ pub const RHAI_SCRIPT_EXTENSION: &str = "rhai"; /// /// engine.set_module_resolver(resolver); /// ``` -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct FileModuleResolver { base_path: Option, extension: Identifier, cache_enabled: bool, + scope: Scope<'static>, #[cfg(not(feature = "sync"))] cache: std::cell::RefCell>>, @@ -57,6 +58,13 @@ pub struct FileModuleResolver { cache: std::sync::RwLock>>, } +impl Default for FileModuleResolver { + #[inline(always)] + fn default() -> Self { + Self::new() + } +} + impl FileModuleResolver { /// Create a new [`FileModuleResolver`] with the current directory as base path. /// @@ -126,6 +134,7 @@ impl FileModuleResolver { extension: extension.into(), cache_enabled: true, cache: BTreeMap::new().into(), + scope: Scope::new(), } } @@ -155,6 +164,7 @@ impl FileModuleResolver { extension: extension.into(), cache_enabled: true, cache: BTreeMap::new().into(), + scope: Scope::new(), } } @@ -185,6 +195,32 @@ impl FileModuleResolver { self } + /// Get a reference to the file module resolver's [scope][Scope]. + /// + /// The [scope][Scope] is used for compiling module scripts. + #[must_use] + #[inline(always)] + pub const fn scope(&self) -> &Scope { + &self.scope + } + + /// Set the file module resolver's [scope][Scope]. + /// + /// The [scope][Scope] is used for compiling module scripts. + #[inline(always)] + pub fn set_scope(&mut self, scope: Scope<'static>) { + self.scope = scope; + } + + /// Get a mutable reference to the file module resolver's [scope][Scope]. + /// + /// The [scope][Scope] is used for compiling module scripts. + #[must_use] + #[inline(always)] + pub fn scope_mut(&mut self) -> &mut Scope<'static> { + &mut self.scope + } + /// Enable/disable the cache. #[inline(always)] pub fn enable_cache(&mut self, enable: bool) -> &mut Self { @@ -281,10 +317,8 @@ impl FileModuleResolver { } } - let scope = Scope::new(); - let mut ast = engine - .compile_file(file_path.clone()) + .compile_file_with_scope(&self.scope, file_path.clone()) .map_err(|err| match *err { ERR::ErrorSystem(.., err) if err.is::() => { Box::new(ERR::ErrorModuleNotFound(path.to_string(), pos)) @@ -294,7 +328,9 @@ impl FileModuleResolver { ast.set_source(path); - let m: Shared = if let Some(global) = global { + let scope = Scope::new(); + + let m: Shared<_> = if let Some(global) = global { Module::eval_ast_as_new_raw(engine, scope, global, &ast) } else { Module::eval_ast_as_new(scope, &ast, engine) diff --git a/src/module/resolvers/stat.rs b/src/module/resolvers/stat.rs index be9110a7..3e2e9659 100644 --- a/src/module/resolvers/stat.rs +++ b/src/module/resolvers/stat.rs @@ -22,7 +22,7 @@ use std::{collections::btree_map::IntoIter, collections::BTreeMap, ops::AddAssig /// /// engine.set_module_resolver(resolver); /// ``` -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub struct StaticModuleResolver(BTreeMap>); impl StaticModuleResolver {