Merge pull request #28 from Eliah-Lakhin/closure-bugs

False-positive capturing prevention bug fix
This commit is contained in:
Stephen Chung 2020-08-09 22:05:40 +08:00 committed by GitHub
commit dbf5cd13c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -414,7 +414,8 @@ struct ParseState<'e> {
/// Tracks a list of external variables (variables that are not explicitly declared in the scope). /// Tracks a list of external variables (variables that are not explicitly declared in the scope).
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
externals: HashMap<String, Position>, externals: HashMap<String, Position>,
/// An indicator that disables variable capturing into externals one single time. /// An indicator that disables variable capturing into externals one single time
/// up until the nearest consumed Identifier token.
/// If set to false the next call to `access_var` will not capture the variable. /// If set to false the next call to `access_var` will not capture the variable.
/// All consequent calls to `access_var` will not be affected /// All consequent calls to `access_var` will not be affected
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
@ -1637,6 +1638,10 @@ fn parse_primary(
// Function call // Function call
Token::Identifier(s) if *next_token == Token::LeftParen || *next_token == Token::Bang => { Token::Identifier(s) if *next_token == Token::LeftParen || *next_token == Token::Bang => {
// Once the identifier consumed we must enable next variables capturing
#[cfg(not(feature = "no_closure"))] {
state.allow_capture = true;
}
Expr::Variable(Box::new(((s, settings.pos), None, 0, None))) Expr::Variable(Box::new(((s, settings.pos), None, 0, None)))
} }
// Normal variable access // Normal variable access

View File

@ -1,6 +1,7 @@
#![cfg(not(feature = "no_function"))] #![cfg(not(feature = "no_function"))]
use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, RegisterFn, INT}; use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, RegisterFn, INT};
use std::any::TypeId; use std::any::TypeId;
use std::mem::take;
#[test] #[test]
fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> { fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
@ -88,6 +89,29 @@ fn test_closures() -> Result<(), Box<EvalAltResult>> {
42 42
); );
engine.register_raw_fn(
"custom_call",
&[TypeId::of::<INT>(), TypeId::of::<FnPtr>()],
|engine: &Engine, module: &Module, args: &mut [&mut Dynamic]| {
let func = take(args[1]).cast::<FnPtr>();
func.call_dynamic(engine, module, None, [])
},
);
assert_eq!(
engine.eval::<INT>(
r#"
let a = 41;
let b = 0;
let f = || b.custom_call(|| a + 1);
f.call()
"#
)?,
42
);
Ok(()) Ok(())
} }