From de241c2b60187fdb49b08b708c5d59aec1a7b507 Mon Sep 17 00:00:00 2001 From: Jason Williams <936006+jasonwilliams@users.noreply.github.com> Date: Sat, 3 Jun 2023 11:47:31 +0100 Subject: [PATCH] show object kind, name and address when using dbg! (#2960) * show object kind, name and address when using dbg! * Update boa_engine/src/object/jsobject.rs Co-authored-by: Haled Odat <8566042+HalidOdat@users.noreply.github.com> * Update boa_engine/src/object/jsobject.rs Co-authored-by: Haled Odat <8566042+HalidOdat@users.noreply.github.com> * Run `rust-fmt` --------- Co-authored-by: Haled Odat <8566042+HalidOdat@users.noreply.github.com> --- boa_engine/src/object/jsobject.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/boa_engine/src/object/jsobject.rs b/boa_engine/src/object/jsobject.rs index 56eed87144..6d48b7bdf4 100644 --- a/boa_engine/src/object/jsobject.rs +++ b/boa_engine/src/object/jsobject.rs @@ -14,7 +14,7 @@ use crate::{ property::{PropertyDescriptor, PropertyKey}, string::utf16, value::PreferredType, - Context, JsResult, JsValue, + Context, JsResult, JsString, JsValue, }; use boa_gc::{self, Finalize, Gc, GcRefCell, Trace}; use boa_interner::Sym; @@ -1092,7 +1092,6 @@ impl RecursionLimiter { impl Debug for JsObject { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result { - let ptr: *const _ = self.vtable(); let limiter = RecursionLimiter::new(self.as_ref()); // Typically, using `!limiter.live` would be good enough here. @@ -1102,10 +1101,26 @@ impl Debug for JsObject { // Instead, we check if the object has appeared before in the entire graph. This means that objects will appear // at most once, hopefully making things a bit clearer. if !limiter.visited && !limiter.live { - f.debug_struct("JsObject") - .field("vtable", &ptr) - .field("object", &self.inner.object) - .finish() + let ptr: *const _ = self.as_ref(); + let obj = self.borrow(); + let kind = obj.kind(); + if obj.is_function() { + let name_prop = obj + .properties() + .get(&PropertyKey::String(JsString::from("name"))); + let name = match name_prop { + None => JsString::default(), + Some(prop) => prop + .value() + .and_then(JsValue::as_string) + .cloned() + .unwrap_or_default(), + }; + + return f.write_fmt(format_args!("({:?}) {:?} 0x{:X}", kind, name, ptr as usize)); + } + + f.write_fmt(format_args!("({:?}) 0x{:X}", kind, ptr as usize)) } else { f.write_str("{ ... }") }