Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Bug fixes
* Fixed undefined behavior (an aliasing violation detected by Miri) when a variable is passed to a function that does not take its first argument by `&mut`, e.g. `x.contains("a")` or `f(x)` for a script function. This was a regression in 1.27.0.
* `sort` and `sort_desc` no longer panic on an array of floating-point numbers that contains NaN. NaN now sorts after every other number (before them for `sort_desc`) (thanks [`@Rockyrok77`](https://github.com/Rockyrok77)).
* `pad` on an array now enforces `max_array_size` when the padding length is large enough to overflow the size calculation. Previously it could pass the check and then panic or abort the process while allocating (thanks [`@Rockyrok77`](https://github.com/Rockyrok77)).
* `eval` is no longer allowed in `Engine::eval_expression` and `Engine::compile_expression`. Previously it could run statements, loops and assignments inside an expression, including assignments to variables in the `Scope` passed in (thanks [`@Rockyrok77`](https://github.com/Rockyrok77)).

Enhancements
------------
Expand Down
15 changes: 15 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,21 @@ impl Engine {
capture_parent_scope: bool,
#[cfg(not(feature = "no_module"))] mut namespace: crate::module::Namespace,
) -> ParseResult<Expr> {
// `eval` runs a full script in the current scope, so it cannot be allowed
// where statements are not
#[cfg(not(feature = "no_module"))]
let is_eval = namespace.is_empty() && id == crate::engine::KEYWORD_EVAL;
#[cfg(feature = "no_module")]
let is_eval = id == crate::engine::KEYWORD_EVAL;

if is_eval && settings.has_flag(ParseSettingFlags::DISALLOW_STATEMENTS_IN_BLOCKS) {
return Err(LexError::ImproperSymbol(
id.to_string(),
"'eval' cannot be used in an expression".into(),
)
.into_err(settings.pos));
}

let (token, token_pos) = if no_args {
&(Token::RightParen, Position::NONE)
} else {
Expand Down
7 changes: 7 additions & 0 deletions tests/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ fn test_expressions() {
let _ = engine.compile_expression("x = 42").unwrap_err();
let _ = engine.compile_expression("let x = 42").unwrap_err();
let _ = engine.compile_expression("do { break 42; } while true").unwrap_err();
let _ = engine.compile_expression(r#"eval("let x = 42; x")"#).unwrap_err();
let _ = engine.eval_expression_with_scope::<()>(&mut scope, r#"eval("x = 42")"#).unwrap_err();
assert_eq!(scope.get_value::<INT>("x").unwrap(), 10);
#[cfg(not(feature = "no_index"))]
#[cfg(not(feature = "no_function"))]
let _ = engine.compile_expression(r#"[1].map(|v| eval("v"))"#).unwrap_err();

engine.compile("40 + { let x = 2; x }").unwrap();
engine.compile(r#"eval("let x = 42; x")"#).unwrap();
}

/// This example taken from https://github.com/rhaiscript/rhai/issues/115
Expand Down
Loading