Merge pull request #513 from schungx/master

Fix bug in try block.
This commit is contained in:
Stephen Chung 2022-01-24 08:57:35 +08:00 committed by GitHub
commit ed7ccb21c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 4 deletions

View File

@ -1,9 +1,20 @@
Rhai Release Notes Rhai Release Notes
================== ==================
Version 1.4.2
=============
Bug fixes
---------
* Variables introduced inside `try` blocks are now properly cleaned up upon an exception.
Version 1.4.1 Version 1.4.1
============= =============
This is primarily a bug-fix version which fixes a large number of bugs.
Bug fixes Bug fixes
--------- ---------

View File

@ -3,7 +3,7 @@ members = [".", "codegen"]
[package] [package]
name = "rhai" name = "rhai"
version = "1.4.1" version = "1.5.0"
edition = "2018" edition = "2018"
authors = ["Jonathan Turner", "Lukáš Hozda", "Stephen Chung", "jhwgh1968"] authors = ["Jonathan Turner", "Lukáš Hozda", "Stephen Chung", "jhwgh1968"]
description = "Embedded scripting for Rust" description = "Embedded scripting for Rust"

View File

@ -35,7 +35,7 @@ impl Engine {
state.scope_level += 1; state.scope_level += 1;
} }
let mut result = Dynamic::UNIT; let mut result = Ok(Dynamic::UNIT);
for stmt in statements { for stmt in statements {
let _mods_len = global.num_imports(); let _mods_len = global.num_imports();
@ -49,7 +49,11 @@ impl Engine {
stmt, stmt,
restore_orig_state, restore_orig_state,
level, level,
)?; );
if result.is_err() {
break;
}
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
if matches!(stmt, Stmt::Import(_, _, _)) { if matches!(stmt, Stmt::Import(_, _, _)) {
@ -89,7 +93,7 @@ impl Engine {
state.always_search_scope = orig_always_search_scope; state.always_search_scope = orig_always_search_scope;
} }
Ok(result) result
} }
/// Evaluate an op-assignment statement. /// Evaluate an op-assignment statement.

View File

@ -302,3 +302,68 @@ fn test_get_set_collection() -> Result<(), Box<EvalAltResult>> {
Ok(()) Ok(())
} }
#[cfg(not(feature = "no_index"))]
#[test]
fn test_get_set_indexer() -> Result<(), Box<EvalAltResult>> {
type MyMap = std::collections::BTreeMap<String, INT>;
let mut engine = Engine::new();
engine
.register_type_with_name::<MyMap>("MyMap")
.register_fn("new_map", || MyMap::new())
.register_indexer_get_result(|map: &mut MyMap, index: &str| {
map.get(index)
.cloned()
.ok_or_else(|| format!("Index `{}` not found!", index).into())
})
.register_indexer_set(|map: &mut MyMap, index: &str, value: INT| {
map.insert(index.to_string(), value);
})
.register_fn("to_string", |map: &mut MyMap| format!("{:?}", map));
assert_eq!(
engine.eval::<INT>(
r#"
let my_map = new_map();
my_map["eggs"] = 42;
my_map["eggs"]
"#,
)?,
42
);
assert!(engine
.eval::<INT>(
r#"
let my_map = new_map();
my_map["eggs"] = 42;
my_map["not_found"]
"#,
)
.is_err());
assert_eq!(
engine.eval::<INT>(
r#"
let my_map = new_map();
my_map["eggs"] = 42;
try {
let eggs = my_map["eggs"];
let eggs = my_map["not found"];
}
catch(x)
{
print("Not found!");
}
my_map["eggs"]
"#,
)?,
42
);
Ok(())
}

View File

@ -29,6 +29,12 @@ fn test_try_catch() -> Result<(), Box<EvalAltResult>> {
123 123
); );
#[cfg(not(feature = "unchecked"))]
assert_eq!(
engine.eval::<INT>("let x = 42; try { let y = 123; print(x/0); } catch { x = 0 } x")?,
0
);
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
assert_eq!( assert_eq!(
engine.eval::<INT>( engine.eval::<INT>(