rhai/tests/modules.rs

660 lines
17 KiB
Rust
Raw Normal View History

2020-05-05 09:00:10 +02:00
#![cfg(not(feature = "no_module"))]
use rhai::{
2021-01-08 17:24:55 +01:00
module_resolvers::{DummyModuleResolver, StaticModuleResolver},
2022-12-30 18:07:39 +01:00
Dynamic, Engine, EvalAltResult, FnNamespace, ImmutableString, Module, ParseError,
ParseErrorType, Scope, INT,
};
2022-12-30 18:07:39 +01:00
//
#[cfg(all(not(feature = "no_function"), feature = "internals"))]
use rhai::{FnPtr, NativeCallContext};
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]
fn test_module_syntax() {
let engine = Engine::new();
assert!(engine.compile("abc.def::xyz").is_err());
assert!(engine.compile("abc.def::xyz()").is_err());
}
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);
2021-03-15 05:39:06 +01:00
let hash_inc = sub_module2.set_native_fn("inc", |x: &mut INT| Ok(*x + 1));
sub_module2.build_index();
assert!(!sub_module2.contains_indexed_global_functions());
2021-03-15 05:39:06 +01:00
let super_hash = sub_module2.set_native_fn("super_inc", |x: &mut INT| Ok(*x + 1));
sub_module2.update_fn_namespace(super_hash, FnNamespace::Global);
sub_module2.build_index();
assert!(sub_module2.contains_indexed_global_functions());
#[cfg(not(feature = "no_object"))]
sub_module2.set_getter_fn("doubled", |x: &mut INT| Ok(*x * 2));
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);
module.set_var("MYSTIC_NUMBER", Dynamic::from(42 as INT));
module.build_index();
2020-05-05 09:00:10 +02:00
assert!(module.contains_indexed_global_functions());
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"));
2021-03-17 02:58:08 +01: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);
module.set_custom_type::<()>("Don't Panic");
let mut engine = Engine::new();
engine.register_static_module("question", module.into());
2020-05-05 11:51:40 +02:00
assert_eq!(engine.eval::<String>("type_of(())")?, "Don't Panic");
assert_eq!(engine.eval::<INT>("question::MYSTIC_NUMBER")?, 42);
assert!(engine.eval::<INT>("MYSTIC_NUMBER").is_err());
2020-12-21 10:39:37 +01:00
assert_eq!(engine.eval::<INT>("question::life::universe::answer")?, 41);
2020-05-05 11:51:40 +02:00
assert_eq!(
engine.eval::<INT>("question::life::universe::answer + 1")?,
42
);
assert_eq!(
engine.eval::<INT>("question::life::universe::inc(question::life::universe::answer)")?,
2020-05-05 11:51:40 +02:00
42
);
assert!(engine
.eval::<INT>("inc(question::life::universe::answer)")
.is_err());
#[cfg(not(feature = "no_object"))]
2020-12-21 10:39:37 +01:00
assert_eq!(engine.eval::<INT>("question::MYSTIC_NUMBER.doubled")?, 84);
#[cfg(not(feature = "no_object"))]
assert_eq!(
engine.eval::<INT>("question::life::universe::answer.doubled")?,
82
);
2020-05-05 11:51:40 +02:00
assert_eq!(
engine.eval::<INT>("super_inc(question::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>> {
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);
2021-03-15 05:39:06 +01:00
module.set_native_fn("sum", |x: INT, y: INT, z: INT, w: INT| Ok(x + y + z + w));
let double_hash = module.set_native_fn("double", |x: &mut INT| {
*x *= 2;
Ok(())
});
2021-03-15 05:39:06 +01:00
module.update_fn_namespace(double_hash, FnNamespace::Global);
2020-05-05 17:57:25 +02:00
2020-09-15 05:03:46 +02:00
#[cfg(not(feature = "no_float"))]
2021-03-15 05:39:06 +01:00
module.set_native_fn(
"sum_of_three_args",
2020-11-01 09:02:10 +01:00
|target: &mut INT, a: INT, b: INT, c: rhai::FLOAT| {
2020-09-15 05:03:46 +02:00
*target = a + b + c as INT;
Ok(())
},
2020-09-15 05:03:46 +02:00
);
resolver.insert("hello", module);
2020-05-05 17:57:25 +02:00
let mut engine = Engine::new();
2020-12-26 06:05:57 +01:00
engine.set_module_resolver(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
);
assert!(engine
.eval::<INT>(
r#"
import "hello" as h;
sum(h::answer, -10, 3, 7)
"#
)
.is_err());
assert_eq!(
engine.eval::<INT>(
r#"
import "hello" as h1;
import "hello" as h2;
let x = 42;
h1::sum(x, -10, 3, 7)
"#
)?,
42
);
assert_eq!(
engine.eval::<INT>(
r#"
import "hello" as h1;
import "hello" as h2;
let x = 42;
h1::sum(x, 0, 0, 0);
x
"#
)?,
42
);
assert_eq!(
engine.eval::<INT>(
r#"
import "hello" as h;
let x = 21;
h::double(x);
x
"#
)?,
42
);
assert_eq!(
engine.eval::<INT>(
r#"
import "hello" as h;
let x = 21;
double(x);
x
"#
)?,
42
);
2020-09-15 05:03:46 +02:00
#[cfg(not(feature = "no_float"))]
{
assert_eq!(
engine.eval::<INT>(
r#"
2021-01-09 09:52:22 +01:00
import "hello" as h;
let x = 21;
h::sum_of_three_args(x, 14, 26, 2.0);
x
"#
2020-09-15 05:03:46 +02:00
)?,
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;
2021-12-15 05:06:17 +01:00
for x in 0..10 {
2020-05-23 18:29:06 +02:00
import "hello" as h;
sum += h::answer;
}
sum
2020-06-27 15:19:53 +02:00
"#
2020-05-23 18:29:06 +02:00
)
2023-04-10 17:23:59 +02:00
.unwrap_err(),
2022-02-08 02:46:14 +01:00
EvalAltResult::ErrorTooManyModules(..)
2020-05-23 18:29:06 +02:00
));
#[cfg(not(feature = "no_function"))]
assert!(matches!(
*engine
.eval::<INT>(
r#"
let sum = 0;
fn foo() {
import "hello" as h;
sum += h::answer;
}
2021-12-15 05:06:17 +01:00
for x in 0..10 {
2020-05-23 18:29:06 +02:00
foo();
}
sum
2020-06-27 15:19:53 +02:00
"#
2020-05-23 18:29:06 +02:00
)
2023-04-10 17:23:59 +02:00
.unwrap_err(),
2022-12-30 18:07:39 +01:00
EvalAltResult::ErrorInFunctionCall(fn_name, ..) if fn_name == "foo"
2020-05-23 18:29:06 +02:00
));
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"))]
2022-01-10 06:26:33 +01:00
engine.run(
2020-05-23 18:29:06 +02:00
r#"
fn foo() {
import "hello" as h;
}
2020-05-17 16:19:49 +02:00
2021-12-15 05:06:17 +01:00
for x in 0..10 {
2020-05-23 18:29:06 +02:00
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
#[cfg(not(feature = "no_function"))]
{
let script = r#"
fn foo(x) {
import "hello" as h;
h::answer * x
}
foo(1) + { import "hello" as h; h::answer }
"#;
2022-12-30 18:07:39 +01:00
let scope = Scope::new();
2021-01-08 17:24:55 +01:00
2022-12-30 18:07:39 +01:00
let ast = engine.compile_into_self_contained(&scope, script)?;
2021-01-08 17:24:55 +01:00
engine.set_module_resolver(DummyModuleResolver::new());
2021-01-08 17:24:55 +01:00
assert_eq!(engine.eval_ast::<INT>(&ast)?, 84);
2021-01-08 17:24:55 +01:00
assert!(engine.eval::<INT>(script).is_err());
2022-08-12 16:48:15 +02:00
let result = engine.call_fn::<INT>(&mut Scope::new(), &ast, "foo", (2 as INT,))?;
assert_eq!(result, 84);
2022-05-21 05:31:15 +02:00
let mut ast2 = engine.compile("fn foo(x) { 42 }")?;
2022-05-21 05:57:23 +02:00
#[cfg(feature = "internals")]
2022-05-21 05:31:15 +02:00
let len = ast.resolver().unwrap().len();
ast2 += ast;
2022-05-21 05:57:23 +02:00
#[cfg(feature = "internals")]
{
assert!(ast2.resolver().is_some());
assert_eq!(ast2.resolver().unwrap().len(), len);
}
2022-05-21 05:31:15 +02:00
2022-08-12 16:48:15 +02:00
let result = engine.call_fn::<INT>(&mut Scope::new(), &ast2, "foo", (2 as INT,))?;
2022-05-21 05:31:15 +02:00
assert_eq!(result, 84);
}
2021-01-08 17:24:55 +01:00
2020-05-05 17:57:25 +02:00
Ok(())
}
#[test]
2020-05-12 10:32:22 +02:00
#[cfg(not(feature = "no_function"))]
fn test_module_from_ast() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let mut resolver1 = StaticModuleResolver::new();
let mut sub_module = Module::new();
sub_module.set_var("foo", true);
resolver1.insert("another module", sub_module);
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)
}
2020-09-30 03:57:21 +02:00
fn cross_call(x) {
calc(x)
}
2020-06-27 15:19:53 +02:00
private fn hidden() {
throw "you shouldn't see me!";
}
2022-12-30 18:07:39 +01:00
2020-06-27 15:19:53 +02:00
// Imported modules become sub-modules
import "another module" as extra;
2022-12-30 18:07:39 +01:00
2020-06-27 15:19:53 +02:00
// Variables defined at global level become module variables
2020-11-09 07:38:33 +01:00
export const x = 123;
2020-06-27 15:19:53 +02:00
let foo = 41;
let hello;
2022-12-30 18:07:39 +01:00
2020-06-27 15:19:53 +02:00
// Final variable values become constant module variable values
foo = calc(foo);
2021-04-04 17:22:45 +02:00
hello = `hello, ${foo} worlds!`;
2020-06-27 15:19:53 +02:00
export x as abc;
export x as xxx;
export foo;
export hello;
2020-06-27 15:19:53 +02:00
"#,
)?;
2020-12-26 06:05:57 +01:00
engine.set_module_resolver(resolver1);
let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?;
let mut resolver2 = StaticModuleResolver::new();
resolver2.insert("testing", module);
2020-12-26 06:05:57 +01:00
engine.set_module_resolver(resolver2);
assert_eq!(
engine.eval::<INT>(r#"import "testing" as ttt; ttt::abc"#)?,
123
);
2020-11-09 07:38:33 +01:00
assert_eq!(
engine.eval::<INT>(r#"import "testing" as ttt; ttt::x"#)?,
123
);
assert_eq!(
engine.eval::<INT>(r#"import "testing" as ttt; ttt::xxx"#)?,
123
);
assert_eq!(
engine.eval::<INT>(r#"import "testing" as ttt; ttt::foo"#)?,
42
);
assert!(engine.eval::<bool>(r#"import "testing" as ttt; ttt::extra::foo"#)?);
assert_eq!(
engine.eval::<String>(r#"import "testing" as ttt; ttt::hello"#)?,
"hello, 42 worlds!"
);
assert_eq!(
engine.eval::<INT>(r#"import "testing" as ttt; ttt::calc(999)"#)?,
1000
);
2020-09-30 03:57:21 +02:00
assert_eq!(
engine.eval::<INT>(r#"import "testing" as ttt; ttt::cross_call(999)"#)?,
1000
);
assert_eq!(
engine.eval::<INT>(r#"import "testing" as ttt; ttt::add_len(ttt::foo, ttt::hello)"#)?,
59
);
assert!(matches!(
*engine
.run(r#"import "testing" as ttt; ttt::hidden()"#)
2023-04-10 17:23:59 +02:00
.unwrap_err(),
2022-02-08 02:02:15 +01:00
EvalAltResult::ErrorFunctionNotFound(fn_name, ..) if fn_name == "ttt::hidden ()"
));
Ok(())
}
#[test]
fn test_module_export() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert!(matches!(
2023-04-10 17:23:59 +02:00
engine.compile("let x = 10; { export x; }").unwrap_err(),
2022-02-08 02:02:15 +01:00
ParseError(x, ..) if *x == ParseErrorType::WrongExport
));
#[cfg(not(feature = "no_function"))]
assert!(matches!(
2023-04-10 17:23:59 +02:00
engine.compile("fn abc(x) { export x; }").unwrap_err(),
2022-02-08 02:02:15 +01:00
ParseError(x, ..) if *x == ParseErrorType::WrongExport
));
Ok(())
}
#[test]
fn test_module_str() -> Result<(), Box<EvalAltResult>> {
fn test_fn(input: ImmutableString) -> Result<INT, Box<EvalAltResult>> {
Ok(input.len() as INT)
}
fn test_fn2(input: &str) -> Result<INT, Box<EvalAltResult>> {
Ok(input.len() as INT)
}
fn test_fn3(input: String) -> Result<INT, Box<EvalAltResult>> {
Ok(input.len() as INT)
}
let mut engine = rhai::Engine::new();
let mut module = Module::new();
2021-03-15 05:39:06 +01:00
module.set_native_fn("test", test_fn);
module.set_native_fn("test2", test_fn2);
module.set_native_fn("test3", test_fn3);
let mut static_modules = rhai::module_resolvers::StaticModuleResolver::new();
static_modules.insert("test", module);
2020-12-26 06:05:57 +01:00
engine.set_module_resolver(static_modules);
assert_eq!(
engine.eval::<INT>(r#"import "test" as test; test::test("test");"#)?,
4
);
assert_eq!(
engine.eval::<INT>(r#"import "test" as test; test::test2("test");"#)?,
4
);
assert_eq!(
engine.eval::<INT>(r#"import "test" as test; test::test3("test");"#)?,
4
);
Ok(())
}
2020-10-02 08:55:02 +02:00
2020-10-02 09:31:59 +02:00
#[cfg(not(feature = "no_function"))]
2020-10-02 08:55:02 +02:00
#[test]
fn test_module_ast_namespace() -> Result<(), Box<EvalAltResult>> {
let script = "
2020-10-02 08:55:02 +02:00
fn foo(x) { x + 1 }
fn bar(x) { foo(x) }
";
2020-10-02 08:55:02 +02:00
let mut engine = Engine::new();
let ast = engine.compile(script)?;
let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?;
2020-10-02 08:55:02 +02:00
let mut resolver = StaticModuleResolver::new();
resolver.insert("testing", module);
2020-12-26 06:05:57 +01:00
engine.set_module_resolver(resolver);
2020-10-02 08:55:02 +02:00
assert_eq!(
engine.eval::<INT>(r#"import "testing" as t; t::foo(41)"#)?,
42
);
assert_eq!(
engine.eval::<INT>(r#"import "testing" as t; t::bar(41)"#)?,
42
);
assert_eq!(
engine.eval::<INT>(r#"fn foo(x) { x - 1 } import "testing" as t; t::foo(41)"#)?,
42
);
assert_eq!(
engine.eval::<INT>(r#"fn foo(x) { x - 1 } import "testing" as t; t::bar(41)"#)?,
42
);
Ok(())
}
#[cfg(not(feature = "no_function"))]
#[test]
fn test_module_ast_namespace2() -> Result<(), Box<EvalAltResult>> {
use rhai::{Engine, Module, Scope};
const MODULE_TEXT: &str = "
fn run_function(function) {
2020-10-16 15:26:38 +02:00
call(function)
}
";
const SCRIPT: &str = r#"
import "test_module" as test;
fn foo() {
print("foo");
}
test::run_function(Fn("foo"));
"#;
let mut engine = Engine::new();
let module_ast = engine.compile(MODULE_TEXT)?;
let module = Module::eval_ast_as_new(Scope::new(), &module_ast, &engine)?;
let mut static_modules = rhai::module_resolvers::StaticModuleResolver::new();
static_modules.insert("test_module", module);
2020-12-26 06:05:57 +01:00
engine.set_module_resolver(static_modules);
engine.run(SCRIPT)?;
Ok(())
}
2020-11-08 16:00:03 +01:00
2022-12-30 18:07:39 +01:00
#[cfg(all(not(feature = "no_function"), feature = "internals"))]
#[test]
fn test_module_context() -> Result<(), Box<EvalAltResult>> {
let script = "fn bar() { calc(|x| x + 1) }";
let mut engine = Engine::new();
let ast = engine.compile(script)?;
let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?;
let mut resolver = StaticModuleResolver::new();
resolver.insert("testing", module);
engine.set_module_resolver(resolver);
engine.register_fn(
"calc",
|context: NativeCallContext, fp: FnPtr| -> Result<INT, Box<EvalAltResult>> {
let engine = context.engine();
2022-11-07 09:19:10 +01:00
// Store context for later use - requires the 'internals' feature
let context_data = context.store_data();
// Recreate the 'NativeCallContext'
let new_context = context_data.create_context(engine);
fp.call_within_context(&new_context, (41 as INT,))
},
);
assert_eq!(
engine.eval::<INT>(r#"import "testing" as t; t::bar()"#)?,
42
);
Ok(())
}
2020-11-08 16:00:03 +01:00
#[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(Scope::new(), &ast, &engine)?;
2020-11-08 16:00:03 +01:00
Ok(())
}
#[cfg(not(feature = "no_function"))]
#[test]
fn test_module_environ() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let ast = engine.compile(
r#"
const SECRET = 42;
fn foo(x) {
print(global::SECRET);
global::SECRET + x
}
"#,
)?;
let mut m = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?;
m.set_id("test");
m.build_index();
engine.register_static_module("test", m.into());
assert_eq!(
engine.eval::<String>(
r#"
const SECRET = "hello";
fn foo(x) {
print(global::SECRET);
global::SECRET + x
}
let t = test::foo(0);
foo(t)
"#
)?,
"hello42"
);
Ok(())
}
#[test]
fn test_module_dynamic() -> Result<(), Box<EvalAltResult>> {
fn test_fn(input: Dynamic, x: INT) -> Result<INT, Box<EvalAltResult>> {
let s = input.into_string().unwrap();
Ok(s.len() as INT + x)
}
let mut engine = rhai::Engine::new();
let mut module = Module::new();
module.set_native_fn("test", test_fn);
let mut static_modules = rhai::module_resolvers::StaticModuleResolver::new();
static_modules.insert("test", module);
engine.set_module_resolver(static_modules);
2022-08-22 16:16:26 +02:00
engine.register_fn("test2", test_fn);
assert_eq!(engine.eval::<INT>(r#"test2("test", 38);"#)?, 42);
assert_eq!(
engine.eval::<INT>(r#"import "test" as test; test::test("test", 38);"#)?,
42
);
Ok(())
}