Skip to content

Commit 6ca96ac

Browse files
lrhnleafpetersen
authored andcommitted
Migrate dart:math to NNBD.
Change-Id: I8da8707957e9b2827e8d8ee0152c7260f7ddecd2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/120060 Reviewed-by: Erik Ernst <eernst@google.com> Reviewed-by: Leaf Petersen <leafp@google.com>
1 parent f11dea9 commit 6ca96ac

File tree

6 files changed

+56
-65
lines changed

6 files changed

+56
-65
lines changed

sdk_nnbd/lib/_internal/js_dev_runtime/patch/math_patch.dart

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.5
6-
75
// Patch file for dart:math library.
86
import 'dart:_foreign_helper' show JS;
97
import 'dart:_js_helper' show patch, nullCheck, notNull;
@@ -12,53 +10,52 @@ import 'dart:typed_data' show ByteData;
1210
@patch
1311
@notNull
1412
T min<T extends num>(@nullCheck T a, @nullCheck T b) =>
15-
JS('-dynamic', r'Math.min(#, #)', a, b);
13+
JS<T>('-dynamic', r'Math.min(#, #)', a, b);
1614

1715
@patch
1816
@notNull
1917
T max<T extends num>(@nullCheck T a, @nullCheck T b) =>
20-
JS('-dynamic', r'Math.max(#, #)', a, b);
18+
JS<T>('-dynamic', r'Math.max(#, #)', a, b);
2119

2220
@patch
2321
@notNull
24-
double sqrt(@nullCheck num x) => JS<num>('!', r'Math.sqrt(#)', x);
22+
double sqrt(@nullCheck num x) => JS<double>('!', r'Math.sqrt(#)', x);
2523

2624
@patch
2725
@notNull
28-
double sin(@nullCheck num radians) => JS<num>('!', r'Math.sin(#)', radians);
26+
double sin(@nullCheck num radians) => JS<double>('!', r'Math.sin(#)', radians);
2927

3028
@patch
3129
@notNull
32-
double cos(@nullCheck num radians) => JS<num>('!', r'Math.cos(#)', radians);
30+
double cos(@nullCheck num radians) => JS<double>('!', r'Math.cos(#)', radians);
3331

3432
@patch
3533
@notNull
36-
double tan(@nullCheck num radians) => JS<num>('!', r'Math.tan(#)', radians);
34+
double tan(@nullCheck num radians) => JS<double>('!', r'Math.tan(#)', radians);
3735

3836
@patch
3937
@notNull
40-
double acos(@nullCheck num x) => JS<num>('!', r'Math.acos(#)', x);
38+
double acos(@nullCheck num x) => JS<double>('!', r'Math.acos(#)', x);
4139

4240
@patch
4341
@notNull
44-
double asin(@nullCheck num x) => JS<num>('!', r'Math.asin(#)', x);
42+
double asin(@nullCheck num x) => JS<double>('!', r'Math.asin(#)', x);
4543

4644
@patch
4745
@notNull
48-
double atan(@nullCheck num x) => JS<num>('!', r'Math.atan(#)', x);
46+
double atan(@nullCheck num x) => JS<double>('!', r'Math.atan(#)', x);
4947

5048
@patch
5149
@notNull
5250
double atan2(@nullCheck num a, @nullCheck num b) =>
53-
JS<num>('!', r'Math.atan2(#, #)', a, b);
51+
JS<double>('!', r'Math.atan2(#, #)', a, b);
5452

5553
@patch
56-
@notNull
57-
double exp(@nullCheck num x) => JS<num>('!', r'Math.exp(#)', x);
54+
double exp(num x) => JS<double>('!', r'Math.exp(#)', x);
5855

5956
@patch
6057
@notNull
61-
double log(@nullCheck num x) => JS<num>('!', r'Math.log(#)', x);
58+
double log(@nullCheck num x) => JS<double>('!', r'Math.log(#)', x);
6259

