use rhai::packages::{Package, StandardPackage}; use rhai::{Engine, EvalAltResult, Module, Scope, INT}; #[test] fn test_packages() -> Result<(), Box> { let engine = Engine::new(); let ast = engine.compile("x")?; let std_pkg = StandardPackage::new(); let make_call = |x: INT| -> Result> { // Create a raw Engine - extremely cheap. let mut engine = Engine::new_raw(); // Load packages - cheap. engine.load_package(std_pkg.get()); // Create custom scope - cheap. let mut scope = Scope::new(); // Push variable into scope - relatively cheap. scope.push("x", x); // Evaluate script. engine.eval_ast_with_scope::(&mut scope, &ast) }; assert_eq!(make_call(42)?, 42); Ok(()) } #[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_module"))] #[test] fn test_packages_with_script() -> Result<(), Box> { let mut engine = Engine::new(); let ast = engine.compile("fn foo(x) { x + 1 } fn bar(x) { foo(x) + 1 }")?; let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?; engine.load_package(module); assert_eq!(engine.eval::("foo(41)")?, 42); assert_eq!(engine.eval::("bar(40)")?, 42); Ok(()) }