Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions src/jsc/JSPropertyIterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,13 @@ pub struct JSPropertyIterator<'a> {
}

impl<'a> JSPropertyIterator<'a> {
pub(crate) fn get_longest_property_name(&self) -> usize {
if let Some(iter) = self.impl_ {
// `JSPropertyIteratorImpl`/`JSObject` are opaque ZST handles;
// `opaque_mut`/`opaque_ref` are the centralised zero-byte deref proofs.
Bun__JSPropertyIterator__getLongestPropertyName(
JSPropertyIteratorImpl::opaque_mut(iter.as_ptr()),
self.global_object,
JSObject::opaque_ref(self.object),
)
} else {
0
}
/// Restarts iteration from the first property. The property names were
/// collected once by [`Self::init`], so a second pass yields the same
/// names as the first.
pub(crate) fn reset(&mut self) {
self.i = 0;
self.iter_i = 0;
self.value = JSValue::ZERO;
}

/// `object` should be a `JSC::JSObject`. Non-objects will be runtime converted.
Expand Down Expand Up @@ -336,9 +331,4 @@ unsafe extern "C" {
i: usize,
);
fn Bun__JSPropertyIterator__deinit(iter: *mut JSPropertyIteratorImpl);
safe fn Bun__JSPropertyIterator__getLongestPropertyName(
iter: &mut JSPropertyIteratorImpl,
global_object: &JSGlobalObject,
object: &JSObject,
) -> usize;
}
174 changes: 105 additions & 69 deletions src/jsc/VirtualMachine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6269,97 +6269,133 @@ impl VirtualMachine {
only_non_index_properties: true,
},
)?;
let longest_name = iterator.get_longest_property_name().min(10);
let mut is_first_property = true;
while let Some(field) = iterator.next()? {
let value = iterator.value;

/// How the property block below shows one own enumerable property.
enum OwnProperty {
/// Already shown elsewhere (the `name: message` line, the stack
/// trace, the `code` line), or not printable.
Hidden,
/// Printed in full after the stack trace (`errors_to_append`).
AppendedError,
/// Printed as a ` name: value` line in the property block.
Inline,
}
let classify = |field: &bun_core::String, value: JSValue| -> OwnProperty {
if field.eql_comptime(b"message")
|| field.eql_comptime(b"name")
|| field.eql_comptime(b"stack")
{
continue;
return OwnProperty::Hidden;
}
if field.eql_comptime(b"code") && code.is_some() {
continue;
return OwnProperty::Hidden;
}

let kind = value.js_type();
if kind == JSType::ErrorInstance && !prev_had_errors {
if field.eql_comptime(b"cause") {
saw_cause = true;
}
value.protect();
errors_to_append.push(value);
OwnProperty::AppendedError
} else if kind.is_object()
|| kind.is_array()
|| value.is_primitive()
|| kind.is_string_like()
{
let prev_disable_inspect_custom = formatter.disable_inspect_custom;
let prev_quote_strings = formatter.quote_strings;
let prev_max_depth = formatter.max_depth;
let prev_format_buffer_as_text = formatter.format_buffer_as_text;
formatter.depth += 1;
formatter.format_buffer_as_text = true;
formatter.max_depth = 1;
formatter.quote_strings = true;
formatter.disable_inspect_custom = true;
// Hand-rolled drop guard restores the formatter state.
struct RestoreFmt<'a, 'f> {
f: &'a mut crate::console_object::Formatter<'f>,
d: bool,
q: bool,
m: u16,
b: bool,
}
impl Drop for RestoreFmt<'_, '_> {
fn drop(&mut self) {
self.f.depth -= 1;
self.f.max_depth = self.m;
self.f.quote_strings = self.q;
self.f.disable_inspect_custom = self.d;
self.f.format_buffer_as_text = self.b;
OwnProperty::Inline
} else {
OwnProperty::Hidden
}
};

// Names are right-aligned to the longest name the block actually
// contains: the inline properties plus the `code` line that follows
// them. Accessors never come out of the iterator (`observable: false`).
let mut longest_name: usize = if code.is_some() { b"code".len() } else { 0 };
while let Some(field) = iterator.next()? {
if matches!(classify(&field, iterator.value), OwnProperty::Inline) {
longest_name = longest_name.max(field.length());
}
}
let longest_name = longest_name.min(10);
iterator.reset();

let mut is_first_property = true;
while let Some(field) = iterator.next()? {
let value = iterator.value;
match classify(&field, value) {
OwnProperty::Hidden => continue,
OwnProperty::AppendedError => {
if field.eql_comptime(b"cause") {
saw_cause = true;
}
value.protect();
errors_to_append.push(value);
continue;
}
let restore = RestoreFmt {
f: formatter,
d: prev_disable_inspect_custom,
q: prev_quote_strings,
m: prev_max_depth,
b: prev_format_buffer_as_text,
};
let formatter = &mut *restore.f;

let pad_left = longest_name.saturating_sub(field.length());
is_first_property = false;
splat_space(writer, pad_left as u64)?;
pretty_write!(writer, " {}<r><d>:<r> ", field)?;
OwnProperty::Inline => {}
}

if allow_side_effects && global_ref.has_exception() {
global_ref.clear_exception();
let prev_disable_inspect_custom = formatter.disable_inspect_custom;
let prev_quote_strings = formatter.quote_strings;
let prev_max_depth = formatter.max_depth;
let prev_format_buffer_as_text = formatter.format_buffer_as_text;
formatter.depth += 1;
formatter.format_buffer_as_text = true;
formatter.max_depth = 1;
formatter.quote_strings = true;
formatter.disable_inspect_custom = true;
// Hand-rolled drop guard restores the formatter state.
struct RestoreFmt<'a, 'f> {
f: &'a mut crate::console_object::Formatter<'f>,
d: bool,
q: bool,
m: u16,
b: bool,
}
impl Drop for RestoreFmt<'_, '_> {
fn drop(&mut self) {
self.f.depth -= 1;
self.f.max_depth = self.m;
self.f.quote_strings = self.q;
self.f.disable_inspect_custom = self.d;
self.f.format_buffer_as_text = self.b;
}
}
let restore = RestoreFmt {
f: formatter,
d: prev_disable_inspect_custom,
q: prev_quote_strings,
m: prev_max_depth,
b: prev_format_buffer_as_text,
};
let formatter = &mut *restore.f;

let tag = Tag::get_advanced(
value,
global_ref,
TagOptions::DISABLE_INSPECT_CUSTOM | TagOptions::HIDE_GLOBAL,
)?;
let _ = if allow_ansi_color {
formatter.format::<true>(tag, writer, value, global_ref)
} else {
formatter.format::<false>(tag, writer, value, global_ref)
};
let pad_left = longest_name.saturating_sub(field.length());
is_first_property = false;
splat_space(writer, pad_left as u64)?;
pretty_write!(writer, " {}<r><d>:<r> ", field)?;

if allow_side_effects {
if global_ref.has_exception() {
global_ref.clear_exception();
}
} else if global_ref.has_exception() || formatter.failed {
return Ok(());
}
if allow_side_effects && global_ref.has_exception() {
global_ref.clear_exception();
}

pretty_write!(writer, "<r><d>,<r>\n")?;
let tag = Tag::get_advanced(
value,
global_ref,
TagOptions::DISABLE_INSPECT_CUSTOM | TagOptions::HIDE_GLOBAL,
)?;
let _ = if allow_ansi_color {
formatter.format::<true>(tag, writer, value, global_ref)
} else {
formatter.format::<false>(tag, writer, value, global_ref)
};

if allow_side_effects {
if global_ref.has_exception() {
global_ref.clear_exception();
}
} else if global_ref.has_exception() || formatter.failed {
return Ok(());
}

pretty_write!(writer, "<r><d>,<r>\n")?;
}

if let Some(code_str) = code {
Expand Down
12 changes: 0 additions & 12 deletions src/jsc/bindings/JSPropertyIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,6 @@ extern "C" JSPropertyIterator* Bun__JSPropertyIterator__create(JSC::JSGlobalObje
return JSPropertyIterator::create(vm, array.releaseData());
}

extern "C" size_t Bun__JSPropertyIterator__getLongestPropertyName(JSPropertyIterator* iter, JSC::JSGlobalObject* globalObject, JSC::JSObject* object)
{
size_t longest = 0;
for (const auto& prop : iter->properties->propertyNameVector()) {
if (prop.length() > longest) {
longest = prop.length();
}
}

return longest;
}

static EncodedJSValue getOwnProxyObject(JSPropertyIterator* iter, JSObject* object, const JSC::Identifier& prop, BunString* propertyName)
{
auto& vm = iter->vm;
Expand Down
Loading