Introduce v8::Exception::GetMessage to find location of an error object.

API=v8::Exception::GetMessage
BUG=chromium:427954
R=yangguo@chromium.org
LOG=Y

Committed: https://code.google.com/p/v8/source/detail?r=25015

Review URL: https://codereview.chromium.org/687253002

Cr-Commit-Position: refs/heads/master@{#25021}
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@25021 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
aandrey@chromium.org 2014-10-30 14:51:17 +00:00
parent ba1aef3fab
commit aeb7ba5259
5 changed files with 85 additions and 18 deletions

View File

@ -4164,6 +4164,9 @@ class V8_EXPORT Exception {
static Local<Value> TypeError(Handle<String> message);
static Local<Value> Error(Handle<String> message);
static Local<Message> GetMessage(Handle<Value> exception);
// DEPRECATED. Use GetMessage()->GetStackTrace()
static Local<StackTrace> GetStackTrace(Handle<Value> exception);
};
@ -4224,6 +4227,8 @@ class PromiseRejectMessage {
V8_INLINE Handle<Promise> GetPromise() const { return promise_; }
V8_INLINE PromiseRejectEvent GetEvent() const { return event_; }
V8_INLINE Handle<Value> GetValue() const { return value_; }
// DEPRECATED. Use v8::Exception::GetMessage(GetValue())->GetStackTrace()
V8_INLINE Handle<StackTrace> GetStackTrace() const { return stack_trace_; }
private:

View File

@ -6971,6 +6971,17 @@ DEFINE_ERROR(Error)
#undef DEFINE_ERROR
Local<Message> Exception::GetMessage(Handle<Value> exception) {
i::Handle<i::Object> obj = Utils::OpenHandle(*exception);
if (!obj->IsHeapObject()) return Local<Message>();
i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
ENTER_V8(isolate);
i::HandleScope scope(isolate);
return Utils::MessageToLocal(
scope.CloseAndEscape(isolate->CreateMessage(obj, NULL)));
}
Local<StackTrace> Exception::GetStackTrace(Handle<Value> exception) {
i::Handle<i::Object> obj = Utils::OpenHandle(*exception);
if (!obj->IsJSObject()) return Local<StackTrace>();

View File

@ -1047,6 +1047,40 @@ void Isolate::ComputeLocation(MessageLocation* target) {
}
void Isolate::ComputeLocationFromStackTrace(MessageLocation* target,
Handle<Object> exception) {
*target = MessageLocation(Handle<Script>(heap_.empty_script()), -1, -1);
if (!exception->IsJSObject()) return;
Handle<Name> key = factory()->stack_trace_symbol();
Handle<Object> property =
JSObject::GetDataProperty(Handle<JSObject>::cast(exception), key);
if (!property->IsJSArray()) return;
Handle<JSArray> simple_stack_trace = Handle<JSArray>::cast(property);
Handle<FixedArray> elements(FixedArray::cast(simple_stack_trace->elements()));
int elements_limit = Smi::cast(simple_stack_trace->length())->value();
for (int i = 1; i < elements_limit; i += 4) {
Handle<JSFunction> fun =
handle(JSFunction::cast(elements->get(i + 1)), this);
if (fun->IsFromNativeScript()) continue;
Handle<Code> code = handle(Code::cast(elements->get(i + 2)), this);
Handle<Smi> offset = handle(Smi::cast(elements->get(i + 3)), this);
Address pc = code->address() + offset->value();
Object* script = fun->shared()->script();
if (script->IsScript() &&
!(Script::cast(script)->source()->IsUndefined())) {
int pos = code->SourcePosition(pc);
Handle<Script> casted_script(Script::cast(script));
*target = MessageLocation(casted_script, pos, pos + 1);
break;
}
}
}
bool Isolate::ShouldReportException(bool* can_be_caught_externally,
bool catchable_by_javascript) {
// Find the top-most try-catch handler.
@ -1106,6 +1140,7 @@ static int fatal_exception_depth = 0;
Handle<JSMessageObject> Isolate::CreateMessage(Handle<Object> exception,
MessageLocation* location) {
Handle<JSArray> stack_trace_object;
MessageLocation potential_computed_location;
if (capture_stack_trace_for_uncaught_exceptions_) {
if (IsErrorObject(exception)) {
// We fetch the stack trace that corresponds to this error object.
@ -1114,14 +1149,22 @@ Handle<JSMessageObject> Isolate::CreateMessage(Handle<Object> exception,
// at this throw site.
stack_trace_object =
GetDetailedStackTrace(Handle<JSObject>::cast(exception));
if (!location) {
ComputeLocationFromStackTrace(&potential_computed_location, exception);
location = &potential_computed_location;
}
}
if (stack_trace_object.is_null()) {
// Not an error object, we capture at throw site.
// Not an error object, we capture stack and location at throw site.
stack_trace_object = CaptureCurrentStackTrace(
stack_trace_for_uncaught_exceptions_frame_limit_,
stack_trace_for_uncaught_exceptions_options_);
}
}
if (!location) {
ComputeLocation(&potential_computed_location);
location = &potential_computed_location;
}
// If the exception argument is a custom object, turn it into a string
// before throwing as uncaught exception. Note that the pending
@ -1227,11 +1270,9 @@ void Isolate::DoThrow(Object* exception, MessageLocation* location) {
Handle<Object> message_obj = CreateMessage(exception_handle, location);
thread_local_top()->pending_message_obj_ = *message_obj;
if (location != NULL) {
thread_local_top()->pending_message_script_ = *location->script();
thread_local_top()->pending_message_start_pos_ = location->start_pos();
thread_local_top()->pending_message_end_pos_ = location->end_pos();
}
thread_local_top()->pending_message_script_ = *location->script();
thread_local_top()->pending_message_start_pos_ = location->start_pos();
thread_local_top()->pending_message_end_pos_ = location->end_pos();
// If the abort-on-uncaught-exception flag is specified, abort on any
// exception not caught by JavaScript, even when an external handler is
@ -1334,7 +1375,6 @@ MessageLocation Isolate::GetMessageLocation() {
if (thread_local_top_.pending_exception_ != heap()->termination_exception() &&
thread_local_top_.has_pending_message_ &&
!thread_local_top_.pending_message_obj_->IsTheHole() &&
!thread_local_top_.pending_message_obj_->IsTheHole()) {
Handle<Script> script(
Script::cast(thread_local_top_.pending_message_script_));

View File

@ -801,6 +801,11 @@ class Isolate {
// Attempts to compute the current source location, storing the
// result in the target out parameter.
void ComputeLocation(MessageLocation* target);
void ComputeLocationFromStackTrace(MessageLocation* target,
Handle<Object> exception);
Handle<JSMessageObject> CreateMessage(Handle<Object> exception,
MessageLocation* location);
// Out of resource exception helpers.
Object* StackOverflow();
@ -1201,9 +1206,6 @@ class Isolate {
// then return true.
bool PropagatePendingExceptionToExternalTryCatch();
Handle<JSMessageObject> CreateMessage(Handle<Object> exception,
MessageLocation* location);
// Traverse prototype chain to find out whether the object is derived from
// the Error object.
bool IsErrorObject(Handle<Object> obj);

View File

@ -8547,7 +8547,7 @@ static void ThrowV8Exception(const v8::FunctionCallbackInfo<v8::Value>& info) {
}
THREADED_TEST(ExceptionGetStackTrace) {
THREADED_TEST(ExceptionGetMessage) {
LocalContext context;
v8::HandleScope scope(context->GetIsolate());
@ -8559,16 +8559,25 @@ THREADED_TEST(ExceptionGetStackTrace) {
global->Set(v8_str("throwV8Exception"), fun->GetFunction());
TryCatch try_catch;
CompileRun("function f1() { throwV8Exception(); }; f1();");
CompileRun(
"function f1() {\n"
" throwV8Exception();\n"
"};\n"
"f1();");
CHECK(try_catch.HasCaught());
v8::Handle<v8::Value> error = try_catch.Exception();
v8::Handle<String> foo = v8_str("foo");
v8::Handle<String> message = v8_str("message");
v8::Handle<String> foo_str = v8_str("foo");
v8::Handle<String> message_str = v8_str("message");
CHECK(error->IsObject());
CHECK(error.As<v8::Object>()->Get(message)->Equals(foo));
CHECK(error.As<v8::Object>()->Get(message_str)->Equals(foo_str));
v8::Handle<v8::StackTrace> stackTrace = v8::Exception::GetStackTrace(error);
v8::Handle<v8::Message> message = v8::Exception::GetMessage(error);
CHECK(!message.IsEmpty());
CHECK_EQ(2, message->GetLineNumber());
CHECK_EQ(2, message->GetStartColumn());
v8::Handle<v8::StackTrace> stackTrace = message->GetStackTrace();
CHECK(!stackTrace.IsEmpty());
CHECK_EQ(2, stackTrace->GetFrameCount());
@ -17873,7 +17882,8 @@ void PromiseRejectCallback(v8::PromiseRejectMessage message) {
promise_reject_counter++;
CcTest::global()->Set(v8_str("rejected"), message.GetPromise());
CcTest::global()->Set(v8_str("value"), message.GetValue());
v8::Handle<v8::StackTrace> stack_trace = message.GetStackTrace();
v8::Handle<v8::StackTrace> stack_trace =
v8::Exception::GetMessage(message.GetValue())->GetStackTrace();
if (!stack_trace.IsEmpty()) {
promise_reject_frame_count = stack_trace->GetFrameCount();
if (promise_reject_frame_count > 0) {
@ -17887,7 +17897,6 @@ void PromiseRejectCallback(v8::PromiseRejectMessage message) {
promise_revoke_counter++;
CcTest::global()->Set(v8_str("revoked"), message.GetPromise());
CHECK(message.GetValue().IsEmpty());
CHECK(message.GetStackTrace().IsEmpty());
}
}