Skip to content

Commit

Permalink
Merge pull request #443 from schungx/bug-fixes
Browse files Browse the repository at this point in the history
Fix bug in catch error variable.
  • Loading branch information
schungx committed Sep 3, 2021
2 parents a31a1ef + 7171199 commit 5cc1fd1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
@@ -1,7 +1,7 @@
Rhai Release Notes
==================

Version 1.0.3
Version 1.0.4
=============

Bug fixes
Expand Down
12 changes: 10 additions & 2 deletions src/parse.rs
Expand Up @@ -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);

Expand All @@ -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,
))
}
Expand Down
12 changes: 12 additions & 0 deletions tests/throw.rs
Expand Up @@ -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>(
"
Expand Down

0 comments on commit 5cc1fd1

Please sign in to comment.