Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: tool/input_sdk/patch/math_patch.dart

Issue 1967773005: update dart:math (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tool/input_sdk/lib/math/rectangle.dart ('k') | tool/sdk_expected_errors.txt » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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, checkNum;
8 import 'dart:typed_data' show ByteData;
8 9
9 @patch 10 @patch
10 double sqrt(num x) 11 double sqrt(num x)
11 => JS('double', r'Math.sqrt(#)', checkNum(x)); 12 => JS('num', r'Math.sqrt(#)', checkNum(x));
12 13
13 @patch 14 @patch
14 double sin(num x) 15 double sin(num x)
15 => JS('double', r'Math.sin(#)', checkNum(x)); 16 => JS('num', r'Math.sin(#)', checkNum(x));
16 17
17 @patch 18 @patch
18 double cos(num x) 19 double cos(num x)
19 => JS('double', r'Math.cos(#)', checkNum(x)); 20 => JS('num', r'Math.cos(#)', checkNum(x));
20 21
21 @patch 22 @patch
22 double tan(num x) 23 double tan(num x)
23 => JS('double', r'Math.tan(#)', checkNum(x)); 24 => JS('num', r'Math.tan(#)', checkNum(x));
24 25
25 @patch 26 @patch
26 double acos(num x) 27 double acos(num x)
27 => JS('double', r'Math.acos(#)', checkNum(x)); 28 => JS('num', r'Math.acos(#)', checkNum(x));
28 29
29 @patch 30 @patch
30 double asin(num x) 31 double asin(num x)
31 => JS('double', r'Math.asin(#)', checkNum(x)); 32 => JS('num', r'Math.asin(#)', checkNum(x));
32 33
33 @patch 34 @patch
34 double atan(num x) 35 double atan(num x)
35 => JS('double', r'Math.atan(#)', checkNum(x)); 36 => JS('num', r'Math.atan(#)', checkNum(x));
36 37
37 @patch 38 @patch
38 double atan2(num a, num b) 39 double atan2(num a, num b)
39 => JS('double', r'Math.atan2(#, #)', checkNum(a), checkNum(b)); 40 => JS('num', r'Math.atan2(#, #)', checkNum(a), checkNum(b));
40 41
41 @patch 42 @patch
42 double exp(num x) 43 double exp(num x)
43 => JS('double', r'Math.exp(#)', checkNum(x)); 44 => JS('num', r'Math.exp(#)', checkNum(x));
44 45
45 @patch 46 @patch
46 double log(num x) 47 double log(num x)
47 => JS('double', r'Math.log(#)', checkNum(x)); 48 => JS('num', r'Math.log(#)', checkNum(x));
48 49
49 @patch 50 @patch
50 num pow(num x, num exponent) { 51 num pow(num x, num exponent) {
51 checkNum(x); 52 checkNum(x);
52 checkNum(exponent); 53 checkNum(exponent);
53 return JS('num', r'Math.pow(#, #)', x, exponent); 54 return JS('num', r'Math.pow(#, #)', x, exponent);
54 } 55 }
55 56
56 const int _POW2_32 = 0x100000000; 57 const int _POW2_32 = 0x100000000;
57 58
58 @patch 59 @patch
59 class Random { 60 class Random {
61 static final _secureRandom = new _JSSecureRandom();
62
60 @patch 63 @patch
61 factory Random([int seed]) => 64 factory Random([int seed]) =>
62 (seed == null) ? const _JSRandom() : new _Random(seed); 65 (seed == null) ? const _JSRandom() : new _Random(seed);
66
67 @patch
68 factory Random.secure() => _secureRandom;
63 } 69 }
64 70
65 class _JSRandom implements Random { 71 class _JSRandom implements Random {
66 // The Dart2JS implementation of Random doesn't use a seed. 72 // The Dart2JS implementation of Random doesn't use a seed.
67 const _JSRandom(); 73 const _JSRandom();
68 74
69 int nextInt(int max) { 75 int nextInt(int max) {
70 if (max <= 0 || max > _POW2_32) { 76 if (max <= 0 || max > _POW2_32) {
71 throw new RangeError("max must be in range 0 < max ≤ 2^32, was $max"); 77 throw new RangeError("max must be in range 0 < max ≤ 2^32, was $max");
72 } 78 }
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 _nextState(); 235 _nextState();
230 int bits27 = _lo & ((1 << 27) - 1); 236 int bits27 = _lo & ((1 << 27) - 1);
231 return (bits26 * _POW2_27_D + bits27) / _POW2_53_D; 237 return (bits26 * _POW2_27_D + bits27) / _POW2_53_D;
232 } 238 }
233 239
234 bool nextBool() { 240 bool nextBool() {
235 _nextState(); 241 _nextState();
236 return (_lo & 1) == 0; 242 return (_lo & 1) == 0;
237 } 243 }
238 } 244 }
245
246
247 class _JSSecureRandom implements Random {
248 // Reused buffer with room enough for a double.
249 final _buffer = new ByteData(8);
250
251 _JSSecureRandom() {
252 var crypto = JS("", "self.crypto");
253 if (crypto != null) {
254 var getRandomValues = JS("", "#.getRandomValues", crypto);
255 if (getRandomValues != null) {
256 return;
257 }
258 }
259 throw new UnsupportedError(
260 "No source of cryptographically secure random numbers available.");
261 }
262
263 /// Fill _buffer from [start] to `start + length` with random bytes.
264 void _getRandomBytes(int start, int length) {
265 JS("void", "crypto.getRandomValues(#)",
266 _buffer.buffer.asUint8List(start, length));
267 }
268
269 bool nextBool() {
270 _getRandomBytes(0, 1);
271 return _buffer.getUint8(0).isOdd;
272 }
273
274 double nextDouble() {
275 _getRandomBytes(1, 7);
276 // Set top bits 12 of double to 0x3FF which is the exponent for numbers
277 // between 1.0 and 2.0.
278 _buffer.setUint8(0, 0x3F);
279 int highByte = _buffer.getUint8(1);
280 _buffer.setUint8(1, highByte | 0xF0);
281
282 // Buffer now contains double in the range [1.0-2.0)
283 // with 52 bits of entropy (not 53).
284 // To get 53 bits, we extract the 53rd bit from higthByte before
285 // overwriting it, and add that as a least significant bit.
286 // The getFloat64 method is big-endian as default.
287 double result = _buffer.getFloat64(0) - 1.0;
288 if (highByte & 0x10 != 0) {
289 result += 1.1102230246251565e-16; // pow(2,-53).
290 }
291 return result;
292 }
293
294 int nextInt(int max) {
295 if (max <= 0 || max > _POW2_32) {
296 throw new RangeError("max must be in range 0 < max ≤ 2^32, was $max");
297 }
298 int byteCount = 1;
299 if (max > 0xFF) {
300 byteCount++;
301 if (max > 0xFFFF) {
302 byteCount++;
303 if (max > 0xFFFFFF) {
304 byteCount++;
305 }
306 }
307 }
308 _buffer.setUint32(0, 0);
309 int start = 4 - byteCount;
310 int randomLimit = pow(256, byteCount);
311 while (true) {
312 _getRandomBytes(start, byteCount);
313 // The getUint32 method is big-endian as default.
314 int random = _buffer.getUint32(0);
315 if (max & (max - 1) == 0) {
316 // Max is power of 2.
317 return random & (max - 1);
318 }
319 int result = random.remainder(max);
320 // Ensure results have equal probability by rejecting values in the
321 // last range of k*max .. 256**byteCount.
322 // TODO: Consider picking a higher byte count if the last range is a
323 // significant portion of the entire range - a 50% chance of having
324 // to use two more bytes is no worse than always using one more.
325 if (random - result + max < randomLimit) {
326 return result;
327 }
328 }
329 }
330 }
OLDNEW
« no previous file with comments | « tool/input_sdk/lib/math/rectangle.dart ('k') | tool/sdk_expected_errors.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698