6360
@patch
6461
@notNull
@@ -69,10 +66,10 @@ const int _POW2_32 = 0x100000000;
6966

7067
@patch
7168
class Random {
72-
static Random _secureRandom;
69+
static Random? _secureRandom;
7370

7471
@patch
75-
factory Random([int seed]) =>
72+
factory Random([int? seed]) =>
7673
(seed == null) ? const _JSRandom() : _Random(seed);
7774

7875
@patch
@@ -88,21 +85,21 @@ class _JSRandom implements Random {
8885
if (max <= 0 || max > _POW2_32) {
8986
throw RangeError("max must be in range 0 < max ≤ 2^32, was $max");
9087
}
91-
return JS("int", "(Math.random() * #) >>> 0", max);
88+
return JS<int>("int", "(Math.random() * #) >>> 0", max);
9289
}
9390

9491
/**
9592
* Generates a positive random floating point value uniformly distributed on
9693
* the range from 0.0, inclusive, to 1.0, exclusive.
9794
*/
9895
@notNull
99-
double nextDouble() => JS("double", "Math.random()");
96+
double nextDouble() => JS<double>("double", "Math.random()");
10097

10198
/**
10299
* Generates a random boolean value.
103100
*/
104101
@notNull
105-
bool nextBool() => JS("bool", "Math.random() < 0.5");
102+
bool nextBool() => JS<bool>("bool", "Math.random() < 0.5");
106103
}
107104

108105
class _Random implements Random {

sdk_nnbd/lib/math/jenkins_smi_hash.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4-
// @dart = 2.5
54

65
part of dart.math;
76

@@ -34,8 +33,8 @@ class _JenkinsSmiHash {
3433
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
3534
}
3635

37-
static int hash2(a, b) => finish(combine(combine(0, a), b));
36+
static int hash2(int a, int b) => finish(combine(combine(0, a), b));
3837

39-
static int hash4(a, b, c, d) =>
38+
static int hash4(int a, int b, int c, int d) =>
4039
finish(combine(combine(combine(combine(0, a), b), c), d));
4140
}

sdk_nnbd/lib/math/math.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.5
6-
75
/// Mathematical constants and functions, plus a random number generator.
86
///
97
/// To use this library in your code:

sdk_nnbd/lib/math/point.dart

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4-
// @dart = 2.5
54

65
part of dart.math;
76

