Encapsulate scope into EvalContext;

Fix bug with custom syntax delta.
This commit is contained in:
Stephen Chung
2020-10-19 19:11:55 +08:00
parent d1f9f4096d
commit a9fd0ff4de
15 changed files with 138 additions and 108 deletions

View File

@@ -1,7 +1,5 @@
#![cfg(not(feature = "no_function"))]
use rhai::{
Dynamic, Engine, EvalAltResult, FnPtr, Func, Module, ParseErrorType, RegisterFn, Scope, INT,
};
use rhai::{Engine, EvalAltResult, FnPtr, Func, ParseErrorType, RegisterFn, Scope, INT};
use std::any::TypeId;
#[test]

View File

@@ -1,4 +1,4 @@
use rhai::{Engine, EvalAltResult, EvalContext, Expression, ParseErrorType, Scope, INT};
use rhai::{Engine, EvalAltResult, EvalContext, Expression, ParseErrorType, INT};
#[test]
fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
@@ -22,18 +22,18 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
"do", "|", "$ident$", "|", "->", "$block$", "while", "$expr$",
],
1,
|scope: &mut Scope, context: &mut EvalContext, inputs: &[Expression]| {
|context: &mut EvalContext, inputs: &[Expression]| {
let var_name = inputs[0].get_variable_name().unwrap().to_string();
let stmt = inputs.get(1).unwrap();
let condition = inputs.get(2).unwrap();
scope.push(var_name, 0 as INT);
context.scope.push(var_name, 0 as INT);
loop {
context.eval_expression_tree(scope, stmt)?;
context.eval_expression_tree(stmt)?;
let stop = !context
.eval_expression_tree(scope, condition)?
.eval_expression_tree(condition)?
.as_bool()
.map_err(|err| {
Box::new(EvalAltResult::ErrorMismatchDataType(
@@ -68,7 +68,7 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
// The first symbol must be an identifier
assert_eq!(
*engine
.register_custom_syntax(&["!"], 0, |_, _, _| Ok(().into()))
.register_custom_syntax(&["!"], 0, |_, _| Ok(().into()))
.expect_err("should error")
.0,
ParseErrorType::BadInput("Improper symbol for custom syntax: '!'".to_string())

View File

@@ -62,7 +62,7 @@ fn test_var_resolver() -> Result<(), Box<EvalAltResult>> {
scope.push("chameleon", 123 as INT);
scope.push("DO_NOT_USE", 999 as INT);
engine.on_var(|name, _, scope, _| {
engine.on_var(|name, _, context| {
match name {
"MYSTIC_NUMBER" => Ok(Some((42 as INT).into())),
// Override a variable - make it not found even if it exists!
@@ -70,9 +70,13 @@ fn test_var_resolver() -> Result<(), Box<EvalAltResult>> {
Err(EvalAltResult::ErrorVariableNotFound(name.to_string(), Position::none()).into())
}
// Silently maps 'chameleon' into 'innocent'.
"chameleon" => scope.get_value("innocent").map(Some).ok_or_else(|| {
EvalAltResult::ErrorVariableNotFound(name.to_string(), Position::none()).into()
}),
"chameleon" => context
.scope
.get_value("innocent")
.map(Some)
.ok_or_else(|| {
EvalAltResult::ErrorVariableNotFound(name.to_string(), Position::none()).into()
}),
// Return Ok(None) to continue with the normal variable resolution process.
_ => Ok(None),
}