Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2f57602

Browse files
nshahancommit-bot@chromium.org
authored andcommitted
[dartdevc] Update is, as, and _check methods for null safety
Change-Id: I67e97c988a0c7a0e9d04e84e85c07152f8f35306 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/129202 Reviewed-by: Sigmund Cherem <sigmund@google.com> Commit-Queue: Nicholas Shahan <nshahan@google.com>
1 parent 3aa23f8 commit 2f57602

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ class Object {
6666

6767
// Everything is an Object.
6868
@JSExportName('is')
69-
static bool _is_Object(Object o) => true;
69+
static bool _is_Object(Object o) => o != null;
7070

7171
@JSExportName('as')
72-
static Object _as_Object(Object o) => o;
72+
static Object _as_Object(Object o) =>
73+
o == null ? dart.cast(o, dart.unwrapType(Object), false) : o;
7374

7475
@JSExportName('_check')
75-
static Object _check_Object(Object o) => o;
76+
static Object _check_Object(Object o) =>
77+
o == null ? dart.cast(o, dart.unwrapType(Object), true) : o;
7678
}
7779

7880
@patch
@@ -132,14 +134,14 @@ class Function {
132134
@JSExportName('as')
133135
static Object _as_Function(Object o) {
134136
// Avoid extra function call to core.Function.is() by manually inlining.
135-
if (JS<bool>('!', 'typeof $o == "function"') || o == null) return o;
137+
if (JS<bool>('!', 'typeof $o == "function"')) return o;
136138
return dart.cast(o, dart.unwrapType(Function), false);
137139
}
138140

139141
@JSExportName('_check')
140142
static Object _check_Function(Object o) {
141143
// Avoid extra function call to core.Function.is() by manually inlining.
142-
if (JS<bool>('!', 'typeof $o == "function"') || o == null) return o;
144+
if (JS<bool>('!', 'typeof $o == "function"')) return o;
143145
return dart.cast(o, dart.unwrapType(Function), true);
144146
}
145147
}
@@ -211,8 +213,7 @@ class int {
211213
@JSExportName('as')
212214
static Object _as_int(Object o) {
213215
// Avoid extra function call to core.int.is() by manually inlining.
214-
if (JS<bool>('!', '(typeof $o == "number" && Math.floor($o) == $o)') ||
215-
o == null) {
216+
if (JS<bool>('!', '(typeof $o == "number" && Math.floor($o) == $o)')) {
216217
return o;
217218
}
218219
return dart.cast(o, dart.unwrapType(int), false);
@@ -221,8 +222,7 @@ class int {
221222
@JSExportName('_check')
222223
static Object _check_int(Object o) {
223224
// Avoid extra function call to core.int.is() by manually inlining.
224-
if (JS<bool>('!', '(typeof $o == "number" && Math.floor($o) == $o)') ||
225-
o == null) {
225+
if (JS<bool>('!', '(typeof $o == "number" && Math.floor($o) == $o)')) {
226226
return o;
227227
}
228228
return dart.cast(o, dart.unwrapType(int), true);
@@ -250,14 +250,14 @@ class double {
250250
@JSExportName('as')
251251
static Object _as_double(o) {
252252
// Avoid extra function call to core.double.is() by manually inlining.
253-
if (JS<bool>('!', 'typeof $o == "number"') || o == null) return o;
253+
if (JS<bool>('!', 'typeof $o == "number"')) return o;
254254
return dart.cast(o, dart.unwrapType(double), false);
255255
}
256256

257257
@JSExportName('_check')
258258
static Object _check_double(o) {
259259
// Avoid extra function call to core.double.is() by manually inlining.
260-
if (JS<bool>('!', 'typeof $o == "number"') || o == null) return o;
260+
if (JS<bool>('!', 'typeof $o == "number"')) return o;
261261
return dart.cast(o, dart.unwrapType(double), true);
262262
}
263263
}
@@ -272,14 +272,14 @@ abstract class num implements Comparable<num> {
272272
@JSExportName('as')
273273
static Object _as_num(o) {
274274
// Avoid extra function call to core.num.is() by manually inlining.
275-
if (JS<bool>('!', 'typeof $o == "number"') || o == null) return o;
275+
if (JS<bool>('!', 'typeof $o == "number"')) return o;
276276
return dart.cast(o, dart.unwrapType(num), false);
277277
}
278278

279279
@JSExportName('_check')
280280
static Object _check_num(o) {
281281
// Avoid extra function call to core.num.is() by manually inlining.
282-
if (JS<bool>('!', 'typeof $o == "number"') || o == null) return o;
282+
if (JS<bool>('!', 'typeof $o == "number"')) return o;
283283
return dart.cast(o, dart.unwrapType(num), true);
284284
}
285285
}
@@ -652,14 +652,14 @@ class String {
652652
@JSExportName('as')
653653
static Object _as_String(Object o) {
654654
// Avoid extra function call to core.String.is() by manually inlining.
655-
if (JS<bool>('!', 'typeof $o == "string"') || o == null) return o;
655+
if (JS<bool>('!', 'typeof $o == "string"')) return o;
656656
return dart.cast(o, dart.unwrapType(String), false);
657657
}
658658

659659
@JSExportName('_check')
660660
static Object _check_String(Object o) {
661661
// Avoid extra function call to core.String.is() by manually inlining.
662-
if (JS<bool>('!', 'typeof $o == "string"') || o == null) return o;
662+
if (JS<bool>('!', 'typeof $o == "string"')) return o;
663663
return dart.cast(o, dart.unwrapType(String), true);
664664
}
665665
}
@@ -683,14 +683,14 @@ class bool {
683683
@JSExportName('as')
684684
static Object _as_bool(Object o) {
685685
// Avoid extra function call to core.bool.is() by manually inlining.
686-
if (JS<bool>("!", '$o === true || $o === false') || o == null) return o;
686+
if (JS<bool>("!", '$o === true || $o === false')) return o;
687687
return dart.cast(o, dart.unwrapType(bool), false);
688688
}
689689

690690
@JSExportName('_check')
691691
static Object _check_bool(Object o) {
692692
// Avoid extra function call to core.bool.is() by manually inlining.
693-
if (JS<bool>("!", '$o === true || $o === false') || o == null) return o;
693+
if (JS<bool>("!", '$o === true || $o === false')) return o;
694694
return dart.cast(o, dart.unwrapType(bool), true);
695695
}
696696
}

sdk_nnbd/lib/_internal/js_dev_runtime/private/ddc_runtime/classes.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ addTypeTests(ctor, isClass) {
508508
JS(
509509
'',
510510
'''#.as = function as_C(obj) {
511-
if (obj == null || obj[#]) return obj;
511+
if (obj != null && obj[#]) return obj;
512512
return #(obj, this, false);
513513
}''',
514514
ctor,
@@ -517,7 +517,7 @@ addTypeTests(ctor, isClass) {
517517
JS(
518518
'',
519519
'''#._check = function check_C(obj) {
520-
if (obj == null || obj[#]) return obj;
520+
if (obj != null && obj[#]) return obj;
521521
return #(obj, this, true);
522522
}''',
523523
ctor,

sdk_nnbd/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ bool instanceOf(obj, type) {
428428

429429
@JSExportName('as')
430430
cast(obj, type, @notNull bool isImplicit) {
431-
if (obj == null) return obj;
431+
if (obj == null && (_isNullable(type) || _isNullType(type))) return obj;
432432
var actual = getReifiedType(obj);
433433
if (isSubtypeOf(actual, type)) {
434434
return obj;

sdk_nnbd/lib/_internal/js_dev_runtime/private/ddc_runtime/types.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,19 @@ class NullableType extends DartType {
292292

293293
@override
294294
String toString() => name;
295+
296+
@JSExportName('is')
297+
bool is_T(obj) => obj == null || JS<bool>('!', '#.is(#)', type, obj);
298+
299+
@JSExportName('as')
300+
as_T(obj) => obj == null || JS<bool>('!', '#.is(#)', type, obj)
301+
? obj
302+
: cast(obj, this, false);
303+
304+
@JSExportName('_check')
305+
check_T(obj) => obj == null || JS<bool>('!', '#.is(#)', type, obj)
306+
? obj
307+
: cast(obj, this, true);
295308
}
296309

297310
/// A wrapper to identify a legacy (star, *) type of the form [type]*.
@@ -307,6 +320,19 @@ class LegacyType extends DartType {
307320

308321
@override
309322
String toString() => name;
323+
324+
@JSExportName('is')
325+
bool is_T(obj) => obj != null && JS<bool>('!', '#.is(#)', type, obj);
326+
327+
@JSExportName('as')
328+
as_T(obj) => obj == null || JS<bool>('!', '#.is(#)', type, obj)
329+
? obj
330+
: cast(obj, this, false);
331+
332+
@JSExportName('_check')
333+
check_T(obj) => obj == null || JS<bool>('!', '#.is(#)', type, obj)
334+
? obj
335+
: cast(obj, this, true);
310336
}
311337

312338
// TODO(nshahan) Add override optimizations for is, as and _check?

0 commit comments

Comments
 (0)