Allow pure setters by making constant Dynamic internally constant as well.

This commit is contained in:
Stephen Chung
2021-05-15 09:45:40 +08:00
parent 4f14fd55f5
commit 60dc41bfac
16 changed files with 138 additions and 163 deletions

View File

@@ -16,7 +16,7 @@ fn test_constant() -> Result<(), Box<EvalAltResult>> {
#[cfg(not(feature = "no_index"))]
assert!(matches!(
*engine.consume("const x = [1, 2, 3, 4, 5]; x[2] = 42;").expect_err("expects error"),
EvalAltResult::ErrorParsing(ParseErrorType::AssignmentToConstant(x), _) if x == "x"
EvalAltResult::ErrorAssignmentToConstant(x, _) if x == "x"
));
Ok(())
@@ -48,6 +48,7 @@ fn test_constant_mut() -> Result<(), Box<EvalAltResult>> {
engine
.register_type_with_name::<TestStruct>("TestStruct")
.register_get("value", |obj: &mut TestStruct| obj.0)
.register_set("value", |obj: &mut TestStruct, value: INT| obj.0 = value)
.register_fn("update_value", |obj: &mut TestStruct, value: INT| {
obj.0 = value
});
@@ -67,5 +68,12 @@ fn test_constant_mut() -> Result<(), Box<EvalAltResult>> {
42
);
assert!(matches!(
*engine
.consume_with_scope(&mut scope, "MY_NUMBER.value = 42;")
.expect_err("should error"),
EvalAltResult::ErrorAssignmentToConstant(_, _)
));
Ok(())
}

View File

@@ -38,9 +38,10 @@ mod test {
pub fn funky_add(x: INT, y: INT) -> INT {
x / 2 + y * 2
}
#[rhai_fn(pure)]
pub fn no_effect(_array: &mut Array, _value: INT) {
// do nothing to array
#[rhai_fn(name = "no_effect", set = "no_effect", pure)]
pub fn no_effect(array: &mut Array, value: INT) {
// array is not modified
println!("Array = {:?}, Value = {}", array, value);
}
}
}
@@ -87,6 +88,7 @@ fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
{
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; a.foo")?, 1);
engine.consume("const A = [1, 2, 3]; A.no_effect(42);")?;
engine.consume("const A = [1, 2, 3]; A.no_effect = 42;")?;
assert!(
matches!(*engine.consume("const A = [1, 2, 3]; A.test(42);").expect_err("should error"),

View File

@@ -128,7 +128,7 @@ fn test_custom_syntax_raw() -> Result<(), Box<EvalAltResult>> {
s => Err(ParseError(
Box::new(ParseErrorType::BadInput(LexError::ImproperSymbol(
s.to_string(),
"".to_string(),
Default::default(),
))),
Position::NONE,
)),