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

Side by Side Diff: dart/frog/leg/lib/js_helper.dart

Issue 9424027: Implement Date. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years, 10 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #library('js_helper'); 5 #library('js_helper');
6 6
7 #import('coreimpl.dart'); 7 #import('coreimpl.dart');
8 8
9 #source('date_helper.dart');
10
9 /** 11 /**
10 * Returns true if both arguments are numbers. 12 * Returns true if both arguments are numbers.
11 * If only the first argument is a number, throws the given message as 13 * If only the first argument is a number, throws the given message as
12 * exception. 14 * exception.
13 */ 15 */
14 bool checkNumbers(var a, var b, var message) { 16 bool checkNumbers(var a, var b, var message) {
15 if (a is num) { 17 if (a is num) {
16 if (b is num) { 18 if (b is num) {
17 return true; 19 return true;
18 } else { 20 } else {
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 static List newList(length) { 358 static List newList(length) {
357 if (length == null) return JS("Object", @"new Array()"); 359 if (length == null) return JS("Object", @"new Array()");
358 if ((length is !int) || (length < 0)) { 360 if ((length is !int) || (length < 0)) {
359 throw new IllegalArgumentException(length); 361 throw new IllegalArgumentException(length);
360 } 362 }
361 return JS("Object", @"new Array($0)", length); 363 return JS("Object", @"new Array($0)", length);
362 } 364 }
363 365
364 static num dateNow() => JS("num", @"Date.now()"); 366 static num dateNow() => JS("num", @"Date.now()");
365 367
366 static num mathFloor(num value) => JS("num", @"Math.floor($0)", value);
367
368 static brokenDownDateToSecondsSinceEpoch(
369 int years, int month, int day, int hours, int minutes, int seconds,
370 bool isUtc) {
371 throw 'Primitives.brokenDownDateToSecondsSinceEpoch is not implemented';
372 }
373
374 static int getCurrentMs() {
375 throw 'Primitives.getCurrentMs is not implemented';
376 }
377
378 static int getYear(int secondsSinceEpoch, bool isUtc) {
379 throw 'Primitives.getYear is not implemented';
380 }
381
382 static int getMonth(int secondsSinceEpoch, bool isUtc) {
383 throw 'Primitives.getMonth is not implemented';
384 }
385
386 static int getDay_(int secondsSinceEpoch, bool isUtc);
387
388 static int getHours(int secondsSinceEpoch, bool isUtc) {
389 throw 'Primitives.getHours is not implemented';
390 }
391
392 static int getMinutes(int secondsSinceEpoch, bool isUtc) {
393 throw 'Primitives.getMinutes is not implemented';
394 }
395
396 static int getSeconds(int secondsSinceEpoch, bool isUtc) {
397 throw 'Primitives.getSeconds is not implemented';
398 }
399
400 static String stringFromCharCodes(charCodes) { 368 static String stringFromCharCodes(charCodes) {
401 for (var i in charCodes) { 369 for (var i in charCodes) {
402 if (i is !int) throw new IllegalArgumentException(i); 370 if (i is !int) throw new IllegalArgumentException(i);
403 } 371 }
404 return JS('String', @'String.fromCharCode.apply($0, $1)', null, charCodes); 372 return JS('String', @'String.fromCharCode.apply($0, $1)', null, charCodes);
405 } 373 }
374
375 static valueFromDecomposedDate(years, month, day, hours, minutes, seconds,
376 milliseconds, isUtc) {
377 checkInt(years);
378 checkInt(month);
379 if (month < 1 || 12 < month) throw new IllegalArgumentException(month);
380 checkInt(day);
381 if (day < 1 || 31 < day) throw new IllegalArgumentException(day);
382 checkInt(hours);
383 if (hours < 0 || 24 < hours) throw new IllegalArgumentException(hours);
384 checkInt(minutes);
385 if (minutes < 0 || 59 < minutes) {
386 throw new IllegalArgumentException(minutes);
387 }
388 checkInt(seconds);
389 if (seconds < 0 || 59 < seconds) {
390 // TODO(ahe): Leap seconds?
391 throw new IllegalArgumentException(seconds);
392 }
393 checkInt(milliseconds);
394 if (milliseconds < 0 || 999 < milliseconds) {
395 throw new IllegalArgumentException(milliseconds);
396 }
397 checkBool(isUtc);
398 var jsMonth = month - 1;
399 var value;
400 if (isUtc) {
401 value = JS('num', @'Date.UTC($0, $1, $2, $3, $4, $5, $6)',
402 years, jsMonth, day, hours, minutes, seconds, milliseconds);
403 } else {
404 value = JS('num', @'new Date($0, $1, $2, $3, $4, $5, $6).valueOf()',
405 years, jsMonth, day, hours, minutes, seconds, milliseconds);
406 }
407 if (value.isNaN()) throw new IllegalArgumentException('');
408 if (years <= 0 || years < 100) return patchUpY2K(value, years, isUtc);
409 return value;
410 }
411
412 static patchUpY2K(value, years, isUtc) {
413 var date = JS('Object', @'new Date($0)', value);
414 if (isUtc) {
415 JS('num', @'$0.setUTCFullYear($1)', date, years);
416 } else {
417 JS('num', @'$0.setFullYear($1)', date, years);
418 }
419 return JS('num', @'$0.valueOf()', date);
420 }
421
422 // Lazily keep a JS Date stored in the JS object.
423 static lazyAsJsDate(receiver) {
424 if (JS('bool', @'$0.date === (void 0)', receiver)) {
425 JS('void', @'$0.date = new Date($1)', receiver, receiver.value);
426 }
427 return JS('Date', @'$0.date', receiver);
428 }
429
430 static getYear(receiver) {
431 return (receiver.timeZone.isUtc)
432 ? JS('int', @'$0.getUTCFullYear()', lazyAsJsDate(receiver))
433 : JS('int', @'$0.getFullYear()', lazyAsJsDate(receiver));
434 }
435
436 static getMonth(receiver) {
437 return (receiver.timeZone.isUtc)
438 ? JS('int', @'$0.getUTCMonth()', lazyAsJsDate(receiver)) + 1
439 : JS('int', @'$0.getMonth()', lazyAsJsDate(receiver)) + 1;
440 }
441
442 static getDay(receiver) {
443 return (receiver.timeZone.isUtc)
444 ? JS('int', @'$0.getUTCDate()', lazyAsJsDate(receiver))
445 : JS('int', @'$0.getDate()', lazyAsJsDate(receiver));
446 }
447
448 static getHours(receiver) {
449 return (receiver.timeZone.isUtc)
450 ? JS('int', @'$0.getUTCHours()', lazyAsJsDate(receiver))
451 : JS('int', @'$0.getHours()', lazyAsJsDate(receiver));
452 }
453
454 static getMinutes(receiver) {
455 return (receiver.timeZone.isUtc)
456 ? JS('int', @'$0.getUTCMinutes()', lazyAsJsDate(receiver))
457 : JS('int', @'$0.getMinutes()', lazyAsJsDate(receiver));
458 }
459
460 static getSeconds(receiver) {
461 return (receiver.timeZone.isUtc)
462 ? JS('int', @'$0.getUTCSeconds()', lazyAsJsDate(receiver))
463 : JS('int', @'$0.getSeconds()', lazyAsJsDate(receiver));
464 }
465
466 static getMilliseconds(receiver) {
467 return (receiver.timeZone.isUtc)
468 ? JS('int', @'$0.getUTCMilliseconds()', lazyAsJsDate(receiver))
469 : JS('int', @'$0.getMilliseconds()', lazyAsJsDate(receiver));
470 }
471
472 static getWeekday(receiver) {
473 return (receiver.timeZone.isUtc)
474 ? JS('int', @'$0.getUTCDay()', lazyAsJsDate(receiver))
475 : JS('int', @'$0.getDay()', lazyAsJsDate(receiver));
476 }
477
478 static valueFromDateString(str) {
479 checkNull(str);
480 if (str is !String) throw new IllegalArgumentException(str);
481 var value = JS('num', @'Date.parse($0)', str);
482 if (value.isNaN()) throw new IllegalArgumentException(str);
483 return value;
484 }
406 } 485 }
407 486
408 builtin$compareTo$1(a, b) { 487 builtin$compareTo$1(a, b) {
409 checkNull(a); 488 checkNull(a);
410 if (checkNumbers(a, b, 'illegal argument')) { 489 if (checkNumbers(a, b, 'illegal argument')) {
411 if (a < b) { 490 if (a < b) {
412 return -1; 491 return -1;
413 } else if (a > b) { 492 } else if (a > b) {
414 return 1; 493 return 1;
415 } else if (a == b) { 494 } else if (a == b) {
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
673 752
674 builtin$sort$1(receiver, compare) { 753 builtin$sort$1(receiver, compare) {
675 checkNull(receiver); 754 checkNull(receiver);
676 if (!isJSArray(receiver)) return UNINTERCEPTED(receiver.sort(compare)); 755 if (!isJSArray(receiver)) return UNINTERCEPTED(receiver.sort(compare));
677 756
678 DualPivotQuicksort.sort(receiver, compare); 757 DualPivotQuicksort.sort(receiver, compare);
679 } 758 }
680 759
681 checkNull(object) { 760 checkNull(object) {
682 if (object === null) throw new NullPointerException(); 761 if (object === null) throw new NullPointerException();
762 return object;
683 } 763 }
684 764
685 checkNum(value) { 765 checkNum(value) {
686 checkNull(value); 766 checkNull(value);
687 if (value is !num) throw new IllegalArgumentException(value); 767 if (value is !num) throw new IllegalArgumentException(value);
688 return value; 768 return value;
689 } 769 }
690 770
771 checkInt(value) {
772 checkNull(value);
773 if (value is !int) throw new IllegalArgumentException(value);
774 return value;
775 }
776
777 checkBool(value) {
778 checkNull(value);
779 if (value is !bool) throw new IllegalArgumentException(value);
780 return value;
781 }
782
691 builtin$isNegative$0(receiver) { 783 builtin$isNegative$0(receiver) {
692 checkNull(receiver); 784 checkNull(receiver);
693 if (receiver is num) { 785 if (receiver is num) {
694 return (receiver === 0) ? (1 / receiver) < 0 : receiver < 0; 786 return (receiver === 0) ? (1 / receiver) < 0 : receiver < 0;
695 } else { 787 } else {
696 return UNINTERCEPTED(receiver.isNegative()); 788 return UNINTERCEPTED(receiver.isNegative());
697 } 789 }
698 } 790 }
699 791
700 builtin$isNaN$0(receiver) { 792 builtin$isNaN$0(receiver) {
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
1024 builtin$hashCode$0(receiver) { 1116 builtin$hashCode$0(receiver) {
1025 if (receiver is num) return receiver & 0x1FFFFFFF; 1117 if (receiver is num) return receiver & 0x1FFFFFFF;
1026 if (receiver is String) { 1118 if (receiver is String) {
1027 throw 'String.hashCode is not implemented'; 1119 throw 'String.hashCode is not implemented';
1028 } 1120 }
1029 if (isJSArray(receiver)) { 1121 if (isJSArray(receiver)) {
1030 throw 'List.hashCode is not implemented'; 1122 throw 'List.hashCode is not implemented';
1031 } 1123 }
1032 return UNINTERCEPTED(receiver.hashCode()); 1124 return UNINTERCEPTED(receiver.hashCode());
1033 } 1125 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698