Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 /** | 9 /** |
| 10 * Returns true if both arguments are numbers. | 10 * Returns true if both arguments are numbers. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 return result - b; | 75 return result - b; |
| 76 } else { | 76 } else { |
| 77 return result + b; | 77 return result + b; |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 return UNINTERCEPTED(a % b); | 80 return UNINTERCEPTED(a % b); |
| 81 } | 81 } |
| 82 | 82 |
| 83 tdiv(var a, var b) { | 83 tdiv(var a, var b) { |
| 84 if (checkNumbers(a, b, "num~/ expects a number as second operand.")) { | 84 if (checkNumbers(a, b, "num~/ expects a number as second operand.")) { |
| 85 var tmp = a / b; | 85 return (a / b).truncate(); |
| 86 // TODO(ngeoffray): Use tmp.floor and tmp.ceil when | |
| 87 // we can handle them. | |
| 88 if (tmp < 0) { | |
| 89 return JS("num", @"Math.ceil($0)", tmp); | |
| 90 } else { | |
| 91 return JS("num", @"Math.floor($0)", tmp); | |
| 92 } | |
| 93 } | 86 } |
| 94 return UNINTERCEPTED(a ~/ b); | 87 return UNINTERCEPTED(a ~/ b); |
| 95 } | 88 } |
| 96 | 89 |
| 97 eq(var a, var b) { | 90 eq(var a, var b) { |
| 98 if (JS("bool", @"typeof $0 === 'object'", a)) { | 91 if (JS("bool", @"typeof $0 === 'object'", a)) { |
| 99 if (JS_HAS_EQUALS(a)) { | 92 if (JS_HAS_EQUALS(a)) { |
| 100 return UNINTERCEPTED(a == b) === true; | 93 return UNINTERCEPTED(a == b) === true; |
| 101 } else { | 94 } else { |
| 102 return JS("bool", @"$0 === $1", a, b); | 95 return JS("bool", @"$0 === $1", a, b); |
| (...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 650 checkNull(receiver); | 643 checkNull(receiver); |
| 651 if (!isJSArray(receiver)) return UNINTERCEPTED(receiver.sort(compare)); | 644 if (!isJSArray(receiver)) return UNINTERCEPTED(receiver.sort(compare)); |
| 652 | 645 |
| 653 DualPivotQuicksort.sort(receiver, compare); | 646 DualPivotQuicksort.sort(receiver, compare); |
| 654 } | 647 } |
| 655 | 648 |
| 656 checkNull(object) { | 649 checkNull(object) { |
| 657 if (object === null) throw new NullPointerException(); | 650 if (object === null) throw new NullPointerException(); |
| 658 } | 651 } |
| 659 | 652 |
| 653 checkNum(value) { | |
| 654 checkNull(value); | |
| 655 if (value is !num) throw new IllegalArgumentException(value); | |
| 656 return value; | |
| 657 } | |
| 658 | |
| 660 builtin$isNegative$0(receiver) { | 659 builtin$isNegative$0(receiver) { |
| 661 checkNull(receiver); | 660 checkNull(receiver); |
| 662 if (receiver is num) { | 661 if (receiver is num) { |
| 663 return (receiver === 0) ? (1 / receiver) < 0 : receiver < 0; | 662 return (receiver === 0) ? (1 / receiver) < 0 : receiver < 0; |
| 664 } else { | 663 } else { |
| 665 return UNINTERCEPTED(receiver.isNegative()); | 664 return UNINTERCEPTED(receiver.isNegative()); |
| 666 } | 665 } |
| 667 } | 666 } |
| 668 | 667 |
| 669 builtin$isNaN$0(receiver) { | 668 builtin$isNaN$0(receiver) { |
| 670 checkNull(receiver); | 669 checkNull(receiver); |
| 671 if (receiver is num) { | 670 if (receiver is num) { |
| 672 return JS("bool", @"isNaN($0)", receiver); | 671 return JS("bool", @"isNaN($0)", receiver); |
| 673 } else { | 672 } else { |
| 674 return UNINTERCEPTED(receiver.isNegative()); | 673 return UNINTERCEPTED(receiver.isNegative()); |
| 675 } | 674 } |
| 676 } | 675 } |
| 677 | 676 |
| 678 builtin$remainder$1(a, b) { | 677 builtin$remainder$1(a, b) { |
| 679 checkNull(a); | 678 checkNull(a); |
| 680 if (checkNumbers(a, b, "num.remainder expects a number as second operand.")) { | 679 if (checkNumbers(a, b, "num.remainder expects a number as second operand.")) { |
| 681 throw new NotImplementedException(); | 680 return JS("num", @"$0 % $1", a, b); |
| 682 } else { | 681 } else { |
| 683 return UNINTERCEPTED(a.remainder(b)); | 682 return UNINTERCEPTED(a.remainder(b)); |
| 684 } | 683 } |
| 685 } | 684 } |
| 686 | 685 |
| 687 builtin$abs$0(receiver) { | 686 builtin$abs$0(receiver) { |
| 688 checkNull(receiver); | 687 checkNull(receiver); |
| 689 if (receiver is !num) return UNINTERCEPTED(receiver.abs()); | 688 if (receiver is !num) return UNINTERCEPTED(receiver.abs()); |
| 690 | 689 |
| 691 throw new NotImplementedException(); | 690 return JS("num", @"Math.abs($0)", receiver); |
| 692 } | 691 } |
| 693 | 692 |
| 694 builtin$toInt$0(receiver) { | 693 builtin$toInt$0(receiver) { |
| 695 checkNull(receiver); | 694 checkNull(receiver); |
| 696 if (receiver is !num) return UNINTERCEPTED(receiver.toInt()); | 695 if (receiver is !num) return UNINTERCEPTED(receiver.toInt()); |
| 697 | 696 |
| 698 throw new NotImplementedException(); | 697 if (receiver.isNaN()) throw new BadNumberFormatException("NaN"); |
| 698 | |
| 699 if (receiver.isInfinite()) throw new BadNumberFormatException("Infinity"); | |
| 700 | |
| 701 var truncated = receiver.truncate(); | |
| 702 return JS("bool", @"$0 == -0.0", truncated) ? 0 : truncated; | |
| 699 } | 703 } |
| 700 | 704 |
| 701 builtin$ceil$0(receiver) { | 705 builtin$ceil$0(receiver) { |
| 702 checkNull(receiver); | 706 checkNull(receiver); |
| 703 if (receiver is !num) return UNINTERCEPTED(receiver.ceil()); | 707 if (receiver is !num) return UNINTERCEPTED(receiver.ceil()); |
| 704 | 708 |
| 705 throw new NotImplementedException(); | 709 return JS("num", @"Math.ceil($0)", receiver); |
| 706 } | 710 } |
| 707 | 711 |
| 708 builtin$floor$0(receiver) { | 712 builtin$floor$0(receiver) { |
| 709 checkNull(receiver); | 713 checkNull(receiver); |
| 710 if (receiver is !num) return UNINTERCEPTED(receiver.floor()); | 714 if (receiver is !num) return UNINTERCEPTED(receiver.floor()); |
| 711 | 715 |
| 712 throw new NotImplementedException(); | 716 return JS("num", @"Math.floor($0)", receiver); |
| 713 } | 717 } |
| 714 | 718 |
| 715 builtin$isInfinite$0(receiver) { | 719 builtin$isInfinite$0(receiver) { |
| 716 checkNull(receiver); | 720 checkNull(receiver); |
| 717 if (receiver is !num) return UNINTERCEPTED(receiver.isInfinite()); | 721 if (receiver is !num) return UNINTERCEPTED(receiver.isInfinite()); |
| 718 | 722 |
| 719 throw new NotImplementedException(); | 723 return JS("bool", @"$0 == Infinity", receiver) |
| 724 || JS("bool", @"$0 == -Infinity", receiver); | |
| 720 } | 725 } |
| 721 | 726 |
| 722 builtin$negate$0(receiver) { | 727 builtin$negate$0(receiver) { |
| 723 checkNull(receiver); | 728 checkNull(receiver); |
| 724 if (receiver is !num) return UNINTERCEPTED(receiver.negate()); | 729 if (receiver is !num) return UNINTERCEPTED(receiver.negate()); |
| 725 | 730 |
| 726 throw new NotImplementedException(); | 731 return JS("num", @"-$0", receiver); |
| 727 } | 732 } |
| 728 | 733 |
| 729 builtin$round$0(receiver) { | 734 builtin$round$0(receiver) { |
| 730 checkNull(receiver); | 735 checkNull(receiver); |
| 731 if (receiver is !num) return UNINTERCEPTED(receiver.round()); | 736 if (receiver is !num) return UNINTERCEPTED(receiver.round()); |
| 732 | 737 |
| 733 throw new NotImplementedException(); | 738 return JS("num", @"Math.round($0)", receiver); |
| 734 } | 739 } |
| 735 | 740 |
| 736 builtin$toDouble$0(receiver) { | 741 builtin$toDouble$0(receiver) { |
| 737 checkNull(receiver); | 742 checkNull(receiver); |
| 738 if (receiver is !num) return UNINTERCEPTED(receiver.toDouble()); | 743 if (receiver is !num) return UNINTERCEPTED(receiver.toDouble()); |
| 739 | 744 |
| 740 throw new NotImplementedException(); | 745 // TODO(ahe): Just return receiver? |
| 746 return JS("double", @"$0 + 0", receiver); | |
|
kasperl
2012/02/21 07:29:02
If you're going for a ToNumber conversion here, ma
floitsch
2012/02/21 13:34:02
There is an is !num check just above. No need to d
ahe
2012/04/20 10:51:03
Follow-up in https://chromiumcodereview.appspot.co
| |
| 741 } | 747 } |
| 742 | 748 |
| 743 builtin$truncate$0(receiver) { | 749 builtin$truncate$0(receiver) { |
| 744 checkNull(receiver); | 750 checkNull(receiver); |
| 745 if (receiver is !num) return UNINTERCEPTED(receiver.truncate()); | 751 if (receiver is !num) return UNINTERCEPTED(receiver.truncate()); |
| 746 | 752 |
| 747 throw new NotImplementedException(); | 753 return receiver < 0 ? receiver.ceil() : receiver.floor(); |
| 748 } | 754 } |
| 749 | 755 |
| 750 | |
| 751 builtin$toStringAsFixed$1(receiver, fractionDigits) { | 756 builtin$toStringAsFixed$1(receiver, fractionDigits) { |
| 752 checkNull(receiver); | 757 checkNull(receiver); |
| 753 if (receiver is !num) { | 758 if (receiver is !num) { |
| 754 return UNINTERCEPTED(receiver.toStringAsFixed(fractionDigits)); | 759 return UNINTERCEPTED(receiver.toStringAsFixed(fractionDigits)); |
| 755 } | 760 } |
| 756 | 761 |
| 757 throw new NotImplementedException(); | 762 return JS("String", @"$0.toFixed($1)", receiver, fractionDigits); |
| 758 } | 763 } |
| 759 | 764 |
| 760 builtin$allMatches$1(receiver, str) { | 765 builtin$allMatches$1(receiver, str) { |
| 761 checkNull(receiver); | 766 checkNull(receiver); |
| 762 if (receiver is !String) return UNINTERCEPTED(receiver.allMatches(str)); | 767 if (receiver is !String) return UNINTERCEPTED(receiver.allMatches(str)); |
| 763 | 768 |
| 764 throw new NotImplementedException(); | 769 throw new NotImplementedException(); |
| 765 } | 770 } |
| 766 | 771 |
| 767 builtin$concat$1(receiver, other) { | 772 builtin$concat$1(receiver, other) { |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 853 | 858 |
| 854 throw new NotImplementedException(); | 859 throw new NotImplementedException(); |
| 855 } | 860 } |
| 856 | 861 |
| 857 builtin$trim$0(receiver) { | 862 builtin$trim$0(receiver) { |
| 858 checkNull(receiver); | 863 checkNull(receiver); |
| 859 if (receiver is !String) return UNINTERCEPTED(receiver.trim()); | 864 if (receiver is !String) return UNINTERCEPTED(receiver.trim()); |
| 860 | 865 |
| 861 throw new NotImplementedException(); | 866 throw new NotImplementedException(); |
| 862 } | 867 } |
| 868 | |
| 869 class MathNatives { | |
| 870 static int parseInt(String str) { | |
| 871 throw 'MathNatives.parseInt is not implemented'; | |
| 872 } | |
| 873 | |
| 874 static double parseDouble(String str) { | |
| 875 throw 'MathNatives.parseDouble is not implemented'; | |
| 876 } | |
| 877 | |
| 878 static double sqrt(num value) | |
| 879 => JS("double", @"Math.sqrt($0)", checkNum(value)); | |
| 880 | |
| 881 static double sin(num value) | |
| 882 => JS("double", @"Math.sin($0)", checkNum(value)); | |
| 883 | |
| 884 static double cos(num value) | |
| 885 => JS("double", @"Math.cos($0)", checkNum(value)); | |
| 886 | |
| 887 static double tan(num value) | |
| 888 => JS("double", @"Math.tan($0)", checkNum(value)); | |
| 889 | |
| 890 static double acos(num value) | |
| 891 => JS("double", @"Math.acos($0)", checkNum(value)); | |
| 892 | |
| 893 static double asin(num value) | |
| 894 => JS("double", @"Math.asin($0)", checkNum(value)); | |
| 895 | |
| 896 static double atan(num value) | |
| 897 => JS("double", @"Math.atan($0)", checkNum(value)); | |
| 898 | |
| 899 static double atan2(num a, num b) | |
| 900 => JS("double", @"Math.atan2($0)", checkNum(value)); | |
| 901 | |
| 902 static double exp(num value) | |
| 903 => JS("double", @"Math.exp($0)", checkNum(value)); | |
| 904 | |
| 905 static double log(num value) | |
| 906 => JS("double", @"Math.log($0)", checkNum(value)); | |
| 907 | |
| 908 static num pow(num value, num exponent) { | |
| 909 checkNum(value); | |
| 910 checkNum(exponent); | |
| 911 return JS("num", @"Math.pow($0, $1)", value, exponent); | |
| 912 } | |
| 913 | |
| 914 static double random() => JS("double", "Math.random()"); | |
| 915 } | |
| OLD | NEW |