Fix closure property access.

This commit is contained in:
Stephen Chung 2020-12-27 11:50:24 +08:00
parent 88f63fa24b
commit 48af8719e7
2 changed files with 17 additions and 10 deletions

View File

@ -1135,6 +1135,12 @@ fn parse_primary(
// Method access
#[cfg(not(feature = "no_object"))]
(expr, Token::Period) => {
// prevents capturing of the object properties as vars: xxx.<var>
#[cfg(not(feature = "no_closure"))]
if let (Token::Identifier(_), _) = input.peek().unwrap() {
state.allow_capture = false;
}
let rhs = parse_unary(input, state, lib, settings.level_up())?;
make_dot_expr(state, expr, rhs, token_pos)?
}
@ -1694,16 +1700,6 @@ fn parse_binary_op(
let (op_token, pos) = input.next().unwrap();
if cfg!(not(feature = "no_object")) && op_token == Token::Period {
if let (Token::Identifier(_), _) = input.peek().unwrap() {
// prevents capturing of the object properties as vars: xxx.<var>
#[cfg(not(feature = "no_closure"))]
{
state.allow_capture = false;
}
}
}
let rhs = parse_unary(input, state, lib, settings)?;
let (next_op, next_pos) = input.peek().unwrap();

View File

@ -54,6 +54,17 @@ fn test_closures() -> Result<(), Box<EvalAltResult>> {
ParseErrorType::BadInput(_)
));
assert_eq!(
engine.eval::<INT>(
r"
let foo = #{ x: 42 };
let f = || { this.x };
foo.call(f)
",
)?,
42
);
assert_eq!(
engine.eval::<INT>(
r#"