From 9b37d84a9b42396f14cd29e899820b1ea4b80680 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 10 Mar 2021 23:01:04 +0800 Subject: [PATCH] Enable/disable caching in FileModuleResolver. --- CHANGELOG.md | 1 + src/bin/rhai-repl.rs | 18 +++++++++++++++--- src/module/resolvers/file.rs | 30 +++++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0481e729..a0d83084 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ Enhancements * Previously, `private` functions in an `AST` cannot be called with `call_fn` etc. This is inconvenient when trying to call a function inside a script which also serves as a loadable module exporting part (but not all) of the functions. Now, all functions (`private` or not) can be called in an `AST`. The `private` keyword is relegated to preventing a function from being exported. * `Dynamic::as_unit` just for completeness sake. * `bytes` method added for strings to get length quickly (if the string is ASCII-only). +* `FileModuleResolver` can now enable/disable caching. Version 0.19.13 diff --git a/src/bin/rhai-repl.rs b/src/bin/rhai-repl.rs index c5c91a18..44fe90f0 100644 --- a/src/bin/rhai-repl.rs +++ b/src/bin/rhai-repl.rs @@ -1,4 +1,6 @@ -use rhai::{Dynamic, Engine, EvalAltResult, Module, Scope, AST}; +use rhai::{ + module_resolvers::FileModuleResolver, Dynamic, Engine, EvalAltResult, Module, Scope, AST, +}; #[cfg(not(feature = "no_optimize"))] use rhai::OptimizationLevel; @@ -56,12 +58,19 @@ fn print_help() { } fn main() { - let mut engine = Engine::new(); - println!("Rhai REPL tool"); println!("=============="); print_help(); + // Initialize scripting engine + let mut engine = Engine::new(); + + // Set a file module resolver without caching + let mut resolver = FileModuleResolver::new(); + resolver.enable_cache(false); + + engine.set_module_resolver(resolver); + // Load init scripts #[cfg(not(feature = "no_module"))] @@ -130,6 +139,9 @@ fn main() { let mut ast_u: AST = Default::default(); let mut ast: AST = Default::default(); + // Make Engine immutable + let engine = engine; + // REPL loop 'main_loop: loop { diff --git a/src/module/resolvers/file.rs b/src/module/resolvers/file.rs index 5d71e0a2..e3a86634 100644 --- a/src/module/resolvers/file.rs +++ b/src/module/resolvers/file.rs @@ -41,6 +41,7 @@ use crate::{Engine, EvalAltResult, Module, ModuleResolver, Position, Shared}; pub struct FileModuleResolver { base_path: PathBuf, extension: String, + cache_enabled: bool, #[cfg(not(feature = "sync"))] cache: crate::stdlib::cell::RefCell>>, @@ -101,6 +102,7 @@ impl FileModuleResolver { Self { base_path: path.into(), extension: extension.into(), + cache_enabled: true, cache: Default::default(), } } @@ -152,9 +154,25 @@ impl FileModuleResolver { self } + /// Enable/disable the cache. + #[inline(always)] + pub fn enable_cache(&mut self, enable: bool) -> &mut Self { + self.cache_enabled = enable; + self + } + /// Is the cache enabled? + #[inline(always)] + pub fn is_cache_enabled(&self) -> bool { + self.cache_enabled + } + /// Is a particular path cached? #[inline(always)] pub fn is_cached(&self, path: &str) -> bool { + if !self.cache_enabled { + return false; + } + let file_path = self.get_file_path(path); #[cfg(not(feature = "sync"))] @@ -211,7 +229,7 @@ impl ModuleResolver for FileModuleResolver { let file_path = self.get_file_path(path); // See if it is cached - { + if self.is_cache_enabled() { #[cfg(not(feature = "sync"))] let c = self.cache.borrow(); #[cfg(feature = "sync")] @@ -242,10 +260,12 @@ impl ModuleResolver for FileModuleResolver { .into(); // Put it into the cache - #[cfg(not(feature = "sync"))] - self.cache.borrow_mut().insert(file_path, m.clone()); - #[cfg(feature = "sync")] - self.cache.write().unwrap().insert(file_path, m.clone()); + if self.is_cache_enabled() { + #[cfg(not(feature = "sync"))] + self.cache.borrow_mut().insert(file_path, m.clone()); + #[cfg(feature = "sync")] + self.cache.write().unwrap().insert(file_path, m.clone()); + } Ok(m) }