Files
sdk/runtime/vm/exceptions.cc
T
turnidge@google.com 0ddd4c04e9 Dart_PropagateError, take 2.
This change hopefully takes care of the non-mac build issues.  The
checked mode failures are fixed in a separate cl.
Review URL: https://chromiumcodereview.appspot.com//9316071

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@3863 260f80e4-7a28-3924-810f-c04153c831b5
2012-02-02 19:05:06 +00:00

223 lines
7.6 KiB
C++

// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/exceptions.h"
#include "vm/cpu.h"
#include "vm/dart_entry.h"
#include "vm/flags.h"
#include "vm/object.h"
#include "vm/stack_frame.h"
namespace dart {
DEFINE_FLAG(bool, print_stack_trace_at_throw, false,
"Prints a stack trace everytime a throw occurs.");
// Iterate through the stack frames and try to find a frame with an
// exception handler. Once found, set the pc, sp and fp so that execution
// can continue in that frame.
static bool FindExceptionHandler(uword* handler_pc,
uword* handler_sp,
uword* handler_fp,
GrowableArray<uword>* stack_frame_pcs) {
StackFrameIterator frames(StackFrameIterator::kDontValidateFrames);
StackFrame* frame = frames.NextFrame();
ASSERT(frame != NULL);
while (!frame->IsEntryFrame()) {
if (frame->IsDartFrame()) {
stack_frame_pcs->Add(frame->pc());
DartFrame* dart_frame = reinterpret_cast<DartFrame*>(frame);
if (dart_frame->FindExceptionHandler(handler_pc)) {
*handler_sp = frame->sp();
*handler_fp = frame->fp();
return true;
}
}
frame = frames.NextFrame();
ASSERT(frame != NULL);
}
ASSERT(frame->IsEntryFrame());
*handler_pc = frame->pc();
*handler_sp = frame->sp();
*handler_fp = frame->fp();
return false;
}
static void FindErrorHandler(uword* handler_pc,
uword* handler_sp,
uword* handler_fp) {
// TODO(turnidge): Is there a faster way to get the next entry frame?
StackFrameIterator frames(StackFrameIterator::kDontValidateFrames);
StackFrame* frame = frames.NextFrame();
ASSERT(frame != NULL);
while (!frame->IsEntryFrame()) {
frame = frames.NextFrame();
ASSERT(frame != NULL);
}
ASSERT(frame->IsEntryFrame());
*handler_pc = frame->pc();
*handler_sp = frame->sp();
*handler_fp = frame->fp();
}
static void ThrowExceptionHelper(const Instance& exception,
const Instance& existing_stacktrace) {
uword handler_pc = 0;
uword handler_sp = 0;
uword handler_fp = 0;
GrowableArray<uword> stack_frame_pcs;
bool handler_exists = FindExceptionHandler(&handler_pc,
&handler_sp,
&handler_fp,
&stack_frame_pcs);
// TODO(5411263): At some point we can optimize by figuring out if a
// stack trace is needed based on whether the catch code specifies a
// stack trace object or there is a rethrow in the catch clause.
Stacktrace& stacktrace = Stacktrace::Handle();
if (stack_frame_pcs.length() > 0) {
if (existing_stacktrace.IsNull()) {
stacktrace = Stacktrace::New(stack_frame_pcs);
} else {
stacktrace ^= existing_stacktrace.raw();
stacktrace.Append(stack_frame_pcs);
}
} else {
stacktrace ^= existing_stacktrace.raw();
}
if (FLAG_print_stack_trace_at_throw) {
OS::Print("Exception '%s' thrown:\n", exception.ToCString());
OS::Print("%s\n", stacktrace.ToCString());
}
if (handler_exists) {
// Found a dart handler for the exception, jump to it.
CPU::JumpToExceptionHandler(handler_pc,
handler_sp,
handler_fp,
exception,
stacktrace);
} else {
// No dart exception handler found in this invocation sequence,
// so we create an unhandled exception object and return to the
// invocation stub so that it returns this unhandled exception
// object. The C++ code which invoked this dart sequence can check
// and do the appropriate thing (rethrow the exception to the
// dart invocation sequence above it, print diagnostics and terminate
// the isolate etc.).
const UnhandledException& unhandled_exception = UnhandledException::Handle(
UnhandledException::New(exception, stacktrace));
CPU::JumpToErrorHandler(handler_pc,
handler_sp,
handler_fp,
unhandled_exception);
}
UNREACHABLE();
}
void Exceptions::Throw(const Instance& exception) {
ThrowExceptionHelper(exception, Instance::Handle());
}
void Exceptions::ReThrow(const Instance& exception,
const Instance& stacktrace) {
ASSERT(!exception.IsNull());
ThrowExceptionHelper(exception, stacktrace);
}
void Exceptions::PropagateError(const Object& obj) {
ASSERT(Isolate::Current()->top_exit_frame_info() != 0);
Error& error = Error::Handle();
error ^= obj.raw();
if (error.IsUnhandledException()) {
// If the error object represents an unhandled exception, then
// rethrow the exception in the normal fashion.
UnhandledException& uhe = UnhandledException::Handle();
uhe ^= error.raw();
const Instance& exc = Instance::Handle(uhe.exception());
const Instance& stk = Instance::Handle(uhe.stacktrace());
Exceptions::ReThrow(exc, stk);
} else {
// Return to the invocation stub and return this error object. The
// C++ code which invoked this dart sequence can check and do the
// appropriate thing.
uword handler_pc = 0;
uword handler_sp = 0;
uword handler_fp = 0;
FindErrorHandler(&handler_pc, &handler_sp, &handler_fp);
CPU::JumpToErrorHandler(handler_pc, handler_sp, handler_fp, error);
}
UNREACHABLE();
}
void Exceptions::ThrowByType(
ExceptionType type, const GrowableArray<const Object*>& arguments) {
const Object& result = Object::Handle(Create(type, arguments));
if (result.IsError()) {
// We got an error while constructing the exception object.
// Propagate the error instead of throwing the exception.
Error& error = Error::Handle();
error ^= result.raw();
PropagateError(error);
} else {
ASSERT(result.IsInstance());
Instance& exception = Instance::Handle();
exception ^= result.raw();
Throw(exception);
}
}
RawObject* Exceptions::Create(
ExceptionType type, const GrowableArray<const Object*>& arguments) {
String& class_name = String::Handle();
switch (type) {
case kIndexOutOfRange:
class_name = String::NewSymbol("IndexOutOfRangeException");
break;
case kIllegalArgument:
class_name = String::NewSymbol("IllegalArgumentException");
break;
case kNoSuchMethod:
class_name = String::NewSymbol("NoSuchMethodException");
break;
case kClosureArgumentMismatch:
class_name = String::NewSymbol("ClosureArgumentMismatchException");
break;
case kObjectNotClosure:
class_name = String::NewSymbol("ObjectNotClosureException");
break;
case kBadNumberFormat:
class_name = String::NewSymbol("BadNumberFormatException");
break;
case kStackOverflow:
class_name = String::NewSymbol("StackOverflowException");
break;
case kOutOfMemory:
class_name = String::NewSymbol("OutOfMemoryException");
break;
case kWrongArgumentCount:
class_name = String::NewSymbol("WrongArgumentCountException");
break;
case kInternalError:
class_name = String::NewSymbol("InternalError");
break;
case kNullPointer:
class_name = String::NewSymbol("NullPointerException");
break;
case kIllegalJSRegExp:
class_name = String::NewSymbol("IllegalJSRegExpException");
break;
}
return DartLibraryCalls::ExceptionCreate(class_name, arguments);
}
} // namespace dart