Skip to content

Commit baf1afa

Browse files
nshahancommit-bot@chromium.org
authored andcommitted
[dartdevc] Opt in dart:collection patch file for NNBD support
Pairs best with https://dart-review.googlesource.com/c/sdk/+/127161 Change-Id: I7450128ddc7a59e05c518d22301b1a493a39e16a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127222 Reviewed-by: Mark Zhou <markzipan@google.com> Commit-Queue: Nicholas Shahan <nshahan@google.com>
1 parent 845fccc commit baf1afa

File tree

3 files changed

+49
-46
lines changed

3 files changed

+49
-46
lines changed

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

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import 'dart:_js_helper'
2424
class HashMap<K, V> {
2525
@patch
2626
factory HashMap(
27-
{bool equals(K key1, K key2),
28-
int hashCode(K key),
29-
bool isValidKey(Object potentialKey)}) {
27+
{bool Function(K, K)? equals,
28+
int Function(K)? hashCode,
29+
bool Function(dynamic)? isValidKey}) {
3030
if (isValidKey == null) {
3131
if (hashCode == null) {
3232
if (equals == null) {
@@ -54,9 +54,9 @@ class HashMap<K, V> {
5454
class LinkedHashMap<K, V> {
5555
@patch
5656
factory LinkedHashMap(
57-
{bool equals(K key1, K key2),
58-
int hashCode(K key),
59-
bool isValidKey(Object potentialKey)}) {
57+
{bool Function(K, K)? equals,
58+
int Function(K)? hashCode,
59+
bool Function(dynamic)? isValidKey}) {
6060
if (isValidKey == null) {
6161
if (hashCode == null) {
6262
if (equals == null) {
@@ -84,9 +84,9 @@ class LinkedHashMap<K, V> {
8484
class HashSet<E> {
8585
@patch
8686
factory HashSet(
87-
{bool equals(E e1, E e2),
88-
int hashCode(E e),
89-
bool isValidKey(Object potentialKey)}) {
87+
{bool Function(E, E)? equals,
88+
int Function(E)? hashCode,
89+
bool Function(dynamic)? isValidKey}) {
9090
if (isValidKey == null) {
9191
if (hashCode == null) {
9292
if (equals == null) {
@@ -115,9 +115,9 @@ class HashSet<E> {
115115
class LinkedHashSet<E> {
116116
@patch
117117
factory LinkedHashSet(
118-
{bool equals(E e1, E e2),
119-
int hashCode(E e),
120-
bool isValidKey(Object potentialKey)}) {
118+
{bool Function(E, E)? equals,
119+
int Function(E)? hashCode,
120+
bool Function(dynamic)? isValidKey}) {
121121
if (isValidKey == null) {
122122
if (hashCode == null) {
123123
if (equals == null) {
@@ -178,13 +178,14 @@ class _HashSet<E> extends _InternalSet<E>
178178

179179
Set<R> _newSimilarSet<R>() => _HashSet<R>();
180180

181-
bool contains(Object key) {
181+
bool contains(Object? key) {
182182
if (key == null) {
183+
// Convert undefined to null, if needed.
183184
key = null;
184185
} else if (JS<bool>('!', '#[#] !== #', key, dart.extensionSymbol('_equals'),
185186
dart.identityEquals)) {
186187
@notNull
187-
var k = key;
188+
Object? k = key;
188189
var buckets = JS('', '#.get(# & 0x3ffffff)', _keyMap, k.hashCode);
189190
if (buckets != null) {
190191
for (int i = 0, n = JS('!', '#.length', buckets); i < n; i++) {
@@ -197,12 +198,12 @@ class _HashSet<E> extends _InternalSet<E>
197198
return JS<bool>('!', '#.has(#)', _map, key);
198199
}
199200

200-
E lookup(Object key) {
201+
E? lookup(Object? key) {
201202
if (key == null) return null;
202203
if (JS<bool>('!', '#[#] !== #', key, dart.extensionSymbol('_equals'),
203204
dart.identityEquals)) {
204205
@notNull
205-
var k = key;
206+
Object? k = key;
206207
var buckets = JS('', '#.get(# & 0x3ffffff)', _keyMap, k.hashCode);
207208
if (buckets != null) {
208209
for (int i = 0, n = JS('!', '#.length', buckets); i < n; i++) {
@@ -219,7 +220,8 @@ class _HashSet<E> extends _InternalSet<E>
219220
var map = _map;
220221
if (key == null) {
221222
if (JS('', '#.has(null)', map)) return false;
222-
key = null;
223+
// Convert undefined to null, if needed.
224+
JS('', '# = null', key);
223225
} else if (JS<bool>('!', '#[#] !== #', key, dart.extensionSymbol('_equals'),
224226
dart.identityEquals)) {
225227
var keyMap = _keyMap;
@@ -249,7 +251,8 @@ class _HashSet<E> extends _InternalSet<E>
249251
int length = JS('', '#.size', map);
250252
for (E key in objects) {
251253
if (key == null) {
252-
key = null; // converts undefined to null, if needed.
254+
// Convert undefined to null, if needed.
255+
JS('', '# = null', key);
253256
} else if (JS<bool>('!', '#[#] !== #', key,
254257
dart.extensionSymbol('_equals'), dart.identityEquals)) {
255258
key = putLinkedMapKey(key, _keyMap);
@@ -261,13 +264,14 @@ class _HashSet<E> extends _InternalSet<E>
261264
}
262265
}
263266

264-
bool remove(Object key) {
267+
bool remove(Object? key) {
265268
if (key == null) {
269+
// Convert undefined to null, if needed.
266270
key = null;
267271
} else if (JS<bool>('!', '#[#] !== #', key, dart.extensionSymbol('_equals'),
268272
dart.identityEquals)) {
269273
@notNull
270-
var k = key;
274+
Object? k = key;
271275
int hash = JS('!', '# & 0x3ffffff', k.hashCode);
272276
var buckets = JS('', '#.get(#)', _keyMap, hash);
273277
if (buckets == null) return false; // not found
@@ -309,7 +313,8 @@ class _ImmutableSet<E> extends _HashSet<E> {
309313
var map = _map;
310314
for (Object key in entries) {
311315
if (key == null) {
312-
key = null; // converts undefined to null, if needed.
316+
// Convert undefined to null, if needed.
317+
JS('', '# = null', key);
313318
} else if (JS<bool>('!', '#[#] !== #', key,
314319
dart.extensionSymbol('_equals'), dart.identityEquals)) {
315320
key = putLinkedMapKey(key, _keyMap);
@@ -318,10 +323,10 @@ class _ImmutableSet<E> extends _HashSet<E> {
318323
}
319324
}
320325

321-
bool add(Object other) => throw _unsupported();
322-
void addAll(Object other) => throw _unsupported();
326+
bool add(E value) => throw _unsupported();
327+
void addAll(Iterable<E> elements) => throw _unsupported();
323328
void clear() => throw _unsupported();
324-
bool remove(Object key) => throw _unsupported();
329+
bool remove(Object? value) => throw _unsupported();
325330

326331
static Error _unsupported() =>
327332
UnsupportedError("Cannot modify unmodifiable set");
@@ -342,12 +347,14 @@ class _IdentityHashSet<E> extends _InternalSet<E>
342347

343348
Set<R> _newSimilarSet<R>() => _IdentityHashSet<R>();
344349

345-
bool contains(Object element) {
346-
return JS('', '#.has(#)', _map, element);
350+
bool contains(Object? element) {
351+
return JS<bool>('!', '#.has(#)', _map, element);
347352
}
348353

349-
E lookup(Object element) {
350-
return JS('', '#.has(#)', _map, element) ? element : null;
354+
E? lookup(Object? element) {
355+
return element is E && JS<bool>('!', '#.has(#)', _map, element)
356+
? element
357+
: null;
351358
}
352359

353360
bool add(E element) {
@@ -369,7 +376,7 @@ class _IdentityHashSet<E> extends _InternalSet<E>
369376
}
370377
}
371378

372-
bool remove(Object element) {
379+
bool remove(Object? element) {
373380
if (JS<bool>('!', '#.delete(#)', _map, element)) {
374381
_modifications = (_modifications + 1) & 0x3ffffff;
375382
return true;
@@ -387,15 +394,15 @@ class _IdentityHashSet<E> extends _InternalSet<E>
387394
}
388395

389396
class _CustomKeyHashSet<E> extends _CustomHashSet<E> {
390-
_Predicate<Object> _validKey;
397+
_Predicate<Object?> _validKey;
391398
_CustomKeyHashSet(_Equality<E> equals, _Hasher<E> hashCode, this._validKey)
392399
: super(equals, hashCode);
393400

394401
Set<E> _newSet() => _CustomKeyHashSet<E>(_equals, _hashCode, _validKey);
395402

396403
Set<R> _newSimilarSet<R>() => _HashSet<R>();
397404

398-
bool contains(Object element) {
405+
bool contains(Object? element) {
399406
// TODO(jmesserly): there is a subtle difference here compared to Dart 1.
400407
// See the comment on CustomKeyHashMap.containsKey for more information.
401408
// Treatment of `null` is different due to strong mode's requirement to
@@ -404,12 +411,12 @@ class _CustomKeyHashSet<E> extends _CustomHashSet<E> {
404411
return super.contains(element);
405412
}
406413

407-
E lookup(Object element) {
414+
E? lookup(Object? element) {
408415
if (!_validKey(element)) return null;
409416
return super.lookup(element);
410417
}
411418

412-
bool remove(Object element) {
419+
bool remove(Object? element) {
413420
if (!_validKey(element)) return false;
414421
return super.remove(element);
415422
}
@@ -445,7 +452,7 @@ class _CustomHashSet<E> extends _InternalSet<E>
445452
Set<E> _newSet() => _CustomHashSet<E>(_equals, _hashCode);
446453
Set<R> _newSimilarSet<R>() => _HashSet<R>();
447454

448-
bool contains(Object key) {
455+
bool contains(Object? key) {
449456
if (key is E) {
450457
var buckets = JS('', '#.get(# & 0x3ffffff)', _keyMap, _hashCode(key));
451458
if (buckets != null) {
@@ -459,7 +466,7 @@ class _CustomHashSet<E> extends _InternalSet<E>
459466
return false;
460467
}
461468

462-
E lookup(Object key) {
469+
E? lookup(Object? key) {
463470
if (key is E) {
464471
var buckets = JS('', '#.get(# & 0x3ffffff)', _keyMap, _hashCode(key));
465472
if (buckets != null) {
@@ -497,7 +504,7 @@ class _CustomHashSet<E> extends _InternalSet<E>
497504
for (E element in objects) add(element);
498505
}
499506

500-
bool remove(Object key) {
507+
bool remove(Object? key) {
501508
if (key is E) {
502509
var hash = JS<int>('!', '# & 0x3ffffff', _hashCode(key));
503510
var keyMap = _keyMap;
@@ -578,24 +585,24 @@ abstract class _InternalSet<E> extends _SetBase<E> {
578585
abstract class _SplayTree<K, Node extends _SplayTreeNode<K>> {
579586
@patch
580587
Node _splayMin(Node node) {
581-
Node current = node;
588+
var current = node;
582589
while (current.left != null) {
583-
Node left = current.left;
590+
var left = current.left!;
584591
current.left = left.right;
585592
left.right = current;
586-
current = left;
593+
current = left as Node;
587594
}
588595
return current;
589596
}
590597

591598
@patch
592599
Node _splayMax(Node node) {
593-
Node current = node;
600+
var current = node;
594601
while (current.right != null) {
595-
Node right = current.right;
602+
var right = current.right!;
596603
current.right = right.left;
597604
right.left = current;
598-
current = right;
605+
current = right as Node;
599606
}
600607
return current;
601608
}

sdk_nnbd/lib/_internal/js_dev_runtime/private/js_array.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
part of dart._interceptors;
86

97
/**

sdk_nnbd/lib/_internal/js_dev_runtime/private/js_number.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
part of dart._interceptors;
86

97
/**

0 commit comments

Comments
 (0)