Hermes: An issue about the Set.prototype.add method

Created on 2 Oct 2020  路  4Comments  路  Source: facebook/hermes

Description

According to ECAM262 Specification, when adding the value -0 to a set object. -0 should be changed to +0. But Hermes seems not to support it. I suspect this is a bug of the Hermes engine.

Version

b6530ae(release)

Testcase

~javascript
function f0() {
var v0 = new Set();
v0.add(-0);
var v1;
v0.forEach(function (value) {
v1 = 1 / value;
});
print(v1);
}
f0()
~

Command

~javascript
./build/bin/hermes -w testcase.js
~

Output

~
-Infinity
~

Expected Output

~
Infinity
~

Ecma262 Specification

23.2.3.1 Set.prototype.add ( value )

  1. If value is -0, set value to +0.
bug

Most helpful comment

@ljharb yes, since it is impossible to add a -0, it looks like there is no need for the special provisions of SameValueZero() at all in this algorithm. May be there is some hidden intent, but without knowing it it looks like not using SameValueZero() is a reasonable optimization.

All 4 comments

Thank you for catching this. We will fix it. I wonder why our test262 suite didn't catch it.

I wonder why the spec was written in such an unnecessarily complex way: https://tc39.es/ecma262/#sec-set.prototype.add . There is literally no need for SameValueZero() in that case, if we are going to only insert +0. @ljharb do you know?

@Huxpro when fixing, please remove the unnecessary call to SameValueZero().

@tmikov i'm not sure what you mean - the intention is that if the Set already contains either zero, then nothing is added; if it doesn't, then a +0 is added. I do see how since it's not possible to add a -0, it's not needed, but I wonder if perhaps an implementation-provided subclass would still have the freedom to insert a -0?

@ljharb yes, since it is impossible to add a -0, it looks like there is no need for the special provisions of SameValueZero() at all in this algorithm. May be there is some hidden intent, but without knowing it it looks like not using SameValueZero() is a reasonable optimization.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

viky293 picture viky293  路  4Comments

benjamingr picture benjamingr  路  6Comments

gaodeng picture gaodeng  路  4Comments

mweststrate picture mweststrate  路  6Comments

djschilling picture djschilling  路  6Comments