Skip to content
Open
Changes from 8 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
101 changes: 87 additions & 14 deletions core/engine/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,26 +277,99 @@ impl Stack {
}

#[cfg(feature = "trace")]
/// Display the stack trace of the current frame.
const MAX_VALUE_LEN: usize = 18;
#[cfg(feature = "trace")]
const MAX_STACK_WIDTH: usize = 68;

#[cfg(feature = "trace")]
fn raw_value(value: &JsValue) -> String {
match value {
v if v.is_callable() => "func".to_string(),
v if v.is_object() => "obj".to_string(),
v if v.is_undefined() => "und".to_string(),
v if v.is_null() => "null".to_string(),
v => v.display().to_string(),
}
}

#[cfg(feature = "trace")]
fn truncate_display(val: &str) -> String {
if val.len() <= Self::MAX_VALUE_LEN {
return val.to_string();
}
let mut end = Self::MAX_VALUE_LEN - 2;
while !val.is_char_boundary(end) && end > 0 {
end -= 1;
}
format!("{}..", &val[..end])
}

#[cfg(feature = "trace")]
fn display_trace(&self, frame: &CallFrame, frame_count: usize) -> String {
let mut string = String::from("[ ");
for (i, (j, value)) in self.stack.iter().enumerate().rev().enumerate() {
match value {
value if value.is_callable() => string.push_str("[function]"),
value if value.is_object() => string.push_str("[object]"),
value => string.push_str(&value.display().to_string()),
}
let total = self.stack.len();
if total == 0 {
return "[ <empty> ]".to_string();
}

if frame.frame_pointer() == j {
let _ = write!(string, " |{frame_count}|");
} else if i + 1 != self.stack.len() {
string.push(',');
// Build (raw_value, index) pairs, top of stack first
let entries: Vec<(String, usize)> = self
.stack
.iter()
.enumerate()
.rev()
.map(|(j, v)| (Self::raw_value(v), j))
.collect();

// Group consecutive identical values, but never merge frame pointer entries
let mut groups: Vec<(String, usize, Option<usize>)> = Vec::new();
for (val, idx) in &entries {
let is_frame = frame.frame_pointer() == *idx;

if !is_frame
&& let Some(last) = groups.last_mut()
&& last.0 == *val
&& last.2.is_none()
{
last.1 += 1;
continue;
}

string.push(' ');
let marker = if is_frame { Some(frame_count) } else { None };
groups.push((val.clone(), 1, marker));
}

string.push(']');
let mut string = String::from("[ ");
let mut truncated = false;

for (i, (val, count, marker)) in groups.iter().enumerate() {
let display_val = Self::truncate_display(val);
let part = if *count > 1 {
format!("{display_val} (x{count})")
} else {
display_val
};

let separator = if let Some(fc) = marker {
format!(" |{fc}|")
} else if i + 1 < groups.len() {
",".to_string()
} else {
String::new()
};

let addition = format!("{part}{separator} ");
if string.len() + addition.len() > Self::MAX_STACK_WIDTH - 10 {
truncated = true;
break;
}
string.push_str(&addition);
}

if truncated {
let _ = write!(string, ".. ({total} total) ]");
} else {
string.push(']');
}
string
}
}
Expand Down
Loading