Enable/disable caching in FileModuleResolver.
This commit is contained in:
parent
728ed81173
commit
9b37d84a9b
@ -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.
|
* 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.
|
* `Dynamic::as_unit` just for completeness sake.
|
||||||
* `bytes` method added for strings to get length quickly (if the string is ASCII-only).
|
* `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
|
Version 0.19.13
|
||||||
|
@ -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"))]
|
#[cfg(not(feature = "no_optimize"))]
|
||||||
use rhai::OptimizationLevel;
|
use rhai::OptimizationLevel;
|
||||||
@ -56,12 +58,19 @@ fn print_help() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut engine = Engine::new();
|
|
||||||
|
|
||||||
println!("Rhai REPL tool");
|
println!("Rhai REPL tool");
|
||||||
println!("==============");
|
println!("==============");
|
||||||
print_help();
|
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
|
// Load init scripts
|
||||||
|
|
||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
@ -130,6 +139,9 @@ fn main() {
|
|||||||
let mut ast_u: AST = Default::default();
|
let mut ast_u: AST = Default::default();
|
||||||
let mut ast: AST = Default::default();
|
let mut ast: AST = Default::default();
|
||||||
|
|
||||||
|
// Make Engine immutable
|
||||||
|
let engine = engine;
|
||||||
|
|
||||||
// REPL loop
|
// REPL loop
|
||||||
|
|
||||||
'main_loop: loop {
|
'main_loop: loop {
|
||||||
|
@ -41,6 +41,7 @@ use crate::{Engine, EvalAltResult, Module, ModuleResolver, Position, Shared};
|
|||||||
pub struct FileModuleResolver {
|
pub struct FileModuleResolver {
|
||||||
base_path: PathBuf,
|
base_path: PathBuf,
|
||||||
extension: String,
|
extension: String,
|
||||||
|
cache_enabled: bool,
|
||||||
|
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
cache: crate::stdlib::cell::RefCell<HashMap<PathBuf, Shared<Module>>>,
|
cache: crate::stdlib::cell::RefCell<HashMap<PathBuf, Shared<Module>>>,
|
||||||
@ -101,6 +102,7 @@ impl FileModuleResolver {
|
|||||||
Self {
|
Self {
|
||||||
base_path: path.into(),
|
base_path: path.into(),
|
||||||
extension: extension.into(),
|
extension: extension.into(),
|
||||||
|
cache_enabled: true,
|
||||||
cache: Default::default(),
|
cache: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,9 +154,25 @@ impl FileModuleResolver {
|
|||||||
self
|
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?
|
/// Is a particular path cached?
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn is_cached(&self, path: &str) -> bool {
|
pub fn is_cached(&self, path: &str) -> bool {
|
||||||
|
if !self.cache_enabled {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
let file_path = self.get_file_path(path);
|
let file_path = self.get_file_path(path);
|
||||||
|
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
@ -211,7 +229,7 @@ impl ModuleResolver for FileModuleResolver {
|
|||||||
let file_path = self.get_file_path(path);
|
let file_path = self.get_file_path(path);
|
||||||
|
|
||||||
// See if it is cached
|
// See if it is cached
|
||||||
{
|
if self.is_cache_enabled() {
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
let c = self.cache.borrow();
|
let c = self.cache.borrow();
|
||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
@ -242,10 +260,12 @@ impl ModuleResolver for FileModuleResolver {
|
|||||||
.into();
|
.into();
|
||||||
|
|
||||||
// Put it into the cache
|
// Put it into the cache
|
||||||
|
if self.is_cache_enabled() {
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
self.cache.borrow_mut().insert(file_path, m.clone());
|
self.cache.borrow_mut().insert(file_path, m.clone());
|
||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
self.cache.write().unwrap().insert(file_path, m.clone());
|
self.cache.write().unwrap().insert(file_path, m.clone());
|
||||||
|
}
|
||||||
|
|
||||||
Ok(m)
|
Ok(m)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user