Fix recursive file import bug.
This commit is contained in:
parent
232ba27548
commit
487a073caf
@ -20,6 +20,7 @@ There are also a number of examples scripts that showcase Rhai's features, all i
|
||||
| [`function_decl3.rhai`]({{repoTree}}/scripts/function_decl3.rhai) | a [function] with many parameters |
|
||||
| [`if1.rhai`]({{repoTree}}/scripts/if1.rhai) | [`if`]({{rootUrl}}/language/if.md) example |
|
||||
| [`loop.rhai`]({{repoTree}}/scripts/loop.rhai) | count-down [`loop`]({{rootUrl}}/language/loop.md) in Rhai, emulating a `do` .. `while` loop |
|
||||
| [`module.rhai`]({{repoTree}}/scripts/module.rhai) | import a script file as a module |
|
||||
| [`oop.rhai`]({{repoTree}}/scripts/oop.rhai) | simulate [object-oriented programming (OOP)][OOP] with [closures] |
|
||||
| [`op1.rhai`]({{repoTree}}/scripts/op1.rhai) | just simple addition |
|
||||
| [`op2.rhai`]({{repoTree}}/scripts/op2.rhai) | simple addition and multiplication |
|
||||
|
3
scripts/module.rhai
Normal file
3
scripts/module.rhai
Normal file
@ -0,0 +1,3 @@
|
||||
import "scripts/loop";
|
||||
|
||||
print("Module test!");
|
@ -4,7 +4,9 @@ use crate::module::{Module, ModuleResolver};
|
||||
use crate::result::EvalAltResult;
|
||||
use crate::token::Position;
|
||||
|
||||
use crate::stdlib::{boxed::Box, collections::HashMap, path::PathBuf, string::String};
|
||||
use crate::stdlib::{
|
||||
boxed::Box, collections::HashMap, io::Error as IoError, path::PathBuf, string::String,
|
||||
};
|
||||
|
||||
/// Module resolution service that loads module script files from the file system.
|
||||
///
|
||||
@ -135,20 +137,30 @@ impl ModuleResolver for FileModuleResolver {
|
||||
let scope = Default::default();
|
||||
|
||||
// See if it is cached
|
||||
let mut module = None;
|
||||
let mut module: Option<Shared<Module>> = None;
|
||||
|
||||
let module_ref = {
|
||||
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) {
|
||||
module.clone()
|
||||
Some(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))
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
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| {
|
||||
@ -157,10 +169,8 @@ impl ModuleResolver for FileModuleResolver {
|
||||
|
||||
m.build_index();
|
||||
|
||||
let m: Shared<Module> = m.into();
|
||||
module = Some(m.clone());
|
||||
m
|
||||
}
|
||||
module = Some(m.into());
|
||||
module_ref = module.clone();
|
||||
};
|
||||
|
||||
if let Some(module) = module {
|
||||
@ -171,6 +181,6 @@ impl ModuleResolver for FileModuleResolver {
|
||||
self.cache.write().unwrap().insert(file_path, module);
|
||||
}
|
||||
|
||||
Ok(module_ref)
|
||||
Ok(module_ref.unwrap())
|
||||
}
|
||||
}
|
||||
|
@ -432,3 +432,16 @@ fn test_module_ast_namespace2() -> Result<(), Box<EvalAltResult>> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_module_file() -> Result<(), Box<EvalAltResult>> {
|
||||
let engine = Engine::new();
|
||||
let ast = engine.compile(
|
||||
r#"
|
||||
import "scripts/module";
|
||||
print("top");
|
||||
"#,
|
||||
)?;
|
||||
Module::eval_ast_as_new(Default::default(), &ast, &engine)?;
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user