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
5 changes: 1 addition & 4 deletions ci/v8_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@

# rust_icu_sys selects its bindings and versioned ICU symbol names from this
# pin, which must match the major version of the ICU that V8 links.
RUST_ICU_VERSION_PINS = (
ROOT / "build/deps/rust.MODULE.bazel",
ROOT / "src/rust/i18n/BUILD.bazel",
)
RUST_ICU_VERSION_PINS = (ROOT / "build/deps/rust.MODULE.bazel",)
Comment thread
littledivy marked this conversation as resolved.
Resolved
RUST_ICU_VERSION = re.compile(r'(?<="RUST_ICU_MAJOR_VERSION_NUMBER": ")\d+(?=")')

# V8 builds these crates from Chromium's third_party/rust. Every crate in their
Expand Down
14 changes: 6 additions & 8 deletions docs/v8-updates.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ from the V8 directory.
to just bump these dependencies to the latest version on github.

If the new `com_googlesource_chromium_icu` commit changes `U_ICU_VERSION_MAJOR_NUM`
(in `source/common/unicode/uvernum.h`), update `RUST_ICU_MAJOR_VERSION_NUMBER` in both
`build/deps/rust.MODULE.bazel` and `src/rust/i18n/BUILD.bazel`. A stale value fails the
build with undefined `<function>_<version>` ICU symbols, or missing items in
`rust_icu_sys` if the two values disagree. If `rust_icu_sys` has no `bindgen/lib_<version>.rs`
for the new version, update the crate as well.
(in `source/common/unicode/uvernum.h`), update `RUST_ICU_MAJOR_VERSION_NUMBER` in
`build/deps/rust.MODULE.bazel`. If `rust_icu_sys` has no `bindgen/lib_<version>.rs` for the
new version, update the crate as well.

V8 builds `temporal_capi` (see `deps/rust/Cargo.toml`) from Chromium's
`third_party/rust`, at the revision in V8's `DEPS`. Every crate in its runtime
Expand Down Expand Up @@ -141,9 +139,9 @@ rebases the patches, regenerates `patches/v8/*.patch`, and updates:
`build/deps/deps.jsonc`.

It also regenerates `com_googlesource_chromium_icu` in `build/deps/gen/deps.MODULE.bazel`
and sets `RUST_ICU_MAJOR_VERSION_NUMBER` in `build/deps/rust.MODULE.bazel` and
`src/rust/i18n/BUILD.bazel` to the major version of the new ICU commit, and aligns the
Rust crates V8 builds from Chromium's `third_party/rust` (see step 10).
and sets `RUST_ICU_MAJOR_VERSION_NUMBER` in `build/deps/rust.MODULE.bazel` to the major
version of the new ICU commit, and aligns the Rust crates V8 builds from Chromium's
`third_party/rust` (see step 10).

If the rebase stops because of conflicts, resolve and continue it in the temporary
checkout:
Expand Down
6 changes: 6 additions & 0 deletions src/bench/buffer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("//:build/wd_bench.bzl", "wd_bench")

wd_bench(
src = "transcode.wd-bench",
data = ["transcode.js"],
)
42 changes: 42 additions & 0 deletions src/bench/buffer/transcode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2026 Cloudflare, Inc.
Comment thread
littledivy marked this conversation as resolved.
Resolved
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

// node:buffer transcode() for each conversion path, on ASCII and non-ASCII input.

import { Buffer, transcode } from 'node:buffer';

const LENGTHS = [64, 1024, 65536];

// [from, to, content], where content is the code point every character of the
// input repeats.
const PAIRS = [
['latin1', 'utf8', 'a'],
['latin1', 'utf8', '\xe9'],
['ascii', 'utf8', 'a'],
['ascii', 'ascii', 'a'],
['ascii', 'latin1', 'a'],
['latin1', 'ascii', 'a'],
['latin1', 'latin1', 'a'],
['utf8', 'latin1', 'a'],
['utf8', 'ascii', 'a'],
['utf8', 'latin1', '\xe9'],
['utf8', 'utf8', 'a'],
['utf8', 'utf8', '€'],
['ucs2', 'ucs2', 'a'],
['ucs2', 'latin1', 'a'],
];

export default {
bench(b) {
for (const [from, to, char] of PAIRS) {
const content = char === 'a' ? 'ascii' : 'non-ascii';
for (const len of LENGTHS) {
const source = Buffer.from(char.repeat(len), from);
b.run(`${from}->${to} ${content} ${len}`, () =>
b.blackBox(transcode(source, from, to))
);
}
}
},
};
11 changes: 11 additions & 0 deletions src/bench/buffer/transcode.wd-bench
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Workerd = import "/workerd/workerd.capnp";

const unitTests :Workerd.Config = (
services = [(
name = "transcode",
worker = (
modules = [(name = "worker", esModule = embed "transcode.js")],
compatibilityFlags = ["nodejs_compat"],
),
)],
);
7 changes: 0 additions & 7 deletions src/rust/i18n/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@ wd_rust_crate(
"@workerd-v8//:v8",
],
cxx_bridge_src = "ffi.rs",
# ICU, which rust_icu_sys binds to, is linked through V8.
link_deps = ["@workerd-v8//:v8"],
# rust_icu_sys's `versioned_function!` expands in this crate and reads the ICU version from
# here. Keep it synchronized with the rust_icu_sys annotation in build/deps/rust.MODULE.bazel;
# ci/v8_update.py updates both.
rustc_env = {"RUST_ICU_MAJOR_VERSION_NUMBER": "78"},
test_deps = ["//src/rust/jsg-test"],
visibility = ["//visibility:public"],
deps = [
"//src/rust/jsg",
"@crates_vendor//rust_icu_sys",
"@crates_vendor//thiserror",
],
)
136 changes: 122 additions & 14 deletions src/rust/i18n/dispatch-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

use jsg_test::Harness;

use super::*;

/// All four transcodable encodings, for exhaustively testing every pair.
Expand All @@ -14,13 +12,6 @@ const ENCODINGS: [Encoding; 4] = [
Encoding::Utf16Le,
];

// `Harness::new()` initializes the V8 platform, which is what installs the
// embedded ICU data. ICU converter opens fail without it, even though these
// tests never create an isolate or run JavaScript.
fn init_icu() -> Harness {
Harness::new()
}

/// Runs both transcode steps the way [`crate::transcode`] does -- size,
/// allocate, convert, narrow to the written length -- against a plain
/// `Vec` rather than a V8 backing store, and returns the written bytes.
Expand All @@ -39,7 +30,6 @@ fn transcode(source: &[u8], from: Encoding, to: Encoding) -> Result<Vec<u8>, Tra

