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,52 +188,43 @@ 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
.compile_file(file_path.clone())
.map_err(|err| match *err {
EvalAltResult::ErrorSystem(_, err) if err.is::<IoError>() => {
Box::new(EvalAltResult::ErrorModuleNotFound(path.to_string(), pos))
}
_ => 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))
})?;
m.set_id(Some(path));
module = Some(m.into());
module_ref = module.clone();
};
if let Some(module) = module {
// Put it into the cache
#[cfg(not(feature = "sync"))]
self.cache.borrow_mut().insert(file_path, module);
#[cfg(feature = "sync")]
self.cache.write().unwrap().insert(file_path, module);
}
Ok(module_ref.unwrap())
// Load the script file and compile it
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>() => {
Box::new(EvalAltResult::ErrorModuleNotFound(path.to_string(), pos))
}
_ => Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos)),
})?;
ast.set_source(Some(path));
// 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();
// Put it into the cache
#[cfg(not(feature = "sync"))]
self.cache.borrow_mut().insert(file_path, m.clone());
#[cfg(feature = "sync")]
self.cache.write().unwrap().insert(file_path, module);
Ok(m)
}
}