Reviewers: Jakob,

Description:
Correctly handle uncaught exception objects.


[email protected]
BUG=


Please review this at https://chromiumcodereview.appspot.com/11365200/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/isolate.cc
  M test/cctest/test-api.cc


Index: src/isolate.cc
diff --git a/src/isolate.cc b/src/isolate.cc
index eec5a54d737e902caddfcfe5679dcb33656c8dd1..73a6310b4c084a8280e2d085cdc38521cf9be7b7 100644
--- a/src/isolate.cc
+++ b/src/isolate.cc
@@ -1228,18 +1228,22 @@ void Isolate::DoThrow(Object* exception, MessageLocation* location) {
               stack_trace_for_uncaught_exceptions_options_);
         }
       }
-      // Stringify custom error objects for the message object.
+
+      Handle<Object> exception_arg = exception_handle;
+ // If the exception argument is a custom object, turn it into a string
+      // before throwing as uncaught exception.  Note that the pending
+      // exception object to be set later must not be turned into a string.
if (exception_handle->IsJSObject() && !IsErrorObject(exception_handle)) {
         bool failed = false;
-        exception_handle = Execution::ToString(exception_handle, &failed);
+        exception_arg = Execution::ToString(exception_arg, &failed);
         if (failed) {
-          exception_handle = factory()->LookupAsciiSymbol("exception");
+          exception_arg = factory()->LookupAsciiSymbol("exception");
         }
       }
       Handle<Object> message_obj = MessageHandler::MakeMessageObject(
           "uncaught_exception",
           location,
-          HandleVector<Object>(&exception_handle, 1),
+          HandleVector<Object>(&exception_arg, 1),
           stack_trace,
           stack_trace_object);
       thread_local_top()->pending_message_obj_ = *message_obj;
Index: test/cctest/test-api.cc
diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc
index 41eb68f231f24d1f5fdc9eff1211429e8efa206e..ec7379c468f7a31a01c46dfa311ad8e9779bdcb0 100644
--- a/test/cctest/test-api.cc
+++ b/test/cctest/test-api.cc
@@ -2554,6 +2554,18 @@ THREADED_TEST(ScriptException) {
 }


+TEST(TryCatchCustomException) {
+  v8::HandleScope scope;
+  LocalContext env;
+  v8::TryCatch try_catch;
+  CompileRun("function CustomError() { this.a = 'b'; }"
+             "(function f() { throw new CustomError(); })();");
+  CHECK(try_catch.HasCaught());
+  CHECK(try_catch.Exception()->ToObject()->
+            Get(v8_str("a"))->Equals(v8_str("b")));
+}
+
+
 bool message_received;




--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to