Skip to content

Commit f6a7d38

Browse files
authored
Emit double as return value instead of num (#256)
Emit double as return value instead of num Closes #57 In cases where the return type is known to be a double value, prefer double over num so users don't try and downcast the type, which won't work consistently on all compilers. In order to do this, _typeReference is amended to take an extra option 'returnType' which determines if the `_RawType` is used in a return type context.
1 parent 6d22294 commit f6a7d38

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+514
-499
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
- Added `onUnload` event stream to `ElementEventGetters` extension methods.
1616
- Expose `ElementStream` as a public class.
1717
- Require Dart `^3.4.0`.
18+
- APIs that return a double value now return `double` instead of `num`. This is
19+
to avoid users accidentally downcasting `num`, which has different semantics
20+
depending on whether you compile to JS or Wasm. See issue [#57][] for more
21+
details.
22+
23+
[#57]: https://github.com/dart-lang/web/issues/57
1824

1925
## 0.5.1
2026

lib/src/dom/battery_status.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extension type BatteryManager._(JSObject _) implements EventTarget, JSObject {
5050
/// > **Note:** Even if the time returned is precise to the second,
5151
/// > browsers round them to a higher interval
5252
/// > (typically to the closest 15 minutes) for privacy reasons.
53-
external num get chargingTime;
53+
external double get chargingTime;
5454

5555
/// The **`dischargingTime`** read-only property of the [BatteryManager]
5656
/// interface indicates the amount of time, in seconds, that remains until the
@@ -63,7 +63,7 @@ extension type BatteryManager._(JSObject _) implements EventTarget, JSObject {
6363
/// > **Note:** Even if the time returned is precise to the second, browsers
6464
/// > round them to a higher
6565
/// > interval (typically to the closest 15 minutes) for privacy reasons.
66-
external num get dischargingTime;
66+
external double get dischargingTime;
6767

6868
/// The **`level`** read-only property of the [BatteryManager] interface
6969
/// indicates the current battery charge level as a value between `0.0` and
@@ -74,7 +74,7 @@ extension type BatteryManager._(JSObject _) implements EventTarget, JSObject {
7474
/// report the battery status information.
7575
/// When its value changes, the [BatteryManager.levelchange_event] event is
7676
/// fired.
77-
external num get level;
77+
external double get level;
7878
external EventHandler get onchargingchange;
7979
external set onchargingchange(EventHandler value);
8080
external EventHandler get onchargingtimechange;

lib/src/dom/cookie_store.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ extension type CookieInit._(JSObject _) implements JSObject {
9494
external set name(String value);
9595
external String get value;
9696
external set value(String value);
97-
external DOMHighResTimeStamp? get expires;
97+
external double? get expires;
9898
external set expires(DOMHighResTimeStamp? value);
9999
external String? get domain;
100100
external set domain(String? value);
@@ -142,7 +142,7 @@ extension type CookieListItem._(JSObject _) implements JSObject {
142142
external set domain(String? value);
143143
external String get path;
144144
external set path(String value);
145-
external DOMHighResTimeStamp? get expires;
145+
external double? get expires;
146146
external set expires(DOMHighResTimeStamp? value);
147147
external bool get secure;
148148
external set secure(bool value);

lib/src/dom/css_animations.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extension type AnimationEvent._(JSObject _) implements Event, JSObject {
4343
/// `elapsedTime` is `0.0` unless there was a negative value for
4444
/// , in which case the event will be fired with
4545
/// `elapsedTime` containing `(-1 * delay)`.
46-
external num get elapsedTime;
46+
external double get elapsedTime;
4747

4848
/// The **`AnimationEvent.pseudoElement`** read-only property is a
4949
/// string, starting with `'::'`, containing the name of the
@@ -65,7 +65,7 @@ extension type AnimationEventInit._(JSObject _) implements EventInit, JSObject {
6565

6666
external String get animationName;
6767
external set animationName(String value);
68-
external num get elapsedTime;
68+
external double get elapsedTime;
6969
external set elapsedTime(num value);
7070
external String get pseudoElement;
7171
external set pseudoElement(String value);

lib/src/dom/css_paint_api.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ extension type PaintRenderingContext2D._(JSObject _) implements JSObject {
116116
]);
117117
external void setLineDash(JSArray<JSNumber> segments);
118118
external JSArray<JSNumber> getLineDash();
119-
external num get globalAlpha;
119+
external double get globalAlpha;
120120
external set globalAlpha(num value);
121121
external String get globalCompositeOperation;
122122
external set globalCompositeOperation(String value);
@@ -128,26 +128,26 @@ extension type PaintRenderingContext2D._(JSObject _) implements JSObject {
128128
external set strokeStyle(JSAny value);
129129
external JSAny get fillStyle;
130130
external set fillStyle(JSAny value);
131-
external num get shadowOffsetX;
131+
external double get shadowOffsetX;
132132
external set shadowOffsetX(num value);
133-
external num get shadowOffsetY;
133+
external double get shadowOffsetY;
134134
external set shadowOffsetY(num value);
135-
external num get shadowBlur;
135+
external double get shadowBlur;
136136
external set shadowBlur(num value);
137137
external String get shadowColor;
138138
external set shadowColor(String value);
139-
external num get lineWidth;
139+
external double get lineWidth;
140140
external set lineWidth(num value);
141141
external CanvasLineCap get lineCap;
142142
external set lineCap(CanvasLineCap value);
143143
external CanvasLineJoin get lineJoin;
144144
external set lineJoin(CanvasLineJoin value);
145-
external num get miterLimit;
145+
external double get miterLimit;
146146
external set miterLimit(num value);
147-
external num get lineDashOffset;
147+
external double get lineDashOffset;
148148
external set lineDashOffset(num value);
149149
}
150150
extension type PaintSize._(JSObject _) implements JSObject {
151-
external num get width;
152-
external num get height;
151+
external double get width;
152+
external double get height;
153153
}

lib/src/dom/css_transitions.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extension type TransitionEvent._(JSObject _) implements Event, JSObject {
3939
/// seconds,
4040
/// when this event fired. This value is not affected by the
4141
/// property.
42-
external num get elapsedTime;
42+
external double get elapsedTime;
4343

4444
/// The **`TransitionEvent.pseudoElement`** read-only property is a
4545
/// string, starting with `'::'`, containing the name of the
@@ -63,7 +63,7 @@ extension type TransitionEventInit._(JSObject _)
6363

6464
external String get propertyName;
6565
external set propertyName(String value);
66-
external num get elapsedTime;
66+
external double get elapsedTime;
6767
external set elapsedTime(num value);
6868
external String get pseudoElement;
6969
external set pseudoElement(String value);

lib/src/dom/css_typed_om.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ extension type CSSUnitValue._(JSObject _) implements CSSNumericValue, JSObject {
302302

303303
/// The **`CSSUnitValue.value`** property of the
304304
/// [CSSUnitValue] interface returns a double indicating the number of units.
305-
external num get value;
305+
external double get value;
306306
external set value(num value);
307307

308308
/// The **`CSSUnitValue.unit`** read-only property

lib/src/dom/cssom_view.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ extension type ScrollToOptions._(JSObject _)
3333
num top,
3434
});
3535

36-
external num get left;
36+
external double get left;
3737
external set left(num value);
38-
external num get top;
38+
external double get top;
3939
external set top(num value);
4040
}
4141

@@ -248,41 +248,41 @@ extension type VisualViewport._(JSObject _) implements EventTarget, JSObject {
248248
/// returns the offset of the left edge of the visual viewport from the left
249249
/// edge of the layout viewport in CSS pixels, or `0` if current document is
250250
/// not fully active.
251-
external num get offsetLeft;
251+
external double get offsetLeft;
252252

253253
/// The **`offsetTop`** read-only property of the [VisualViewport] interface
254254
/// returns the offset of the top edge of the visual viewport from the top
255255
/// edge of the layout viewport in CSS pixels, or `0` if current document is
256256
/// not fully active.
257-
external num get offsetTop;
257+
external double get offsetTop;
258258

259259
/// The **`pageLeft`** read-only property of the [VisualViewport] interface
260260
/// returns the x coordinate of the left edge of the visual viewport relative
261261
/// to the initial containing block origin, in CSS pixels, or `0` if current
262262
/// document is not fully active.
263-
external num get pageLeft;
263+
external double get pageLeft;
264264

265265
/// The **`pageTop`** read-only property of the [VisualViewport] interface
266266
/// returns the y coordinate of the top edge of the visual viewport relative
267267
/// to the initial containing block origin, in CSS pixels, or `0` if current
268268
/// document is not fully active.
269-
external num get pageTop;
269+
external double get pageTop;
270270

271271
/// The **`width`** read-only property of the [VisualViewport] interface
272272
/// returns the width of the visual viewport, in CSS pixels, or `0` if current
273273
/// document is not fully active.
274-
external num get width;
274+
external double get width;
275275

276276
/// The **`height`** read-only property of the [VisualViewport] interface
277277
/// returns the height of the visual viewport, in CSS pixels, or `0` if
278278
/// current document is not fully active.
279-
external num get height;
279+
external double get height;
280280

281281
/// The **`scale`** read-only property of the [VisualViewport] interface
282282
/// returns the pinch-zoom scaling factor applied to the visual viewport, or
283283
/// `0` if current document is not fully active, or `1` if there is no output
284284
/// device.
285-
external num get scale;
285+
external double get scale;
286286
external EventHandler get onresize;
287287
external set onresize(EventHandler value);
288288
external EventHandler get onscroll;

lib/src/dom/dom.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import 'cssom.dart';
1919
import 'cssom_view.dart';
2020
import 'fullscreen.dart';
2121
import 'geometry.dart';
22-
import 'hr_time.dart';
2322
import 'html.dart';
2423
import 'selection_api.dart';
2524
import 'svg.dart';
@@ -286,7 +285,7 @@ extension type Event._(JSObject _) implements JSObject {
286285

287286
/// The **`timeStamp`** read-only property of the [Event] interface returns
288287
/// the time (in milliseconds) at which the event was created.
289-
external DOMHighResTimeStamp get timeStamp;
288+
external double get timeStamp;
290289
}
291290
extension type EventInit._(JSObject _) implements JSObject {
292291
external factory EventInit({
@@ -3406,7 +3405,7 @@ extension type Element._(JSObject _) implements Node, JSObject {
34063405
///
34073406
/// > **Warning:** On systems using display scaling, `scrollTop` may give you
34083407
/// > a decimal value.
3409-
external num get scrollTop;
3408+
external double get scrollTop;
34103409
external set scrollTop(num value);
34113410

34123411
/// The **`Element.scrollLeft`** property gets or sets the number
@@ -3432,7 +3431,7 @@ extension type Element._(JSObject _) implements Node, JSObject {
34323431
/// > **Warning:** On systems using display scaling, `scrollLeft` may give you
34333432
/// > a decimal
34343433
/// > value.
3435-
external num get scrollLeft;
3434+
external double get scrollLeft;
34363435
external set scrollLeft(num value);
34373436

34383437
/// The **`Element.scrollWidth`** read-only property is a
@@ -5253,7 +5252,7 @@ extension type XPathResult._(JSObject _) implements JSObject {
52535252
/// The read-only **`numberValue`** property of the
52545253
/// [XPathResult] interface returns the numeric value of a result with
52555254
/// [XPathResult.resultType] being `NUMBER_TYPE`.
5256-
external num get numberValue;
5255+
external double get numberValue;
52575256

52585257
/// The read-only **`stringValue`** property of the
52595258
/// [XPathResult] interface returns the string value of a result with

lib/src/dom/encrypted_media.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ extension type MediaKeySession._(JSObject _) implements EventTarget, JSObject {
171171
/// measured in milliseconds since January 1, 1970, UTC. This value may change
172172
/// during a
173173
/// session lifetime, such as when an action triggers the start of a window.
174-
external num get expiration;
174+
external double get expiration;
175175

176176
/// The `MediaKeySession.closed` read-only property returns a
177177
/// `Promise` signaling when a [MediaKeySession] closes. This

0 commit comments

Comments
 (0)