Reviewers: Søren Gjesse, Description: Added API call to get the stack trace for an exception Added TryCatch::StackTrace that gets the stack trace for the thrown exception.
Please review this at http://codereview.chromium.org/171042 Affected files: M include/v8.h M src/api.cc M test/cctest/test-api.cc Index: include/v8.h diff --git a/include/v8.h b/include/v8.h index 77223a0e4c52abc746f1c2c2614838efcc590d26..a0cc4ea8b0ac951551c7211b2ebda141ac3e1c81 100644 --- a/include/v8.h +++ b/include/v8.h @@ -2290,6 +2290,12 @@ class V8EXPORT TryCatch { Local<Value> Exception() const; /** + * Returns the .stack property of the thrown object. If no .stack + * property is present an empty handle is returned. + */ + Local<Value> StackTrace() const; + + /** * Returns the message associated with this exception. If there is * no message associated an empty handle is returned. * Index: src/api.cc diff --git a/src/api.cc b/src/api.cc index 2f7de08550b54909cebe53ccf6585ba5c7113e45..5bc8ea453fbe5af90fb2f1af0a8bd959ebf05549 100644 --- a/src/api.cc +++ b/src/api.cc @@ -1219,6 +1219,23 @@ v8::Local<Value> v8::TryCatch::Exception() const { } +v8::Local<Value> v8::TryCatch::StackTrace() const { + if (HasCaught()) { + i::Object* raw_ptr = reinterpret_cast<i::Object*>(exception_); + if (!raw_ptr->IsJSObject()) return v8::Local<Value>(); + v8::HandleScope scope; + i::Handle<i::Object> raw_obj(raw_ptr); + i::Handle<i::JSObject> obj = i::Handle<i::JSObject>::cast(raw_obj); + i::Handle<i::String> name = i::Factory::LookupAsciiSymbol("stack"); + if (!obj->HasProperty(*name)) + return v8::Local<Value>(); + return scope.Close(v8::Utils::ToLocal(i::GetProperty(obj, name))); + } else { + return v8::Local<Value>(); + } +} + + v8::Local<v8::Message> v8::TryCatch::Message() const { if (HasCaught() && message_ != i::Smi::FromInt(0)) { i::Object* message = reinterpret_cast<i::Object*>(message_); Index: test/cctest/test-api.cc diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index e0c9f3eb65b4d35db49356e965cbb08f5ac3b6e9..e1caba3c836b318baae697b592dd66f8f1bf10ce 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -7753,3 +7753,16 @@ THREADED_TEST(ScriptContextDependence) { CHECK_EQ(dep->Run()->Int32Value(), 100); CHECK_EQ(indep->Run()->Int32Value(), 101); } + +THREADED_TEST(StackTrace) { + v8::HandleScope scope; + LocalContext context; + v8::TryCatch try_catch; + const char *source = "function foo() { FAIL.FAIL; }; foo();"; + v8::Handle<v8::String> src = v8::String::New(source); + v8::Handle<v8::String> origin = v8::String::New("stack-trace-test"); + v8::Script::New(src, origin)->Run(); + CHECK(try_catch.HasCaught()); + v8::String::Utf8Value stack(try_catch.StackTrace()); + CHECK(strstr(*stack, "at foo (stack-trace-test") != NULL); +} --~--~---------~--~----~------------~-------~--~----~ v8-dev mailing list [email protected] http://groups.google.com/group/v8-dev -~----------~----~----~----~------~----~------~--~---
