Fix bug on chaining function calls returning shared values.

This commit is contained in:
Stephen Chung
2022-06-07 11:31:46 +08:00
parent 005692ef78
commit 84e3296559
3 changed files with 38 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
#![cfg(not(feature = "no_function"))]
use rhai::{Engine, EvalAltResult, FnPtr, ParseErrorType, Scope, INT};
use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, ParseErrorType, Scope, INT};
use std::any::TypeId;
use std::cell::RefCell;
use std::mem::take;
@@ -217,6 +217,34 @@ fn test_closures_sharing() -> Result<(), Box<EvalAltResult>> {
6
);
#[cfg(not(feature = "no_object"))]
{
let mut m = Map::new();
m.insert("hello".into(), "world".into());
let m = Dynamic::from(m).into_shared();
engine.register_fn("baz", move || m.clone());
assert!(!engine.eval::<bool>(
"
let m = baz();
m.is_shared()
"
)?);
assert_eq!(
engine.eval::<String>(
"
let m = baz();
m.hello
"
)?,
"world"
);
assert_eq!(engine.eval::<String>("baz().hello")?, "world");
}
Ok(())
}