@@ -16,14 +15,12 @@ class Point<T extends num> {
1615

1716
String toString() => 'Point($x, $y)';
1817

19-
/// A `Point` is only equal to another `Point` with the same coordinates.
18+
/// Whether [other] is a point with the same coordinates as this point.
2019
///
21-
/// This point is equal to `other` if, and only if,
22-
/// `other` is a `Point` with
23-
/// [x] equal to `other.x` and [y] equal to `other.y`.
24-
bool operator ==(dynamic other) =>
25-
// Cannot change parameter type to `Object` in case some class
26-
// inherits the type and uses their argument dynamically.
20+
/// Returns `true` if [other] is a [Point] with [x] and [y]
21+
/// coordinates equal to the corresponding coordiantes of this point,
22+
/// and `false` otherwise.
23+
bool operator ==(Object other) =>
2724
other is Point && x == other.x && y == other.y;
2825

2926
int get hashCode => _JenkinsSmiHash.hash2(x.hashCode, y.hashCode);
@@ -32,25 +29,25 @@ class Point<T extends num> {
3229
///
3330
/// Returns the resulting "vector" as a Point.
3431
Point<T> operator +(Point<T> other) {
35-
return Point<T>(x + other.x, y + other.y);
32+
return Point<T>((x + other.x) as T, (y + other.y) as T);
3633
}
3734

3835
/// Subtract [other] from `this`, as if both points were vectors.
3936
///
4037
/// Returns the resulting "vector" as a Point.
4138
Point<T> operator -(Point<T> other) {
42-
return Point<T>(x - other.x, y - other.y);
39+
return Point<T>((x - other.x) as T, (y - other.y) as T);
4340
}
4441

4542
/// Scale this point by [factor] as if it were a vector.
4643
///
4744
/// *Important* *Note*: This function accepts a `num` as its argument only so
48-
/// that you can scale Point<double> objects by an `int` factor. Because the
49-
/// star operator always returns the same type of Point that originally called
50-
/// it, passing in a double [factor] on a `Point<int>` _causes_ _a_
51-
/// _runtime_ _error_ in checked mode.
45+
/// that you can scale `Point<double>` objects by an `int` factor. Because the
46+
/// `*` operator always returns the same type of `Point` as it is called on,
47+
/// passing in a double [factor] on a `Point<int>` _causes_ _a_
48+
/// _runtime_ _error_.
5249
Point<T> operator *(num /*T|int*/ factor) {
53-
return Point<T>((x * factor), (y * factor));
50+
return Point<T>((x * factor) as T, (y * factor) as T);
5451
}
5552

5653
/// Get the straight line (Euclidean) distance between the origin (0, 0) and
@@ -71,6 +68,6 @@ class Point<T extends num> {
7168
T squaredDistanceTo(Point<T> other) {
7269
var dx = x - other.x;
7370
var dy = y - other.y;
74-
return dx * dx + dy * dy;
71+
return (dx * dx + dy * dy) as T;
7572
}
7673
}

sdk_nnbd/lib/math/random.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.5
6-
75
part of dart.math;
86

97
/// A generator of random bool, int, or double values.
@@ -18,7 +16,7 @@ abstract class Random {
1816
/// The optional parameter [seed] is used to initialize the
1917
/// internal state of the generator. The implementation of the
2018
/// random stream can change between releases of the library.
21-
external factory Random([int seed]);
19+
external factory Random([int? seed]);
2220

2321
/// Creates a cryptographically secure random number generator.
2422
///

sdk_nnbd/lib/math/rectangle.dart

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4-
// @dart = 2.5
54

65
part of dart.math;
76

@@ -33,18 +32,16 @@ abstract class _RectangleBase<T extends num> {
3332
T get height;
3433

3534
/// The x-coordinate of the right edge.
36-
T get right => left + width;
35+
T get right => (left + width) as T;
3736

3837
/// The y-coordinate of the bottom edge.
39-
T get bottom => top + height;
38+
T get bottom => (top + height) as T;
4039

4140
String toString() {
4241
return 'Rectangle ($left, $top) $width x $height';
4342
}
4443

45-
bool operator ==(dynamic other) =>
46-
// Can't change argument type to `Object` since subclasses inherit it
47-
// and uses their argument dynamically.
44+
bool operator ==(Object other) =>
4845
other is Rectangle &&
4946
left == other.left &&
5047
top == other.top &&
@@ -61,7 +58,7 @@ abstract class _RectangleBase<T extends num> {
6158
///
6259
/// Returns the intersection of this and `other`, or `null` if they don't
6360
/// intersect.
64-
Rectangle<T> intersection(Rectangle<T> other) {
61+
Rectangle<T>? intersection(Rectangle<T> other) {
6562
var x0 = max(left, other.left);
6663
var x1 = min(left + width, other.left + other.width);
6764

@@ -70,7 +67,7 @@ abstract class _RectangleBase<T extends num> {
7067
var y1 = min(top + height, other.top + other.height);
7168

7269
if (y0 <= y1) {
73-
return Rectangle<T>(x0, y0, x1 - x0, y1 - y0);
70+
return Rectangle<T>(x0, y0, (x1 - x0) as T, (y1 - y0) as T);
7471
}
7572
}
7673
return null;
@@ -92,7 +89,7 @@ abstract class _RectangleBase<T extends num> {
9289
var left = min(this.left, other.left);
9390
var top = min(this.top, other.top);
9491

95-
return Rectangle<T>(left, top, right - left, bottom - top);
92+
return Rectangle<T>(left, top, (right - left) as T, (bottom - top) as T);
9693
}
9794

9895
/// Tests whether `this` entirely contains [another].
@@ -112,19 +109,20 @@ abstract class _RectangleBase<T extends num> {
112109
}
113110

114111
Point<T> get topLeft => Point<T>(this.left, this.top);
115-
Point<T> get topRight => Point<T>(this.left + this.width, this.top);
112+
Point<T> get topRight => Point<T>((this.left + this.width) as T, this.top);
116113
Point<T> get bottomRight =>
117-
Point<T>(this.left + this.width, this.top + this.height);
118-
Point<T> get bottomLeft => Point<T>(this.left, this.top + this.height);
114+
Point<T>((this.left + this.width) as T, (this.top + this.height) as T);
115+
Point<T> get bottomLeft => Point<T>(this.left, (this.top + this.height) as T);
119116
}
120117

121118
/// A class for representing two-dimensional rectangles whose properties are
122119
/// immutable.
123120
class Rectangle<T extends num> extends _RectangleBase<T> {
124121
final T left;
125122
final T top;
126-
final T width;
127-
final T height;
123+
// Type `num` because constructor cannot cast to `T`.
124+
final num _width;
125+
final num _height;
128126

129127
/// Create a rectangle spanned by `(left, top)` and
130128
/// `(left+width, top+height)`.
@@ -139,8 +137,12 @@ class Rectangle<T extends num> extends _RectangleBase<T> {
139137
/// If `width` and `height` are zero, the "rectangle" comprises only the
140138
/// single point `(left, top)`.
141139
const Rectangle(this.left, this.top, T width, T height)
142-
: this.width = (width < 0) ? -width * 0 : width, // Inline _clampToZero.
143-
this.height = (height < 0) ? -height * 0 : height;
140+
: _width = (width < 0) ? -width * 0 : width, // Inline _clampToZero<num>.
141+
_height = (height < 0) ? -height * 0 : height;
142+
143+
T get width => _width as T;
144+
145+
T get height => _height as T;
144146

145147
/// Create a rectangle spanned by the points [a] and [b];
146148
///
@@ -154,9 +156,9 @@ class Rectangle<T extends num> extends _RectangleBase<T> {
154156
/// Similar for the y-coordinates and the bottom edge.
155157
factory Rectangle.fromPoints(Point<T> a, Point<T> b) {
156158
T left = min(a.x, b.x);
157-
T width = max(a.x, b.x) - left;
159+
T width = (max(a.x, b.x) - left) as T;
158160
T top = min(a.y, b.y);
159-
T height = max(a.y, b.y) - top;
161+
T height = (max(a.y, b.y) - top) as T;
160162
return Rectangle<T>(left, top, width, height);
161163
}
162164
}
@@ -205,9 +207,9 @@ class MutableRectangle<T extends num> extends _RectangleBase<T>
205207
/// Similar for the y-coordinates and the bottom edge.
206208
factory MutableRectangle.fromPoints(Point<T> a, Point<T> b) {
207209
T left = min(a.x, b.x);
208-
T width = max(a.x, b.x) - left;
210+
T width = (max(a.x, b.x) - left) as T;
209211
T top = min(a.y, b.y);
210-
T height = max(a.y, b.y) - top;
212+
T height = (max(a.y, b.y) - top) as T;
211213
return MutableRectangle<T>(left, top, width, height);
212214
}
213215

@@ -245,5 +247,5 @@ class MutableRectangle<T extends num> extends _RectangleBase<T>
245247
/// Returns `0` if value is int, `0.0` if value is double.
246248
T _clampToZero<T extends num>(T value) {
247249
assert(value < 0);
248-
return -value * 0;
250+
return (-value * 0) as T;
249251
}

0 commit comments

Comments
 (0)