Skip to content

Change ToInteger(-0) to return +0? #1637

Closed

Description

While working on some SpiderMonkey JIT compiler optimisations, I was wondering if it makes sense to change ToInteger(-0) to return +0 instead of -0, because in all but one case, the result of ToInteger(-0) is either explicitly or implicitly changed from -0 to +0 anyway.


Example for implicit conversion from String.prototype.startsWith:

  • An implicit conversion through the mathematical function max.
  1. If endPosition is undefined, let pos be len; else let pos be ? ToInteger(endPosition).
  2. Let end be min(max(pos, 0), len).

And from String.prototype.charAt:

  • An implicit conversion because -0 is treated the same as +0 for index positions.
  1. Let position be ? ToInteger(pos).
    ...
  2. Return the String value of length 1, containing one code unit from S, namely the code unit at index position.

And from Array.prototype.includes:

  • Implicit conversion, because ToString(-0) = ToString(+0) = "0" holds.
  1. Let n be ? ToInteger(fromIndex).
    ...
  2. If n ≥ 0, then
    a. Let k be n.
    ...
  3. Repeat, while k < len
    a. Let elementK be the result of ? Get(O, ! ToString(k)).

Example for explicit conversion from Array.prototype.indexOf:

  1. Let n be ? ToInteger(fromIndex).
    ...
  2. If n ≥ 0, then
    a. If n is -0, let k be +0; else let k be n.

Or TimeClip:

  1. Let clippedTime be ! ToInteger(time).
  2. If clippedTime is -0, set clippedTime to +0.

The only place where changing ToInteger(-0) to return +0 instead of -0 would make a difference, is Atomics.store:

  1. Let v be ? ToInteger(value).
    ...
  2. Return v.

So, changing ToInteger(-0) return +0 would allow to remove some explicit -0 to +0 conversions in various operations, but also result in a noticeable change for Atomics.store, (which we could avoid if necessary, but I'm not actually sure anyone would really notice the difference for that function when +0 instead of -0 would be returned.) Does it sound useful to anyone else to make this change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions