Put source into AST.

This commit is contained in:
Stephen Chung 2021-01-03 13:30:01 +08:00
parent d3894e8aef
commit 33f7c8557c
2 changed files with 33 additions and 42 deletions

View File

@ -89,11 +89,13 @@ fn main() {
}
}
let mut module = match engine
let module = match engine
.compile(&contents)
.map_err(|err| err.into())
.and_then(|ast| Module::eval_ast_as_new(Default::default(), &ast, &engine))
{
.and_then(|mut ast| {
ast.set_source(Some(&filename));
Module::eval_ast_as_new(Default::default(), &ast, &engine)
}) {
Err(err) => {
eprintln!("{:=<1$}", "", filename.len());
eprintln!("{}", filename);
@ -106,8 +108,6 @@ fn main() {
Ok(m) => m,
};
module.set_id(Some(&filename));
engine.register_global_module(module.into());
has_init_scripts = true;

View File

@ -188,27 +188,22 @@ impl ModuleResolver for FileModuleResolver {
file_path.push(path);
file_path.set_extension(&self.extension); // Force extension
let scope = Default::default();
// See if it is cached
let mut module: Option<Shared<Module>> = None;
let mut module_ref = {
{
#[cfg(not(feature = "sync"))]
let c = self.cache.borrow();
#[cfg(feature = "sync")]
let c = self.cache.read().unwrap();
if let Some(module) = c.get(&file_path) {
Some(module.clone())
} else {
None
return Ok(module.clone());
}
}
};
if module_ref.is_none() {
// Load the script file and compile it
let ast = engine
let scope = Default::default();
let mut ast = engine
.compile_file(file_path.clone())
.map_err(|err| match *err {
EvalAltResult::ErrorSystem(_, err) if err.is::<IoError>() => {
@ -217,23 +212,19 @@ impl ModuleResolver for FileModuleResolver {
_ => Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos)),
})?;
let mut m = Module::eval_ast_as_new(scope, &ast, engine).map_err(|err| {
Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos))
})?;
ast.set_source(Some(path));
m.set_id(Some(path));
module = Some(m.into());
module_ref = module.clone();
};
// Make a module from the AST
let m: Shared<Module> = Module::eval_ast_as_new(scope, &ast, engine)
.map_err(|err| Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos)))?
.into();
if let Some(module) = module {
// Put it into the cache
#[cfg(not(feature = "sync"))]
self.cache.borrow_mut().insert(file_path, module);
self.cache.borrow_mut().insert(file_path, m.clone());
#[cfg(feature = "sync")]
self.cache.write().unwrap().insert(file_path, module);
}
Ok(module_ref.unwrap())
Ok(m)
}
}