Skip to content

Commit

Permalink
rune: Fix formatting panic for 0.13.x (#749)
Browse files Browse the repository at this point in the history
This ensures that we don't get an error on formatting a value without a debug protocol.

Instead attempt to print *something*

(fixes #747)
  • Loading branch information
VorpalBlade authored Jul 17, 2024
1 parent aac3bb7 commit 7ca2581
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions crates/rune/src/runtime/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,16 @@ impl Value {
vm_write!(f, "{:?}", value);
}
value => {
// reborrow f to avoid moving it
let result =
vm_try!(caller.call_protocol_fn(Protocol::STRING_DEBUG, value.clone(), (f,),));
caller.call_protocol_fn(Protocol::STRING_DEBUG, self.clone(), (&mut *f,));

vm_try!(<()>::from_value(result));
return VmResult::Ok(());
if let VmResult::Ok(result) = result {
vm_try!(<()>::from_value(result));
} else {
let type_info = vm_try!(value.type_info());
vm_write!(f, "<{} object at {:p}>", type_info, value);
}
}
};

Expand Down Expand Up @@ -2030,7 +2035,15 @@ impl fmt::Debug for Value {
let mut o = Formatter::new();

if value.string_debug(&mut o).is_err() {
return Err(fmt::Error);
match value.type_info() {
VmResult::Ok(type_info) => {
write!(f, "<{} object at {:p}>", type_info, value)?;
}
VmResult::Err(e) => {
write!(f, "<unknown object at {:p}: {}>", value, e)?;
}
}
return Ok(());
}

f.write_str(o.as_str())?;
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2058,7 +2058,7 @@ impl Vm {
}

#[inline]
#[tracing::instrument(skip(self))]
#[tracing::instrument(skip(self, return_value))]
fn op_return_internal(
&mut self,
return_value: Value,
Expand Down

0 comments on commit 7ca2581

Please sign in to comment.