Skip to content
Merged
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
15 changes: 15 additions & 0 deletions crates/perry-runtime/src/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,4 +758,19 @@ mod tests {
let output = unsafe { js_json_stringify(outer_boxed, TYPE_UNKNOWN) };
assert_eq!(unsafe { str_from_header(output).unwrap() }, r#"{"o":{}}"#);
}

#[test]
fn stringify_uses_two_char_short_forms_for_backspace_and_formfeed() {
// #5047: QuoteJSONString mandates the two-character escapes for
// U+0008 (\b) and U+000C (\f); Perry emitted the 4-hex-digit form for both. Other
// control characters keep the 4-hex-digit form.
let input = b"a\x08b\x0cc\x0bd";
let s = js_string_from_bytes(input.as_ptr(), input.len() as u32);
let boxed = f64::from_bits(STRING_TAG | (s as u64 & POINTER_MASK));
let output = unsafe { js_json_stringify(boxed, TYPE_UNKNOWN) };
assert_eq!(
unsafe { str_from_header(output).unwrap() },
"\"a\\bb\\fc\\u000bd\""
);
}
}
2 changes: 2 additions & 0 deletions crates/perry-runtime/src/json/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ pub(crate) unsafe fn write_escaped_string(buf: &mut String, s: &str) {
b'\n' => Some("\\n"),
b'\r' => Some("\\r"),
b'\t' => Some("\\t"),
0x08 => Some("\\b"),
0x0c => Some("\\f"),
0..=0x1f => {
if start < i {
buf_vec.extend_from_slice(&bytes[start..i]);
Expand Down