Limit modules loading.

This commit is contained in:
Stephen Chung
2020-05-15 21:40:54 +08:00
parent 55c97eb649
commit be97047e51
15 changed files with 147 additions and 69 deletions

View File

@@ -72,13 +72,60 @@ fn test_module_resolver() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r#"
import "hello" as h;
h::answer
import "hello" as h1;
import "hello" as h2;
h2::answer
"#
)?,
42
);
engine.set_max_modules(5);
assert!(matches!(
*engine
.eval::<()>(
r#"
for x in range(0, 10) {
import "hello" as h;
}
"#
)
.expect_err("should error"),
EvalAltResult::ErrorTooManyModules(_)
));
assert!(matches!(
*engine
.eval::<()>(
r#"
fn foo() {
import "hello" as h;
}
for x in range(0, 10) {
foo();
}
"#
)
.expect_err("should error"),
EvalAltResult::ErrorInFunctionCall(fn_name, _, _) if fn_name == "foo"
));
engine.set_max_modules(0);
engine.eval::<()>(
r#"
fn foo() {
import "hello" as h;
}
for x in range(0, 10) {
foo();
}
"#,
)?;
Ok(())
}