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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « test/codegen/expect/notnull.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /// This library defines runtime operations on objects used by the code 5 /// This library defines runtime operations on objects used by the code
6 /// generator. 6 /// generator.
7 part of dart._runtime; 7 part of dart._runtime;
8 8
9 _canonicalFieldName(obj, name, args, displayName) => JS('', '''(() => { 9 _canonicalFieldName(obj, name, args, displayName) => JS('', '''(() => {
10 $name = $canonicalMember($obj, $name); 10 $name = $canonicalMember($obj, $name);
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 } 214 }
215 return false; 215 return false;
216 }); 216 });
217 })()'''); 217 })()''');
218 218
219 /// Returns true if [obj] is an instance of [type] 219 /// Returns true if [obj] is an instance of [type]
220 /// Returns false if [obj] is not an instance of [type] in both spec 220 /// Returns false if [obj] is not an instance of [type] in both spec
221 /// and strong mode 221 /// and strong mode
222 /// Returns null if [obj] is not an instance of [type] in strong mode 222 /// Returns null if [obj] is not an instance of [type] in strong mode
223 /// but might be in spec mode 223 /// but might be in spec mode
224 strongInstanceOf(obj, type, ignoreFromWhiteList) => JS('', '''(() => { 224 bool strongInstanceOf(obj, type, ignoreFromWhiteList) => JS('', '''(() => {
225 let actual = $getReifiedType($obj); 225 let actual = $getReifiedType($obj);
226 let result = $isSubtype(actual, $type); 226 let result = $isSubtype(actual, $type);
227 if (result || actual == $jsobject || 227 if (result || actual == $jsobject ||
228 actual == $int && type == $double) return true; 228 actual == $int && type == $double) return true;
229 if (result === false) return false; 229 if (result === false) return false;
230 if ($ignoreFromWhiteList == void 0) return result; 230 if ($ignoreFromWhiteList == void 0) return result;
231 if ($_ignoreTypeFailure(actual, $type)) return true; 231 if ($_ignoreTypeFailure(actual, $type)) return true;
232 return result; 232 return result;
233 })()'''); 233 })()''');
234 234
(...skipping 10 matching lines...) Expand all
245 instanceOf(obj, type) => JS('', '''(() => { 245 instanceOf(obj, type) => JS('', '''(() => {
246 let result = $strongInstanceOf($obj, $type); 246 let result = $strongInstanceOf($obj, $type);
247 if (result !== null) return result; 247 if (result !== null) return result;
248 let actual = $getReifiedType($obj); 248 let actual = $getReifiedType($obj);
249 $throwStrongModeError('Strong mode is check failure: ' + 249 $throwStrongModeError('Strong mode is check failure: ' +
250 $typeName(actual) + ' does not soundly subtype ' + 250 $typeName(actual) + ' does not soundly subtype ' +
251 $typeName($type)); 251 $typeName($type));
252 })()'''); 252 })()''');
253 253
254 @JSExportName('as') 254 @JSExportName('as')
255 cast(obj, type) => JS('', '''(() => { 255 cast(obj, type) {
256 if ($obj == null) return $obj; 256 if (obj == null) return obj;
257 257
258 let result = $strongInstanceOf($obj, $type, true); 258 bool result = strongInstanceOf(obj, type, true);
259 if (result) return $obj; 259 if (JS('bool', '#', result)) return obj;
260 _throwCastError(obj, type, result);
261 }
260 262
261 let actual = $getReifiedType($obj); 263 bool test(obj) {
264 if (JS('bool', 'typeof # == "boolean"', obj)) return JS('bool', '#', obj);
265 throwCastError(getReifiedType(obj), JS('', '#', bool));
266 }
262 267
263 if (result === false) $throwCastError(actual, $type); 268 void _throwCastError(obj, type, bool result) {
269 var actual = getReifiedType(obj);
270 if (result == false) throwCastError(actual, type);
264 271
265 $throwStrongModeError('Strong mode cast failure from ' + 272 throwStrongModeError('Strong mode cast failure from ' +
266 $typeName(actual) + ' to ' + $typeName($type)); 273 typeName(actual) + ' to ' + typeName(type));
267 })()'''); 274 }
268 275
269 asInt(obj) => JS('', '''(() => { 276 asInt(obj) {
270 if ($obj == null) { 277 if (obj == null) return null;
271 return null; 278
279 if (JS('bool', 'Math.floor(#) != #', obj, obj)) {
280 throwCastError(getReifiedType(obj), int);
272 } 281 }
273 if (Math.floor($obj) != $obj) { 282 return obj;
274 // Note: null will also be caught by this check 283 }
275 $throwCastError($getReifiedType($obj), $int);
276 }
277 return $obj;
278 })()''');
279
280 arity(f) => JS('', '''(() => {
281 // TODO(jmesserly): need to parse optional params.
282 // In ES6, length is the number of required arguments.
283 return { min: $f.length, max: $f.length };
284 })()''');
285 284
286 equals(x, y) => JS('', '''(() => { 285 equals(x, y) => JS('', '''(() => {
287 if ($x == null || $y == null) return $x == $y; 286 if ($x == null || $y == null) return $x == $y;
288 let eq = $x['==']; 287 let eq = $x['=='];
289 return eq ? eq.call($x, $y) : $x === $y; 288 return eq ? eq.call($x, $y) : $x === $y;
290 })()'''); 289 })()''');
291 290
292 /// Checks that `x` is not null or undefined. */ 291 /// Checks that `x` is not null or undefined. */
293 notNull(x) { 292 notNull(x) {
294 if (x == null) throwNullValueError(); 293 if (x == null) throwNullValueError();
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 constructor(dartIterator) { 504 constructor(dartIterator) {
506 this.dartIterator = dartIterator; 505 this.dartIterator = dartIterator;
507 } 506 }
508 next() { 507 next() {
509 let i = this.dartIterator; 508 let i = this.dartIterator;
510 let done = !i.moveNext(); 509 let done = !i.moveNext();
511 return { done: done, value: done ? void 0 : i.current }; 510 return { done: done, value: done ? void 0 : i.current };
512 } 511 }
513 } 512 }
514 '''); 513 ''');
OLDNEW
« no previous file with comments | « test/codegen/expect/notnull.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698