RESOLVED FIXED 130966
Implement Array.prototype.find()
https://bugs.webkit.org/show_bug.cgi?id=130966
Summary Implement Array.prototype.find()
Antoine Quint
Reported 2014-03-31 04:21:04 PDT
I've recently stumbled upon the Array.prototype.find() method proposed in Harmony while looking for a way to establish which of the values contained in an array was found in a string first. Mozilla implements this method, and it would be nice for JSC to implement it too.
Attachments
Patch (15.75 KB, patch)
2014-04-01 02:30 PDT, Antoine Quint
no flags
Patch (15.62 KB, patch)
2014-04-24 03:29 PDT, Antoine Quint
no flags
Patch (19.66 KB, patch)
2014-04-24 03:41 PDT, Antoine Quint
no flags
Patch for landing (24.44 KB, patch)
2014-04-25 00:48 PDT, Antoine Quint
no flags
Antoine Quint
Comment 1 2014-04-01 02:30:39 PDT
Oliver Hunt
Comment 2 2014-04-01 10:31:27 PDT
Comment on attachment 228259 [details] Patch Please add tests for 0 vs undefined vs false vs null vs "" -- we want tests that cover the specific kind of equality. Also perf numbers relative to v8 and SM would be good. Test cases for array modification during search, Both test cases need to be bigger and should be using the shouldBe() shouldBeTrue(), etc functions. In general js/regress is for performancy tests where we're trying to measure speed and jit behaviour. and they use throw on incorrect result as they need to ensure that the jit's don't DCE the entire test. So r-, you need more tests. I suspect the filter, etc tests in js/ should give a good example of expected behaviour.
Antoine Quint
Comment 3 2014-04-02 01:17:24 PDT
Thanks for the feedback, I had a feeling I wasn't writing the right kind of tests. V8 doesn't implement those methods.
Antoine Quint
Comment 4 2014-04-24 03:29:05 PDT
Antoine Quint
Comment 5 2014-04-24 03:41:52 PDT
Darin Adler
Comment 6 2014-04-24 09:18:42 PDT
Comment on attachment 230068 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=230068&action=review > Source/JavaScriptCore/builtins/Array.prototype.js:195 > + throw new @TypeError("Array.prototype.find requires that |this| not be null"); I don’t see any test coverage of this. > Source/JavaScriptCore/builtins/Array.prototype.js:198 > + throw new @TypeError("Array.prototype.find requires that |this| not be undefined"); I don’t see any test coverage of this. > Source/JavaScriptCore/builtins/Array.prototype.js:204 > + throw new @TypeError("Array.prototype.find callback must be a function"); I don’t see any test coverage of this. > Source/JavaScriptCore/builtins/Array.prototype.js:217 > + throw new @TypeError("Array.prototype.findIndex requires that |this| not be null"); I don’t see any test coverage of this. > Source/JavaScriptCore/builtins/Array.prototype.js:220 > + throw new @TypeError("Array.prototype.findIndex requires that |this| not be undefined"); I don’t see any test coverage of this. > Source/JavaScriptCore/builtins/Array.prototype.js:226 > + throw new @TypeError("Array.prototype.findIndex callback must be a function"); I don’t see any test coverage of this.
Oliver Hunt
Comment 7 2014-04-24 09:59:48 PDT
Comment on attachment 230068 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=230068&action=review r=me, but add edge case tests before landing >> Source/JavaScriptCore/builtins/Array.prototype.js:198 >> + throw new @TypeError("Array.prototype.find requires that |this| not be undefined"); > > I don’t see any test coverage of this. Add .find.call(...) tests >> Source/JavaScriptCore/builtins/Array.prototype.js:204 >> + throw new @TypeError("Array.prototype.find callback must be a function"); > > I don’t see any test coverage of this. coverage test >> Source/JavaScriptCore/builtins/Array.prototype.js:226 >> + throw new @TypeError("Array.prototype.findIndex callback must be a function"); > > I don’t see any test coverage of this. ditto
Oliver Hunt
Comment 8 2014-04-24 10:00:17 PDT
Oh, also add tests for arrays with holes
Antoine Quint
Comment 9 2014-04-25 00:48:34 PDT
Created attachment 230148 [details] Patch for landing
Antoine Quint
Comment 10 2014-04-25 00:53:38 PDT
Patch for landing should have the exceptions and array with holes covered. Thanks for the review!
WebKit Commit Bot
Comment 11 2014-04-25 01:26:27 PDT
Comment on attachment 230148 [details] Patch for landing Clearing flags on attachment: 230148 Committed r167797: <http://trac.webkit.org/changeset/167797>
WebKit Commit Bot
Comment 12 2014-04-25 01:26:30 PDT
All reviewed patches have been landed. Closing bug.
Joseph Pecoraro
Comment 13 2014-04-25 10:11:40 PDT
Comment on attachment 230148 [details] Patch for landing View in context: https://bugs.webkit.org/attachment.cgi?id=230148&action=review > Source/JavaScriptCore/builtins/Array.prototype.js:210 > + for (var i = 0; i < length; i++) { > + if (callback.@call(thisArg, array[i], i, array)) > + return array[i]; > + } Should we only call the callback if "i in array" for sparse arrays? <http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.prototype.find> Seems to indicate it only proceeds to calling the callback if the "i" was present: . Repeat, while k < len . Let Pk be ToString(k). . Let kPresent be HasProperty(O, Pk). . ReturnIfAbrupt(kPresent). . If kPresent is true, then . . Let kValue be Get(O, Pk). . . ReturnIfAbrupt(kValue). . . Let testResult be the result of calling the [[Call]] internal method of predicate with T as thisArgument and a List containing kValue, k, and O as argumentsList. I'm guessing a test for this would be: arrayWithHoles = []; arrayWithHoles[10] = 0; arrayWithHoles[20] = null; arrayWithHoles[30] = false; arrayWithHoles[40] = ""; arrayWithHoles.findIndex(passUndefined); // -1 arrayWithHoles[50] = undefined; arrayWithHoles.findIndex(passUndefined); // 50 I could be wrong though. I didn't test in Firefox to see what they do.
Antoine Quint
Comment 14 2014-05-07 10:51:46 PDT
*** Bug 131698 has been marked as a duplicate of this bug. ***
Antoine Quint
Comment 15 2014-05-07 10:51:58 PDT
*** Bug 131702 has been marked as a duplicate of this bug. ***
Antoine Quint
Comment 16 2014-05-07 10:52:55 PDT
*** Bug 131701 has been marked as a duplicate of this bug. ***
Joseph Pecoraro
Comment 17 2014-05-07 12:32:23 PDT
> I could be wrong though. I didn't test in Firefox to see what they do. Just tested in Firefox, and they do -1 and 50. We do 0 and 0. Filed: <https://webkit.org/b/132658> Array.prototype.find and findIndex should skip holes
Note You need to log in before you can comment on or make changes to this bug.