[Fix] avoid relying on indexOf's HasProperty+Get semantics, unlike includes's bare Get - #54
Open
rajanpanth wants to merge 1 commit into
Open
Conversation
…ike `includes`'s bare `Get` The fast path delegated to $indexOf.apply(this, arguments) whenever searchElement wasn't NaN/undefined and fromIndex was finite. For ordinary arrays this is equivalent to the spec's own includes algorithm, but indexOf's per-index step is "HasProperty, then Get only if present" while includes's is a bare Get -- so for an exotic object whose has trap disagrees with its get trap (e.g. a Proxy), delegating to indexOf can silently miss an element that includes must find. Removed the optimization; the existing manual loop already implements the correct includes algorithm (bare Get, SameValueZero) and was only being skipped by this shortcut.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #54 +/- ##
==========================================
+ Coverage 94.54% 98.00% +3.45%
==========================================
Files 5 5
Lines 55 50 -5
Branches 8 7 -1
==========================================
- Hits 52 49 -3
+ Misses 3 1 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The fast path delegates to
$indexOf.apply(this, arguments)wheneversearchElementisn't NaN/undefined andfromIndexis finite. For ordinary arrays this matches the spec's ownincludesalgorithm, butindexOf's per-index step is HasProperty, then Get only if present, whileincludes's is a bareGet— per spec (Array.prototype.includesexplicitly notes it "does not skip missing array elements, instead treating them as undefined", unlikeindexOf).For an exotic object whose
hastrap disagrees with itsgettrap, that difference is observable:Changes
implementation.js: removed theindexOf-based fast path (and its now-unused$isNaN/$isFinite/$indexOfrequires). The existing manual loop already implements the correctincludesalgorithm (bareGetviaO[k],SameValueZero) and was only being skipped by this shortcut — so removing it is enough, no new logic needed.test/tests.js: one new case (guarded bytypeof Proxy === 'function', matching the pattern inis-callable's own tests) confirming a lyinghastrap doesn't hide an element that's still reachable viaget.Testing
implementation.jspath is actually exercised by the bug, sinceindex.js/shimmed.jsprefer the native method when available).eslintreports the same pre-existinglinebreak-style/no-magic-numbersfindings with and without this change (Windows checkout CRLF artifact + pre-existing style, unrelated) — no new lint issues, and one magic-number warning (-1) actually goes away since that line is removed.