Revision: 21456
Author:   [email protected]
Date:     Fri May 23 08:34:10 2014 UTC
Log: Make v8::TryCatch able to consume natively thrown exceptions (again).

[email protected]
BUG=chromium:362388
TEST=cctest/test-api/TryCatchNative
LOG=N

Review URL: https://codereview.chromium.org/291393002
http://code.google.com/p/v8/source/detail?r=21456

Modified:
 /branches/bleeding_edge/src/api.cc
 /branches/bleeding_edge/src/isolate.cc
 /branches/bleeding_edge/src/isolate.h
 /branches/bleeding_edge/test/cctest/test-api.cc

=======================================
--- /branches/bleeding_edge/src/api.cc  Thu May 22 15:27:57 2014 UTC
+++ /branches/bleeding_edge/src/api.cc  Fri May 23 08:34:10 2014 UTC
@@ -1830,6 +1830,12 @@
     reinterpret_cast<Isolate*>(isolate_)->ThrowException(exc);
     ASSERT(!isolate_->thread_local_top()->rethrowing_message_);
   } else {
+    if (HasCaught() && isolate_->has_scheduled_exception()) {
+ // If an exception was caught but is still scheduled because no API call + // promoted it, then it is canceled to prevent it from being propagated.
+      // Note that this will not cancel termination exceptions.
+      isolate_->CancelScheduledExceptionFromTryCatch(this);
+    }
     isolate_->UnregisterTryCatchHandler(this);
     v8::internal::SimulatorStack::UnregisterCTryCatch();
   }
=======================================
--- /branches/bleeding_edge/src/isolate.cc      Thu May 22 15:27:57 2014 UTC
+++ /branches/bleeding_edge/src/isolate.cc      Fri May 23 08:34:10 2014 UTC
@@ -906,6 +906,15 @@
thread_local_top()->pending_message_start_pos_ = handler->message_start_pos_;
   thread_local_top()->pending_message_end_pos_ = handler->message_end_pos_;
 }
+
+
+void Isolate::CancelScheduledExceptionFromTryCatch(v8::TryCatch* handler) {
+  ASSERT(has_scheduled_exception());
+  if (scheduled_exception() == handler->exception_) {
+    ASSERT(scheduled_exception() != heap()->termination_exception());
+    clear_scheduled_exception();
+  }
+}


 Object* Isolate::PromoteScheduledException() {
@@ -1213,8 +1222,7 @@
   PropagatePendingExceptionToExternalTryCatch();

   HandleScope scope(this);
-  if (thread_local_top_.pending_exception_ ==
-          heap()->termination_exception()) {
+ if (thread_local_top_.pending_exception_ == heap()->termination_exception()) {
     // Do nothing: if needed, the exception has been already propagated to
     // v8::TryCatch.
   } else {
@@ -1751,8 +1759,7 @@

   if (!external_caught) return;

-  if (thread_local_top_.pending_exception_ ==
-             heap()->termination_exception()) {
+ if (thread_local_top_.pending_exception_ == heap()->termination_exception()) {
     try_catch_handler()->can_continue_ = false;
     try_catch_handler()->has_terminated_ = true;
     try_catch_handler()->exception_ = heap()->null_value();
=======================================
--- /branches/bleeding_edge/src/isolate.h       Thu May 22 09:36:20 2014 UTC
+++ /branches/bleeding_edge/src/isolate.h       Fri May 23 08:34:10 2014 UTC
@@ -735,6 +735,8 @@
   // Re-set pending message, script and positions reported to the TryCatch
   // back to the TLS for re-use when rethrowing.
   void RestorePendingMessageFromTryCatch(v8::TryCatch* handler);
+  // Un-schedule an exception that was caught by a TryCatch handler.
+  void CancelScheduledExceptionFromTryCatch(v8::TryCatch* handler);
   void ReportPendingMessages();
   // Return pending location if any or unfilled structure.
   MessageLocation GetMessageLocation();
=======================================
--- /branches/bleeding_edge/test/cctest/test-api.cc Thu May 22 15:27:57 2014 UTC +++ /branches/bleeding_edge/test/cctest/test-api.cc Fri May 23 08:34:10 2014 UTC
@@ -5343,15 +5343,28 @@
 }


-static void TryCatchNestedHelper(int depth) {
+static void TryCatchNested1Helper(int depth) {
   if (depth > 0) {
     v8::TryCatch try_catch;
     try_catch.SetVerbose(true);
-    TryCatchNestedHelper(depth - 1);
+    TryCatchNested1Helper(depth - 1);
     CHECK(try_catch.HasCaught());
     try_catch.ReThrow();
   } else {
-    CcTest::isolate()->ThrowException(v8_str("back"));
+    CcTest::isolate()->ThrowException(v8_str("E1"));
+  }
+}
+
+
+static void TryCatchNested2Helper(int depth) {
+  if (depth > 0) {
+    v8::TryCatch try_catch;
+    try_catch.SetVerbose(true);
+    TryCatchNested2Helper(depth - 1);
+    CHECK(try_catch.HasCaught());
+    try_catch.ReThrow();
+  } else {
+    CompileRun("throw 'E2';");
   }
 }

@@ -5360,10 +5373,22 @@
   v8::V8::Initialize();
   LocalContext context;
   v8::HandleScope scope(context->GetIsolate());
-  v8::TryCatch try_catch;
-  TryCatchNestedHelper(5);
-  CHECK(try_catch.HasCaught());
- CHECK_EQ(0, strcmp(*v8::String::Utf8Value(try_catch.Exception()), "back"));
+
+  {
+    // Test nested try-catch with a native throw in the end.
+    v8::TryCatch try_catch;
+    TryCatchNested1Helper(5);
+    CHECK(try_catch.HasCaught());
+ CHECK_EQ(0, strcmp(*v8::String::Utf8Value(try_catch.Exception()), "E1"));
+  }
+
+  {
+    // Test nested try-catch with a JavaScript throw in the end.
+    v8::TryCatch try_catch;
+    TryCatchNested2Helper(5);
+    CHECK(try_catch.HasCaught());
+ CHECK_EQ(0, strcmp(*v8::String::Utf8Value(try_catch.Exception()), "E2"));
+  }
 }


@@ -5407,6 +5432,28 @@
   CompileRunWithOrigin("TryCatchMixedNestingHelper();\n", "outer", 1, 1);
   TryCatchMixedNestingCheck(&try_catch);
 }
+
+
+void TryCatchNativeHelper(const v8::FunctionCallbackInfo<v8::Value>& args) {
+  ApiTestFuzzer::Fuzz();
+  v8::TryCatch try_catch;
+  args.GetIsolate()->ThrowException(v8_str("boom"));
+  CHECK(try_catch.HasCaught());
+}
+
+
+TEST(TryCatchNative) {
+  v8::Isolate* isolate = CcTest::isolate();
+  v8::HandleScope scope(isolate);
+  v8::V8::Initialize();
+  v8::TryCatch try_catch;
+  Local<ObjectTemplate> templ = ObjectTemplate::New(isolate);
+  templ->Set(v8_str("TryCatchNativeHelper"),
+             v8::FunctionTemplate::New(isolate, TryCatchNativeHelper));
+  LocalContext context(0, templ);
+  CompileRun("TryCatchNativeHelper();");
+  CHECK(!try_catch.HasCaught());
+}


 THREADED_TEST(Equality) {

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to