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