hermes icon indicating copy to clipboard operation
hermes copied to clipboard

A bug in Array.prototype.sort

Open Georgezxh opened this issue 2 years ago • 1 comments

Hermes version: 0.12.0 OS version (if any): ubuntu20.04

Bug Description

The following code should print a sorted array but hermes didn't sort it.

Testcase:

function foo1() {
     foo2.prototype = arguments;
     new foo2();
}
function foo2() {
var x = Array.prototype.sort.call(this);
for (var i = 0; i < x.length; i++) {
    print(x[i]);
    }
}
foo1(3,2,4,1);

Output

3 2 4 1

The Expected Behavior

1 2 3 4

Georgezxh avatar Apr 09 '23 16:04 Georgezxh

You are trying to sort an empty object where all the indexed properties belong to the parent. As far as I can tell, our implementation follows ES2020, which treats this as implementation defined. From https://262.ecma-international.org/11.0/#sec-array.prototype.sort:

Let proto be obj.[GetPrototypeOf]. If proto is not null and there exists an integer j such that all of the conditions below are satisfied then the sort order is implementation-defined:

Not sure if later versions change the requirements.

tmikov avatar Apr 10 '23 14:04 tmikov