Skip to content

Commit 0aaa1c5

Browse files
committed
Port of dart:core to NNBD.
Change-Id: Id6a784ca45d702785aaa662b9d5d9ac6b6c46b43 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/123621 Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
1 parent b76ab40 commit 0aaa1c5

38 files changed

+622
-760
lines changed

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

Lines changed: 59 additions & 63 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:core classes.
86
import "dart:_internal" as _symbol_dev;
97
import 'dart:_interceptors';
@@ -33,11 +31,11 @@ String _symbolToString(Symbol symbol) => symbol is PrivateSymbol
3331
: _symbol_dev.Symbol.getName(symbol);
3432

3533
@patch
36-
int identityHashCode(Object object) {
34+
int identityHashCode(Object? object) {
3735
if (object == null) return 0;
3836
// Note: this works for primitives because we define the `identityHashCode`
3937
// for them to be equivalent to their computed hashCode function.
40-
int hash = JS('int|Null', r'#[#]', object, dart.identityHashCode_);
38+
int? hash = JS<int?>('int|Null', r'#[#]', object, dart.identityHashCode_);
4139
if (hash == null) {
4240
hash = JS<int>('!', '(Math.random() * 0x3fffffff) | 0');
4341
JS('void', r'#[#] = #', object, dart.identityHashCode_, hash);
@@ -49,7 +47,7 @@ int identityHashCode(Object object) {
4947
@patch
5048
class Object {
5149
@patch
52-
bool operator ==(other) => identical(this, other);
50+
bool operator ==(Object other) => identical(this, other);
5351

5452
@patch
5553
int get hashCode => identityHashCode(this);
@@ -59,7 +57,7 @@ class Object {
5957
"Instance of '${dart.typeName(dart.getReifiedType(this))}'";
6058

6159
@patch
62-
noSuchMethod(Invocation invocation) {
60+
dynamic noSuchMethod(Invocation invocation) {
6361
return dart.defaultNoSuchMethod(this, invocation);
6462
}
6563

@@ -77,8 +75,8 @@ class Null {
7775
@patch
7876
class Function {
7977
@patch
80-
static apply(Function f, List positionalArguments,
81-
[Map<Symbol, dynamic> namedArguments]) {
78+
static apply(Function f, List<Object>? positionalArguments,
79+
[Map<Symbol, dynamic>? namedArguments]) {
8280
positionalArguments ??= [];
8381
// dcall expects the namedArguments as a JS map in the last slot.
8482
if (namedArguments != null && namedArguments.isNotEmpty) {
@@ -104,18 +102,18 @@ class Function {
104102
// TODO(jmesserly): switch to WeakMap
105103
// Patch for Expando implementation.
106104
@patch
107-
class Expando<T> {
105+
class Expando<T extends Object> {
108106
@patch
109-
Expando([String name]) : this.name = name;
107+
Expando([String? name]) : this.name = name;
110108

111109
@patch
112-
T operator [](Object object) {
110+
T? operator [](Object object) {
113111
var values = Primitives.getProperty(object, _EXPANDO_PROPERTY_NAME);
114112
return (values == null) ? null : Primitives.getProperty(values, _getKey());
115113
}
116114

117115
@patch
118-
void operator []=(Object object, T value) {
116+
void operator []=(Object object, T? value) {
119117
var values = Primitives.getProperty(object, _EXPANDO_PROPERTY_NAME);
120118
if (values == null) {
121119
values = Object();
@@ -144,17 +142,17 @@ Null _kNull(_) => null;
144142
class int {
145143
@patch
146144
static int parse(String source,
147-
{int radix, @deprecated int onError(String source)}) {
148-
return Primitives.parseInt(source, radix, onError);
145+
{int? radix, @deprecated int onError(String source)?}) {
146+
return Primitives.parseInt(source, radix, onError)!;
149147
}
150148

151149
@patch
152-
static int tryParse(String source, {int radix}) {
150+
static int? tryParse(String source, {int? radix}) {
153151
return Primitives.parseInt(source, radix, _kNull);
154152
}
155153

156154
@patch
157-
factory int.fromEnvironment(String name, {int defaultValue}) {
155+
factory int.fromEnvironment(String name, {int defaultValue = 0}) {
158156
// ignore: const_constructor_throws_exception
159157
throw UnsupportedError(
160158
'int.fromEnvironment can only be used as a const constructor');
@@ -166,11 +164,11 @@ class double {
166164
@patch
167165
static double parse(String source,
168166
[@deprecated double onError(String source)]) {
169-
return Primitives.parseDouble(source, onError);
167+
return Primitives.parseDouble(source, onError)!;
170168
}
171169

172170
@patch
173-
static double tryParse(String source) {
171+
static double? tryParse(String source) {
174172
return Primitives.parseDouble(source, _kNull);
175173
}
176174
}
@@ -185,11 +183,11 @@ class BigInt implements Comparable<BigInt> {
185183
static BigInt get two => _BigIntImpl.two;
186184

187185
@patch
188-
static BigInt parse(String source, {int radix}) =>
186+
static BigInt parse(String source, {int? radix}) =>
189187
_BigIntImpl.parse(source, radix: radix);
190188

191189
@patch
192-
static BigInt tryParse(String source, {int radix}) =>
190+
static BigInt? tryParse(String source, {int? radix}) =>
193191
_BigIntImpl._tryParse(source, radix: radix);
194192

195193
@patch
@@ -199,7 +197,7 @@ class BigInt implements Comparable<BigInt> {
199197
@patch
200198
class Error {
201199
@patch
202-
static String _objectToString(Object object) {
200+
static String _objectToString(Object? object) {
203201
return "Instance of '${dart.typeName(dart.getReifiedType(object))}'";
204202
}
205203

@@ -245,10 +243,7 @@ class DateTime {
245243
@patch
246244
DateTime._internal(int year, int month, int day, int hour, int minute,
247245
int second, int millisecond, int microsecond, bool isUtc)
248-
// checkBool is manually inlined here because dart2js doesn't inline it
249-
// and [isUtc] is usually a constant.
250-
: this.isUtc =
251-
isUtc is bool ? isUtc : throw ArgumentError.value(isUtc, 'isUtc'),
246+
: isUtc = isUtc,
252247
_value = checkInt(Primitives.valueFromDecomposedDate(
253248
year,
254249
month,
@@ -272,7 +267,7 @@ class DateTime {
272267
}
273268

274269
@patch
275-
static int _brokenDownDateToValue(int year, int month, int day, int hour,
270+
static int? _brokenDownDateToValue(int year, int month, int day, int hour,
276271
int minute, int second, int millisecond, int microsecond, bool isUtc) {
277272
return Primitives.valueFromDecomposedDate(
278273
year,
@@ -293,7 +288,7 @@ class DateTime {
293288

294289
@patch
295290
Duration get timeZoneOffset {
296-
if (isUtc) return Duration();
291+
if (isUtc) return Duration.zero;
297292
return Duration(minutes: Primitives.getTimeZoneOffsetInMinutes(this));
298293
}
299294

@@ -370,9 +365,9 @@ class DateTime {
370365
@patch
371366
class Stopwatch {
372367
@patch
373-
static void _initTicker() {
368+
static int _initTicker() {
374369
Primitives.initTicker();
375-
_frequency = Primitives.timerFrequency;
370+
return Primitives.timerFrequency;
376371
}
377372

378373
@patch
@@ -464,7 +459,7 @@ class Map<K, V> {
464459
class String {
465460
@patch
466461
factory String.fromCharCodes(Iterable<int> charCodes,
467-
[int start = 0, int end]) {
462+
[int start = 0, int? end]) {
468463
if (charCodes is JSArray) {
469464
return _stringFromJSArray(charCodes, start, end);
470465
}
@@ -480,7 +475,7 @@ class String {
480475
}
481476

482477
@patch
483-
factory String.fromEnvironment(String name, {String defaultValue}) {
478+
factory String.fromEnvironment(String name, {String defaultValue = ""}) {
484479
// ignore: const_constructor_throws_exception
485480
throw UnsupportedError(
486481
'String.fromEnvironment can only be used as a const constructor');
@@ -489,7 +484,7 @@ class String {
489484
static String _stringFromJSArray(
490485
/*=JSArray<int>*/ list,
491486
int start,
492-
int endOrNull) {
487+
int? endOrNull) {
493488
int len = list.length;
494489
int end = RangeError.checkValidRange(start, endOrNull, len);
495490
if (start > 0 || end < len) {
@@ -499,14 +494,14 @@ class String {
499494
}
500495

501496
static String _stringFromUint8List(
502-
NativeUint8List charCodes, int start, int endOrNull) {
497+
NativeUint8List charCodes, int start, int? endOrNull) {
503498
int len = charCodes.length;
504499
int end = RangeError.checkValidRange(start, endOrNull, len);
505500
return Primitives.stringFromNativeUint8List(charCodes, start, end);
506501
}
507502

508503
static String _stringFromIterable(
509-
Iterable<int> charCodes, int start, int end) {
504+
Iterable<int> charCodes, int start, int? end) {
510505
if (start < 0) throw RangeError.range(start, 0, charCodes.length);
511506
if (end != null && end < start) {
512507
throw RangeError.range(end, start, charCodes.length);
@@ -565,7 +560,7 @@ class RegExp {
565560

566561
// Patch for 'identical' function.
567562
@patch
568-
bool identical(Object a, Object b) {
563+
bool identical(Object? a, Object? b) {
569564
return JS<bool>('!', '(# == null ? # == null : # === #)', a, b, a, b);
570565
}
571566

@@ -580,7 +575,7 @@ class StringBuffer {
580575
int get length => _contents.length;
581576

582577
@patch
583-
void write(Object obj) {
578+
void write(Object? obj) {
584579
_writeString('$obj');
585580
}
586581

@@ -595,7 +590,7 @@ class StringBuffer {
595590
}
596591

597592
@patch
598-
void writeln([Object obj = ""]) {
593+
void writeln([Object? obj = ""]) {
599594
_writeString('$obj\n');
600595
}
601596

@@ -607,7 +602,7 @@ class StringBuffer {
607602
@patch
608603
String toString() => Primitives.flattenString(_contents);
609604

610-
void _writeString(str) {
605+
void _writeString(String str) {
611606
_contents = Primitives.stringConcatUnchecked(_contents, str);
612607
}
613608

@@ -642,17 +637,17 @@ class _CompileTimeError extends Error {
642637

643638
@patch
644639
class NoSuchMethodError {
645-
final Object _receiver;
640+
final Object? _receiver;
646641
final Symbol _memberName;
647-
final List _arguments;
648-
final Map<Symbol, dynamic> _namedArguments;
649-
final List _existingArgumentNames;
650-
final Invocation _invocation;
642+
final List? _arguments;
643+
final Map<Symbol, dynamic>? _namedArguments;
644+
final List? _existingArgumentNames;
645+
final Invocation? _invocation;
651646

652647
@patch
653-
NoSuchMethodError(Object receiver, Symbol memberName,
654-
List positionalArguments, Map<Symbol, dynamic> namedArguments,
655-
[List existingArgumentNames = null])
648+
NoSuchMethodError(Object? receiver, Symbol memberName,
649+
List? positionalArguments, Map<Symbol, dynamic>? namedArguments,
650+
[List? existingArgumentNames = null])
656651
: _receiver = receiver,
657652
_memberName = memberName,
658653
_arguments = positionalArguments,
@@ -661,7 +656,7 @@ class NoSuchMethodError {
661656
_invocation = null;
662657

663658
@patch
664-
NoSuchMethodError.withInvocation(Object receiver, Invocation invocation)
659+
NoSuchMethodError.withInvocation(Object? receiver, Invocation invocation)
665660
: _receiver = receiver,
666661
_memberName = invocation.memberName,
667662
_arguments = invocation.positionalArguments,
@@ -673,15 +668,17 @@ class NoSuchMethodError {
673668
String toString() {
674669
StringBuffer sb = StringBuffer('');
675670
String comma = '';
676-
if (_arguments != null) {
677-
for (var argument in _arguments) {
671+
var arguments = _arguments;
672+
if (arguments != null) {
673+
for (var argument in arguments) {
678674
sb.write(comma);
679675
sb.write(Error.safeToString(argument));
680676
comma = ', ';
681677
}
682678
}
683-
if (_namedArguments != null) {
684-
_namedArguments.forEach((Symbol key, var value) {
679+
var namedArguments = _namedArguments;
680+
if (namedArguments != null) {
681+
namedArguments.forEach((Symbol key, var value) {
685682
sb.write(comma);
686683
sb.write(_symbolToString(key));
687684
sb.write(": ");
@@ -692,16 +689,18 @@ class NoSuchMethodError {
692689
String memberName = _symbolToString(_memberName);
693690
String receiverText = Error.safeToString(_receiver);
694691
String actualParameters = '$sb';
695-
var failureMessage = (_invocation is dart.InvocationImpl)
696-
? (_invocation as dart.InvocationImpl).failureMessage
692+
var invocation = _invocation;
693+
var failureMessage = (invocation is dart.InvocationImpl)
694+
? invocation.failureMessage
697695
: 'method not found';
698-
if (_existingArgumentNames == null) {
696+
List? existingArgumentNames = _existingArgumentNames;
697+
if (existingArgumentNames == null) {
699698
return "NoSuchMethodError: '$memberName'\n"
700699
"$failureMessage\n"
701700
"Receiver: ${receiverText}\n"
702701
"Arguments: [$actualParameters]";
703702
} else {
704-
String formalParameters = _existingArgumentNames.join(', ');
703+
String formalParameters = existingArgumentNames.join(', ');
705704
return "NoSuchMethodError: incorrect number of arguments passed to "
706705
"method named '$memberName'\n"
707706
"Receiver: ${receiverText}\n"
@@ -908,7 +907,7 @@ class _BigIntImpl implements BigInt {
908907
* Throws a [FormatException] if the [source] is not a valid integer literal,
909908
* optionally prefixed by a sign.
910909
*/
911-
static _BigIntImpl parse(String source, {int radix}) {
910+
static _BigIntImpl parse(String source, {int? radix}) {
912911
var result = _tryParse(source, radix: radix);
913912
if (result == null) {
914913
throw FormatException("Could not parse BigInt", source);
@@ -1020,7 +1019,7 @@ class _BigIntImpl implements BigInt {
10201019
/// Returns the parsed big integer, or `null` if it failed.
10211020
///
10221021
/// If the [radix] is `null` accepts decimal literals or `0x` hex literals.
1023-
static _BigIntImpl _tryParse(String source, {int radix}) {
1022+
static _BigIntImpl? _tryParse(String source, {int? radix}) {
10241023
if (source == "") return null;
10251024

10261025
var match = _parseRE.firstMatch(source);
@@ -1032,9 +1031,9 @@ class _BigIntImpl implements BigInt {
10321031

10331032
bool isNegative = match[signIndex] == "-";
10341033

1035-
String decimalMatch = match[decimalIndex];
1036-
String hexMatch = match[hexIndex];
1037-
String nonDecimalMatch = match[nonDecimalHexIndex];
1034+
String? decimalMatch = match[decimalIndex];
1035+
String? hexMatch = match[hexIndex];
1036+
String? nonDecimalMatch = match[nonDecimalHexIndex];
10381037

10391038
if (radix == null) {
10401039
if (decimalMatch != null) {
@@ -1048,9 +1047,6 @@ class _BigIntImpl implements BigInt {
10481047
return null;
10491048
}
10501049

1051-
if (radix is! int) {
1052-
throw ArgumentError.value(radix, 'radix', 'is not an integer');
1053-
}
10541050
if (radix < 2 || radix > 36) {
10551051
throw RangeError.range(radix, 2, 36, 'radix');
10561052
}

sdk_nnbd/lib/_internal/js_runtime/lib/core_patch.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,9 @@ class DateTime {
386386
@patch
387387
class Stopwatch {
388388
@patch
389-
static void _initTicker() {
389+
static int _initTicker() {
390390
Primitives.initTicker();
391-
_frequency = Primitives.timerFrequency;
391+
return Primitives.timerFrequency;
392392
}
393393

394394
@patch

0 commit comments

Comments
 (0)