Merge branch 'bug-fixes'

This commit is contained in:
Stephen Chung 2021-09-03 10:19:58 +08:00
commit cbe08fb64e
3 changed files with 23 additions and 3 deletions

View File

@ -28,7 +28,7 @@ Enhancements
* `Engine::register_type_XXX` are now available even under `no_object`.
Version 1.0.3
Version 1.0.4
=============
* Fixed bug with `catch` variable used in `catch` block.

View File

@ -2916,7 +2916,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);
@ -2938,8 +2938,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>(
"