Skip to content

Commit

Permalink
[dartdevc] Update is, as, and _check methods for null safety
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
nshahan authored and commit-bot@chromium.org committed Jan 2, 2020
1 parent 3aa23f8 commit 2f57602
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
34 changes: 17 additions & 17 deletions sdk_nnbd/lib/_internal/js_dev_runtime/patch/core_patch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ class Object {

// Everything is an Object.
@JSExportName('is')
static bool _is_Object(Object o) => true;
static bool _is_Object(Object o) => o != null;

@JSExportName('as')
static Object _as_Object(Object o) => o;
static Object _as_Object(Object o) =>
o == null ? dart.cast(o, dart.unwrapType(Object), false) : o;

@JSExportName('_check')
static Object _check_Object(Object o) => o;
static Object _check_Object(Object o) =>
o == null ? dart.cast(o, dart.unwrapType(Object), true) : o;
}

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

@JSExportName('_check')
static Object _check_Function(Object o) {
// Avoid extra function call to core.Function.is() by manually inlining.
if (JS<bool>('!', 'typeof $o == "function"') || o == null) return o;
if (JS<bool>('!', 'typeof $o == "function"')) return o;
return dart.cast(o, dart.unwrapType(Function), true);
}
}
Expand Down Expand Up @@ -211,8 +213,7 @@ class int {
@JSExportName('as')
static Object _as_int(Object o) {
// Avoid extra function call to core.int.is() by manually inlining.
if (JS<bool>('!', '(typeof $o == "number" && Math.floor($o) == $o)') ||
o == null) {
if (JS<bool>('!', '(typeof $o == "number" && Math.floor($o) == $o)')) {
return o;
}
return dart.cast(o, dart.unwrapType(int), false);
Expand All @@ -221,8 +222,7 @@ class int {
@JSExportName('_check')
static Object _check_int(Object o) {
// Avoid extra function call to core.int.is() by manually inlining.
if (JS<bool>('!', '(typeof $o == "number" && Math.floor($o) == $o)') ||
o == null) {
if (JS<bool>('!', '(typeof $o == "number" && Math.floor($o) == $o)')) {
return o;
}
return dart.cast(o, dart.unwrapType(int), true);
Expand Down Expand Up @@ -250,14 +250,14 @@ class double {
@JSExportName('as')
static Object _as_double(o) {
// Avoid extra function call to core.double.is() by manually inlining.
if (JS<bool>('!', 'typeof $o == "number"') || o == null) return o;
if (JS<bool>('!', 'typeof $o == "number"')) return o;
return dart.cast(o, dart.unwrapType(double), false);
}

@JSExportName('_check')
static Object _check_double(o) {
// Avoid extra function call to core.double.is() by manually inlining.
if (JS<bool>('!', 'typeof $o == "number"') || o == null) return o;
if (JS<bool>('!', 'typeof $o == "number"')) return o;
return dart.cast(o, dart.unwrapType(double), true);
}
}
Expand All @@ -272,14 +272,14 @@ abstract class num implements Comparable<num> {
@JSExportName('as')
static Object _as_num(o) {
// Avoid extra function call to core.num.is() by manually inlining.
if (JS<bool>('!', 'typeof $o == "number"') || o == null) return o;
if (JS<bool>('!', 'typeof $o == "number"')) return o;
return dart.cast(o, dart.unwrapType(num), false);
}

@JSExportName('_check')
static Object _check_num(o) {
// Avoid extra function call to core.num.is() by manually inlining.
if (JS<bool>('!', 'typeof $o == "number"') || o == null) return o;
if (JS<bool>('!', 'typeof $o == "number"')) return o;
return dart.cast(o, dart.unwrapType(num), true);
}
}
Expand Down Expand Up @@ -652,14 +652,14 @@ class String {
@JSExportName('as')
static Object _as_String(Object o) {
// Avoid extra function call to core.String.is() by manually inlining.
if (JS<bool>('!', 'typeof $o == "string"') || o == null) return o;
if (JS<bool>('!', 'typeof $o == "string"')) return o;
return dart.cast(o, dart.unwrapType(String), false);
}

@JSExportName('_check')
static Object _check_String(Object o) {
// Avoid extra function call to core.String.is() by manually inlining.
if (JS<bool>('!', 'typeof $o == "string"') || o == null) return o;
if (JS<bool>('!', 'typeof $o == "string"')) return o;
return dart.cast(o, dart.unwrapType(String), true);
}
}
Expand All @@ -683,14 +683,14 @@ class bool {
@JSExportName('as')
static Object _as_bool(Object o) {
// Avoid extra function call to core.bool.is() by manually inlining.
if (JS<bool>("!", '$o === true || $o === false') || o == null) return o;
if (JS<bool>("!", '$o === true || $o === false')) return o;
return dart.cast(o, dart.unwrapType(bool), false);
}

@JSExportName('_check')
static Object _check_bool(Object o) {
// Avoid extra function call to core.bool.is() by manually inlining.
if (JS<bool>("!", '$o === true || $o === false') || o == null) return o;
if (JS<bool>("!", '$o === true || $o === false')) return o;
return dart.cast(o, dart.unwrapType(bool), true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ addTypeTests(ctor, isClass) {
JS(
'',
'''#.as = function as_C(obj) {
if (obj == null || obj[#]) return obj;
if (obj != null && obj[#]) return obj;
return #(obj, this, false);
}''',
ctor,
Expand All @@ -517,7 +517,7 @@ addTypeTests(ctor, isClass) {
JS(
'',
'''#._check = function check_C(obj) {
if (obj == null || obj[#]) return obj;
if (obj != null && obj[#]) return obj;
return #(obj, this, true);
}''',
ctor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ bool instanceOf(obj, type) {

@JSExportName('as')
cast(obj, type, @notNull bool isImplicit) {
if (obj == null) return obj;
if (obj == null && (_isNullable(type) || _isNullType(type))) return obj;
var actual = getReifiedType(obj);
if (isSubtypeOf(actual, type)) {
return obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,19 @@ class NullableType extends DartType {

@override
String toString() => name;

@JSExportName('is')
bool is_T(obj) => obj == null || JS<bool>('!', '#.is(#)', type, obj);

@JSExportName('as')
as_T(obj) => obj == null || JS<bool>('!', '#.is(#)', type, obj)
? obj
: cast(obj, this, false);

@JSExportName('_check')
check_T(obj) => obj == null || JS<bool>('!', '#.is(#)', type, obj)
? obj
: cast(obj, this, true);
}

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

@override
String toString() => name;

@JSExportName('is')
bool is_T(obj) => obj != null && JS<bool>('!', '#.is(#)', type, obj);

@JSExportName('as')
as_T(obj) => obj == null || JS<bool>('!', '#.is(#)', type, obj)
? obj
: cast(obj, this, false);

@JSExportName('_check')
check_T(obj) => obj == null || JS<bool>('!', '#.is(#)', type, obj)
? obj
: cast(obj, this, true);
}

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

0 comments on commit 2f57602

Please sign in to comment.