Skip to content

Commit a4bac1f

Browse files
Markzipancommit-bot@chromium.org
authored andcommitted
[dartdevc] Migrating dart:_js_helper and dart:_interceptors to NNBD.
Subsumes the reland of https://dart-review.googlesource.com/c/sdk/+/125994 Change-Id: Ie12c8d473303d0ce7379828caf7523b26733ab75 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/126610 Reviewed-by: Leaf Petersen <leafp@google.com> Reviewed-by: Sigmund Cherem <sigmund@google.com> Commit-Queue: Mark Zhou <markzipan@google.com>
1 parent 2bb6076 commit a4bac1f

File tree

13 files changed

+107
-113
lines changed

13 files changed

+107
-113
lines changed

sdk_nnbd/lib/_internal/js_dev_runtime/private/annotations.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class JsPeerInterface {
8181
/// Used for classes where Dart subclasses should be callable from JavaScript
8282
/// matching the JavaScript calling conventions.
8383
final String name;
84-
const JsPeerInterface({this.name});
84+
const JsPeerInterface({required this.name});
8585
}
8686

8787
/// A Dart interface may only be implemented by a native JavaScript object

sdk_nnbd/lib/_internal/js_dev_runtime/private/custom_hash_map.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55
part of dart._js_helper;
66

