Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Unified Diff: tool/input_sdk/private/ddc_runtime/operations.dart

Issue 1964263002: fuse some null checks with type checks, introduce a special bool variant (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« test/codegen/expect/notnull.js ('K') | « test/codegen/expect/notnull.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tool/input_sdk/private/ddc_runtime/operations.dart
diff --git a/tool/input_sdk/private/ddc_runtime/operations.dart b/tool/input_sdk/private/ddc_runtime/operations.dart
index 9f4891f4158adf4e6562d5a6907fb0245a4484bc..48c76ac8c342f72413daff30953b4f847d9e3258 100644
--- a/tool/input_sdk/private/ddc_runtime/operations.dart
+++ b/tool/input_sdk/private/ddc_runtime/operations.dart
@@ -221,7 +221,7 @@ final _ignoreTypeFailure = JS('', '''(() => {
/// and strong mode
/// Returns null if [obj] is not an instance of [type] in strong mode
/// but might be in spec mode
-strongInstanceOf(obj, type, ignoreFromWhiteList) => JS('', '''(() => {
+bool strongInstanceOf(obj, type, ignoreFromWhiteList) => JS('', '''(() => {
let actual = $getReifiedType($obj);
let result = $isSubtype(actual, $type);
if (result || actual == $jsobject ||
@@ -252,36 +252,43 @@ instanceOf(obj, type) => JS('', '''(() => {
})()''');
@JSExportName('as')
-cast(obj, type) => JS('', '''(() => {
- if ($obj == null) return $obj;
+cast(obj, type) {
+ if (obj == null) return obj;
- let result = $strongInstanceOf($obj, $type, true);
- if (result) return $obj;
+ bool result = strongInstanceOf(obj, type, true);
+ if (JS('bool', '#', result)) return obj;
+ _throwCastError(obj, type, result);
+}
- let actual = $getReifiedType($obj);
+asNotNull(obj, type) {
+ if (obj == null) throwNullValueError();
- if (result === false) $throwCastError(actual, $type);
+ var result = strongInstanceOf(obj, type, true);
+ if (JS('bool', '#', result)) return obj;
+ _throwCastError(obj, type, result);
+}
- $throwStrongModeError('Strong mode cast failure from ' +
- $typeName(actual) + ' to ' + $typeName($type));
-})()''');
+bool test(obj) {
+ if (JS('bool', 'typeof # == "boolean"', obj)) return JS('bool', '#', obj);
+ throwCastError(getReifiedType(obj), JS('', '#', bool));
+}
-asInt(obj) => JS('', '''(() => {
- if ($obj == null) {
- return null;
- }
- if (Math.floor($obj) != $obj) {
- // Note: null will also be caught by this check
- $throwCastError($getReifiedType($obj), $int);
- }
- return $obj;
-})()''');
+void _throwCastError(obj, type, bool result) {
+ var actual = getReifiedType(obj);
+ if (result == false) throwCastError(actual, type);
-arity(f) => JS('', '''(() => {
- // TODO(jmesserly): need to parse optional params.
- // In ES6, length is the number of required arguments.
- return { min: $f.length, max: $f.length };
-})()''');
+ throwStrongModeError('Strong mode cast failure from ' +
+ typeName(actual) + ' to ' + typeName(type));
+}
+
+asInt(obj) {
+ if (obj == null) return null;
+
+ if (JS('bool', 'Math.floor(#) != #', obj, obj)) {
+ throwCastError(getReifiedType(obj), int);
+ }
+ return obj;
+}
equals(x, y) => JS('', '''(() => {
if ($x == null || $y == null) return $x == $y;
« test/codegen/expect/notnull.js ('K') | « test/codegen/expect/notnull.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698