Skip to content

Commit

Permalink
Merge pull request #842 from schungx/master
Browse files Browse the repository at this point in the history
Fix tests.
  • Loading branch information
schungx committed Mar 4, 2024
2 parents 184427d + b70c893 commit f44af63
Show file tree
Hide file tree
Showing 18 changed files with 237 additions and 117 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -12,3 +12,4 @@ doc/rhai.json
.idea/
.idea
.idea/*
src/eval/chaining.rs
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -30,6 +30,7 @@ Enhancements
* Array/BLOB/string iterators are defined also within the `BasicIteratorPackage` in addition to the regular array/BLOB/string packages.
* `LexError::Runtime` is added for use with `Engine::on_parse_token`.
* Shared values under `sync` are now handled more elegantly -- instead of deadlocking and hanging indefinitely, it spins for a number of tries (waiting one second between each), then errors out.
* `parse_json` is also available without the `metadata` or `serde` feature -- it uses `Engine::parse_json` to parse the JSON text.


Version 1.17.2
Expand Down
2 changes: 1 addition & 1 deletion src/api/call_fn.rs
Expand Up @@ -275,7 +275,7 @@ impl Engine {
if self.is_debugger_registered() {
global.debugger_mut().status = crate::eval::DebuggerStatus::Terminate;
let node = &crate::ast::Stmt::Noop(Position::NONE);
self.run_debugger(global, caches, scope, this_ptr, node)?;
self.dbg(global, caches, scope, this_ptr, node)?;
}

#[cfg(not(feature = "no_module"))]
Expand Down
2 changes: 1 addition & 1 deletion src/api/eval.rs
Expand Up @@ -251,7 +251,7 @@ impl Engine {
if self.is_debugger_registered() {
global.debugger_mut().status = crate::eval::DebuggerStatus::Terminate;
let node = &crate::ast::Stmt::Noop(Position::NONE);
self.run_debugger(global, caches, scope, None, node)?;
self.dbg(global, caches, scope, None, node)?;
}

Ok(r)
Expand Down
85 changes: 82 additions & 3 deletions src/api/events.rs
Expand Up @@ -348,13 +348,55 @@ impl Engine {
///
/// ## Raising errors
///
/// Return `Err(...)` if there is an error, usually [`EvalAltResult::ErrorPropertyNotFound`][crate::EvalAltResult::ErrorPropertyNotFound].
/// Return `Err(...)` if there is an error, usually
/// [`EvalAltResult::ErrorPropertyNotFound`][crate::EvalAltResult::ErrorPropertyNotFound].
///
/// # Example
///
/// ```
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// # use rhai::{Engine, Dynamic, EvalAltResult, Position};
/// let mut engine = Engine::new();
///
/// engine.on_invalid_array_index(|arr, index, _| match index
/// {
/// -100 => {
/// // The array can be modified in place
/// arr.push((42_i64).into());
/// // Return a mutable reference to an element
/// let value_ref = arr.last_mut().unwrap();
/// Ok(value_ref.into())
/// }
/// 100 => {
/// let value = Dynamic::from(100_i64);
/// // Return a temporary value (not a reference)
/// Ok(value.into())
/// }
/// // Return the standard out-of-bounds error
/// _ => Err(EvalAltResult::ErrorArrayBounds(
/// arr.len(), index, Position::NONE
/// ).into()),
/// });
///
/// let r = engine.eval::<i64>("
/// let a = [1, 2, 3];
/// a[-100] += 1;
/// a[3] + a[100]
/// ")?;
///
/// assert_eq!(r, 143);
/// # Ok(()) }
/// ```
#[cfg(not(feature = "no_index"))]
#[cfg(feature = "internals")]
#[inline(always)]
pub fn on_invalid_array_index(
&mut self,
callback: impl for<'a> Fn(&'a mut crate::Array, crate::INT) -> RhaiResultOf<crate::Target<'a>>
callback: impl for<'a> Fn(
&'a mut crate::Array,
crate::INT,
EvalContext,
) -> RhaiResultOf<crate::Target<'a>>
+ SendSync
+ 'static,
) -> &mut Self {
Expand Down Expand Up @@ -385,12 +427,49 @@ impl Engine {
/// ## Raising errors
///
/// Return `Err(...)` if there is an error, usually [`EvalAltResult::ErrorPropertyNotFound`][crate::EvalAltResult::ErrorPropertyNotFound].
///
/// # Example
///
/// ```
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// # use rhai::{Engine, Dynamic, EvalAltResult, Position};
/// let mut engine = Engine::new();
///
/// engine.on_map_missing_property(|map, prop, _| match prop
/// {
/// "x" => {
/// // The object-map can be modified in place
/// map.insert("y".into(), (42_i64).into());
/// // Return a mutable reference to an element
/// let value_ref = map.get_mut("y").unwrap();
/// Ok(value_ref.into())
/// }
/// "z" => {
/// // Return a temporary value (not a reference)
/// let value = Dynamic::from(100_i64);
/// Ok(value.into())
/// }
/// // Return the standard property-not-found error
/// _ => Err(EvalAltResult::ErrorPropertyNotFound(
/// prop.to_string(), Position::NONE
/// ).into()),
/// });
///
/// let r = engine.eval::<i64>("
/// let obj = #{ a:1, b:2 };
/// obj.x += 1;
/// obj.y + obj.z
/// ")?;
///
/// assert_eq!(r, 143);
/// # Ok(()) }
/// ```
#[cfg(not(feature = "no_object"))]
#[cfg(feature = "internals")]
#[inline(always)]
pub fn on_map_missing_property(
&mut self,
callback: impl for<'a> Fn(&'a mut crate::Map, &str) -> RhaiResultOf<crate::Target<'a>>
callback: impl for<'a> Fn(&'a mut crate::Map, &str, EvalContext) -> RhaiResultOf<crate::Target<'a>>
+ SendSync
+ 'static,
) -> &mut Self {
Expand Down
2 changes: 1 addition & 1 deletion src/api/run.rs
Expand Up @@ -138,7 +138,7 @@ impl Engine {
if self.is_debugger_registered() {
global.debugger_mut().status = crate::eval::DebuggerStatus::Terminate;
let node = &crate::ast::Stmt::Noop(crate::Position::NONE);
self.run_debugger(global, caches, scope, None, node)?;
self.dbg(global, caches, scope, None, node)?;
}

Ok(())
Expand Down

0 comments on commit f44af63

Please sign in to comment.