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

YearFromTime() in NativeDate infinite loop #235

Open
enricoscoda opened this issue Sep 8, 2015 · 3 comments
Open

YearFromTime() in NativeDate infinite loop #235

enricoscoda opened this issue Sep 8, 2015 · 3 comments
Labels
bug Issues considered a bug

Comments

@enricoscoda
Copy link

I don't know why my program was receiving that input but YearFromTime(-6.77681005679532E19) causes an invite loop. Simple fix is changing mid, hi and lo variables to long. I guess the function can continue returning an int as result without changing or compromising current functionality and normal inputs.

@gbrail
Copy link
Collaborator

gbrail commented Sep 8, 2015

Thanks for finding this -- I agree it's a problem, because even when it's not a loop it can take a very very long time -- we need a better solution. If you know of any date/time parsing code we can re-use then that might help a lot.

I tried using the standard Java date-time parsing code here, but the problem is that ECMAScript has very very specific ideas of time which are not exactly the same as Java's.

(I also don't think that we want to include JODATime, because so far Rhino has no dependencies and adding one will greatly complicate other people's lives.)

@enricoscoda
Copy link
Author

Yes, 22 iterations (with int to long fix) are too much!
Maybe it's crazy but project Nashorn must implement the same specs. I love the ability of using interpreted mode of Rhino and ability to completely serialise an entire scope to disk. But sharing parts of code from Nashorn might help solving problems like this only once. For example Nashorn has this simple code (from its NativeDate.java)

// ECMA 15.9.1.3 Year Number
private static int yearFromTime(final double t) {
    int y = (int) Math.floor(t / (msPerDay * 365.2425)) + 1970;
    final double t2 = timeFromYear(y);
    if (t2 > t) {
        y--;
    } else if (t2 + msPerDay * daysInYear(y) <= t) {
        y++;
    }
    return y;
}

Not sure if it works in all cases but worth a try.

@gbrail
Copy link
Collaborator

gbrail commented Sep 8, 2015

Yep, that code snippet is also in jsdate.cpp, part of the Mozilla code base (and quite venerable I believe):

https://dxr.mozilla.org/mozilla-central/source/js/src/jsdate.cpp

@p-bakker p-bakker added the bug Issues considered a bug label Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues considered a bug
Projects
None yet
Development

No branches or pull requests

3 participants