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.
~javascript
function f0() {
var v0 = new Set();
v0.add(-0);
var v1;
v0.forEach(function (value) {
v1 = 1 / value;
});
print(v1);
}
f0()
~
~javascript
./build/bin/hermes -w testcase.js
~
~
-Infinity
~
~
Infinity
~
23.2.3.1 Set.prototype.add ( value )
- If value is -0, set value to +0.
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.
Most helpful comment
@ljharb yes, since it is impossible to add a
-0, it looks like there is no need for the special provisions ofSameValueZero()at all in this algorithm. May be there is some hidden intent, but without knowing it it looks like not usingSameValueZero()is a reasonable optimization.