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

Side by Side Diff: tool/input_sdk/private/ddc_runtime/operations.dart

Issue 2026133002: Throw TypeError instead of CastError for type coercions. (Closed) Base URL: https://github.com/dart-lang/dev_compiler@master
Patch Set: Created 4 years, 6 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
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 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 // If strongInstanceOf returns null, convert to false here. 239 // If strongInstanceOf returns null, convert to false here.
240 if (($obj == null) || $strongInstanceOf($obj, $type, true)) return true; 240 if (($obj == null) || $strongInstanceOf($obj, $type, true)) return true;
241 return false; 241 return false;
242 })()'''); 242 })()''');
243 243
244 @JSExportName('is') 244 @JSExportName('is')
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) { 255 cast(obj, type) {
256 if (JS('bool', '# == #', type, dynamic) || obj == null) return obj; 256 if (JS('bool', '# == #', type, dynamic) || obj == null) return obj;
257 bool result = strongInstanceOf(obj, type, true); 257 bool result = strongInstanceOf(obj, type, true);
258 if (JS('bool', '#', result)) return obj; 258 if (JS('bool', '#', result)) return obj;
259 _throwCastError(obj, type, result); 259 _throwCastError(obj, type, result);
260 } 260 }
261 261
262 check(obj, type) {
263 if (JS('bool', '# == #', type, dynamic) || obj == null) return obj;
264 bool result = strongInstanceOf(obj, type, true);
265 if (JS('bool', '#', result)) return obj;
266 _throwTypeError(obj, type, result);
267 }
268
262 bool test(obj) { 269 bool test(obj) {
263 if (obj is bool) return obj; 270 if (obj is bool) return obj;
264 return booleanConversionFailed(obj); 271 return booleanConversionFailed(obj);
265 } 272 }
266 273
267 bool booleanConversionFailed(obj) { 274 bool booleanConversionFailed(obj) {
268 if (obj == null) { 275 if (obj == null) {
269 throw new BooleanConversionAssertionError(); 276 throw new BooleanConversionAssertionError();
270 } 277 }
271 var actual = getReifiedType(obj); 278 var actual = getReifiedType(obj);
272 var expected = JS('', '#', bool); 279 var expected = JS('', '#', bool);
273 throw new TypeErrorImplementation.fromMessage( 280 throw new TypeErrorImplementation.fromMessage(
274 "type '${typeName(actual)}' is not a subtype of " 281 "type '${typeName(actual)}' is not a subtype of "
275 "type '${typeName(expected)}' in boolean expression"); 282 "type '${typeName(expected)}' in boolean expression");
276 } 283 }
277 284
278 void _throwCastError(obj, type, bool result) { 285 void _throwCastError(obj, type, bool result) {
279 var actual = getReifiedType(obj); 286 var actual = getReifiedType(obj);
280 if (result == false) throwCastError(actual, type); 287 if (result == false) throwCastError(obj, actual, type);
281 288
282 throwStrongModeError('Strong mode cast failure from ' + 289 throwStrongModeCastError(obj, actual, type);
283 typeName(actual) + ' to ' + typeName(type)); 290 }
291
292 void _throwTypeError(obj, type, bool result) {
293 var actual = getReifiedType(obj);
294 if (result == false) throwTypeError(obj, actual, type);
295
296 throwStrongModeTypeError(obj, actual, type);
284 } 297 }
285 298
286 asInt(obj) { 299 asInt(obj) {
287 if (obj == null) return null; 300 if (obj == null) return null;
288 301
289 if (JS('bool', 'Math.floor(#) != #', obj, obj)) { 302 if (JS('bool', 'Math.floor(#) != #', obj, obj)) {
290 throwCastError(getReifiedType(obj), JS('', '#', int)); 303 throwCastError(obj, getReifiedType(obj), JS('', '#', int));
291 } 304 }
292 return obj; 305 return obj;
293 } 306 }
294 307
295 equals(x, y) => JS('', '''(() => { 308 equals(x, y) => JS('', '''(() => {
296 if ($x == null || $y == null) return $x == $y; 309 if ($x == null || $y == null) return $x == $y;
297 let eq = $x['==']; 310 let eq = $x['=='];
298 return eq ? eq.call($x, $y) : $x === $y; 311 return eq ? eq.call($x, $y) : $x === $y;
299 })()'''); 312 })()''');
300 313
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 constructor(dartIterator) { 600 constructor(dartIterator) {
588 this.dartIterator = dartIterator; 601 this.dartIterator = dartIterator;
589 } 602 }
590 next() { 603 next() {
591 let i = this.dartIterator; 604 let i = this.dartIterator;
592 let done = !i.moveNext(); 605 let done = !i.moveNext();
593 return { done: done, value: done ? void 0 : i.current }; 606 return { done: done, value: done ? void 0 : i.current };
594 } 607 }
595 } 608 }
596 '''); 609 ''');
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698