Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js.Date functions can return NaN, so they should be typed as Double #2751

Closed
sjrd opened this issue Jan 25, 2017 · 2 comments
Closed

js.Date functions can return NaN, so they should be typed as Double #2751

sjrd opened this issue Jan 25, 2017 · 2 comments
Assignees
Labels
bug Confirmed bug. Needs to be fixed.
Milestone

Comments

@sjrd
Copy link
Member

sjrd commented Jan 25, 2017

Was: On Firefox, (some) js.Date functions can return -0, which cause CCEs

Reproducible with testSuite/testHtmlFastOpt with Firefox. Also:

    import js.DynamicImplicits._

    val utc = java.util.Date.UTC(0, 0, 0, 0, 0, 0)
    val date = new js.Date(utc)

    println("getUTCDate = " + date.getUTCDate())
    println("getUTCMonth = " + date.getUTCMonth())
    println("getUTCFullYear = " + date.getUTCFullYear())

    val dyndate = date.asInstanceOf[js.Dynamic]
    val h = dyndate.getUTCHours()
    println(js.typeOf(h))
    println(h)
    println(1.0 / h)

    println("getUTCHours = " + date.getUTCHours())
    println("getUTCMinutes = " + date.getUTCMinutes())
    println("getUTCSeconds = " + date.getUTCSeconds())

gives:

getUTCDate = 31
getUTCMonth = 11
getUTCFullYear = 1899
number
0
-Infinity
uncaught exception: scala.scalajs.runtime.UndefinedBehaviorError: An undefined behavior was detected: 0 is not an instance of java.lang.Integer
@sjrd sjrd added the bug Confirmed bug. Needs to be fixed. label Jan 25, 2017
@sjrd
Copy link
Member Author

sjrd commented Mar 23, 2017

Fixing this requires a source-incompatible change, so it can't go in 0.6.x.

@sjrd sjrd added this to the v1.0.0 milestone Mar 23, 2017
@sjrd sjrd modified the milestones: v1.0.0-M1, v1.0.0 Jun 3, 2017
@sjrd sjrd modified the milestones: v1.0.0-M2, v1.0.0-M3 Oct 11, 2017
@sjrd sjrd modified the milestones: v1.0.0-M3, v1.0.0-M4 Jan 26, 2018
@sjrd
Copy link
Member Author

sjrd commented Apr 1, 2018

So, actually, it's much worse than that: all methods of js.Date can return NaN on all engines, if the js.Date was created with an invalid string:

C:\Users\Sepi>node
> var d = new Date("foobar")
undefined
> d.getYear()
NaN
> d.getFullYear()
NaN
> d.getDay()
NaN
> d.getDate()
NaN
> d.getMonth()
NaN

etc.

So whether Firefox is allowed to return -0 in some cases is pretty irrelevant at this point. All the methods must clearly marked as returning Doubles.

Also, it's OK to call one of the setX method with NaN, in which case the date will be set to an invalid date, and therefore all methods will return NaN after that.

@sjrd sjrd changed the title On Firefox, (some) js.Date functions can return -0, which cause CCEs js.Date functions can return NaN, so they should be typed as Double Apr 1, 2018
@sjrd sjrd self-assigned this Apr 1, 2018
sjrd added a commit to sjrd/scala-js that referenced this issue Apr 1, 2018
For invalid dates, created from a string that cannot be parsed, the
methods of `js.Date` return `NaN`. Therefore, they must be typed as
`Double`s. By extension, this covers up for Firefox returning -0
for some valid date as well, instead of +0 like everyone else.

In the process, we fix the behavior of `j.u.Date.parse` for invalid
inputs.
@sjrd sjrd closed this as completed in 387273b Apr 2, 2018
sjrd added a commit that referenced this issue Apr 2, 2018
Fix #2751: Fix the definition of `js.Date` for invalid dates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug. Needs to be fixed.
Projects
None yet
Development

No branches or pull requests

1 participant