#[test]
fn every_pair_round_trips_ascii_text() {
let _harness = init_icu();
for &from in &ENCODINGS {
// UTF16LE source bytes must have even length; every other encoding
// is happy with plain ASCII bytes.
Expand All @@ -57,7 +47,6 @@ fn every_pair_round_trips_ascii_text() {

#[test]
fn every_pair_empty_input_is_empty_output() {
let _harness = init_icu();
for &from in &ENCODINGS {
for &to in &ENCODINGS {
let result = transcode(&[], from, to);
Expand All @@ -72,7 +61,6 @@ fn every_pair_empty_input_is_empty_output() {

#[test]
fn identity_pairs_round_trip() {
let _harness = init_icu();
for &encoding in &ENCODINGS {
// UTF16LE source bytes must be well-formed UTF-16LE to pass through
// unchanged.
Expand All @@ -89,9 +77,130 @@ fn identity_pairs_round_trip() {
}
}

#[test]
fn identity_pairs_replace_ill_formed_input() {
const FFFD: &[u8] = "\u{fffd}".as_bytes();
for (source, expected) in [
(&[0xe2, 0x82][..], FFFD.to_vec()),
(&[0xed, 0xa0, 0x80], FFFD.repeat(3)),
(&[0x41, 0xff, 0x42], [b"A", FFFD, b"B"].concat()),
] {
assert_eq!(
transcode(source, Encoding::Utf8, Encoding::Utf8).unwrap(),
expected,
"{source:x?}"
);
}
for (source, expected) in [
(&[0x61, 0x00, 0x62][..], &[0x61, 0x00, 0xfd, 0xff][..]),
(&[0x00, 0xde, 0x3d, 0xd8], &[0xfd, 0xff, 0xfd, 0xff]),
(&[0x3d, 0xd8, 0x00, 0xde], &[0x3d, 0xd8, 0x00, 0xde]),
(&[0x61], &[0xfd, 0xff]),
(&[0x41, 0x00, 0x00, 0xd8, 0x42], &[0x41, 0x00, 0xfd, 0xff]),
(&[0x00, 0xdc, 0x42], &[0xfd, 0xff, 0xfd, 0xff]),
] {
assert_eq!(
transcode(source, Encoding::Utf16Le, Encoding::Utf16Le).unwrap(),
expected,
"{source:x?}"
);
}
}

#[test]
fn identity_pairs_copy_the_well_formed_prefix() {
let prefix = "\u{e9}".repeat(50);
let source = [prefix.as_bytes(), &[0xe2, 0x82], b"z"].concat();
let transcoder = Transcoder::new(&source, Encoding::Utf8, Encoding::Utf8).unwrap();
// The prefix is copied, and only the three bytes after it are sized at
// three bytes each.
assert_eq!(transcoder.dest_len(), prefix.len() + 9);
assert_eq!(
transcode(&source, Encoding::Utf8, Encoding::Utf8).unwrap(),
[prefix.as_bytes(), "\u{fffd}z".as_bytes()].concat()
);

let prefix: Vec<u8> = "\u{1f600}"
.repeat(20)
.encode_utf16()
.flat_map(u16::to_le_bytes)
.collect();
let source = [&prefix[..], &[0x00, 0xd8, 0x42]].concat();
assert_eq!(
transcode(&source, Encoding::Utf16Le, Encoding::Utf16Le).unwrap(),
[&prefix[..], &[0xfd, 0xff]].concat()
);
}

#[test]
fn ascii_sources_are_copied_into_single_byte_and_utf8_targets() {
let source = b"plain ascii";
for (from, to) in [
(Encoding::Ascii, Encoding::Ascii),
(Encoding::Ascii, Encoding::Latin1),
(Encoding::Ascii, Encoding::Utf8),
(Encoding::Latin1, Encoding::Ascii),
(Encoding::Latin1, Encoding::Utf8),
(Encoding::Utf8, Encoding::Ascii),
(Encoding::Utf8, Encoding::Latin1),
] {
let transcoder = Transcoder::new(source, from, to).unwrap();
assert_eq!(transcoder.dest_len(), source.len(), "{from:?} -> {to:?}");
assert_eq!(
transcode(source, from, to).unwrap(),
source,
"{from:?} -> {to:?}"
);
}
}

#[test]
fn text_after_an_ascii_prefix_is_still_converted() {
// Long enough that the first non-ASCII byte is past simdutf's first
// 64-byte block.
let prefix = "a".repeat(100);
let cases = [
(
Encoding::Ascii,
Encoding::Utf8,
vec![0xe9],
"\u{fffd}".as_bytes(),
),
(
Encoding::Ascii,
Encoding::Latin1,
vec![0xe9],
b"?".as_slice(),
),
(Encoding::Latin1, Encoding::Ascii, vec![0xe9], b"?"),
(Encoding::Utf8, Encoding::Latin1, "\u{e9}".into(), &[0xe9]),
(Encoding::Utf8, Encoding::Ascii, "\u{20ac}".into(), b"?"),
(Encoding::Utf8, Encoding::Latin1, vec![0xe2, 0x82], b"?"),
];
for (from, to, tail, expected) in cases {
let source = [prefix.as_bytes(), &tail, b"z"].concat();
let expected = [prefix.as_bytes(), expected, b"z"].concat();
assert_eq!(
transcode(&source, from, to).unwrap(),
expected,
"{from:?} -> {to:?}"
);
}
}

#[test]
fn latin1_to_utf8_is_sized_exactly() {
let source = [0x41, 0xe9, 0xff];
let transcoder = Transcoder::new(&source, Encoding::Latin1, Encoding::Utf8).unwrap();
assert_eq!(transcoder.dest_len(), 5);
assert_eq!(
transcode(&source, Encoding::Latin1, Encoding::Utf8).unwrap(),
"A\u{e9}\u{ff}".as_bytes()
);
}

#[test]
fn unmappable_characters_become_question_marks() {
let _harness = init_icu();
// '☕' (U+2615, HOT BEVERAGE) has no representation in ASCII or Latin-1.
let utf8_source = "☕".as_bytes();
let utf16le_source = [0x15, 0x26];
Expand Down Expand Up @@ -140,7 +249,6 @@ fn odd_length_utf16le_to_utf8_is_rejected() {

#[test]
fn odd_length_utf16le_to_latin1_is_rejected() {
let _harness = init_icu();
let result = transcode(&[0x61], Encoding::Utf16Le, Encoding::Latin1);
assert_eq!(result, Err(TranscodeError::OddUtf16leInput));
}
Expand Down
Loading
Loading