Skip to content

fix: clone Date and RegExp values in structuredClone - #10975

Closed
proggeramlug wants to merge 2 commits into
mainfrom
fix/10829-structured-clone-date
Closed

proggeramlug wants to merge 2 commits into
mainfrom
fix/10829-structured-clone-date

Conversation

@proggeramlug

@proggeramlug proggeramlug commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Clone Date cells by copying their timestamp into a new GC cell. Nested references to one Date resolve to one cloned Date through the existing memo.
  • Handle RegExp's current GC_TYPE_REGEXP layout. The former branch under GC_TYPE_OBJECT could no longer run, so RegExp clones also kept the source identity. A clone retains source and flags, starts with lastIndex = 0, and can be mutated independently.
  • Add runtime regressions and a Node parity fixture for direct, invalid, and nested Dates plus RegExp.

Closes #10829.

Validation

  • RUST_TEST_THREADS=1 CARGO_BUILD_JOBS=4 cargo test -p perry-runtime --lib structured_clone -- --test-threads=1 (6 passed)
  • cargo fmt --all --check and git diff --check
  • Node v26.5.1 and a fresh Perry build produce identical output for test-files/test_gap_10829_structured_clone_date_regexp.ts.

Scope

The existing Map, Set, ArrayBuffer, typed buffer, and DataView branches already allocate clone objects. Error and Blob/File behavior needs separate work; this PR fixes the Date aliasing reported in #10829 and the adjacent dead RegExp clone branch.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed structuredClone so cloned Date and RegExp objects are independent from their originals.
    • Preserved date values and regular expression patterns and flags during cloning.
    • Ensured mutations to cloned objects do not affect the source objects.
    • Reset cloned regular expressions’ lastIndex to match expected runtime behavior.

@coderabbitai

coderabbitai Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 9e290d74-db08-40c9-80bc-11f5bc427b82

📥 Commits

Reviewing files that changed from the base of the PR and between c1569e2 and 6d481f7.

📒 Files selected for processing (3)
  • changelog.d/10975-structured-clone-date-regexp.md
  • crates/perry-runtime/src/builtins/globals.rs
  • test-files/test_gap_10829_structured_clone_date_regexp.ts

Included review availability: Your plan provides up to 8 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

structuredClone now creates independent Date and RegExp objects. The runtime preserves Date values, rebuilds RegExp values, memoizes clones, and adds tests for identity, mutable state, nested references, and lastIndex.

Changes

Structured clone object support

Layer / File(s) Summary
Date and RegExp clone paths
crates/perry-runtime/src/builtins/globals.rs
js_structured_clone_inner now allocates fresh Dates and rebuilds RegExps from their source and flags. The clone memo records each result. The previous generic RegExp path was removed.
Clone behavior validation
crates/perry-runtime/src/builtins/globals.rs, test-files/test_gap_10829_structured_clone_date_regexp.ts, changelog.d/10975-structured-clone-date-regexp.md
Runtime and TypeScript tests verify independent identity, preserved timestamps and RegExp properties, nested clone identity, invalid Date values, and RegExp lastIndex behavior. The changelog records the fix.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~15 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant structuredClone
  participant js_structured_clone_inner
  participant RuntimeObject
  Caller->>structuredClone: clone Date or RegExp
  structuredClone->>js_structured_clone_inner: process value
  js_structured_clone_inner->>RuntimeObject: allocate independent clone
  js_structured_clone_inner-->>structuredClone: return memoized clone
  structuredClone-->>Caller: return clone
Loading

Merge Risk: ⚪ Minimal · up to 6d481

structuredClone now creates independent Date and RegExp values with the expected copied state. The supplied regression coverage validates the behavior, so the change is ready to merge.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The description explains the fix, lists the implementation and tests, identifies the related issue, and states the scope. It does not use every template heading or checklist item, but it provides the …
Title check ✅ Passed The title clearly and concisely describes the main change: fixing Date and RegExp cloning in structuredClone.
Linked Issues check ✅ Passed The changes satisfy the coding requirements in [#10829]. js_structured_clone_inner now allocates a new Date cell with the source timestamp and records it in the existing clone memo. This supports di…
Out of Scope Changes check ✅ Passed All changed files support [#10829]. The runtime changes implement Date and RegExp structured cloning. The runtime tests and TypeScript fixture provide regression coverage. The changelog documents the …
Full details: Docstring Coverage

Explanation

Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@proggeramlug

Copy link
Copy Markdown
Contributor Author

Landed on main in merge train 256 (#11018, v0.5.1638), main f5cfbff882.

Carried at head 6d481f7036. The landed tree is byte-identical to the validated train tree (9b108dd3c9), and CI on the train head passed every job except the known public-baseline lint step: all 6 gap shards, cargo-test, e2e-scoped, gc-stress, check, warnings and security-audit green.

Trains rebase-merge, so commits get new SHAs and GitHub cannot mark this PR merged. Closed as landed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

structuredClone(date) returns the SAME object — mutating the clone mutates the original

1 participant