mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Scripts: an unhandled rejection's reason is read only up to the report's length
The report keeps the first 500 characters of a rejection reason. The reason string is now read up to that length along the rope, instead of being copied whole before it is cut.
This commit is contained in:
1 parent
b7278c9b2f
commit
ff9ed631fa
4 files changed
+58
-3
No files matched your search
@@ -741,7 +741,9 @@ impl Interp {
|
||||
if out.len() >= MAX_REPORTED {
|
||||
continue;
|
||||
}
|
||||
let mut message = self.describe(&reason);
|
||||
// Two code units per character keeps MAX_REASON_CHARS characters and one more, so the
|
||||
// cut below still marks a longer reason.
|
||||
let mut message = self.describe_prefix(&reason, 2 * MAX_REASON_CHARS + 2);
|
||||
if let Some((cut, _)) = message.char_indices().nth(MAX_REASON_CHARS) {
|
||||
message.truncate(cut);
|
||||
message.push('…');
|
||||
|
||||
@@ -3095,9 +3095,15 @@ impl Interp {
|
||||
/// A description of a thrown value that runs no JavaScript: `Name: message` for an error,
|
||||
/// the text of a primitive.
|
||||
pub(crate) fn describe(&mut self, v: &Value) -> String {
|
||||
let Value::Object(o) = v else { return self.primitive_to_str(v).to_rust_string() };
|
||||
self.describe_prefix(v, usize::MAX)
|
||||
}
|
||||
|
||||
/// [`Interp::describe`] with each string it reads cut to its first `max_units` code units
|
||||
/// before it is copied.
|
||||
pub(crate) fn describe_prefix(&mut self, v: &Value, max_units: usize) -> String {
|
||||
let Value::Object(o) = v else { return self.primitive_to_str(v).prefix_to_rust_string(max_units) };
|
||||
let data = |interp: &Self, k: &str| match interp.find_property(*o, &key(k)) {
|
||||
Ok(Some(Prop::Data { value: Value::String(s), .. })) => Some(s.to_rust_string()),
|
||||
Ok(Some(Prop::Data { value: Value::String(s), .. })) => Some(s.prefix_to_rust_string(max_units)),
|
||||
_ => None,
|
||||
};
|
||||
match (data(self, "name"), data(self, "message")) {
|
||||
|
||||
@@ -182,6 +182,28 @@ fn an_unhandled_rejection_is_reported_with_its_line() {
|
||||
assert!(r.unhandled_rejections().iter().all(|j| j.authored));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_long_rejection_reason_is_reported_cut_to_its_first_500_characters() {
|
||||
let mut r = realm();
|
||||
r.run(
|
||||
"function rep(t, n) { var s = ''; for (var i = 0; i < n; i++) s = s + t; return s; }\n\
|
||||
var ab = 'ab'; for (var i = 0; i < 16; i++) ab = ab + ab;\n\
|
||||
Promise.reject(ab);\n\
|
||||
Promise.reject(new RangeError(ab));\n\
|
||||
Promise.reject(rep('\\ud83d\\ude00', 600));\n\
|
||||
Promise.reject(rep('x', 500));",
|
||||
)
|
||||
.unwrap();
|
||||
let got: Vec<String> = r.unhandled_rejections().iter().map(|j| j.message.clone()).collect();
|
||||
let want = vec![
|
||||
format!("{}…", "ab".repeat(250)),
|
||||
format!("RangeError: {}…", "ab".repeat(244)),
|
||||
format!("{}…", "\u{1F600}".repeat(500)),
|
||||
"x".repeat(500),
|
||||
];
|
||||
assert_eq!(got, want);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_rejection_handled_in_a_later_job_is_not_reported() {
|
||||
let mut r = realm();
|
||||
|
||||
@@ -194,6 +194,31 @@ impl Str {
|
||||
pub(crate) fn to_rust_string(&self) -> String {
|
||||
String::from_utf16_lossy(&self.flat())
|
||||
}
|
||||
|
||||
/// The first `max_units` code units as a Rust string, read along the rope without flattening
|
||||
/// it, so a long string is not copied whole.
|
||||
pub(crate) fn prefix_to_rust_string(&self, max_units: usize) -> String {
|
||||
if self.len() <= max_units {
|
||||
return self.to_rust_string();
|
||||
}
|
||||
let mut units = Vec::with_capacity(max_units);
|
||||
let mut stack = vec![self.clone()];
|
||||
while let Some(s) = stack.pop() {
|
||||
let room = max_units - units.len();
|
||||
if room == 0 {
|
||||
break;
|
||||
}
|
||||
match &*s.0.borrow() {
|
||||
Node::Flat(f) => units.extend_from_slice(&f[..f.len().min(room)]),
|
||||
Node::Concat { left, right, .. } => {
|
||||
stack.push(right.clone());
|
||||
stack.push(left.clone());
|
||||
}
|
||||
Node::Taken => {}
|
||||
}
|
||||
}
|
||||
String::from_utf16_lossy(&units)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Str {
|
||||
|
||||
Reference in new issue
Block a user