Allow passing in custom Scope to call_fn.

This commit is contained in:
Stephen Chung
2020-04-05 12:17:31 +08:00
parent ae9a975576
commit 3f247fd695
5 changed files with 134 additions and 76 deletions

View File

@@ -1,5 +1,5 @@
#![cfg(not(feature = "no_function"))]
use rhai::{Engine, EvalAltResult, ParseErrorType, INT};
use rhai::{Engine, EvalAltResult, ParseErrorType, Scope, INT};
#[test]
fn test_fn() -> Result<(), EvalAltResult> {
@@ -21,6 +21,9 @@ fn test_fn() -> Result<(), EvalAltResult> {
#[test]
fn test_call_fn() -> Result<(), EvalAltResult> {
let mut engine = Engine::new();
let mut scope = Scope::new();
scope.push("foo", 42 as INT);
let ast = engine.compile(
r"
@@ -28,22 +31,22 @@ fn test_call_fn() -> Result<(), EvalAltResult> {
x + y
}
fn hello(x) {
x * 2
x * foo
}
fn hello() {
42
42 + foo
}
",
)?;
let r: i64 = engine.call_fn(&ast, "hello", (42 as INT, 123 as INT))?;
let r: i64 = engine.call_fn(&mut scope, &ast, "hello", (42 as INT, 123 as INT))?;
assert_eq!(r, 165);
let r: i64 = engine.call_fn1(&ast, "hello", 123 as INT)?;
assert_eq!(r, 246);
let r: i64 = engine.call_fn1(&mut scope, &ast, "hello", 123 as INT)?;
assert_eq!(r, 5166);
let r: i64 = engine.call_fn0(&ast, "hello")?;
assert_eq!(r, 42);
let r: i64 = engine.call_fn0(&mut scope, &ast, "hello")?;
assert_eq!(r, 84);
Ok(())
}