Add scope to FileModuleResolver.

This commit is contained in:
Stephen Chung 2022-04-22 13:39:51 +08:00
parent 2755d39cdf
commit 71bc605fe6
3 changed files with 43 additions and 6 deletions

View File

@ -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

View File

@ -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<PathBuf>,
extension: Identifier,
cache_enabled: bool,
scope: Scope<'static>,
#[cfg(not(feature = "sync"))]
cache: std::cell::RefCell<BTreeMap<PathBuf, Shared<Module>>>,
@ -57,6 +58,13 @@ pub struct FileModuleResolver {
cache: std::sync::RwLock<BTreeMap<PathBuf, Shared<Module>>>,
}
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::<IoError>() => {
Box::new(ERR::ErrorModuleNotFound(path.to_string(), pos))
@ -294,7 +328,9 @@ impl FileModuleResolver {
ast.set_source(path);
let m: Shared<Module> = 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)

View File

@ -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<Identifier, Shared<Module>>);
impl StaticModuleResolver {