diff --git a/AUTHORS b/AUTHORS index 6f82d1207e..318029b091 100644 --- a/AUTHORS +++ b/AUTHORS @@ -44,7 +44,7 @@ Alexis Campailla Andreas Anyuru Andrew Paprocki Andrei Kashcha -Anna Henningsen +Anna Henningsen Bangfu Tao Ben Noordhuis Benjamin Tan diff --git a/src/messages.cc b/src/messages.cc index d088c48e81..926c6e145b 100644 --- a/src/messages.cc +++ b/src/messages.cc @@ -113,6 +113,9 @@ void MessageHandler::ReportMessage(Isolate* isolate, const MessageLocation* loc, } if (!maybe_stringified.ToHandle(&stringified)) { + DCHECK(isolate->has_pending_exception()); + isolate->clear_pending_exception(); + isolate->set_external_caught_exception(false); stringified = isolate->factory()->NewStringFromAsciiChecked("exception"); } diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index 116ddc5304..f5b280a13e 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -5696,23 +5696,55 @@ TEST(CustomErrorMessage) { static void check_custom_rethrowing_message(v8::Local message, v8::Local data) { + CHECK(data->IsExternal()); + int* callcount = static_cast(data.As()->Value()); + ++*callcount; + const char* uncaught_error = "Uncaught exception"; CHECK(message->Get() ->Equals(CcTest::isolate()->GetCurrentContext(), v8_str(uncaught_error)) .FromJust()); + // Test that compiling code inside a message handler works. + CHECK(CompileRunChecked(CcTest::isolate(), "(function(a) { return a; })(42)") + ->Equals(CcTest::isolate()->GetCurrentContext(), + v8::Integer::NewFromUnsigned(CcTest::isolate(), 42)) + .FromJust()); } TEST(CustomErrorRethrowsOnToString) { + int callcount = 0; LocalContext context; - v8::HandleScope scope(context->GetIsolate()); - context->GetIsolate()->AddMessageListener(check_custom_rethrowing_message); + v8::Isolate* isolate = context->GetIsolate(); + v8::HandleScope scope(isolate); + context->GetIsolate()->AddMessageListener( + check_custom_rethrowing_message, v8::External::New(isolate, &callcount)); CompileRun( "var e = { toString: function() { throw e; } };" "try { throw e; } finally {}"); + CHECK_EQ(callcount, 1); + context->GetIsolate()->RemoveMessageListeners( + check_custom_rethrowing_message); +} + +TEST(CustomErrorRethrowsOnToStringInsideVerboseTryCatch) { + int callcount = 0; + LocalContext context; + v8::Isolate* isolate = context->GetIsolate(); + v8::HandleScope scope(isolate); + v8::TryCatch try_catch(isolate); + try_catch.SetVerbose(true); + context->GetIsolate()->AddMessageListener( + check_custom_rethrowing_message, v8::External::New(isolate, &callcount)); + + CompileRun( + "var e = { toString: function() { throw e; } };" + "try { throw e; } finally {}"); + + CHECK_EQ(callcount, 1); context->GetIsolate()->RemoveMessageListeners( check_custom_rethrowing_message); }