| 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 // Patch file for dart:math library. | 5 // Patch file for dart:math library. |
| 6 | 6 |
| 7 // Imports checkNum etc. used below. | 7 // Imports checkNum etc. used below. |
| 8 #import("js_helper.dart"); | 8 #import("js_helper.dart"); |
| 9 | 9 |
| 10 // TODO(lrn): Consider not using the JS function directly, but instead calling | |
| 11 // helper functions in the "js_helper" library. Then we can stop enabling JS | |
| 12 // in all patched library files. | |
| 13 | |
| 14 patch int parseInt(str) { | |
| 15 checkString(str); | |
| 16 if (!JS('bool', | |
| 17 @'/^\s*[+-]?(?:0[xX][abcdefABCDEF0-9]+|\d+)\s*$/.test(#)', | |
| 18 str)) { | |
| 19 throw new FormatException(str); | |
| 20 } | |
| 21 var trimmed = str.trim(); | |
| 22 var base = 10; | |
| 23 if ((trimmed.length > 2 && (trimmed[1] == 'x' || trimmed[1] == 'X')) || | |
| 24 (trimmed.length > 3 && (trimmed[2] == 'x' || trimmed[2] == 'X'))) { | |
| 25 base = 16; | |
| 26 } | |
| 27 var ret = JS('num', @'parseInt(#, #)', trimmed, base); | |
| 28 if (ret.isNaN()) throw new FormatException(str); | |
| 29 return ret; | |
| 30 } | |
| 31 | |
| 32 patch double parseDouble(String str) { | |
| 33 checkString(str); | |
| 34 var ret = JS('num', @'parseFloat(#)', str); | |
| 35 if (ret == 0 && (str.startsWith("0x") || str.startsWith("0X"))) { | |
| 36 // TODO(ahe): This is unspecified, but tested by co19. | |
| 37 ret = JS('num', @'parseInt(#)', str); | |
| 38 } | |
| 39 if (ret.isNaN() && str != 'NaN' && str != '-NaN') { | |
| 40 throw new FormatException(str); | |
| 41 } | |
| 42 return ret; | |
| 43 } | |
| 44 | |
| 45 patch double sqrt(num value) | 10 patch double sqrt(num value) |
| 46 => JS('double', @'Math.sqrt(#)', checkNum(value)); | 11 => JS('double', @'Math.sqrt(#)', checkNum(value)); |
| 47 | 12 |
| 48 patch double sin(num value) | 13 patch double sin(num value) |
| 49 => JS('double', @'Math.sin(#)', checkNum(value)); | 14 => JS('double', @'Math.sin(#)', checkNum(value)); |
| 50 | 15 |
| 51 patch double cos(num value) | 16 patch double cos(num value) |
| 52 => JS('double', @'Math.cos(#)', checkNum(value)); | 17 => JS('double', @'Math.cos(#)', checkNum(value)); |
| 53 | 18 |
| 54 patch double tan(num value) | 19 patch double tan(num value) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 * Generates a positive random floating point value uniformly distributed on | 61 * Generates a positive random floating point value uniformly distributed on |
| 97 * the range from 0.0, inclusive, to 1.0, exclusive. | 62 * the range from 0.0, inclusive, to 1.0, exclusive. |
| 98 */ | 63 */ |
| 99 double nextDouble() => JS("double", "Math.random()"); | 64 double nextDouble() => JS("double", "Math.random()"); |
| 100 | 65 |
| 101 /** | 66 /** |
| 102 * Generates a random boolean value. | 67 * Generates a random boolean value. |
| 103 */ | 68 */ |
| 104 bool nextBool() => JS("bool", "Math.random() < 0.5"); | 69 bool nextBool() => JS("bool", "Math.random() < 0.5"); |
| 105 } | 70 } |
| OLD | NEW |