ReviewOS

zig-utils/zig-js

Structured clone: encode flat string fields directly

#671
Closed chris opened this 10 days ago ·
10 days ago

Parent: #473 Related: #480, #493

Problem

Structured clone writes ordinary string values through canonical WTF-8, but Blob/File metadata still passes physical StringCell bytes directly to the wire writer. A non-ASCII flat-Latin-1 File name can therefore be serialized as a raw one-byte image rather than canonical WTF-8. The existing primitive/Error paths also allocate an arena transcode before copying the same bytes into the final wire buffer.

Scope

  • Add one representation-aware wire-string writer for String Values.
  • Borrow canonical and ASCII storage directly.
  • Encode physical flat Latin-1 directly into one exactly reserved wire field without an intermediate conversion allocation.
  • Route primitive strings, Error messages, Blob types, and File names through the shared path.
  • Preserve WTF-8 lone surrogates, astral pairs, frame limits, failure atomicity, object identity, transfer/SAB behavior, and the versioned wire format.

No-workaround rules

  • No wire-version fork, invalid byte acceptance, system transcoder, hidden second copy, lossy UTF-8 replacement, source-specific special case, or disabled frame limit.
  • No stack, queue, job, Promise, Worker, or Map/Set iterator changes.

Acceptance

  • Canonical, ASCII, physical flat-Latin-1, astral, and lone-surrogate strings round-trip exactly.
  • File name/type metadata uses the same exact representation-aware writer.
  • Flat-Latin-1 encoding reserves and fills the final wire field once with no temporary conversion allocation.
  • Malformed/frame-limit, SAB/transfer, OOM, focused/full unit, TSan where applicable, and relevant Test262 gates remain exact.
10 days ago

Landed on main in da430db1.

The shared writer now covers primitive String values, Error messages, Blob types, and File names. Canonical storage is borrowed directly. A physical flat-Latin-1 image is counted once, exactly reserved, and encoded directly into the final wire field with no temporary allocation or wire-version change.

Coverage includes ASCII, non-Latin-1 canonical text, astral pairs, lone surrogates, Latin-1 content, Error and File metadata, exact raw flat-image wire bytes, one-allocation accounting, allocator failure atomicity, frame limits, malformed frames, SAB tokens, and transfer behavior. The storage master switch remains intentionally off; the raw flat-image primitive is tested directly while integration exercises the current canonical representation.

Verification:

  • focused structured-clone correctness: 12 passed across the two exact filter groups
  • focused TSan: 13 passed, 0 failed, 0 leaked
  • full unit inventory: 1766 passed, 1 skipped, 0 failed, 0 leaked
  • Test262: 0 relevant structuredClone, Blob, or File host cases exist in this checkout, so no flip measurement applies

The first full sharded run hit an unrelated cooperative-GC attempt-counter assertion. Its exact focused rerun passed, and the complete rerun was clean.

Sign in to comment on this issue.