Skip to content

Commit

Permalink
Add test to ref loop printing.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Jan 5, 2024
1 parent f3f4b41 commit bbe9a35
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
30 changes: 28 additions & 2 deletions src/types/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,23 @@ impl fmt::Display for Dynamic {
f.write_str((***v).type_name())
}

#[cfg(not(feature = "no_closure"))]
Union::Shared(ref cell, ..) if cfg!(feature = "unchecked") => {
#[cfg(not(feature = "sync"))]
match cell.try_borrow() {
Ok(v) => {
fmt::Display::fmt(&*v, f)?;
f.write_str(" (shared)")
}
Err(_) => f.write_str("<shared>"),
}
#[cfg(feature = "sync")]
fmt::Display::fmt(&*cell.read().unwrap(), f)
}
#[cfg(not(feature = "no_closure"))]
Union::Shared(..) => {
// Avoid infinite recursion for shared values in a reference loop.
pub fn display_fmt(
fn display_fmt(
value: &Dynamic,
f: &mut fmt::Formatter<'_>,
dict: &mut std::collections::HashSet<*const Dynamic>,

Check failure on line 510 in src/types/dynamic.rs

View workflow job for this annotation

GitHub Actions / NoStdBuild (ubuntu-latest, --profile unix, false)

cannot find type `HashSet` in module `std::collections`
Expand Down Expand Up @@ -646,10 +659,23 @@ impl fmt::Debug for Dynamic {
f.write_str((***v).type_name())
}

#[cfg(not(feature = "no_closure"))]
Union::Shared(ref cell, ..) if cfg!(feature = "unchecked") => {
#[cfg(not(feature = "sync"))]
match cell.try_borrow() {
Ok(v) => {
fmt::Debug::fmt(&*v, f)?;
f.write_str(" (shared)")
}
Err(_) => f.write_str("<shared>"),
}
#[cfg(feature = "sync")]
fmt::Debug::fmt(&*cell.read().unwrap(), f)
}
#[cfg(not(feature = "no_closure"))]
Union::Shared(..) => {
// Avoid infinite recursion for shared values in a reference loop.
pub fn debug_fmt(
fn debug_fmt(
value: &Dynamic,
f: &mut fmt::Formatter<'_>,
dict: &mut std::collections::HashSet<*const Dynamic>,

Check failure on line 681 in src/types/dynamic.rs

View workflow job for this annotation

GitHub Actions / NoStdBuild (ubuntu-latest, --profile unix, false)

cannot find type `HashSet` in module `std::collections`
Expand Down
10 changes: 2 additions & 8 deletions src/types/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,13 @@ pub struct Scope<'a> {
impl fmt::Display for Scope<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, (name, constant, value)) in self.iter_inner().enumerate() {
#[cfg(not(feature = "no_closure"))]
let value_is_shared = if value.is_shared() { " (shared)" } else { "" };
#[cfg(feature = "no_closure")]
let value_is_shared = "";

writeln!(
f,
"[{}] {}{}{} = {:?}",
"[{}] {}{} = {:?}",
i + 1,
if constant { "const " } else { "" },
name,
value_is_shared,
*value.read_lock::<Dynamic>().unwrap(),
value,
)?;
}

Expand Down
24 changes: 22 additions & 2 deletions tests/stack.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![cfg(not(feature = "unchecked"))]
use rhai::{Engine, EvalAltResult, ParseErrorType, INT};
use rhai::{Dynamic, Engine, EvalAltResult, ParseErrorType, INT};

#[test]
#[cfg(not(feature = "no_function"))]
Expand All @@ -20,7 +20,6 @@ fn test_stack_overflow_fn_calls() {

let max = engine.max_call_levels();

#[cfg(not(feature = "unchecked"))]
assert!(matches!(
*engine
.run(&format!(
Expand Down Expand Up @@ -132,3 +131,24 @@ fn test_stack_overflow_parsing() {
#[cfg(not(feature = "no_function"))]
engine.compile("fn abc(x) { x + 1 }").unwrap();
}

#[test]
#[cfg(not(feature = "no_closure"))]
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_object"))]
fn test_stack_overflow_ref_loop() {
let engine = Engine::new();

let x = engine
.eval::<Dynamic>(
"
let x;
let data = #{ foo: || x = this };
data.foo();
x
",
)
.unwrap();

println!("{x:?}");
}

0 comments on commit bbe9a35

Please sign in to comment.