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

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: Rebase 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
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 416 // Set the fields 'url', 'line', and 'column' in the given instance
417 // according to the given token location in the given script. 417 // according to the given token location in the given script.
418 void Exceptions::SetLocationFields(const Instance& instance, 418 //
419 const Class& cls, 419 // Returns the number of items that have been added to the 'args' array.
420 const Script& script, 420 intptr_t Exceptions::SetLocationFields(const Array& args,
421 intptr_t location) { 421 intptr_t index,
422 SetField(instance, cls, "url", String::Handle(script.url())); 422 const Script& script,
423 intptr_t location) {
424 args.SetAt(index, String::Handle(script.url()));
423 intptr_t line, column; 425 intptr_t line, column;
424 script.GetTokenLocation(location, &line, &column); 426 script.GetTokenLocation(location, &line, &column);
425 SetField(instance, cls, "line", Smi::Handle(Smi::New(line))); 427 args.SetAt(index + 1, Smi::Handle(Smi::New(line)));
426 SetField(instance, cls, "column", Smi::Handle(Smi::New(column))); 428 args.SetAt(index + 2, Smi::Handle(Smi::New(column)));
429 return 3;
427 } 430 }
428 431
429 432
430 // Allocate, initialize, and throw a TypeError. 433 // Allocate, initialize, and throw a TypeError.
431 void Exceptions::CreateAndThrowTypeError(intptr_t location, 434 void Exceptions::CreateAndThrowTypeError(intptr_t location,
432 const String& src_type_name, 435 const String& src_type_name,
433 const String& dst_type_name, 436 const String& dst_type_name,
434 const String& dst_name, 437 const String& dst_name,
435 const String& malformed_error) { 438 const String& malformed_error) {
436 // Allocate a new instance of TypeError or CastError. 439 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 440
448 // Initialize 'url', 'line', and 'column' fields. 441 ExceptionType exception_type =
449 DartFrameIterator iterator; 442 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 443
455 // Initialize field 'failedAssertion' in AssertionError superclass. 444 // Initialize argument 'failedAssertion'.
456 // Printing the src_obj value would be possible, but ToString() is expensive 445 // 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...'. 446 // and not meaningful for all classes, so we just print '$expr instanceof...'.
458 // Users should look at TypeError.ToString(), which contains more useful 447 // Users should look at TypeError.ToString(), which contains more useful
459 // information than AssertionError.failedAssertion. 448 // information than AssertionError.failedAssertion.
460 String& failed_assertion = String::Handle(String::New("$expr instanceof ")); 449 String& failed_assertion = String::Handle(String::New("$expr instanceof "));
461 failed_assertion = String::Concat(failed_assertion, dst_type_name); 450 failed_assertion = String::Concat(failed_assertion, dst_type_name);
462 SetField(type_error, 451 intptr_t args_index = 0;
463 assertion_error_class, 452 args.SetAt(args_index++, failed_assertion);
464 "failedAssertion",
465 failed_assertion);
466 453
467 // Initialize field 'srcType'. 454 // Initialize 'url', 'line', and 'column' arguments.
468 SetField(type_error, cls, "srcType", src_type_name); 455 DartFrameIterator iterator;
456 const Script& script = Script::Handle(GetCallerScript(&iterator));
457 args_index += SetLocationFields(args, args_index, script, location);
458 ASSERT(args_index == 4);
469 459
470 // Initialize field 'dstType'. 460 // Initialize argument 'srcType'.
471 SetField(type_error, cls, "dstType", dst_type_name); 461 args.SetAt(args_index++, src_type_name);
472 462 args.SetAt(args_index++, dst_type_name);
473 // Initialize field 'dstName'. 463 args.SetAt(args_index++, dst_name);
474 SetField(type_error, cls, "dstName", dst_name); 464 args.SetAt(args_index++, malformed_error);
475 465 ASSERT(args_index == args.Length());
siva 2013/07/12 16:49:36 ditto comment about using 0, 1, 2 etc. instead of
floitsch 2013/07/12 17:12:36 Done.
476 // Initialize field 'malformedError'.
477 SetField(type_error, cls, "malformedError", malformed_error);
478 466
479 // Type errors in the core library may be difficult to diagnose. 467 // Type errors in the core library may be difficult to diagnose.
480 // Print type error information before throwing the error when debugging. 468 // Print type error information before throwing the error when debugging.
481 if (FLAG_print_stacktrace_at_throw) { 469 if (FLAG_print_stacktrace_at_throw) {
482 if (!malformed_error.IsNull()) { 470 if (!malformed_error.IsNull()) {
483 OS::Print("%s\n", malformed_error.ToCString()); 471 OS::Print("%s\n", malformed_error.ToCString());
484 } 472 }
485 intptr_t line, column; 473 intptr_t line, column;
486 script.GetTokenLocation(location, &line, &column); 474 script.GetTokenLocation(location, &line, &column);
487 OS::Print("'%s': Failed type check: line %"Pd" pos %"Pd": ", 475 OS::Print("'%s': Failed type check: line %"Pd" pos %"Pd": ",
488 String::Handle(script.url()).ToCString(), line, column); 476 String::Handle(script.url()).ToCString(), line, column);
489 if (!dst_name.IsNull() && (dst_name.Length() > 0)) { 477 if (!dst_name.IsNull() && (dst_name.Length() > 0)) {
490 OS::Print("type '%s' is not a subtype of type '%s' of '%s'.\n", 478 OS::Print("type '%s' is not a subtype of type '%s' of '%s'.\n",
491 src_type_name.ToCString(), 479 src_type_name.ToCString(),
492 dst_type_name.ToCString(), 480 dst_type_name.ToCString(),
493 dst_name.ToCString()); 481 dst_name.ToCString());
494 } else { 482 } else {
495 OS::Print("malformed type used.\n"); 483 OS::Print("malformed type used.\n");
496 } 484 }
497 } 485 }
498 // Throw TypeError instance. 486 // Throw TypeError instance.
499 Exceptions::Throw(type_error); 487 Exceptions::ThrowByType(exception_type, args);
500 UNREACHABLE(); 488 UNREACHABLE();
501 } 489 }
502 490
503 491
504 void Exceptions::Throw(const Instance& exception) { 492 void Exceptions::Throw(const Instance& exception) {
505 Isolate* isolate = Isolate::Current(); 493 Isolate* isolate = Isolate::Current();
506 isolate->debugger()->SignalExceptionThrown(exception); 494 isolate->debugger()->SignalExceptionThrown(exception);
507 // Null object is a valid exception object. 495 // Null object is a valid exception object.
508 ThrowExceptionHelper(exception, Instance::Handle(isolate)); 496 ThrowExceptionHelper(exception, Instance::Handle(isolate));
509 } 497 }
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 class_name = &Symbols::IsolateSpawnException(); 607 class_name = &Symbols::IsolateSpawnException();
620 break; 608 break;
621 case kIsolateUnhandledException: 609 case kIsolateUnhandledException:
622 library = Library::IsolateLibrary(); 610 library = Library::IsolateLibrary();
623 class_name = &Symbols::IsolateUnhandledException(); 611 class_name = &Symbols::IsolateUnhandledException();
624 break; 612 break;
625 case kFiftyThreeBitOverflowError: 613 case kFiftyThreeBitOverflowError:
626 library = Library::CoreLibrary(); 614 library = Library::CoreLibrary();
627 class_name = &Symbols::FiftyThreeBitOverflowError(); 615 class_name = &Symbols::FiftyThreeBitOverflowError();
628 break; 616 break;
617 case kAssertion:
618 library = Library::CoreLibrary();
619 class_name = &Symbols::AssertionError();
620 break;
621 case kCast:
622 library = Library::CoreLibrary();
623 class_name = &Symbols::CastError();
624 break;
625 case kType:
626 library = Library::CoreLibrary();
627 class_name = &Symbols::TypeError();
628 break;
629 case kFallThrough:
630 library = Library::CoreLibrary();
631 class_name = &Symbols::FallThroughError();
632 break;
633 case kAbstractClassInstantiation:
634 library = Library::CoreLibrary();
635 class_name = &Symbols::AbstractClassInstantiationError();
636 break;
629 } 637 }
630 638
631 return DartLibraryCalls::ExceptionCreate(library, 639 return DartLibraryCalls::ExceptionCreate(library,
632 *class_name, 640 *class_name,
633 *constructor_name, 641 *constructor_name,
634 arguments); 642 arguments);
635 } 643 }
636 644
637 } // namespace dart 645 } // namespace dart
OLDNEW
« runtime/vm/exceptions.h ('K') | « runtime/vm/exceptions.h ('k') | runtime/vm/object.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698