Module resolver returns shared module.

This commit is contained in:
Stephen Chung
2020-11-07 23:33:21 +08:00
parent d5d70367fa
commit b3d318ef7f
20 changed files with 158 additions and 136 deletions

View File

@@ -329,7 +329,7 @@ impl Module {
&& fn_name == name
},
)
.map(|FuncInfo { func, .. }| func.get_shared_fn_def())
.map(|FuncInfo { func, .. }| func.get_fn_def())
}
/// Does a sub-module exist in the module?
@@ -1286,7 +1286,7 @@ impl Module {
.values()
.map(|f| &f.func)
.filter(|f| f.is_script())
.map(CallableFunction::get_shared_fn_def)
.map(CallableFunction::get_fn_def)
.map(|f| {
let func = f.clone();
(f.access, f.name.as_str(), f.params.len(), func)
@@ -1374,7 +1374,7 @@ impl Module {
// Modules left in the scope become sub-modules
mods.into_iter().for_each(|(alias, m)| {
module.modules.insert(alias.to_string(), m);
module.modules.insert(alias.to_string(), m.as_ref().clone());
});
// Non-private functions defined become module functions

View File

@@ -1,4 +1,5 @@
use crate::engine::Engine;
use crate::fn_native::Shared;
use crate::module::{Module, ModuleResolver};
use crate::result::EvalAltResult;
use crate::token::Position;
@@ -90,7 +91,7 @@ impl ModuleResolver for ModuleResolversCollection {
engine: &Engine,
path: &str,
pos: Position,
) -> Result<Module, Box<EvalAltResult>> {
) -> Result<Shared<Module>, Box<EvalAltResult>> {
for resolver in self.0.iter() {
match resolver.resolve(engine, path, pos) {
Ok(module) => return Ok(module),

View File

@@ -1,6 +1,5 @@
use crate::ast::AST;
use crate::engine::Engine;
use crate::fn_native::Locked;
use crate::fn_native::{Locked, Shared};
use crate::module::{Module, ModuleResolver};
use crate::result::EvalAltResult;
use crate::token::Position;
@@ -40,7 +39,7 @@ use crate::stdlib::{boxed::Box, collections::HashMap, path::PathBuf, string::Str
pub struct FileModuleResolver {
path: PathBuf,
extension: String,
cache: Locked<HashMap<PathBuf, AST>>,
cache: Locked<HashMap<PathBuf, Shared<Module>>>,
}
impl Default for FileModuleResolver {
@@ -119,16 +118,6 @@ impl FileModuleResolver {
pub fn new() -> Self {
Default::default()
}
/// Create a `Module` from a file path.
#[inline(always)]
pub fn create_module<P: Into<PathBuf>>(
&self,
engine: &Engine,
path: &str,
) -> Result<Module, Box<EvalAltResult>> {
self.resolve(engine, path, Default::default())
}
}
impl ModuleResolver for FileModuleResolver {
@@ -137,48 +126,51 @@ impl ModuleResolver for FileModuleResolver {
engine: &Engine,
path: &str,
pos: Position,
) -> Result<Module, Box<EvalAltResult>> {
) -> Result<Shared<Module>, Box<EvalAltResult>> {
// Construct the script file path
let mut file_path = self.path.clone();
file_path.push(path);
file_path.set_extension(&self.extension); // Force extension
let scope = Default::default();
let module;
// See if it is cached
let ast = {
let mut module = None;
let module_ref = {
#[cfg(not(feature = "sync"))]
let c = self.cache.borrow();
#[cfg(feature = "sync")]
let c = self.cache.read().unwrap();
if let Some(ast) = c.get(&file_path) {
module = Module::eval_ast_as_new(scope, ast, engine).map_err(|err| {
Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos))
})?;
None
if let Some(module) = c.get(&file_path) {
module.clone()
} else {
// Load the file and compile it if not found
let ast = engine.compile_file(file_path.clone()).map_err(|err| {
Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos))
})?;
module = Module::eval_ast_as_new(scope, &ast, engine).map_err(|err| {
let mut m = Module::eval_ast_as_new(scope, &ast, engine).map_err(|err| {
Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos))
})?;
Some(ast)
m.index_all_sub_modules();
let m: Shared<Module> = m.into();
module = Some(m.clone());
m
}
};
if let Some(ast) = ast {
if let Some(module) = module {
// Put it into the cache
#[cfg(not(feature = "sync"))]
self.cache.borrow_mut().insert(file_path, ast);
self.cache.borrow_mut().insert(file_path, module);
#[cfg(feature = "sync")]
self.cache.write().unwrap().insert(file_path, ast);
self.cache.write().unwrap().insert(file_path, module);
}
Ok(module)
Ok(module_ref)
}
}

View File

@@ -1,5 +1,5 @@
use crate::engine::Engine;
use crate::fn_native::SendSync;
use crate::fn_native::{SendSync, Shared};
use crate::module::Module;
use crate::result::EvalAltResult;
use crate::token::Position;
@@ -28,5 +28,5 @@ pub trait ModuleResolver: SendSync {
engine: &Engine,
path: &str,
pos: Position,
) -> Result<Module, Box<EvalAltResult>>;
) -> Result<Shared<Module>, Box<EvalAltResult>>;
}

View File

@@ -1,4 +1,5 @@
use crate::engine::Engine;
use crate::fn_native::Shared;
use crate::module::{Module, ModuleResolver};
use crate::result::EvalAltResult;
use crate::token::Position;
@@ -23,7 +24,7 @@ use crate::stdlib::{boxed::Box, collections::HashMap, ops::AddAssign, string::St
/// engine.set_module_resolver(Some(resolver));
/// ```
#[derive(Debug, Clone, Default)]
pub struct StaticModuleResolver(HashMap<String, Module>);
pub struct StaticModuleResolver(HashMap<String, Shared<Module>>);
impl StaticModuleResolver {
/// Create a new `StaticModuleResolver`.
@@ -48,12 +49,13 @@ impl StaticModuleResolver {
}
/// Add a module keyed by its path.
#[inline(always)]
pub fn insert(&mut self, path: impl Into<String>, module: Module) {
self.0.insert(path.into(), module);
pub fn insert(&mut self, path: impl Into<String>, mut module: Module) {
module.index_all_sub_modules();
self.0.insert(path.into(), module.into());
}
/// Remove a module given its path.
#[inline(always)]
pub fn remove(&mut self, path: &str) -> Option<Module> {
pub fn remove(&mut self, path: &str) -> Option<Shared<Module>> {
self.0.remove(path)
}
/// Does the path exist?
@@ -63,17 +65,12 @@ impl StaticModuleResolver {
}
/// Get an iterator of all the modules.
#[inline(always)]
pub fn iter(&self) -> impl Iterator<Item = (&str, &Module)> {
self.0.iter().map(|(k, v)| (k.as_str(), v))
pub fn iter(&self) -> impl Iterator<Item = (&str, Shared<Module>)> {
self.0.iter().map(|(k, v)| (k.as_str(), v.clone()))
}
/// Get a mutable iterator of all the modules.
#[inline(always)]
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut Module)> {
self.0.iter_mut().map(|(k, v)| (k.as_str(), v))
}
/// Get a mutable iterator of all the modules.
#[inline(always)]
pub fn into_iter(self) -> impl Iterator<Item = (String, Module)> {
pub fn into_iter(self) -> impl Iterator<Item = (String, Shared<Module>)> {
self.0.into_iter()
}
/// Get an iterator of all the module paths.
@@ -83,13 +80,8 @@ impl StaticModuleResolver {
}
/// Get an iterator of all the modules.
#[inline(always)]
pub fn values(&self) -> impl Iterator<Item = &Module> {
self.0.values()
}
/// Get a mutable iterator of all the modules.
#[inline(always)]
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut Module> {
self.0.values_mut()
pub fn values<'a>(&'a self) -> impl Iterator<Item = Shared<Module>> + 'a {
self.0.values().map(|m| m.clone())
}
/// Remove all modules.
#[inline(always)]
@@ -118,7 +110,12 @@ impl StaticModuleResolver {
impl ModuleResolver for StaticModuleResolver {
#[inline(always)]
fn resolve(&self, _: &Engine, path: &str, pos: Position) -> Result<Module, Box<EvalAltResult>> {
fn resolve(
&self,
_: &Engine,
path: &str,
pos: Position,
) -> Result<Shared<Module>, Box<EvalAltResult>> {
self.0
.get(path)
.cloned()