Fixes in Engine to properly interpret Shared Dynamic
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#![cfg(not(feature = "no_function"))]
|
||||
use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, INT};
|
||||
use std::any::TypeId;
|
||||
use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, INT, Array};
|
||||
use std::any::{TypeId, Any};
|
||||
|
||||
#[test]
|
||||
fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
|
||||
@@ -58,3 +58,150 @@ fn test_closures() -> Result<(), Box<EvalAltResult>> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(feature = "no_shared"))]
|
||||
fn test_shared() -> Result<(), Box<EvalAltResult>> {
|
||||
let engine = Engine::new();
|
||||
|
||||
// assert_eq!(
|
||||
// engine.eval::<INT>(
|
||||
// r#"
|
||||
// shared(42)
|
||||
// "#
|
||||
// )?,
|
||||
// 42
|
||||
// );
|
||||
//
|
||||
// assert_eq!(
|
||||
// engine.eval::<bool>(
|
||||
// r#"
|
||||
// shared(true)
|
||||
// "#
|
||||
// )?,
|
||||
// true
|
||||
// );
|
||||
//
|
||||
// #[cfg(not(feature = "no_float"))]
|
||||
// assert_eq!(
|
||||
// engine.eval::<f64>(
|
||||
// r#"
|
||||
// shared(4.2)
|
||||
// "#
|
||||
// )?,
|
||||
// 4.2
|
||||
// );
|
||||
//
|
||||
// assert_eq!(
|
||||
// engine.eval::<String>(
|
||||
// r#"
|
||||
// shared("test")
|
||||
// "#
|
||||
// )?,
|
||||
// "test"
|
||||
// );
|
||||
//
|
||||
// #[cfg(not(feature = "no_index"))]
|
||||
// {
|
||||
// assert_eq!(
|
||||
// engine.eval::<Array>(
|
||||
// r#"
|
||||
// let x = shared([1, 2, 3]);
|
||||
// let y = shared([4, 5]);
|
||||
// x + y
|
||||
// "#
|
||||
// )?.len(),
|
||||
// 5
|
||||
// );
|
||||
//
|
||||
// assert_eq!(
|
||||
// engine.eval::<INT>(
|
||||
// r"
|
||||
// let x = shared([2, 9]);
|
||||
// x.insert(-1, 1);
|
||||
// x.insert(999, 3);
|
||||
//
|
||||
// let r = x.remove(2);
|
||||
//
|
||||
// let y = shared([4, 5]);
|
||||
// x.append(y);
|
||||
//
|
||||
// x.len + r
|
||||
// "
|
||||
// )?,
|
||||
// 14
|
||||
// );
|
||||
//
|
||||
// assert_eq!(
|
||||
// engine.eval::<bool>(
|
||||
// r#"
|
||||
// let x = shared([1, 2, 3]);
|
||||
//
|
||||
// if x[0] + x[2] == 4 {
|
||||
// true
|
||||
// } else {
|
||||
// false
|
||||
// }
|
||||
// "#
|
||||
// )?,
|
||||
// true
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// #[cfg(not(feature = "no_object"))]
|
||||
// assert_eq!(
|
||||
// engine.eval::<INT>(r#"
|
||||
// let y = shared(#{a: 1, b: 2, c: 3});
|
||||
// y.c = shared(5);
|
||||
// y.c
|
||||
// "#)?,
|
||||
// 5
|
||||
// );
|
||||
//
|
||||
// #[cfg(not(feature = "no_object"))]
|
||||
// assert_eq!(
|
||||
// engine.eval::<INT>(r#"
|
||||
// let y = shared(#{a: 1, b: 2, c: shared(3)});
|
||||
// let c = y.c;
|
||||
// c = 5;// "c" still holds Dynamic Shared
|
||||
// y.c
|
||||
// "#)?,
|
||||
// 5
|
||||
// );
|
||||
//
|
||||
// #[cfg(not(feature = "no_capture"))]
|
||||
// assert_eq!(
|
||||
// engine.eval::<INT>(r#"
|
||||
// let x = shared(1);
|
||||
// (|| x = x + 41).call();
|
||||
// x
|
||||
// "#)?,
|
||||
// 42
|
||||
// );
|
||||
|
||||
#[cfg(all(not(feature = "no_object"), not(feature = "no_capture")))]
|
||||
assert_eq!(
|
||||
engine.eval::<INT>(r#"
|
||||
// let x = shared(#{a: 1, b: shared(2), c: 3});
|
||||
// let a = x.a;
|
||||
// let b = x.b;
|
||||
// a = 100;
|
||||
// b = 20;
|
||||
//
|
||||
// let f = |a| {
|
||||
// x.c = x.a + x.b;// + a;
|
||||
// };
|
||||
//
|
||||
// f.call(20);
|
||||
//
|
||||
// x.c
|
||||
|
||||
let x = #{a: 1, b: 2};
|
||||
|
||||
x.a + x.b
|
||||
"#)?,
|
||||
42
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user