Skip to content

fix(engine): preserve map and set iterator indices after clear (#5523) - #5528

Open
MayankSharma-2812 wants to merge 1 commit into
boa-dev:mainfrom
MayankSharma-2812:fix/map-set-iterator-clear
Open

MayankSharma-2812 wants to merge 1 commit into
boa-dev:mainfrom
MayankSharma-2812:fix/map-set-iterator-clear

Conversation

@MayankSharma-2812

Copy link
Copy Markdown

This Pull Request fixes/closes #5523.

Background

Per ECMAScript specifications (§24.1.3.1 Map.prototype.clear and §24.2.3.2 Set.prototype.clear), calling .clear() on a Map or Set does not reset or shift the underlying entry sequence when an iterator or forEach loop is actively traversing the collection. Instead, existing entries are marked as empty. Any entries subsequently inserted into the collection must retain index positions after the cleared entries so that in-flight iterators can observe them.

Previously, OrderedMap::clear() and OrderedSet::clear() unconditionally called .clear() on the backing IndexMap / IndexSet. When subsequent insertions were performed, they were placed at index 0. Active iterators (whose next_index >= 1) skipped them and terminated prematurely with { value: undefined, done: true }.

Changes

  • OrderedMap::clear(): When self.lock > 0 (indicating an active iterator or loop), preserve index offsets by populating existing slots with MapKey::Empty(i) tombstones instead of dropping the backing map. If self.lock == 0, standard .clear() is retained.
  • OrderedSet::clear(): Mirrored the tombstone preservation pattern for OrderedSet when self.lock > 0.
  • OrderedSet::is_empty(): Fixed a latent bug where is_empty() checked self.inner.len() == 0 instead of self.len() == 0, which caused false negatives when tombstones were present.
  • Unit Tests:
    • Added regression test iterator_after_clear_and_set in core/engine/src/builtins/map/tests.rs.
    • Added regression test set_iterator_after_clear_and_add in core/engine/src/builtins/set/tests.rs.

Verification

  • cargo test -p boa_engine iterator_after_clear passes.
  • All 28 existing tests in builtins::map::tests and all 30 tests in builtins::set::tests pass.
  • cargo clippy -p boa_engine -- -D warnings passed with 0 warnings.
  • cargo fmt -- --check passed cleanly.

@MayankSharma-2812
MayankSharma-2812 requested a review from a team as a code owner September 16, 2026 05:08
@github-actions github-actions Bot added the Waiting On Review Waiting on reviews from the maintainers label Sep 16, 2026
@github-actions github-actions Bot added this to the v0.23 milestone Sep 16, 2026
@github-actions github-actions Bot added C-Tests Issues and PRs related to the tests. C-Builtins PRs and Issues related to builtins/intrinsics labels Sep 16, 2026
@github-actions

Copy link
Copy Markdown

Test262 conformance changes

Test result main count PR count difference
Total 53,578 53,578 0
Passed 51,432 51,432 0
Ignored 1,648 1,648 0
Failed 498 498 0
Panics 0 0 0
Conformance 95.99% 95.99% 0.00%

Tested main commit: 69388e59f789ed0846a8d6aad1e4dc0c91b35816
Tested PR commit: 746a1fb0756b025019fe9499d12c12b15d6de182
Compare commits: 69388e5...746a1fb

@codecov

codecov Bot commented Sep 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 62.82%. Comparing base (6ddc2b4) to head (746a1fb).
⚠️ Report is 1054 commits behind head on main.

Files with missing lines Patch % Lines
core/engine/src/builtins/set/ordered_set.rs 90.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #5528       +/-   ##
===========================================
+ Coverage   47.24%   62.82%   +15.58%     
===========================================
  Files         476      536       +60     
  Lines       46892    60298    +13406     
===========================================
+ Hits        22154    37883    +15729     
+ Misses      24738    22415     -2323     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

C-Builtins PRs and Issues related to builtins/intrinsics C-Tests Issues and PRs related to the tests. Waiting On Review Waiting on reviews from the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Map iterators terminate after clear() followed by insertion

1 participant