Fix bug when passing shared string variable to &str parameter.

This commit is contained in:
Stephen Chung
2021-03-06 10:44:55 +08:00
parent f92e6f3983
commit fe633ea7d3
5 changed files with 96 additions and 31 deletions

View File

@@ -179,6 +179,51 @@ fn test_closures() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
#[cfg(not(feature = "no_closure"))]
fn test_closures_sharing() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.register_fn("foo", |x: INT, s: &str| s.len() as INT + x);
engine.register_fn("bar", |x: INT, s: String| s.len() as INT + x);
assert_eq!(
engine.eval::<INT>(
r#"
let s = "hello";
let f = || s;
foo(1, s)
"#
)?,
6
);
assert_eq!(
engine.eval::<String>(
r#"
let s = "hello";
let f = || s;
let n = foo(1, s);
s
"#
)?,
"hello"
);
assert_eq!(
engine.eval::<INT>(
r#"
let s = "hello";
let f = || s;
bar(1, s)
"#
)?,
6
);
Ok(())
}
#[test]
#[cfg(not(feature = "no_closure"))]
#[cfg(not(feature = "no_object"))]