Fix bug in catch error variable.

This commit is contained in:
Stephen Chung 2021-09-03 10:05:58 +08:00
parent 2980683153
commit 71711998f1
3 changed files with 23 additions and 3 deletions

View File

@ -1,7 +1,7 @@
Rhai Release Notes
==================
Version 1.0.3
Version 1.0.4
=============
Bug fixes

View File

@ -2902,7 +2902,7 @@ fn parse_try_catch(
}
// try { body } catch (
let var_def = if match_token(input, Token::LeftParen).0 {
let err_var = if match_token(input, Token::LeftParen).0 {
let (name, pos) = parse_var_name(input)?;
let (matched, err_pos) = match_token(input, Token::RightParen);
@ -2924,8 +2924,16 @@ fn parse_try_catch(
// try { body } catch ( var ) { catch_block }
let catch_body = parse_block(input, state, lib, settings.level_up())?;
if err_var.is_some() {
// Remove the error variable from the stack
state
.stack
.pop()
.expect("stack contains at least one entry");
}
Ok(Stmt::TryCatch(
(body.into(), var_def, catch_body.into()).into(),
(body.into(), err_var, catch_body.into()).into(),
settings.pos,
))
}

View File

@ -51,6 +51,18 @@ fn test_try_catch() -> Result<(), Box<EvalAltResult>> {
42
);
assert_eq!(
engine.eval::<INT>(
"
let err = 123;
let x = 0;
try { throw 42; } catch(err) { print(err); }
err
"
)?,
123
);
assert_eq!(
engine.eval::<INT>(
"