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
Milestone

Comments

@sjrd
Copy link
Member

@sjrd 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 label Jan 25, 2017
@sjrd

This comment has been minimized.

Copy link
Member Author

@sjrd sjrd commented Mar 23, 2017

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

@sjrd

This comment has been minimized.

Copy link
Member Author

@sjrd 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 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
Projects
None yet
1 participant
You can’t perform that action at this time.