| 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 import 'dart:_foreign_helper' show JS; | 6 import 'dart:_foreign_helper' show JS; |
| 7 import 'dart:_js_helper' show patch, checkNum; | 7 import 'dart:_js_helper' show patch, nullCheck, notNull; |
| 8 import 'dart:typed_data' show ByteData; | 8 import 'dart:typed_data' show ByteData; |
| 9 | 9 |
| 10 @patch | 10 @patch |
| 11 T min<T extends num>(T a, T b) => | 11 @notNull |
| 12 JS('num', r'Math.min(#, #)', checkNum(a), checkNum(b)) as T; | 12 T min<T extends num>(@nullCheck T a, @nullCheck T b) => |
| 13 JS('-dynamic', r'Math.min(#, #)', a, b); |
| 13 | 14 |
| 14 @patch | 15 @patch |
| 15 T max<T extends num>(T a, T b) => | 16 @notNull |
| 16 JS('num', r'Math.max(#, #)', checkNum(a), checkNum(b)) as T; | 17 T max<T extends num>(@nullCheck T a, @nullCheck T b) => |
| 18 JS('-dynamic', r'Math.max(#, #)', a, b); |
| 17 | 19 |
| 18 @patch | 20 @patch |
| 19 double sqrt(num x) => JS('num', r'Math.sqrt(#)', checkNum(x)); | 21 @notNull |
| 22 double sqrt(@nullCheck num x) => JS('num', r'Math.sqrt(#)', x); |
| 20 | 23 |
| 21 @patch | 24 @patch |
| 22 double sin(num radians) => JS('num', r'Math.sin(#)', checkNum(radians)); | 25 @notNull |
| 26 double sin(@nullCheck num radians) => JS('num', r'Math.sin(#)', radians); |
| 23 | 27 |
| 24 @patch | 28 @patch |
| 25 double cos(num radians) => JS('num', r'Math.cos(#)', checkNum(radians)); | 29 @notNull |
| 30 double cos(@nullCheck num radians) => JS('num', r'Math.cos(#)', radians); |
| 26 | 31 |
| 27 @patch | 32 @patch |
| 28 double tan(num radians) => JS('num', r'Math.tan(#)', checkNum(radians)); | 33 @notNull |
| 34 double tan(@nullCheck num radians) => JS('num', r'Math.tan(#)', radians); |
| 29 | 35 |
| 30 @patch | 36 @patch |
| 31 double acos(num x) => JS('num', r'Math.acos(#)', checkNum(x)); | 37 @notNull |
| 38 double acos(@nullCheck num x) => JS('num', r'Math.acos(#)', x); |
| 32 | 39 |
| 33 @patch | 40 @patch |
| 34 double asin(num x) => JS('num', r'Math.asin(#)', checkNum(x)); | 41 @notNull |
| 42 double asin(@nullCheck num x) => JS('num', r'Math.asin(#)', x); |
| 35 | 43 |
| 36 @patch | 44 @patch |
| 37 double atan(num x) => JS('num', r'Math.atan(#)', checkNum(x)); | 45 @notNull |
| 46 double atan(@nullCheck num x) => JS('num', r'Math.atan(#)', x); |
| 38 | 47 |
| 39 @patch | 48 @patch |
| 40 double atan2(num a, num b) => | 49 @notNull |
| 41 JS('num', r'Math.atan2(#, #)', checkNum(a), checkNum(b)); | 50 double atan2(@nullCheck num a, @nullCheck num b) => |
| 51 JS('num', r'Math.atan2(#, #)', a, b); |
| 42 | 52 |
| 43 @patch | 53 @patch |
| 44 double exp(num x) => JS('num', r'Math.exp(#)', checkNum(x)); | 54 @notNull |
| 55 double exp(@nullCheck num x) => JS('num', r'Math.exp(#)', x); |
| 45 | 56 |
| 46 @patch | 57 @patch |
| 47 double log(num x) => JS('num', r'Math.log(#)', checkNum(x)); | 58 @notNull |
| 59 double log(@nullCheck num x) => JS('num', r'Math.log(#)', x); |
| 48 | 60 |
| 49 @patch | 61 @patch |
| 50 num pow(num x, num exponent) { | 62 @notNull |
| 51 checkNum(x); | 63 num pow(@nullCheck num x, @nullCheck num exponent) => |
| 52 checkNum(exponent); | 64 JS('num', r'Math.pow(#, #)', x, exponent); |
| 53 return JS('num', r'Math.pow(#, #)', x, exponent); | |
| 54 } | |
| 55 | 65 |
| 56 const int _POW2_32 = 0x100000000; | 66 const int _POW2_32 = 0x100000000; |
| 57 | 67 |
| 58 @patch | 68 @patch |
| 59 class Random { | 69 class Random { |
| 60 static final _secureRandom = new _JSSecureRandom(); | 70 static final _secureRandom = new _JSSecureRandom(); |
| 61 | 71 |
| 62 @patch | 72 @patch |
| 63 factory Random([int seed]) => | 73 factory Random([int seed]) => |
| 64 (seed == null) ? const _JSRandom() : new _Random(seed); | 74 (seed == null) ? const _JSRandom() : new _Random(seed); |
| 65 | 75 |
| 66 @patch | 76 @patch |
| 67 factory Random.secure() => _secureRandom; | 77 factory Random.secure() => _secureRandom; |
| 68 } | 78 } |
| 69 | 79 |
| 70 class _JSRandom implements Random { | 80 class _JSRandom implements Random { |
| 71 // The Dart2JS implementation of Random doesn't use a seed. | 81 // The Dart2JS implementation of Random doesn't use a seed. |
| 72 const _JSRandom(); | 82 const _JSRandom(); |
| 73 | 83 |
| 84 @notNull |
| 74 int nextInt(int max) { | 85 int nextInt(int max) { |
| 75 if (max <= 0 || max > _POW2_32) { | 86 if (max <= 0 || max > _POW2_32) { |
| 76 throw new RangeError("max must be in range 0 < max ≤ 2^32, was $max"); | 87 throw new RangeError("max must be in range 0 < max ≤ 2^32, was $max"); |
| 77 } | 88 } |
| 78 return JS("int", "(Math.random() * #) >>> 0", max); | 89 return JS("int", "(Math.random() * #) >>> 0", max); |
| 79 } | 90 } |
| 80 | 91 |
| 81 /** | 92 /** |
| 82 * Generates a positive random floating point value uniformly distributed on | 93 * Generates a positive random floating point value uniformly distributed on |
| 83 * the range from 0.0, inclusive, to 1.0, exclusive. | 94 * the range from 0.0, inclusive, to 1.0, exclusive. |
| 84 */ | 95 */ |
| 96 @notNull |
| 85 double nextDouble() => JS("double", "Math.random()"); | 97 double nextDouble() => JS("double", "Math.random()"); |
| 86 | 98 |
| 87 /** | 99 /** |
| 88 * Generates a random boolean value. | 100 * Generates a random boolean value. |
| 89 */ | 101 */ |
| 102 @notNull |
| 90 bool nextBool() => JS("bool", "Math.random() < 0.5"); | 103 bool nextBool() => JS("bool", "Math.random() < 0.5"); |
| 91 } | 104 } |
| 92 | 105 |
| 93 class _Random implements Random { | 106 class _Random implements Random { |
| 94 // Constants used by the algorithm or masking. | 107 // Constants used by the algorithm or masking. |
| 95 static const double _POW2_53_D = 1.0 * (0x20000000000000); | 108 static const double _POW2_53_D = 1.0 * (0x20000000000000); |
| 96 static const double _POW2_27_D = 1.0 * (1 << 27); | 109 static const double _POW2_27_D = 1.0 * (1 << 27); |
| 97 static const int _MASK32 = 0xFFFFFFFF; | 110 static const int _MASK32 = 0xFFFFFFFF; |
| 98 | 111 |
| 99 // State comprised of two unsigned 32 bit integers. | 112 // State comprised of two unsigned 32 bit integers. |
| 113 @notNull |
| 100 int _lo = 0; | 114 int _lo = 0; |
| 115 @notNull |
| 101 int _hi = 0; | 116 int _hi = 0; |
| 102 | 117 |
| 103 // Implements: | 118 // Implements: |
| 104 // uint64_t hash = 0; | 119 // uint64_t hash = 0; |
| 105 // do { | 120 // do { |
| 106 // hash = hash * 1037 ^ mix64((uint64_t)seed); | 121 // hash = hash * 1037 ^ mix64((uint64_t)seed); |
| 107 // seed >>= 64; | 122 // seed >>= 64; |
| 108 // } while (seed != 0 && seed != -1); // Limits for pos/neg seed. | 123 // } while (seed != 0 && seed != -1); // Limits for pos/neg seed. |
| 109 // if (hash == 0) { | 124 // if (hash == 0) { |
| 110 // hash = 0x5A17; | 125 // hash = 0x5A17; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 int tmpLoHi = tmpLo - tmpLoLo; | 215 int tmpLoHi = tmpLo - tmpLoLo; |
| 201 | 216 |
| 202 int newLo = tmpLoLo + tmpHiLo + _hi; | 217 int newLo = tmpLoLo + tmpHiLo + _hi; |
| 203 _lo = newLo & _MASK32; | 218 _lo = newLo & _MASK32; |
| 204 int newLoHi = newLo - _lo; | 219 int newLoHi = newLo - _lo; |
| 205 _hi = ((tmpLoHi + tmpHiHi + newLoHi) ~/ _POW2_32) & _MASK32; | 220 _hi = ((tmpLoHi + tmpHiHi + newLoHi) ~/ _POW2_32) & _MASK32; |
| 206 assert(_lo < _POW2_32); | 221 assert(_lo < _POW2_32); |
| 207 assert(_hi < _POW2_32); | 222 assert(_hi < _POW2_32); |
| 208 } | 223 } |
| 209 | 224 |
| 210 int nextInt(int max) { | 225 @notNull |
| 226 int nextInt(@nullCheck int max) { |
| 211 if (max <= 0 || max > _POW2_32) { | 227 if (max <= 0 || max > _POW2_32) { |
| 212 throw new RangeError("max must be in range 0 < max ≤ 2^32, was $max"); | 228 throw new RangeError("max must be in range 0 < max ≤ 2^32, was $max"); |
| 213 } | 229 } |
| 214 if ((max & (max - 1)) == 0) { | 230 if ((max & (max - 1)) == 0) { |
| 215 // Fast case for powers of two. | 231 // Fast case for powers of two. |
| 216 _nextState(); | 232 _nextState(); |
| 217 return _lo & (max - 1); | 233 return _lo & (max - 1); |
| 218 } | 234 } |
| 219 | 235 |
| 220 int rnd32; | 236 int rnd32; |
| 221 int result; | 237 int result; |
| 222 do { | 238 do { |
| 223 _nextState(); | 239 _nextState(); |
| 224 rnd32 = _lo; | 240 rnd32 = _lo; |
| 225 result = rnd32.remainder(max); // % max; | 241 result = rnd32.remainder(max); // % max; |
| 226 } while ((rnd32 - result + max) >= _POW2_32); | 242 } while ((rnd32 - result + max) >= _POW2_32); |
| 227 return result; | 243 return result; |
| 228 } | 244 } |
| 229 | 245 |
| 246 @notNull |
| 230 double nextDouble() { | 247 double nextDouble() { |
| 231 _nextState(); | 248 _nextState(); |
| 232 int bits26 = _lo & ((1 << 26) - 1); | 249 int bits26 = _lo & ((1 << 26) - 1); |
| 233 _nextState(); | 250 _nextState(); |
| 234 int bits27 = _lo & ((1 << 27) - 1); | 251 int bits27 = _lo & ((1 << 27) - 1); |
| 235 return (bits26 * _POW2_27_D + bits27) / _POW2_53_D; | 252 return (bits26 * _POW2_27_D + bits27) / _POW2_53_D; |
| 236 } | 253 } |
| 237 | 254 |
| 255 @notNull |
| 238 bool nextBool() { | 256 bool nextBool() { |
| 239 _nextState(); | 257 _nextState(); |
| 240 return (_lo & 1) == 0; | 258 return (_lo & 1) == 0; |
| 241 } | 259 } |
| 242 } | 260 } |
| 243 | 261 |
| 244 class _JSSecureRandom implements Random { | 262 class _JSSecureRandom implements Random { |
| 245 // Reused buffer with room enough for a double. | 263 // Reused buffer with room enough for a double. |
| 246 final _buffer = new ByteData(8); | 264 final _buffer = new ByteData(8); |
| 247 | 265 |
| 248 _JSSecureRandom() { | 266 _JSSecureRandom() { |
| 249 var crypto = JS("", "self.crypto"); | 267 var crypto = JS("", "self.crypto"); |
| 250 if (crypto != null) { | 268 if (crypto != null) { |
| 251 var getRandomValues = JS("", "#.getRandomValues", crypto); | 269 var getRandomValues = JS("", "#.getRandomValues", crypto); |
| 252 if (getRandomValues != null) { | 270 if (getRandomValues != null) { |
| 253 return; | 271 return; |
| 254 } | 272 } |
| 255 } | 273 } |
| 256 throw new UnsupportedError( | 274 throw new UnsupportedError( |
| 257 "No source of cryptographically secure random numbers available."); | 275 "No source of cryptographically secure random numbers available."); |
| 258 } | 276 } |
| 259 | 277 |
| 260 /// Fill _buffer from [start] to `start + length` with random bytes. | 278 /// Fill _buffer from [start] to `start + length` with random bytes. |
| 261 void _getRandomBytes(int start, int length) { | 279 void _getRandomBytes(int start, int length) { |
| 262 JS("void", "crypto.getRandomValues(#)", | 280 JS("void", "crypto.getRandomValues(#)", |
| 263 _buffer.buffer.asUint8List(start, length)); | 281 _buffer.buffer.asUint8List(start, length)); |
| 264 } | 282 } |
| 265 | 283 |
| 284 @notNull |
| 266 bool nextBool() { | 285 bool nextBool() { |
| 267 _getRandomBytes(0, 1); | 286 _getRandomBytes(0, 1); |
| 268 return _buffer.getUint8(0).isOdd; | 287 return _buffer.getUint8(0).isOdd; |
| 269 } | 288 } |
| 270 | 289 |
| 290 @notNull |
| 271 double nextDouble() { | 291 double nextDouble() { |
| 272 _getRandomBytes(1, 7); | 292 _getRandomBytes(1, 7); |
| 273 // Set top bits 12 of double to 0x3FF which is the exponent for numbers | 293 // Set top bits 12 of double to 0x3FF which is the exponent for numbers |
| 274 // between 1.0 and 2.0. | 294 // between 1.0 and 2.0. |
| 275 _buffer.setUint8(0, 0x3F); | 295 _buffer.setUint8(0, 0x3F); |
| 276 int highByte = _buffer.getUint8(1); | 296 int highByte = _buffer.getUint8(1); |
| 277 _buffer.setUint8(1, highByte | 0xF0); | 297 _buffer.setUint8(1, highByte | 0xF0); |
| 278 | 298 |
| 279 // Buffer now contains double in the range [1.0-2.0) | 299 // Buffer now contains double in the range [1.0-2.0) |
| 280 // with 52 bits of entropy (not 53). | 300 // with 52 bits of entropy (not 53). |
| 281 // To get 53 bits, we extract the 53rd bit from higthByte before | 301 // To get 53 bits, we extract the 53rd bit from higthByte before |
| 282 // overwriting it, and add that as a least significant bit. | 302 // overwriting it, and add that as a least significant bit. |
| 283 // The getFloat64 method is big-endian as default. | 303 // The getFloat64 method is big-endian as default. |
| 284 double result = _buffer.getFloat64(0) - 1.0; | 304 double result = _buffer.getFloat64(0) - 1.0; |
| 285 if (highByte & 0x10 != 0) { | 305 if (highByte & 0x10 != 0) { |
| 286 result += 1.1102230246251565e-16; // pow(2,-53). | 306 result += 1.1102230246251565e-16; // pow(2,-53). |
| 287 } | 307 } |
| 288 return result; | 308 return result; |
| 289 } | 309 } |
| 290 | 310 |
| 291 int nextInt(int max) { | 311 @notNull |
| 312 int nextInt(@nullCheck int max) { |
| 292 if (max <= 0 || max > _POW2_32) { | 313 if (max <= 0 || max > _POW2_32) { |
| 293 throw new RangeError("max must be in range 0 < max ≤ 2^32, was $max"); | 314 throw new RangeError("max must be in range 0 < max ≤ 2^32, was $max"); |
| 294 } | 315 } |
| 295 int byteCount = 1; | 316 int byteCount = 1; |
| 296 if (max > 0xFF) { | 317 if (max > 0xFF) { |
| 297 byteCount++; | 318 byteCount++; |
| 298 if (max > 0xFFFF) { | 319 if (max > 0xFFFF) { |
| 299 byteCount++; | 320 byteCount++; |
| 300 if (max > 0xFFFFFF) { | 321 if (max > 0xFFFFFF) { |
| 301 byteCount++; | 322 byteCount++; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 318 // last range of k*max .. 256**byteCount. | 339 // last range of k*max .. 256**byteCount. |
| 319 // TODO: Consider picking a higher byte count if the last range is a | 340 // TODO: Consider picking a higher byte count if the last range is a |
| 320 // significant portion of the entire range - a 50% chance of having | 341 // significant portion of the entire range - a 50% chance of having |
| 321 // to use two more bytes is no worse than always using one more. | 342 // to use two more bytes is no worse than always using one more. |
| 322 if (random - result + max < randomLimit) { | 343 if (random - result + max < randomLimit) { |
| 323 return result; | 344 return result; |
| 324 } | 345 } |
| 325 } | 346 } |
| 326 } | 347 } |
| 327 } | 348 } |
| OLD | NEW |