Fix test.

This commit is contained in:
Stephen Chung 2022-10-18 16:54:26 +08:00
parent 4642895de8
commit d49dca8a29
2 changed files with 75 additions and 78 deletions

View File

@ -358,24 +358,14 @@ impl Engine {
}); });
match guard_val { match guard_val {
Ok(true) => { Ok(true) if if_block.is_empty() => Ok(Dynamic::UNIT),
if if_block.is_empty() { Ok(true) => self.eval_stmt_block(
Ok(Dynamic::UNIT)
} else {
self.eval_stmt_block(
scope, global, caches, lib, this_ptr, if_block, true, level, scope, global, caches, lib, this_ptr, if_block, true, level,
) ),
} Ok(false) if else_block.is_empty() => Ok(Dynamic::UNIT),
} Ok(false) => self.eval_stmt_block(
Ok(false) => {
if else_block.is_empty() {
Ok(Dynamic::UNIT)
} else {
self.eval_stmt_block(
scope, global, caches, lib, this_ptr, else_block, true, level, scope, global, caches, lib, this_ptr, else_block, true, level,
) ),
}
}
err => err.map(Into::into), err => err.map(Into::into),
} }
} }
@ -490,15 +480,18 @@ impl Engine {
} }
// Loop // Loop
Stmt::While(x, ..) if matches!(x.0, Expr::Unit(..)) => loop { Stmt::While(x, ..) if matches!(x.0, Expr::Unit(..) | Expr::BoolConstant(true, ..)) => {
let (.., body) = &**x; let (.., body) = &**x;
if body.is_empty() { if body.is_empty() {
loop {
self.track_operation(global, body.position())?; self.track_operation(global, body.position())?;
}
} else { } else {
match self loop {
.eval_stmt_block(scope, global, caches, lib, this_ptr, body, true, level) match self.eval_stmt_block(
{ scope, global, caches, lib, this_ptr, body, true, level,
) {
Ok(_) => (), Ok(_) => (),
Err(err) => match *err { Err(err) => match *err {
ERR::LoopBreak(false, ..) => (), ERR::LoopBreak(false, ..) => (),
@ -507,12 +500,14 @@ impl Engine {
}, },
} }
} }
}, }
}
// While loop // While loop
Stmt::While(x, ..) => loop { Stmt::While(x, ..) => {
let (expr, body) = &**x; let (expr, body) = &**x;
loop {
let condition = self let condition = self
.eval_expr(scope, global, caches, lib, this_ptr, expr, level) .eval_expr(scope, global, caches, lib, this_ptr, expr, level)
.and_then(|v| { .and_then(|v| {
@ -538,17 +533,19 @@ impl Engine {
} }
err => break err.map(|_| Dynamic::UNIT), err => break err.map(|_| Dynamic::UNIT),
} }
}, }
}
// Do loop // Do loop
Stmt::Do(x, options, ..) => loop { Stmt::Do(x, options, ..) => {
let (expr, body) = &**x; let (expr, body) = &**x;
let is_while = !options.contains(ASTFlags::NEGATED); let is_while = !options.contains(ASTFlags::NEGATED);
loop {
if !body.is_empty() { if !body.is_empty() {
match self match self.eval_stmt_block(
.eval_stmt_block(scope, global, caches, lib, this_ptr, body, true, level) scope, global, caches, lib, this_ptr, body, true, level,
{ ) {
Ok(_) => (), Ok(_) => (),
Err(err) => match *err { Err(err) => match *err {
ERR::LoopBreak(false, ..) => continue, ERR::LoopBreak(false, ..) => continue,
@ -571,7 +568,8 @@ impl Engine {
Ok(_) => (), Ok(_) => (),
err => break err.map(|_| Dynamic::UNIT), err => break err.map(|_| Dynamic::UNIT),
} }
}, }
}
// For loop // For loop
Stmt::For(x, ..) => { Stmt::For(x, ..) => {

View File

@ -1,5 +1,4 @@
#![cfg(not(feature = "no_std"))] #![cfg(not(feature = "no_time"))]
#![cfg(not(target_family = "wasm"))]
use rhai::{Engine, EvalAltResult}; use rhai::{Engine, EvalAltResult};