Remove modules from Scope and use separate stack.

This commit is contained in:
Stephen Chung
2020-06-27 23:56:24 +08:00
parent f3bde843cb
commit aac04a5e53
11 changed files with 434 additions and 334 deletions

View File

@@ -1,6 +1,7 @@
#![cfg(not(feature = "no_module"))]
use rhai::{
module_resolvers, Engine, EvalAltResult, Module, ParseError, ParseErrorType, Scope, INT,
module_resolvers::StaticModuleResolver, Engine, EvalAltResult, Module, ParseError,
ParseErrorType, Scope, INT,
};
#[test]
@@ -37,22 +38,25 @@ fn test_module_sub_module() -> Result<(), Box<EvalAltResult>> {
assert_eq!(m2.get_var_value::<INT>("answer").unwrap(), 41);
let engine = Engine::new();
let mut resolver = StaticModuleResolver::new();
resolver.insert("question", module);
let mut engine = Engine::new();
engine.set_module_resolver(Some(resolver));
let mut scope = Scope::new();
scope.push_module("question", module);
assert_eq!(
engine.eval_expression_with_scope::<INT>(
engine.eval_with_scope::<INT>(
&mut scope,
"question::life::universe::answer + 1"
r#"import "question" as q; q::life::universe::answer + 1"#
)?,
42
);
assert_eq!(
engine.eval_expression_with_scope::<INT>(
engine.eval_with_scope::<INT>(
&mut scope,
"question::life::universe::inc(question::life::universe::answer)"
r#"import "question" as q; q::life::universe::inc(q::life::universe::answer)"#
)?,
42
);
@@ -62,7 +66,7 @@ fn test_module_sub_module() -> Result<(), Box<EvalAltResult>> {
#[test]
fn test_module_resolver() -> Result<(), Box<EvalAltResult>> {
let mut resolver = module_resolvers::StaticModuleResolver::new();
let mut resolver = StaticModuleResolver::new();
let mut module = Module::new();
module.set_var("answer", 42 as INT);
@@ -70,7 +74,7 @@ fn test_module_resolver() -> Result<(), Box<EvalAltResult>> {
Ok(x + y + z + w)
});
resolver.insert("hello".to_string(), module);
resolver.insert("hello", module);
let mut engine = Engine::new();
engine.set_module_resolver(Some(resolver));
@@ -155,12 +159,10 @@ fn test_module_resolver() -> Result<(), Box<EvalAltResult>> {
fn test_module_from_ast() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let mut resolver = rhai::module_resolvers::StaticModuleResolver::new();
let mut resolver1 = StaticModuleResolver::new();
let mut sub_module = Module::new();
sub_module.set_var("foo", true);
resolver.insert("another module".to_string(), sub_module);
engine.set_module_resolver(Some(resolver));
resolver1.insert("another module", sub_module);
let ast = engine.compile(
r#"
@@ -190,44 +192,48 @@ fn test_module_from_ast() -> Result<(), Box<EvalAltResult>> {
export
x as abc,
foo,
hello,
extra as foobar;
hello;
"#,
)?;
engine.set_module_resolver(Some(resolver1));
let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?;
let mut resolver2 = StaticModuleResolver::new();
resolver2.insert("testing", module);
engine.set_module_resolver(Some(resolver2));
let mut scope = Scope::new();
scope.push_module("testing", module);
assert_eq!(
engine.eval_expression_with_scope::<INT>(&mut scope, "testing::abc")?,
engine.eval_with_scope::<INT>(&mut scope, r#"import "testing" as ttt; ttt::abc"#)?,
123
);
assert_eq!(
engine.eval_expression_with_scope::<INT>(&mut scope, "testing::foo")?,
engine.eval_with_scope::<INT>(&mut scope, r#"import "testing" as ttt; ttt::foo"#)?,
42
);
assert!(engine.eval_expression_with_scope::<bool>(&mut scope, "testing::foobar::foo")?);
assert!(engine
.eval_with_scope::<bool>(&mut scope, r#"import "testing" as ttt; ttt::extra::foo"#)?);
assert_eq!(
engine.eval_expression_with_scope::<String>(&mut scope, "testing::hello")?,
engine.eval_with_scope::<String>(&mut scope, r#"import "testing" as ttt; ttt::hello"#)?,
"hello, 42 worlds!"
);
assert_eq!(
engine.eval_expression_with_scope::<INT>(&mut scope, "testing::calc(999)")?,
engine.eval_with_scope::<INT>(&mut scope, r#"import "testing" as ttt; ttt::calc(999)"#)?,
1000
);
assert_eq!(
engine.eval_expression_with_scope::<INT>(
engine.eval_with_scope::<INT>(
&mut scope,
"testing::add_len(testing::foo, testing::hello)"
r#"import "testing" as ttt; ttt::add_len(ttt::foo, ttt::hello)"#
)?,
59
);
assert!(matches!(
*engine
.eval_expression_with_scope::<()>(&mut scope, "testing::hidden()")
.eval_with_scope::<()>(&mut scope, r#"import "testing" as ttt; ttt::hidden()"#)
.expect_err("should error"),
EvalAltResult::ErrorFunctionNotFound(fn_name, _) if fn_name == "hidden"
));

View File

@@ -38,19 +38,13 @@ fn test_optimizer_parse() -> Result<(), Box<EvalAltResult>> {
let ast = engine.compile("{ const DECISION = false; if DECISION { 42 } }")?;
assert_eq!(
format!("{:?}", ast),
"AST([], <module vars={}, functions=0>)"
);
assert!(format!("{:?}", ast).starts_with("AST([], Module("));
engine.set_optimization_level(OptimizationLevel::Full);
let ast = engine.compile("if 1 == 2 { 42 }")?;
assert_eq!(
format!("{:?}", ast),
"AST([], <module vars={}, functions=0>)"
);
assert!(format!("{:?}", ast).starts_with("AST([], Module("));
Ok(())
}