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

Side by Side Diff: runtime/vm/exceptions.cc

Issue 18531003: Cleanup VM error handling. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix typo. Created 7 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « runtime/vm/exceptions.h ('k') | runtime/vm/object.cc » ('j') | 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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 #include "vm/exceptions.h" 5 #include "vm/exceptions.h"
6 6
7 #include "vm/dart_api_impl.h" 7 #include "vm/dart_api_impl.h"
8 #include "vm/dart_entry.h" 8 #include "vm/dart_entry.h"
9 #include "vm/debugger.h" 9 #include "vm/debugger.h"
10 #include "vm/flags.h" 10 #include "vm/flags.h"
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 const Class& cls, 406 const Class& cls,
407 const char* field_name, 407 const char* field_name,
408 const Object& value) { 408 const Object& value) {
409 const Field& field = Field::Handle(cls.LookupInstanceField( 409 const Field& field = Field::Handle(cls.LookupInstanceField(
410 String::Handle(Symbols::New(field_name)))); 410 String::Handle(Symbols::New(field_name))));
411 ASSERT(!field.IsNull()); 411 ASSERT(!field.IsNull());
412 instance.SetField(field, value); 412 instance.SetField(field, value);
413 } 413 }
414 414
415 415
416 // Initialize the fields 'url', 'line', and 'column' in the given instance
417 // according to the given token location in the given script.
418 void Exceptions::SetLocationFields(const Instance& instance,
419 const Class& cls,
420 const Script& script,
421 intptr_t location) {
422 SetField(instance, cls, "url", String::Handle(script.url()));
423 intptr_t line, column;
424 script.GetTokenLocation(location, &line, &column);
425 SetField(instance, cls, "line", Smi::Handle(Smi::New(line)));
426 SetField(instance, cls, "column", Smi::Handle(Smi::New(column)));
427 }
428
429
430 // Allocate, initialize, and throw a TypeError. 416 // Allocate, initialize, and throw a TypeError.
431 void Exceptions::CreateAndThrowTypeError(intptr_t location, 417 void Exceptions::CreateAndThrowTypeError(intptr_t location,
432 const String& src_type_name, 418 const String& src_type_name,
433 const String& dst_type_name, 419 const String& dst_type_name,
434 const String& dst_name, 420 const String& dst_name,
435 const String& malformed_error) { 421 const String& malformed_error) {
436 // Allocate a new instance of TypeError or CastError. 422 const Array& args = Array::Handle(Array::New(8));
437 Instance& type_error = Instance::Handle();
438 Class& cls = Class::Handle();
439 if (dst_name.Equals(kCastErrorDstName)) {
440 type_error = NewInstance("CastErrorImplementation");
441 cls = type_error.clazz();
442 cls = cls.SuperClass();
443 } else {
444 type_error = NewInstance("TypeErrorImplementation");
445 cls = type_error.clazz();
446 }
447 423
448 // Initialize 'url', 'line', and 'column' fields. 424 ExceptionType exception_type =
449 DartFrameIterator iterator; 425 dst_name.Equals(kCastErrorDstName) ? kCast : kType;
450 const Script& script = Script::Handle(GetCallerScript(&iterator));
451 // Location fields are defined in AssertionError, the superclass of TypeError.
452 const Class& assertion_error_class = Class::Handle(cls.SuperClass());
453 SetLocationFields(type_error, assertion_error_class, script, location);
454 426
455 // Initialize field 'failedAssertion' in AssertionError superclass. 427 // Initialize argument 'failedAssertion'.
456 // Printing the src_obj value would be possible, but ToString() is expensive 428 // Printing the src_obj value would be possible, but ToString() is expensive
457 // and not meaningful for all classes, so we just print '$expr instanceof...'. 429 // and not meaningful for all classes, so we just print '$expr instanceof...'.
458 // Users should look at TypeError.ToString(), which contains more useful 430 // Users should look at TypeError.ToString(), which contains more useful
459 // information than AssertionError.failedAssertion. 431 // information than AssertionError.failedAssertion.
460 String& failed_assertion = String::Handle(String::New("$expr instanceof ")); 432 String& failed_assertion = String::Handle(String::New("$expr instanceof "));
461 failed_assertion = String::Concat(failed_assertion, dst_type_name); 433 failed_assertion = String::Concat(failed_assertion, dst_type_name);
462 SetField(type_error, 434 args.SetAt(0, failed_assertion);
463 assertion_error_class,
464 "failedAssertion",
465 failed_assertion);
466 435
467 // Initialize field 'srcType'. 436 // Initialize 'url', 'line', and 'column' arguments.
468 SetField(type_error, cls, "srcType", src_type_name); 437 DartFrameIterator iterator;
438 const Script& script = Script::Handle(GetCallerScript(&iterator));
439 intptr_t line, column;
440 script.GetTokenLocation(location, &line, &column);
441 args.SetAt(1, String::Handle(script.url()));
442 args.SetAt(2, Smi::Handle(Smi::New(line)));
443 args.SetAt(3, Smi::Handle(Smi::New(column)));
469 444
470 // Initialize field 'dstType'. 445 // Initialize argument 'srcType'.
471 SetField(type_error, cls, "dstType", dst_type_name); 446 args.SetAt(4, src_type_name);
472 447 args.SetAt(5, dst_type_name);
473 // Initialize field 'dstName'. 448 args.SetAt(6, dst_name);
474 SetField(type_error, cls, "dstName", dst_name); 449 args.SetAt(7, malformed_error);
475
476 // Initialize field 'malformedError'.
477 SetField(type_error, cls, "malformedError", malformed_error);
478 450
479 // Type errors in the core library may be difficult to diagnose. 451 // Type errors in the core library may be difficult to diagnose.
480 // Print type error information before throwing the error when debugging. 452 // Print type error information before throwing the error when debugging.
481 if (FLAG_print_stacktrace_at_throw) { 453 if (FLAG_print_stacktrace_at_throw) {
482 if (!malformed_error.IsNull()) { 454 if (!malformed_error.IsNull()) {
483 OS::Print("%s\n", malformed_error.ToCString()); 455 OS::Print("%s\n", malformed_error.ToCString());
484 } 456 }
485 intptr_t line, column; 457 intptr_t line, column;
486 script.GetTokenLocation(location, &line, &column); 458 script.GetTokenLocation(location, &line, &column);
487 OS::Print("'%s': Failed type check: line %"Pd" pos %"Pd": ", 459 OS::Print("'%s': Failed type check: line %"Pd" pos %"Pd": ",
488 String::Handle(script.url()).ToCString(), line, column); 460 String::Handle(script.url()).ToCString(), line, column);
489 if (!dst_name.IsNull() && (dst_name.Length() > 0)) { 461 if (!dst_name.IsNull() && (dst_name.Length() > 0)) {
490 OS::Print("type '%s' is not a subtype of type '%s' of '%s'.\n", 462 OS::Print("type '%s' is not a subtype of type '%s' of '%s'.\n",
491 src_type_name.ToCString(), 463 src_type_name.ToCString(),
492 dst_type_name.ToCString(), 464 dst_type_name.ToCString(),
493 dst_name.ToCString()); 465 dst_name.ToCString());
494 } else { 466 } else {
495 OS::Print("malformed type used.\n"); 467 OS::Print("malformed type used.\n");
496 } 468 }
497 } 469 }
498 // Throw TypeError instance. 470 // Throw TypeError instance.
499 Exceptions::Throw(type_error); 471 Exceptions::ThrowByType(exception_type, args);
500 UNREACHABLE(); 472 UNREACHABLE();
501 } 473 }
502 474
503 475
504 void Exceptions::Throw(const Instance& exception) { 476 void Exceptions::Throw(const Instance& exception) {
505 Isolate* isolate = Isolate::Current(); 477 Isolate* isolate = Isolate::Current();
506 isolate->debugger()->SignalExceptionThrown(exception); 478 isolate->debugger()->SignalExceptionThrown(exception);
507 // Null object is a valid exception object. 479 // Null object is a valid exception object.
508 ThrowExceptionHelper(exception, Instance::Handle(isolate)); 480 ThrowExceptionHelper(exception, Instance::Handle(isolate));
509 } 481 }
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 class_name = &Symbols::IsolateSpawnException(); 591 class_name = &Symbols::IsolateSpawnException();
620 break; 592 break;
621 case kIsolateUnhandledException: 593 case kIsolateUnhandledException:
622 library = Library::IsolateLibrary(); 594 library = Library::IsolateLibrary();
623 class_name = &Symbols::IsolateUnhandledException(); 595 class_name = &Symbols::IsolateUnhandledException();
624 break; 596 break;
625 case kFiftyThreeBitOverflowError: 597 case kFiftyThreeBitOverflowError:
626 library = Library::CoreLibrary(); 598 library = Library::CoreLibrary();
627 class_name = &Symbols::FiftyThreeBitOverflowError(); 599 class_name = &Symbols::FiftyThreeBitOverflowError();
628 break; 600 break;
601 case kAssertion:
602 library = Library::CoreLibrary();
603 class_name = &Symbols::AssertionError();
604 break;
605 case kCast:
606 library = Library::CoreLibrary();
607 class_name = &Symbols::CastError();
608 break;
609 case kType:
610 library = Library::CoreLibrary();
611 class_name = &Symbols::TypeError();
612 break;
613 case kFallThrough:
614 library = Library::CoreLibrary();
615 class_name = &Symbols::FallThroughError();
616 break;
617 case kAbstractClassInstantiation:
618 library = Library::CoreLibrary();
619 class_name = &Symbols::AbstractClassInstantiationError();
620 break;
629 } 621 }
630 622
631 return DartLibraryCalls::ExceptionCreate(library, 623 return DartLibraryCalls::ExceptionCreate(library,
632 *class_name, 624 *class_name,
633 *constructor_name, 625 *constructor_name,
634 arguments); 626 arguments);
635 } 627 }
636 628
637 } // namespace dart 629 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/exceptions.h ('k') | runtime/vm/object.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698