Revision: 2698 Author: [email protected] Date: Mon Aug 17 06:34:41 2009 Log: Added API call to get the stack trace for an exception Added TryCatch::StackTrace that gets the stack trace for the thrown exception.
Review URL: http://codereview.chromium.org/171042 http://code.google.com/p/v8/source/detail?r=2698 Modified: /branches/bleeding_edge/include/v8.h /branches/bleeding_edge/src/api.cc /branches/bleeding_edge/test/cctest/test-api.cc ======================================= --- /branches/bleeding_edge/include/v8.h Mon Aug 17 04:41:00 2009 +++ /branches/bleeding_edge/include/v8.h Mon Aug 17 06:34:41 2009 @@ -2289,6 +2289,12 @@ */ 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. ======================================= --- /branches/bleeding_edge/src/api.cc Mon Aug 17 04:41:00 2009 +++ /branches/bleeding_edge/src/api.cc Mon Aug 17 06:34:41 2009 @@ -1217,6 +1217,22 @@ return v8::Local<Value>(); } } + + +v8::Local<Value> v8::TryCatch::StackTrace() const { + if (HasCaught()) { + i::Object* raw_obj = reinterpret_cast<i::Object*>(exception_); + if (!raw_obj->IsJSObject()) return v8::Local<Value>(); + v8::HandleScope scope; + i::Handle<i::JSObject> obj(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 { ======================================= --- /branches/bleeding_edge/test/cctest/test-api.cc Mon Aug 17 04:41:00 2009 +++ /branches/bleeding_edge/test/cctest/test-api.cc Mon Aug 17 06:34:41 2009 @@ -7753,3 +7753,16 @@ 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 -~----------~----~----~----~------~----~------~--~---
