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 |
|
| [`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 |
|
| [`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 |
|
| [`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] |
|
| [`oop.rhai`]({{repoTree}}/scripts/oop.rhai) | simulate [object-oriented programming (OOP)][OOP] with [closures] |
|
||||||
| [`op1.rhai`]({{repoTree}}/scripts/op1.rhai) | just simple addition |
|
| [`op1.rhai`]({{repoTree}}/scripts/op1.rhai) | just simple addition |
|
||||||
| [`op2.rhai`]({{repoTree}}/scripts/op2.rhai) | simple addition and multiplication |
|
| [`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::result::EvalAltResult;
|
||||||
use crate::token::Position;
|
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.
|
/// Module resolution service that loads module script files from the file system.
|
||||||
///
|
///
|
||||||
@ -135,34 +137,42 @@ impl ModuleResolver for FileModuleResolver {
|
|||||||
let scope = Default::default();
|
let scope = Default::default();
|
||||||
|
|
||||||
// See if it is cached
|
// 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"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
let c = self.cache.borrow();
|
let c = self.cache.borrow();
|
||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
let c = self.cache.read().unwrap();
|
let c = self.cache.read().unwrap();
|
||||||
|
|
||||||
if let Some(module) = c.get(&file_path) {
|
if let Some(module) = c.get(&file_path) {
|
||||||
module.clone()
|
Some(module.clone())
|
||||||
} else {
|
} else {
|
||||||
// Load the file and compile it if not found
|
None
|
||||||
let ast = engine.compile_file(file_path.clone()).map_err(|err| {
|
|
||||||
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.build_index();
|
|
||||||
|
|
||||||
let m: Shared<Module> = m.into();
|
|
||||||
module = Some(m.clone());
|
|
||||||
m
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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.build_index();
|
||||||
|
|
||||||
|
module = Some(m.into());
|
||||||
|
module_ref = module.clone();
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(module) = module {
|
if let Some(module) = module {
|
||||||
// Put it into the cache
|
// Put it into the cache
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
@ -171,6 +181,6 @@ impl ModuleResolver for FileModuleResolver {
|
|||||||
self.cache.write().unwrap().insert(file_path, module);
|
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(())
|
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