77
class CustomKeyHashMap<K, V> extends CustomHashMap<K, V> {
8-
final _Predicate<Object> _validKey;
8+
final _Predicate<Object?> _validKey;
99
CustomKeyHashMap(_Equality<K> equals, _Hasher<K> hashCode, this._validKey)
1010
: super(equals, hashCode);
1111

1212
@override
1313
@notNull
14-
bool containsKey(Object key) {
14+
bool containsKey(Object? key) {
1515
if (!_validKey(key)) return false;
1616
return super.containsKey(key);
1717
}
1818

1919
@override
20-
V operator [](Object key) {
20+
V? operator [](Object? key) {
2121
if (!_validKey(key)) return null;
2222
return super[key];
2323
}
2424

2525
@override
26-
V remove(Object key) {
26+
V? remove(Object? key) {
2727
if (!_validKey(key)) return null;
2828
return super.remove(key);
2929
}
@@ -67,7 +67,7 @@ class CustomHashMap<K, V> extends InternalMap<K, V> {
6767
Iterable<V> get values => _JSMapIterable<V>(this, false);
6868

6969
@notNull
70-
bool containsKey(Object key) {
70+
bool containsKey(Object? key) {
7171
if (key is K) {
7272
var buckets = JS('', '#.get(# & 0x3ffffff)', _keyMap, _hashCode(key));
7373
if (buckets != null) {
@@ -81,7 +81,7 @@ class CustomHashMap<K, V> extends InternalMap<K, V> {
8181
return false;
8282
}
8383

84-
bool containsValue(Object value) {
84+
bool containsValue(Object? value) {
8585
for (var v in JS('', '#.values()', _map)) {
8686
if (value == v) return true;
8787
}
@@ -94,7 +94,7 @@ class CustomHashMap<K, V> extends InternalMap<K, V> {
9494
});
9595
}
9696

97-
V operator [](Object key) {
97+
V? operator [](Object? key) {
9898
if (key is K) {
9999
var buckets = JS('', '#.get(# & 0x3ffffff)', _keyMap, _hashCode(key));
100100
if (buckets != null) {
@@ -150,13 +150,13 @@ class CustomHashMap<K, V> extends InternalMap<K, V> {
150150
JS('', '#.push(#)', buckets, key);
151151
}
152152
V value = ifAbsent();
153-
if (value == null) value = null; // coerce undefined to null.
153+
if (value == null) JS('', '# = null', value); // coerce undefined to null.
154154
JS('', '#.set(#, #)', _map, key, value);
155155
_modifications = (_modifications + 1) & 0x3ffffff;
156156
return value;
157157
}
158158

159-
V remove(Object key) {
159+
V? remove(Object? key) {
160160
if (key is K) {
161161
int hash = JS('!', '# & 0x3ffffff', _hashCode(key));
162162
var keyMap = _keyMap;

sdk_nnbd/lib/_internal/js_dev_runtime/private/identity_hash_map.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class IdentityMap<K, V> extends InternalMap<K, V> {
3333
Iterable<K> get keys => _JSMapIterable<K>(this, true);
3434
Iterable<V> get values => _JSMapIterable<V>(this, false);
3535

36-
bool containsKey(Object key) {
36+
bool containsKey(Object? key) {
3737
return JS<bool>('!', '#.has(#)', _map, key);
3838
}
3939

40-
bool containsValue(Object value) {
40+
bool containsValue(Object? value) {
4141
for (var v in JS('', '#.values()', _map)) {
4242
if (v == value) return true;
4343
}
@@ -54,7 +54,7 @@ class IdentityMap<K, V> extends InternalMap<K, V> {
5454
}
5555
}
5656

57-
V operator [](Object key) {
57+
V? operator [](Object? key) {
5858
V value = JS('', '#.get(#)', _map, key);
5959
return value == null ? null : value; // coerce undefined to null.
6060
}
@@ -73,13 +73,13 @@ class IdentityMap<K, V> extends InternalMap<K, V> {
7373
return JS('', '#.get(#)', _map, key);
7474
}
7575
V value = ifAbsent();
76-
if (value == null) value = null; // coerce undefined to null.
76+
if (value == null) JS('', '# = null', value);
7777
JS('', '#.set(#, #)', _map, key, value);
7878
_modifications = (_modifications + 1) & 0x3ffffff;
7979
return value;
8080
}
8181

82-
V remove(Object key) {
82+
V? remove(Object? key) {
8383
V value = JS('', '#.get(#)', _map, key);
8484
if (JS<bool>('!', '#.delete(#)', _map, key)) {
8585
_modifications = (_modifications + 1) & 0x3ffffff;
@@ -128,10 +128,10 @@ class _JSMapIterable<E> extends EfficientLengthIterable<E> {
128128

129129
Iterator<E> get iterator => DartIterator<E>(_jsIterator());
130130

131-
bool contains(Object element) =>
131+
bool contains(Object? element) =>
132132
_isKeys ? _map.containsKey(element) : _map.containsValue(element);
133133

134-
void forEach(void f(E element)) {
134+
void forEach(void Function(E) f) {
135135
for (var entry in this) f(entry);
136136
}
137137
}

sdk_nnbd/lib/_internal/js_dev_runtime/private/interceptors.dart

Lines changed: 4 additions & 6 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
library dart._interceptors;
86

97
import 'dart:collection';
@@ -122,15 +120,15 @@ class JSNoSuchMethodError extends NativeError implements NoSuchMethodError {
122120
static final _extensionName = RegExp(r"^Symbol\(dartx\.(.+)\)$");
123121
static final _privateName = RegExp(r"^Symbol\((_.+)\)$");
124122

125-
String _fieldName(String message) {
123+
String? _fieldName(String message) {
126124
var match = _nullError.firstMatch(message);
127125
if (match == null) return null;
128126
var name = match[1];
129127
match = _extensionName.firstMatch(name) ?? _privateName.firstMatch(name);
130128
return match != null ? match[1] : name;
131129
}
132130

133-
String _functionCallTarget(String message) {
131+
String? _functionCallTarget(String message) {
134132
var match = _notAFunction.firstMatch(message);
135133
return match != null ? match[1] : null;
136134
}
@@ -180,7 +178,7 @@ class JSFunction extends Interceptor {
180178
// TODO(jmesserly): remove these once we canonicalize tearoffs.
181179
operator ==(other) {
182180
if (other == null) return false;
183-
var boundObj = JS<Object>('', '#._boundObject', this);
181+
var boundObj = JS<Object?>('', '#._boundObject', this);
184182
if (boundObj == null) return JS<bool>('!', '# === #', this, other);
185183
return JS(
186184
'bool',
@@ -192,7 +190,7 @@ class JSFunction extends Interceptor {
192190
}
193191

194192
get hashCode {
195-
var boundObj = JS<Object>('', '#._boundObject', this);
193+
var boundObj = JS<Object?>('', '#._boundObject', this);
196194
if (boundObj == null) return identityHashCode(this);
197195

198196
var boundMethod = JS<Object>('!', '#._boundMethod', this);

sdk_nnbd/lib/_internal/js_dev_runtime/private/js_helper.dart

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ const _Patch patch = _Patch();
4343
// https://github.com/dart-lang/sdk/issues/28320
4444
class DartIterator<E> implements Iterator<E> {
4545
final _jsIterator;
46-
E _current;
46+
E? _current;
4747

4848
DartIterator(this._jsIterator);
4949

50-
E get current => _current;
50+
E get current => _current as E;
5151

5252
bool moveNext() {
5353
final ret = JS('', '#.next()', _jsIterator);
@@ -69,17 +69,18 @@ class SyncIterable<E> extends IterableBase<E> {
6969

7070
class Primitives {
7171
@NoInline()
72-
static int? _parseIntError(String source, int? handleError(String source)?) {
72+
static int? _parseIntError(
73+
String source, int? Function(String)? handleError) {
7374
if (handleError == null) throw FormatException(source);
7475
return handleError(source);
7576
}
7677

77-
static int? parseInt(
78-
@nullCheck String source, int? _radix, int? handleError(String source)?) {
78+
static int? parseInt(@nullCheck String source, int? _radix,
79+
int? Function(String)? handleError) {
7980
var re = JS('', r'/^\s*[+-]?((0x[a-f0-9]+)|(\d+)|([a-z0-9]+))\s*$/i');
8081
// TODO(jmesserly): this isn't reified List<String>, but it's safe to use as
8182
// long as we use it locally and don't expose it to user code.
82-
List<String> match = JS('', '#.exec(#)', re, source);
83+
List<String>? match = JS('', '#.exec(#)', re, source);
8384
int digitsIndex = 1;
8485
int hexIndex = 2;
8586
int decimalIndex = 3;
@@ -89,7 +90,7 @@ class Primitives {
8990
// again.
9091
return _parseIntError(source, handleError);
9192
}
92-
String decimalMatch = match[decimalIndex];
93+
String? decimalMatch = match[decimalIndex];
9394
if (_radix == null) {
9495
if (decimalMatch != null) {
9596
// Cannot fail because we know that the digits are all decimal.
@@ -147,15 +148,15 @@ class Primitives {
147148

148149
@NoInline()
149150
static double? _parseDoubleError(
150-
String source, double? handleError(String source)?) {
151+
String source, double? Function(String)? handleError) {
151152
if (handleError == null) {
152153
throw FormatException('Invalid double', source);
153154
}
154155
return handleError(source);
155156
}
156157

157158
static double? parseDouble(
158-
@nullCheck String source, double? handleError(String source)?) {
159+
@nullCheck String source, double? Function(String)? handleError) {
159160
// Notice that JS parseFloat accepts garbage at the end of the string.
160161
// Accept only:
161162
// - [+/-]NaN
@@ -169,7 +170,7 @@ class Primitives {
169170
source)) {
170171
return _parseDoubleError(source, handleError);
171172
}
172-
num result = JS('!', r'parseFloat(#)', source);
173+
double result = JS('!', r'parseFloat(#)', source);
173174
if (result.isNaN) {
174175
var trimmed = source.trim();
175176
if (trimmed == 'NaN' || trimmed == '+NaN' || trimmed == '-NaN') {
@@ -186,7 +187,7 @@ class Primitives {
186187
static int dateNow() => JS<int>('!', r'Date.now()');
187188

188189
static void initTicker() {
189-
if (timerFrequency != null) return;
190+
if (timerFrequency != 0) return;
190191
// Start with low-resolution. We overwrite the fields if we find better.
191192
timerFrequency = 1000;
192193
timerTicks = dateNow;
@@ -200,8 +201,9 @@ class Primitives {
200201
timerTicks = () => (1000 * JS<num>('!', '#.now()', performance)).floor();
201202
}
202203

203-
static int timerFrequency;
204-
static int Function() timerTicks;
204+
/// 0 frequency indicates the default uninitialized state.
205+
static int timerFrequency = 0;
206+
static late int Function() timerTicks;
205207

206208
static bool get isD8 {
207209
return JS(
@@ -266,7 +268,7 @@ class Primitives {
266268
}
267269

268270
@notNull
269-
static String stringFromCharCodes(List<int> charCodes) {
271+
static String stringFromCharCodes(JSArray<int> charCodes) {
270272
for (@nullCheck var i in charCodes) {
271273
if (i < 0) throw argumentErrorValue(i);
272274
if (i > 0xffff) return stringFromCodePoints(charCodes);
@@ -325,7 +327,7 @@ class Primitives {
325327
// Example: "Wed May 16 2012 21:13:00 GMT+0200 (CEST)".
326328
// We extract this name using a regexp.
327329
var d = lazyAsJsDate(receiver);
328-
List match = JS('JSArray|Null', r'/\((.*)\)/.exec(#.toString())', d);
330+
List? match = JS('JSArray|Null', r'/\((.*)\)/.exec(#.toString())', d);
329331
if (match != null) return match[1];
330332

331333
// Internet Explorer 10+ emits the zone name without parenthesis:
@@ -360,7 +362,7 @@ class Primitives {
360362
return -JS<int>('!', r'#.getTimezoneOffset()', lazyAsJsDate(receiver));
361363
}
362364

363-
static int valueFromDecomposedDate(
365+
static int? valueFromDecomposedDate(
364366
@nullCheck int years,
365367
@nullCheck int month,
366368
@nullCheck int day,
@@ -371,7 +373,7 @@ class Primitives {
371373
@nullCheck bool isUtc) {
372374
final int MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000;
373375
var jsMonth = month - 1;
374-
num value;
376+
int value;
375377
if (isUtc) {
376378
value = JS<int>('!', r'Date.UTC(#, #, #, #, #, #, #)', years, jsMonth,
377379
day, hours, minutes, seconds, milliseconds);
@@ -504,7 +506,7 @@ Error diagnoseIndexError(indexable, int index) {
504506
* describes the problem.
505507
*/
506508
@NoInline()
507-
Error diagnoseRangeError(int start, int end, int length) {
509+
Error diagnoseRangeError(int? start, int? end, int length) {
508510
if (start == null) {
509511
return ArgumentError.value(start, 'start');
510512
}
@@ -553,9 +555,9 @@ throwConcurrentModificationError(collection) {
553555
}
554556

555557
class JsNoSuchMethodError extends Error implements NoSuchMethodError {
556-
final String _message;
557-
final String _method;
558-
final String _receiver;
558+
final String? _message;
559+
final String? _method;
560+
final String? _receiver;
559561

560562
JsNoSuchMethodError(this._message, match)
561563
: _method = match == null ? null : JS('String|Null', '#.method', match),
@@ -597,11 +599,11 @@ fillLiteralMap(keyValuePairs, Map result) {
597599
return result;
598600
}
599601

600-
bool jsHasOwnProperty(var jsObject, String property) {
602+
bool jsHasOwnProperty(jsObject, String property) {
601603
return JS<bool>('!', r'#.hasOwnProperty(#)', jsObject, property);
602604
}
603605

604-
jsPropertyAccess(var jsObject, String property) {
606+
jsPropertyAccess(jsObject, String property) {
605607
return JS('var', r'#[#]', jsObject, property);
606608
}
607609

@@ -738,12 +740,12 @@ class RuntimeError extends Error {
738740

739741
/// Error thrown by DDC when an `assert()` fails (with or without a message).
740742
class AssertionErrorImpl extends AssertionError {
741-
final String _fileUri;
742-
final int _line;
743-
final int _column;
744-
final String _conditionSource;
743+
final String? _fileUri;
744+
final int? _line;
745+
final int? _column;
746+
final String? _conditionSource;
745747

746-
AssertionErrorImpl(Object message,
748+
AssertionErrorImpl(Object? message,
747749
[this._fileUri, this._line, this._column, this._conditionSource])
748750
: super(message);
749751

@@ -809,7 +811,7 @@ class PrivateSymbol implements Symbol {
809811

810812
static String getName(Symbol symbol) => (symbol as PrivateSymbol)._name;
811813

812-
static Object getNativeSymbol(Symbol symbol) {
814+
static Object? getNativeSymbol(Symbol symbol) {
813815
if (symbol is PrivateSymbol) return symbol._nativeSymbol;
814816
return null;
815817
}

sdk_nnbd/lib/_internal/js_dev_runtime/private/js_primitives.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
/// dart2js "primitives", that is, features that cannot be implemented without
86
/// access to JavaScript features.
97
library dart2js._js_primitives;

0 commit comments

Comments
 (0)