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. |
| 11 * If only the first argument is a number, throws the given message as | 11 * If only the first argument is a number, throws the given message as |
| 12 * exception. | 12 * exception. |
| 13 */ | 13 */ |
| 14 bool checkNumbers(var a, var b, var message) { | 14 bool checkNumbers(var a, var b, var message) { |
| 15 if (a is num) { | 15 if (a is num) { |
| 16 if (b is num) { | 16 if (b is num) { |
| 17 return true; | 17 return true; |
| 18 } else { | 18 } else { |
| 19 checkNull(b); | |
| 19 throw new IllegalArgumentException(message); | 20 throw new IllegalArgumentException(message); |
| 20 } | 21 } |
| 21 } | 22 } |
| 22 return false; | 23 return false; |
| 23 } | 24 } |
| 24 | 25 |
| 25 | 26 |
| 26 bool isJSArray(var value) { | 27 bool isJSArray(var value) { |
| 27 return JS("bool", @"$0.constructor === Array", value); | 28 return JS("bool", @"$0.constructor === Array", value); |
| 28 } | 29 } |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 386 static int getMinutes(int secondsSinceEpoch, bool isUtc) { | 387 static int getMinutes(int secondsSinceEpoch, bool isUtc) { |
| 387 throw 'Primitives.getMinutes is not implemented'; | 388 throw 'Primitives.getMinutes is not implemented'; |
| 388 } | 389 } |
| 389 | 390 |
| 390 static int getSeconds(int secondsSinceEpoch, bool isUtc) { | 391 static int getSeconds(int secondsSinceEpoch, bool isUtc) { |
| 391 throw 'Primitives.getSeconds is not implemented'; | 392 throw 'Primitives.getSeconds is not implemented'; |
| 392 } | 393 } |
| 393 } | 394 } |
| 394 | 395 |
| 395 builtin$compareTo$1(a, b) { | 396 builtin$compareTo$1(a, b) { |
| 397 checkNull(a); | |
| 396 if (checkNumbers(a, b, 'illegal argument')) { | 398 if (checkNumbers(a, b, 'illegal argument')) { |
| 397 if (a < b) { | 399 if (a < b) { |
| 398 return -1; | 400 return -1; |
| 399 } else if (a > b) { | 401 } else if (a > b) { |
| 400 return 1; | 402 return 1; |
| 401 } else if (a == b) { | 403 } else if (a == b) { |
| 402 if (a == 0) { | 404 if (a == 0) { |
| 403 bool aIsNegative = a.isNegative(); | 405 bool aIsNegative = a.isNegative(); |
| 404 bool bIsNegative = b.isNegative(); | 406 bool bIsNegative = b.isNegative(); |
| 405 if (aIsNegative == bIsNegative) return 0; | 407 if (aIsNegative == bIsNegative) return 0; |
| (...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 886 } | 888 } |
| 887 | 889 |
| 888 builtin$toUpperCase$0(receiver) { | 890 builtin$toUpperCase$0(receiver) { |
| 889 checkNull(receiver); | 891 checkNull(receiver); |
| 890 if (receiver is !String) return UNINTERCEPTED(receiver.toUpperCase()); | 892 if (receiver is !String) return UNINTERCEPTED(receiver.toUpperCase()); |
| 891 | 893 |
| 892 throw new NotImplementedException(); | 894 throw new NotImplementedException(); |
| 893 } | 895 } |
| 894 | 896 |
| 895 builtin$trim$0(receiver) { | 897 builtin$trim$0(receiver) { |
| 896 checkNull(receiver); | 898 checkNull(receiver); |
|
ngeoffray
2012/02/21 09:39:43
No need for check null.
| |
| 897 if (receiver is !String) return UNINTERCEPTED(receiver.trim()); | 899 if (receiver is !String) return UNINTERCEPTED(receiver.trim()); |
| 898 | 900 |
| 899 throw new NotImplementedException(); | 901 return JS('String', @'$0.trim()', receiver); |
| 900 } | 902 } |
| 901 | 903 |
| 902 class MathNatives { | 904 class MathNatives { |
| 903 static int parseInt(String str) { | 905 static int parseInt(str) { |
| 904 throw 'MathNatives.parseInt is not implemented'; | 906 checkNull(str); |
|
ngeoffray
2012/02/21 09:39:43
No need for check null.
| |
| 907 if (str is !String) throw new IllegalArgumentException(str); | |
| 908 var trimmed = str.trim(); | |
| 909 if (!JS('bool', @'/^(0[xX])?[+-]?[0-9]+$/.test($0)', trimmed)) { | |
| 910 throw new BadNumberFormatException(str); | |
| 911 } | |
| 912 var ret = JS("num", @"parseInt($0, 10)", str); | |
| 913 if (ret.isNaN()) throw new BadNumberFormatException(str); | |
| 914 return ret; | |
| 905 } | 915 } |
| 906 | 916 |
| 907 static double parseDouble(String str) { | 917 static double parseDouble(String str) { |
| 908 throw 'MathNatives.parseDouble is not implemented'; | 918 checkNull(str); |
|
ngeoffray
2012/02/21 09:39:43
No need for check null.
| |
| 919 if (str is !String) throw new IllegalArgumentException(); | |
| 920 var ret = JS("num", @"parseFloat($0)", str); | |
| 921 if (ret.isNaN() && str != 'NaN') throw new BadNumberFormatException(str); | |
| 922 return ret; | |
| 909 } | 923 } |
| 910 | 924 |
| 911 static double sqrt(num value) | 925 static double sqrt(num value) |
| 912 => JS("double", @"Math.sqrt($0)", checkNum(value)); | 926 => JS("double", @"Math.sqrt($0)", checkNum(value)); |
| 913 | 927 |
| 914 static double sin(num value) | 928 static double sin(num value) |
| 915 => JS("double", @"Math.sin($0)", checkNum(value)); | 929 => JS("double", @"Math.sin($0)", checkNum(value)); |
| 916 | 930 |
| 917 static double cos(num value) | 931 static double cos(num value) |
| 918 => JS("double", @"Math.cos($0)", checkNum(value)); | 932 => JS("double", @"Math.cos($0)", checkNum(value)); |
| 919 | 933 |
| 920 static double tan(num value) | 934 static double tan(num value) |
| 921 => JS("double", @"Math.tan($0)", checkNum(value)); | 935 => JS("double", @"Math.tan($0)", checkNum(value)); |
| 922 | 936 |
| 923 static double acos(num value) | 937 static double acos(num value) |
| 924 => JS("double", @"Math.acos($0)", checkNum(value)); | 938 => JS("double", @"Math.acos($0)", checkNum(value)); |
| 925 | 939 |
| 926 static double asin(num value) | 940 static double asin(num value) |
| 927 => JS("double", @"Math.asin($0)", checkNum(value)); | 941 => JS("double", @"Math.asin($0)", checkNum(value)); |
| 928 | 942 |
| 929 static double atan(num value) | 943 static double atan(num value) |
| 930 => JS("double", @"Math.atan($0)", checkNum(value)); | 944 => JS("double", @"Math.atan($0)", checkNum(value)); |
| 931 | 945 |
| 932 static double atan2(num a, num b) | 946 static double atan2(num a, num b) |
| 933 => JS("double", @"Math.atan2($0)", checkNum(value)); | 947 => JS("double", @"Math.atan2($0, $1)", checkNum(a), checkNum(b)); |
| 934 | 948 |
| 935 static double exp(num value) | 949 static double exp(num value) |
| 936 => JS("double", @"Math.exp($0)", checkNum(value)); | 950 => JS("double", @"Math.exp($0)", checkNum(value)); |
| 937 | 951 |
| 938 static double log(num value) | 952 static double log(num value) |
| 939 => JS("double", @"Math.log($0)", checkNum(value)); | 953 => JS("double", @"Math.log($0)", checkNum(value)); |
| 940 | 954 |
| 941 static num pow(num value, num exponent) { | 955 static num pow(num value, num exponent) { |
| 942 checkNum(value); | 956 checkNum(value); |
| 943 checkNum(exponent); | 957 checkNum(exponent); |
| 944 return JS("num", @"Math.pow($0, $1)", value, exponent); | 958 return JS("num", @"Math.pow($0, $1)", value, exponent); |
| 945 } | 959 } |
| 946 | 960 |
| 947 static double random() => JS("double", "Math.random()"); | 961 static double random() => JS("double", @"Math.random()"); |
| 948 } | 962 } |
| 949 | 963 |
| 950 builtin$hashCode$0(receiver) { | 964 builtin$hashCode$0(receiver) { |
| 951 if (receiver is num) return receiver & 0x1FFFFFFF; | 965 if (receiver is num) return receiver & 0x1FFFFFFF; |
| 952 if (receiver is String) { | 966 if (receiver is String) { |
| 953 throw new NotImplementedException(); | 967 throw new NotImplementedException(); |
| 954 } | 968 } |
| 955 if (isJSArray(receiver)) { | 969 if (isJSArray(receiver)) { |
| 956 throw new NotImplementedException(); | 970 throw new NotImplementedException(); |
| 957 } | 971 } |
| 958 return UNINTERCEPTED(receiver.hashCode()); | 972 return UNINTERCEPTED(receiver.hashCode()); |
| 959 } | 973 } |
| OLD | NEW |