Merge restore_state and rewind_scope.

This commit is contained in:
Stephen Chung 2021-12-28 17:50:49 +08:00
parent 5b667a69b7
commit cbaf095c7a
4 changed files with 26 additions and 36 deletions

View File

@ -2271,7 +2271,7 @@ impl Engine {
// Statement block // Statement block
Expr::Stmt(x) if x.is_empty() => Ok(Dynamic::UNIT), Expr::Stmt(x) if x.is_empty() => Ok(Dynamic::UNIT),
Expr::Stmt(x) => { Expr::Stmt(x) => {
self.eval_stmt_block(scope, global, state, lib, this_ptr, x, true, true, level) self.eval_stmt_block(scope, global, state, lib, this_ptr, x, true, level)
} }
// lhs[idx_expr] // lhs[idx_expr]
@ -2447,7 +2447,6 @@ impl Engine {
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
statements: &[Stmt], statements: &[Stmt],
restore_orig_state: bool, restore_orig_state: bool,
rewind_scope: bool,
level: usize, level: usize,
) -> RhaiResult { ) -> RhaiResult {
if statements.is_empty() { if statements.is_empty() {
@ -2459,7 +2458,7 @@ impl Engine {
let orig_mods_len = global.num_imported_modules(); let orig_mods_len = global.num_imported_modules();
let orig_fn_resolution_caches_len = state.fn_resolution_caches_len(); let orig_fn_resolution_caches_len = state.fn_resolution_caches_len();
if rewind_scope { if restore_orig_state {
state.scope_level += 1; state.scope_level += 1;
} }
@ -2498,11 +2497,9 @@ impl Engine {
// If imports list is modified, pop the functions lookup cache // If imports list is modified, pop the functions lookup cache
state.rewind_fn_resolution_caches(orig_fn_resolution_caches_len); state.rewind_fn_resolution_caches(orig_fn_resolution_caches_len);
if rewind_scope { if restore_orig_state {
scope.rewind(orig_scope_len); scope.rewind(orig_scope_len);
state.scope_level -= 1; state.scope_level -= 1;
}
if restore_orig_state {
global.truncate_modules(orig_mods_len); global.truncate_modules(orig_mods_len);
// The impact of new local variables goes away at the end of a block // The impact of new local variables goes away at the end of a block
@ -2684,9 +2681,9 @@ impl Engine {
// Block scope // Block scope
Stmt::Block(statements, _) if statements.is_empty() => Ok(Dynamic::UNIT), Stmt::Block(statements, _) if statements.is_empty() => Ok(Dynamic::UNIT),
Stmt::Block(statements, _) => self.eval_stmt_block( Stmt::Block(statements, _) => {
scope, global, state, lib, this_ptr, statements, true, true, level, self.eval_stmt_block(scope, global, state, lib, this_ptr, statements, true, level)
), }
// If statement // If statement
Stmt::If(expr, x, _) => { Stmt::If(expr, x, _) => {
@ -2697,17 +2694,13 @@ impl Engine {
if guard_val { if guard_val {
if !x.0.is_empty() { if !x.0.is_empty() {
self.eval_stmt_block( self.eval_stmt_block(scope, global, state, lib, this_ptr, &x.0, true, level)
scope, global, state, lib, this_ptr, &x.0, true, true, level,
)
} else { } else {
Ok(Dynamic::UNIT) Ok(Dynamic::UNIT)
} }
} else { } else {
if !x.1.is_empty() { if !x.1.is_empty() {
self.eval_stmt_block( self.eval_stmt_block(scope, global, state, lib, this_ptr, &x.1, true, level)
scope, global, state, lib, this_ptr, &x.1, true, true, level,
)
} else { } else {
Ok(Dynamic::UNIT) Ok(Dynamic::UNIT)
} }
@ -2785,7 +2778,7 @@ impl Engine {
if let Some(statements) = stmt_block { if let Some(statements) = stmt_block {
if !statements.is_empty() { if !statements.is_empty() {
self.eval_stmt_block( self.eval_stmt_block(
scope, global, state, lib, this_ptr, statements, true, true, level, scope, global, state, lib, this_ptr, statements, true, level,
) )
} else { } else {
Ok(Dynamic::UNIT) Ok(Dynamic::UNIT)
@ -2794,7 +2787,7 @@ impl Engine {
// Default match clause // Default match clause
if !def_stmt.is_empty() { if !def_stmt.is_empty() {
self.eval_stmt_block( self.eval_stmt_block(
scope, global, state, lib, this_ptr, def_stmt, true, true, level, scope, global, state, lib, this_ptr, def_stmt, true, level,
) )
} else { } else {
Ok(Dynamic::UNIT) Ok(Dynamic::UNIT)
@ -2805,9 +2798,9 @@ impl Engine {
// Loop // Loop
Stmt::While(Expr::Unit(_), body, _) => loop { Stmt::While(Expr::Unit(_), body, _) => loop {
if !body.is_empty() { if !body.is_empty() {
match self.eval_stmt_block( match self
scope, global, state, lib, this_ptr, body, true, true, level, .eval_stmt_block(scope, global, state, lib, this_ptr, body, true, level)
) { {
Ok(_) => (), Ok(_) => (),
Err(err) => match *err { Err(err) => match *err {
ERR::LoopBreak(false, _) => (), ERR::LoopBreak(false, _) => (),
@ -2832,9 +2825,9 @@ impl Engine {
return Ok(Dynamic::UNIT); return Ok(Dynamic::UNIT);
} }
if !body.is_empty() { if !body.is_empty() {
match self.eval_stmt_block( match self
scope, global, state, lib, this_ptr, body, true, true, level, .eval_stmt_block(scope, global, state, lib, this_ptr, body, true, level)
) { {
Ok(_) => (), Ok(_) => (),
Err(err) => match *err { Err(err) => match *err {
ERR::LoopBreak(false, _) => (), ERR::LoopBreak(false, _) => (),
@ -2850,9 +2843,9 @@ impl Engine {
let is_while = !options.contains(AST_OPTION_NEGATED); let is_while = !options.contains(AST_OPTION_NEGATED);
if !body.is_empty() { if !body.is_empty() {
match self.eval_stmt_block( match self
scope, global, state, lib, this_ptr, body, true, true, level, .eval_stmt_block(scope, global, state, lib, this_ptr, body, true, level)
) { {
Ok(_) => (), Ok(_) => (),
Err(err) => match *err { Err(err) => match *err {
ERR::LoopBreak(false, _) => continue, ERR::LoopBreak(false, _) => continue,
@ -2950,7 +2943,7 @@ impl Engine {
} }
let result = self.eval_stmt_block( let result = self.eval_stmt_block(
scope, global, state, lib, this_ptr, statements, true, true, level, scope, global, state, lib, this_ptr, statements, true, level,
); );
match result { match result {
@ -3014,9 +3007,7 @@ impl Engine {
let (try_stmt, err_var, catch_stmt) = x.as_ref(); let (try_stmt, err_var, catch_stmt) = x.as_ref();
let result = self let result = self
.eval_stmt_block( .eval_stmt_block(scope, global, state, lib, this_ptr, try_stmt, true, level)
scope, global, state, lib, this_ptr, try_stmt, true, true, level,
)
.map(|_| Dynamic::UNIT); .map(|_| Dynamic::UNIT);
match result { match result {
@ -3068,7 +3059,7 @@ impl Engine {
}); });
let result = self.eval_stmt_block( let result = self.eval_stmt_block(
scope, global, state, lib, this_ptr, catch_stmt, true, true, level, scope, global, state, lib, this_ptr, catch_stmt, true, level,
); );
scope.rewind(orig_scope_len); scope.rewind(orig_scope_len);

View File

@ -684,7 +684,7 @@ impl Engine {
level: usize, level: usize,
) -> RhaiResult { ) -> RhaiResult {
self.eval_stmt_block( self.eval_stmt_block(
scope, global, state, lib, &mut None, statements, false, false, level, scope, global, state, lib, &mut None, statements, false, level,
) )
.or_else(|err| match *err { .or_else(|err| match *err {
ERR::Return(out, _) => Ok(out), ERR::Return(out, _) => Ok(out),

View File

@ -122,7 +122,6 @@ impl Engine {
lib, lib,
this_ptr, this_ptr,
&fn_def.body, &fn_def.body,
true,
rewind_scope, rewind_scope,
level, level,
) )

View File

@ -18,9 +18,9 @@ fn test_eval_blocks() -> Result<(), Box<EvalAltResult>> {
r#" r#"
let x = 999; let x = 999;
eval("let x = x + 123"); eval("let x = x - 1000");
let y = if x > 0 { let y = if x < 0 {
eval("let x = 42"); eval("let x = 42");
x x
} else { } else {
@ -30,7 +30,7 @@ fn test_eval_blocks() -> Result<(), Box<EvalAltResult>> {
x + y x + y
"# "#
)?, )?,
1164 41
); );
Ok(()) Ok(())