2020-05-05 09:00:10 +02:00
|
|
|
#![cfg(not(feature = "no_module"))]
|
2020-06-12 12:04:16 +02:00
|
|
|
use rhai::{
|
2020-07-17 08:50:23 +02:00
|
|
|
module_resolvers::StaticModuleResolver, Dynamic, Engine, EvalAltResult, Module, ParseError,
|
2020-06-27 17:56:24 +02:00
|
|
|
ParseErrorType, Scope, INT,
|
2020-06-12 12:04:16 +02:00
|
|
|
};
|
2020-05-05 09:00:10 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_module() {
|
|
|
|
let mut module = Module::new();
|
2020-05-05 11:51:40 +02:00
|
|
|
module.set_var("answer", 42 as INT);
|
2020-05-05 09:00:10 +02:00
|
|
|
|
2020-05-05 11:51:40 +02:00
|
|
|
assert!(module.contains_var("answer"));
|
|
|
|
assert_eq!(module.get_var_value::<INT>("answer").unwrap(), 42);
|
2020-05-05 09:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-05-05 17:57:25 +02:00
|
|
|
fn test_module_sub_module() -> Result<(), Box<EvalAltResult>> {
|
2020-05-05 09:00:10 +02:00
|
|
|
let mut module = Module::new();
|
|
|
|
|
|
|
|
let mut sub_module = Module::new();
|
|
|
|
|
|
|
|
let mut sub_module2 = Module::new();
|
2020-05-05 11:51:40 +02:00
|
|
|
sub_module2.set_var("answer", 41 as INT);
|
2020-06-15 15:49:02 +02:00
|
|
|
|
2020-05-09 18:19:13 +02:00
|
|
|
let hash_inc = sub_module2.set_fn_1("inc", |x: INT| Ok(x + 1));
|
2020-05-05 09:00:10 +02:00
|
|
|
|
2020-05-05 11:51:40 +02:00
|
|
|
sub_module.set_sub_module("universe", sub_module2);
|
|
|
|
module.set_sub_module("life", sub_module);
|
2020-07-17 08:50:23 +02:00
|
|
|
module.set_var("MYSTIC_NUMBER", Dynamic::from(42 as INT));
|
2020-05-05 09:00:10 +02:00
|
|
|
|
2020-05-05 11:51:40 +02:00
|
|
|
assert!(module.contains_sub_module("life"));
|
|
|
|
let m = module.get_sub_module("life").unwrap();
|
2020-05-05 09:00:10 +02:00
|
|
|
|
2020-05-05 11:51:40 +02:00
|
|
|
assert!(m.contains_sub_module("universe"));
|
|
|
|
let m2 = m.get_sub_module("universe").unwrap();
|
2020-05-05 09:00:10 +02:00
|
|
|
|
2020-05-05 11:51:40 +02:00
|
|
|
assert!(m2.contains_var("answer"));
|
2020-05-09 05:29:30 +02:00
|
|
|
assert!(m2.contains_fn(hash_inc));
|
2020-05-05 11:51:40 +02:00
|
|
|
|
|
|
|
assert_eq!(m2.get_var_value::<INT>("answer").unwrap(), 41);
|
|
|
|
|
2020-06-27 17:56:24 +02:00
|
|
|
let mut resolver = StaticModuleResolver::new();
|
|
|
|
resolver.insert("question", module);
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_module_resolver(Some(resolver));
|
2020-05-05 11:51:40 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
2020-07-17 08:50:23 +02:00
|
|
|
engine.eval::<INT>(r#"import "question" as q; q::MYSTIC_NUMBER"#)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(r#"import "question" as q; q::life::universe::answer + 1"#)?,
|
2020-05-05 11:51:40 +02:00
|
|
|
42
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2020-07-17 08:50:23 +02:00
|
|
|
engine.eval::<INT>(
|
2020-06-27 17:56:24 +02:00
|
|
|
r#"import "question" as q; q::life::universe::inc(q::life::universe::answer)"#
|
2020-05-05 11:51:40 +02:00
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
2020-05-05 09:00:10 +02:00
|
|
|
}
|
2020-05-05 17:57:25 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_module_resolver() -> Result<(), Box<EvalAltResult>> {
|
2020-06-27 17:56:24 +02:00
|
|
|
let mut resolver = StaticModuleResolver::new();
|
2020-05-05 17:57:25 +02:00
|
|
|
|
|
|
|
let mut module = Module::new();
|
2020-07-12 05:46:53 +02:00
|
|
|
|
2020-05-05 17:57:25 +02:00
|
|
|
module.set_var("answer", 42 as INT);
|
2020-05-28 08:08:21 +02:00
|
|
|
module.set_fn_4("sum".to_string(), |x: INT, y: INT, z: INT, w: INT| {
|
|
|
|
Ok(x + y + z + w)
|
|
|
|
});
|
2020-07-08 07:06:00 +02:00
|
|
|
module.set_fn_1_mut("double".to_string(), |x: &mut INT| {
|
|
|
|
*x *= 2;
|
|
|
|
Ok(())
|
|
|
|
});
|
2020-05-05 17:57:25 +02:00
|
|
|
|
2020-06-27 17:56:24 +02:00
|
|
|
resolver.insert("hello", module);
|
2020-05-05 17:57:25 +02:00
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
2020-05-06 10:09:44 +02:00
|
|
|
engine.set_module_resolver(Some(resolver));
|
2020-05-05 17:57:25 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
2020-05-15 15:40:54 +02:00
|
|
|
import "hello" as h1;
|
|
|
|
import "hello" as h2;
|
2020-05-28 08:08:21 +02:00
|
|
|
h1::sum(h2::answer, -10, 3, 7)
|
2020-06-27 15:19:53 +02:00
|
|
|
"#
|
2020-05-05 17:57:25 +02:00
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
2020-07-08 07:06:00 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
import "hello" as h;
|
|
|
|
let x = 21;
|
|
|
|
h::double(x);
|
|
|
|
x
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
2020-05-23 18:29:06 +02:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
{
|
|
|
|
engine.set_max_modules(5);
|
|
|
|
|
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
|
|
|
.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let sum = 0;
|
|
|
|
|
|
|
|
for x in range(0, 10) {
|
|
|
|
import "hello" as h;
|
|
|
|
sum += h::answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
sum
|
2020-06-27 15:19:53 +02:00
|
|
|
"#
|
2020-05-23 18:29:06 +02:00
|
|
|
)
|
|
|
|
.expect_err("should error"),
|
|
|
|
EvalAltResult::ErrorTooManyModules(_)
|
|
|
|
));
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
|
|
|
.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let sum = 0;
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
import "hello" as h;
|
|
|
|
sum += h::answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
for x in range(0, 10) {
|
|
|
|
foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
sum
|
2020-06-27 15:19:53 +02:00
|
|
|
"#
|
2020-05-23 18:29:06 +02:00
|
|
|
)
|
|
|
|
.expect_err("should error"),
|
|
|
|
EvalAltResult::ErrorInFunctionCall(fn_name, _, _) if fn_name == "foo"
|
|
|
|
));
|
2020-05-15 15:40:54 +02:00
|
|
|
|
2020-06-14 08:25:47 +02:00
|
|
|
engine.set_max_modules(1000);
|
2020-05-17 16:19:49 +02:00
|
|
|
|
2020-05-23 18:29:06 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
engine.eval::<()>(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
import "hello" as h;
|
|
|
|
}
|
2020-05-17 16:19:49 +02:00
|
|
|
|
2020-05-23 18:29:06 +02:00
|
|
|
for x in range(0, 10) {
|
|
|
|
foo();
|
|
|
|
}
|
2020-06-27 15:19:53 +02:00
|
|
|
"#,
|
2020-05-23 18:29:06 +02:00
|
|
|
)?;
|
|
|
|
}
|
2020-05-15 15:40:54 +02:00
|
|
|
|
2020-05-05 17:57:25 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-05-06 17:00:26 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-05-12 10:32:22 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-05-06 17:00:26 +02:00
|
|
|
fn test_module_from_ast() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
2020-06-27 17:56:24 +02:00
|
|
|
let mut resolver1 = StaticModuleResolver::new();
|
2020-05-06 17:00:26 +02:00
|
|
|
let mut sub_module = Module::new();
|
|
|
|
sub_module.set_var("foo", true);
|
2020-06-27 17:56:24 +02:00
|
|
|
resolver1.insert("another module", sub_module);
|
2020-05-06 17:00:26 +02:00
|
|
|
|
|
|
|
let ast = engine.compile(
|
|
|
|
r#"
|
2020-06-27 15:19:53 +02:00
|
|
|
// Functions become module functions
|
|
|
|
fn calc(x) {
|
|
|
|
x + 1
|
|
|
|
}
|
|
|
|
fn add_len(x, y) {
|
|
|
|
x + len(y)
|
|
|
|
}
|
|
|
|
private fn hidden() {
|
|
|
|
throw "you shouldn't see me!";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Imported modules become sub-modules
|
|
|
|
import "another module" as extra;
|
|
|
|
|
|
|
|
// Variables defined at global level become module variables
|
|
|
|
const x = 123;
|
|
|
|
let foo = 41;
|
|
|
|
let hello;
|
|
|
|
|
|
|
|
// Final variable values become constant module variable values
|
|
|
|
foo = calc(foo);
|
|
|
|
hello = "hello, " + foo + " worlds!";
|
|
|
|
|
|
|
|
export
|
|
|
|
x as abc,
|
|
|
|
foo,
|
2020-06-27 17:56:24 +02:00
|
|
|
hello;
|
2020-06-27 15:19:53 +02:00
|
|
|
"#,
|
2020-05-06 17:00:26 +02:00
|
|
|
)?;
|
|
|
|
|
2020-06-27 17:56:24 +02:00
|
|
|
engine.set_module_resolver(Some(resolver1));
|
|
|
|
|
2020-05-08 16:38:56 +02:00
|
|
|
let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?;
|
2020-05-06 17:00:26 +02:00
|
|
|
|
2020-06-27 17:56:24 +02:00
|
|
|
let mut resolver2 = StaticModuleResolver::new();
|
|
|
|
resolver2.insert("testing", module);
|
|
|
|
engine.set_module_resolver(Some(resolver2));
|
2020-06-01 04:58:14 +02:00
|
|
|
|
2020-05-06 17:00:26 +02:00
|
|
|
assert_eq!(
|
2020-07-17 08:50:23 +02:00
|
|
|
engine.eval::<INT>(r#"import "testing" as ttt; ttt::abc"#)?,
|
2020-05-06 17:00:26 +02:00
|
|
|
123
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2020-07-17 08:50:23 +02:00
|
|
|
engine.eval::<INT>(r#"import "testing" as ttt; ttt::foo"#)?,
|
2020-05-06 17:00:26 +02:00
|
|
|
42
|
|
|
|
);
|
2020-07-17 08:50:23 +02:00
|
|
|
assert!(engine.eval::<bool>(r#"import "testing" as ttt; ttt::extra::foo"#)?);
|
2020-05-06 17:00:26 +02:00
|
|
|
assert_eq!(
|
2020-07-17 08:50:23 +02:00
|
|
|
engine.eval::<String>(r#"import "testing" as ttt; ttt::hello"#)?,
|
2020-05-06 17:00:26 +02:00
|
|
|
"hello, 42 worlds!"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2020-07-17 08:50:23 +02:00
|
|
|
engine.eval::<INT>(r#"import "testing" as ttt; ttt::calc(999)"#)?,
|
2020-05-06 17:00:26 +02:00
|
|
|
1000
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2020-07-17 08:50:23 +02:00
|
|
|
engine.eval::<INT>(r#"import "testing" as ttt; ttt::add_len(ttt::foo, ttt::hello)"#)?,
|
2020-05-06 17:00:26 +02:00
|
|
|
59
|
|
|
|
);
|
2020-05-09 05:29:30 +02:00
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2020-07-17 08:50:23 +02:00
|
|
|
.consume(r#"import "testing" as ttt; ttt::hidden()"#)
|
2020-05-09 05:29:30 +02:00
|
|
|
.expect_err("should error"),
|
2020-07-26 09:53:22 +02:00
|
|
|
EvalAltResult::ErrorFunctionNotFound(fn_name, _) if fn_name == "ttt::hidden ()"
|
2020-05-09 05:29:30 +02:00
|
|
|
));
|
2020-05-06 17:00:26 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-06-12 12:04:16 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_module_export() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let engine = Engine::new();
|
|
|
|
|
|
|
|
assert!(matches!(
|
|
|
|
engine.compile(r"let x = 10; { export x; }").expect_err("should error"),
|
|
|
|
ParseError(x, _) if *x == ParseErrorType::WrongExport
|
|
|
|
));
|
|
|
|
|
2020-06-30 12:34:58 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-06-12 12:04:16 +02:00
|
|
|
assert!(matches!(
|
|
|
|
engine.compile(r"fn abc(x) { export x; }").expect_err("should error"),
|
|
|
|
ParseError(x, _) if *x == ParseErrorType::WrongExport
|
|
|
|
));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|