From 4bd482352e02526f690a9d6e6ba5e1f895c784de Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 28 Dec 2021 23:00:31 +0800 Subject: [PATCH] Fix eval call. --- src/func/call.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/func/call.rs b/src/func/call.rs index 6a0444fd..55e9e9e1 100644 --- a/src/func/call.rs +++ b/src/func/call.rs @@ -1039,9 +1039,15 @@ impl Engine { let script = &value .into_immutable_string() .map_err(|typ| self.make_type_mismatch_err::(typ, pos))?; - let level = level + 1; - let result = - self.eval_script_expr_in_place(scope, global, state, lib, script, pos, level); + let result = self.eval_script_expr_in_place( + scope, + global, + state, + lib, + script, + pos, + level + 1, + ); // IMPORTANT! If the eval defines new variables in the current scope, // all variable offsets from this point on will be mis-aligned. @@ -1339,7 +1345,19 @@ impl Engine { return Ok(Dynamic::UNIT); } + // Backup the original state + let orig_scope_level = state.scope_level; + + // Scope level is set to zero in order to retain all variables + state.scope_level = 0; + // Evaluate the AST - self.eval_global_statements(scope, global, state, statements, lib, level) + let result = self.eval_global_statements(scope, global, state, statements, lib, level); + + // Restore original state + state.scope_level = orig_scope_level; + + // Return result + result } }