Add scope to FileModuleResolver.
This commit is contained in:
parent
2755d39cdf
commit
71bc605fe6
@ -22,6 +22,7 @@ Enhancements
|
|||||||
* `Engine::parse_json` now natively handles nested JSON inputs (using a token remap filter) without needing to replace `{` with `#{`.
|
* `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)
|
* `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.
|
* 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
|
Version 1.6.1
|
||||||
|
@ -45,11 +45,12 @@ pub const RHAI_SCRIPT_EXTENSION: &str = "rhai";
|
|||||||
///
|
///
|
||||||
/// engine.set_module_resolver(resolver);
|
/// engine.set_module_resolver(resolver);
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct FileModuleResolver {
|
pub struct FileModuleResolver {
|
||||||
base_path: Option<PathBuf>,
|
base_path: Option<PathBuf>,
|
||||||
extension: Identifier,
|
extension: Identifier,
|
||||||
cache_enabled: bool,
|
cache_enabled: bool,
|
||||||
|
scope: Scope<'static>,
|
||||||
|
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
cache: std::cell::RefCell<BTreeMap<PathBuf, Shared<Module>>>,
|
cache: std::cell::RefCell<BTreeMap<PathBuf, Shared<Module>>>,
|
||||||
@ -57,6 +58,13 @@ pub struct FileModuleResolver {
|
|||||||
cache: std::sync::RwLock<BTreeMap<PathBuf, Shared<Module>>>,
|
cache: std::sync::RwLock<BTreeMap<PathBuf, Shared<Module>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for FileModuleResolver {
|
||||||
|
#[inline(always)]
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FileModuleResolver {
|
impl FileModuleResolver {
|
||||||
/// Create a new [`FileModuleResolver`] with the current directory as base path.
|
/// Create a new [`FileModuleResolver`] with the current directory as base path.
|
||||||
///
|
///
|
||||||
@ -126,6 +134,7 @@ impl FileModuleResolver {
|
|||||||
extension: extension.into(),
|
extension: extension.into(),
|
||||||
cache_enabled: true,
|
cache_enabled: true,
|
||||||
cache: BTreeMap::new().into(),
|
cache: BTreeMap::new().into(),
|
||||||
|
scope: Scope::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,6 +164,7 @@ impl FileModuleResolver {
|
|||||||
extension: extension.into(),
|
extension: extension.into(),
|
||||||
cache_enabled: true,
|
cache_enabled: true,
|
||||||
cache: BTreeMap::new().into(),
|
cache: BTreeMap::new().into(),
|
||||||
|
scope: Scope::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,6 +195,32 @@ impl FileModuleResolver {
|
|||||||
self
|
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.
|
/// Enable/disable the cache.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn enable_cache(&mut self, enable: bool) -> &mut Self {
|
pub fn enable_cache(&mut self, enable: bool) -> &mut Self {
|
||||||
@ -281,10 +317,8 @@ impl FileModuleResolver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let scope = Scope::new();
|
|
||||||
|
|
||||||
let mut ast = engine
|
let mut ast = engine
|
||||||
.compile_file(file_path.clone())
|
.compile_file_with_scope(&self.scope, file_path.clone())
|
||||||
.map_err(|err| match *err {
|
.map_err(|err| match *err {
|
||||||
ERR::ErrorSystem(.., err) if err.is::<IoError>() => {
|
ERR::ErrorSystem(.., err) if err.is::<IoError>() => {
|
||||||
Box::new(ERR::ErrorModuleNotFound(path.to_string(), pos))
|
Box::new(ERR::ErrorModuleNotFound(path.to_string(), pos))
|
||||||
@ -294,7 +328,9 @@ impl FileModuleResolver {
|
|||||||
|
|
||||||
ast.set_source(path);
|
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)
|
Module::eval_ast_as_new_raw(engine, scope, global, &ast)
|
||||||
} else {
|
} else {
|
||||||
Module::eval_ast_as_new(scope, &ast, engine)
|
Module::eval_ast_as_new(scope, &ast, engine)
|
||||||
|
@ -22,7 +22,7 @@ use std::{collections::btree_map::IntoIter, collections::BTreeMap, ops::AddAssig
|
|||||||
///
|
///
|
||||||
/// engine.set_module_resolver(resolver);
|
/// engine.set_module_resolver(resolver);
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct StaticModuleResolver(BTreeMap<Identifier, Shared<Module>>);
|
pub struct StaticModuleResolver(BTreeMap<Identifier, Shared<Module>>);
|
||||||
|
|
||||||
impl StaticModuleResolver {
|
impl StaticModuleResolver {
|
||||||
|
Loading…
Reference in New Issue
Block a user