You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The exact issue is the if (changes <= 1) break; line. Stepping through the algorithm for the broken case, we can see that we're breaking too early, when only one swap has occurred in the pass but there's still more work to do.
i is 0
begin 5,4,5,1
j is 0
swap 5 and 4
j is 1
j is 2
swap 5 and 1
end 4,5,1,5
i is 1
begin 4,5,1,5
j is 0
j is 1
swap 5 and 1
end 4,1,5,5
[4,1,5,5]
The solution is to check changes < 1 or just !changes. That's what's happened upstream in addition to some other optimizations we should eventually grab, but for now I want a very targeted bugfix.
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I know Paul did a bunch of work on testing the interpreter. It would be good to (a) see if this changes the pass/fail state of any tests and (b) perhaps add a test for it if not.
I'm working on a regression unit test for this in the code-dot-org repo right now. The tests Paul set up for the interpreter are running in Circle on this PR right now. It runs the test262 EcmaScript Conformance Test Suite, which I suspect will not hit the particular sort edge case we're fixing here, but I also don't feel this merits the effort to set up a second test harness on this repo for custom tests.
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.
Turns out the
Array.prototype.sortpolyfill, implemented as a bubble sort, is broken.Brought to our attention by a Zendesk ticket and easily reproduced in App Lab with the following code:
The problem is that this
Array.prototype.sortpolyfill, which uses a bubble sort, is implemented incorrectly:JS-Interpreter/interpreter.js
Lines 1100 to 1117 in 37933e4
The exact issue is the
if (changes <= 1) break;line. Stepping through the algorithm for the broken case, we can see that we're breaking too early, when only one swap has occurred in the pass but there's still more work to do.The solution is to check
changes < 1or just!changes. That's what's happened upstream in addition to some other optimizations we should eventually grab, but for now I want a very targeted bugfix.