Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Unified Diff: sdk/lib/_internal/js_runtime/lib/js_helper.dart

Issue 2830393002: js_runtime: Avoid calling JS Date constructor with years in range 00-99 (Closed)
Patch Set: add test Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/corelib/date_time10_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/js_runtime/lib/js_helper.dart
diff --git a/sdk/lib/_internal/js_runtime/lib/js_helper.dart b/sdk/lib/_internal/js_runtime/lib/js_helper.dart
index eb81e0c130dc57fc5afe29ef9806c1c02aed99a7..96c3f3c3a631bcd9dffb55e1e2c1ba0dea81a24a 100644
--- a/sdk/lib/_internal/js_runtime/lib/js_helper.dart
+++ b/sdk/lib/_internal/js_runtime/lib/js_helper.dart
@@ -1106,6 +1106,14 @@ class Primitives {
checkInt(milliseconds);
checkBool(isUtc);
var jsMonth = month - 1;
+ // The JavaScript Date constructor 'corrects' year NN to 19NN. Sidestep that
+ // correction by adjusting years out of that range and compensating with an
+ // adjustment of months. This hack should not be sensitive to leap years but
+ // use 400 just in case.
+ if (0 <= years && years < 100) {
+ years += 400;
+ jsMonth -= 400 * 12;
+ }
var value;
if (isUtc) {
value = JS('num', r'Date.UTC(#, #, #, #, #, #, #)', years, jsMonth, day,
@@ -1119,24 +1127,9 @@ class Primitives {
value > MAX_MILLISECONDS_SINCE_EPOCH) {
return null;
}
- // The JavaScript Date constructor 'corrects' year NN to 19NN. Undo that
- // correction.
- if (0 <= years && years < 100) return patchUpY2K(value, years, isUtc);
return JS('int', '#', value);
}
- static patchUpY2K(value, years, isUtc) {
- var date = JS('', r'new Date(#)', value);
- // TODO(sra): Does this work correctly if the original input wrapped months
- // into a different year?
- if (isUtc) {
- JS('num', r'#.setUTCFullYear(#)', date, years);
- } else {
- JS('num', r'#.setFullYear(#)', date, years);
- }
- return JS('num', r'#.valueOf()', date);
- }
-
// Lazily keep a JS Date stored in the JS object.
static lazyAsJsDate(DateTime receiver) {
if (JS('bool', r'#.date === (void 0)', receiver)) {
« no previous file with comments | « no previous file | tests/corelib/date_time10_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698