From 8d04c8c89f703e351c2015171bb59cfe91c9c354 Mon Sep 17 00:00:00 2001 From: "yangguo@chromium.org" Date: Fri, 14 Sep 2012 11:43:46 +0000 Subject: [PATCH] Replace r12503. Explicitly check toString() for exception in d8's print(). R=jkummerow@chromium.org BUG=v8:2317 Review URL: https://chromiumcodereview.appspot.com/10911305 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@12507 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/api.cc | 3 +++ src/d8.cc | 8 +++++++- test/cctest/test-api.cc | 33 --------------------------------- 3 files changed, 10 insertions(+), 34 deletions(-) diff --git a/src/api.cc b/src/api.cc index bcec7f9004..bb343b5be8 100644 --- a/src/api.cc +++ b/src/api.cc @@ -5500,6 +5500,7 @@ String::Utf8Value::Utf8Value(v8::Handle obj) if (obj.IsEmpty()) return; ENTER_V8(isolate); i::HandleScope scope(isolate); + TryCatch try_catch; Handle str = obj->ToString(); if (str.IsEmpty()) return; i::Handle i_str = Utils::OpenHandle(*str); @@ -5521,6 +5522,7 @@ String::AsciiValue::AsciiValue(v8::Handle obj) if (obj.IsEmpty()) return; ENTER_V8(isolate); i::HandleScope scope(isolate); + TryCatch try_catch; Handle str = obj->ToString(); if (str.IsEmpty()) return; length_ = str->Length(); @@ -5541,6 +5543,7 @@ String::Value::Value(v8::Handle obj) if (obj.IsEmpty()) return; ENTER_V8(isolate); i::HandleScope scope(isolate); + TryCatch try_catch; Handle str = obj->ToString(); if (str.IsEmpty()) return; length_ = str->Length(); diff --git a/src/d8.cc b/src/d8.cc index f20a41aa5f..b3b1bb8a17 100644 --- a/src/d8.cc +++ b/src/d8.cc @@ -200,7 +200,13 @@ Handle Shell::Write(const Arguments& args) { if (i != 0) { printf(" "); } - v8::String::Utf8Value str(args[i]); + + // Explicitly catch potential exceptions in toString(). + v8::TryCatch try_catch; + Handle str_obj = args[i]->ToString(); + if (try_catch.HasCaught()) return try_catch.ReThrow(); + + v8::String::Utf8Value str(str_obj); int n = static_cast(fwrite(*str, sizeof(**str), str.length(), stdout)); if (n != str.length()) { printf("Error in fwrite\n"); diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index 027c0d6c19..18025e14a0 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -17513,37 +17513,4 @@ class ThreadInterruptTest { THREADED_TEST(SemaphoreInterruption) { ThreadInterruptTest().RunTest(); } - - -TEST(Utf8ValueException) { - v8::HandleScope scope; - LocalContext context; - - Handle object = CompileRun( - "var obj = { toString : function() { throw 'deadbeef'; } }; obj"); - - { v8::TryCatch try_catch; - v8::String::Utf8Value utf8_value(object); - CHECK(try_catch.HasCaught()); - CHECK_EQ("deadbeef", *v8::String::Utf8Value(try_catch.Exception())); - } - - { v8::TryCatch try_catch; - v8::String::AsciiValue ascii_value(object); - CHECK(try_catch.HasCaught()); - CHECK_EQ("deadbeef", *v8::String::Utf8Value(try_catch.Exception())); - } - - { v8::TryCatch try_catch; - v8::String::Value value(object); - CHECK(try_catch.HasCaught()); - CHECK_EQ("deadbeef", *v8::String::Utf8Value(try_catch.Exception())); - } - - // It should work fine without any TryCatch. - v8::String::Utf8Value utf8_value(object); - v8::String::AsciiValue ascii_value(object); - v8::String::Value value(object); -} - #endif // WIN32