Fixed bug in using error variable within catch block.

This commit is contained in:
Stephen Chung 2021-09-02 22:17:45 +08:00
parent 73c0ba4ec5
commit 457be797a2
3 changed files with 54 additions and 0 deletions

View File

@ -1,6 +1,15 @@
Rhai Release Notes
==================
Version 1.0.3
=============
Bug fixes
---------
* Fixed bug with `catch` variable used in `catch` block.
Version 1.0.2
=============

View File

@ -2915,6 +2915,7 @@ fn parse_try_catch(
}
let name = state.get_identifier(name);
state.stack.push((name.clone(), AccessMode::ReadWrite));
Some(Ident { name, pos })
} else {
None

View File

@ -29,6 +29,50 @@ fn test_try_catch() -> Result<(), Box<EvalAltResult>> {
123
);
#[cfg(not(feature = "no_function"))]
assert_eq!(
engine.eval::<INT>(
"
fn foo(x) { try { throw 42; } catch (x) { return x; } }
foo(0)
"
)?,
42
);
assert_eq!(
engine.eval::<INT>(
"
let err = 123;
let x = 0;
try { throw 42; } catch(err) { return err; }
"
)?,
42
);
assert_eq!(
engine.eval::<INT>(
"
let foo = 123;
let x = 0;
try { throw 42; } catch(err) { return foo; }
"
)?,
123
);
assert_eq!(
engine.eval::<INT>(
"
let foo = 123;
let x = 0;
try { throw 42; } catch(err) { return err; }
"
)?,
42
);
#[cfg(not(feature = "unchecked"))]
assert!(matches!(
*engine