From 893f413434ad174900d95c70f00b9c92ff0066d7 Mon Sep 17 00:00:00 2001 From: "yangguo@chromium.org" Date: Thu, 3 Apr 2014 12:30:08 +0000 Subject: [PATCH] Return MaybeHandle from NewRaw???String. R=ishell@chromium.org Review URL: https://codereview.chromium.org/223573002 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@20479 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/factory.cc | 35 ++++++++------- src/factory.h | 4 +- src/handles.h | 5 ++- src/json-parser.h | 9 ++-- src/json-stringifier.h | 22 ++++----- src/runtime.cc | 86 ++++++++++++++++++------------------ src/uri.h | 22 ++++----- test/cctest/test-mementos.cc | 3 +- 8 files changed, 98 insertions(+), 88 deletions(-) diff --git a/src/factory.cc b/src/factory.cc index 9fa1e12abe..6218c643af 100644 --- a/src/factory.cc +++ b/src/factory.cc @@ -255,8 +255,8 @@ Handle Factory::NewStringFromTwoByte(Vector string, } -Handle Factory::NewRawOneByteString(int length, - PretenureFlag pretenure) { +MaybeHandle Factory::NewRawOneByteString( + int length, PretenureFlag pretenure) { CALL_HEAP_FUNCTION( isolate(), isolate()->heap()->AllocateRawOneByteString(length, pretenure), @@ -264,8 +264,8 @@ Handle Factory::NewRawOneByteString(int length, } -Handle Factory::NewRawTwoByteString(int length, - PretenureFlag pretenure) { +MaybeHandle Factory::NewRawTwoByteString( + int length, PretenureFlag pretenure) { CALL_HEAP_FUNCTION( isolate(), isolate()->heap()->AllocateRawTwoByteString(length, pretenure), @@ -298,13 +298,15 @@ static inline Handle MakeOrFindTwoCharacterString(Isolate* isolate, if (static_cast(c1 | c2) <= String::kMaxOneByteCharCodeU) { // We can do this. ASSERT(IsPowerOf2(String::kMaxOneByteCharCodeU + 1)); // because of this. - Handle str = isolate->factory()->NewRawOneByteString(2); + Handle str = + isolate->factory()->NewRawOneByteString(2).ToHandleChecked(); uint8_t* dest = str->GetChars(); dest[0] = static_cast(c1); dest[1] = static_cast(c2); return str; } else { - Handle str = isolate->factory()->NewRawTwoByteString(2); + Handle str = + isolate->factory()->NewRawTwoByteString(2).ToHandleChecked(); uc16* dest = str->GetChars(); dest[0] = c1; dest[1] = c2; @@ -380,7 +382,8 @@ Handle Factory::NewConsString(Handle left, STATIC_ASSERT(ConsString::kMinLength <= String::kMaxLength); if (is_one_byte) { - Handle result = NewRawOneByteString(length); + Handle result = + NewRawOneByteString(length).ToHandleChecked(); DisallowHeapAllocation no_gc; uint8_t* dest = result->GetChars(); // Copy left part. @@ -397,8 +400,10 @@ Handle Factory::NewConsString(Handle left, } return (is_one_byte_data_in_two_byte_string) - ? ConcatStringContent(NewRawOneByteString(length), left, right) - : ConcatStringContent(NewRawTwoByteString(length), left, right); + ? ConcatStringContent( + NewRawOneByteString(length).ToHandleChecked(), left, right) + : ConcatStringContent( + NewRawTwoByteString(length).ToHandleChecked(), left, right); } Handle result = NewRawConsString( @@ -422,10 +427,10 @@ Handle Factory::NewFlatConcatString(Handle first, int total_length = first->length() + second->length(); if (first->IsOneByteRepresentation() && second->IsOneByteRepresentation()) { return ConcatStringContent( - NewRawOneByteString(total_length), first, second); + NewRawOneByteString(total_length).ToHandleChecked(), first, second); } else { return ConcatStringContent( - NewRawTwoByteString(total_length), first, second); + NewRawTwoByteString(total_length).ToHandleChecked(), first, second); } } @@ -463,15 +468,15 @@ Handle Factory::NewProperSubString(Handle str, if (!FLAG_string_slices || length < SlicedString::kMinLength) { if (str->IsOneByteRepresentation()) { - Handle result = NewRawOneByteString(length); - ASSERT(!result.is_null()); + Handle result = + NewRawOneByteString(length).ToHandleChecked(); uint8_t* dest = result->GetChars(); DisallowHeapAllocation no_gc; String::WriteToFlat(*str, dest, begin, end); return result; } else { - Handle result = NewRawTwoByteString(length); - ASSERT(!result.is_null()); + Handle result = + NewRawTwoByteString(length).ToHandleChecked(); uc16* dest = result->GetChars(); DisallowHeapAllocation no_gc; String::WriteToFlat(*str, dest, begin, end); diff --git a/src/factory.h b/src/factory.h index e838ec6a41..61105a7172 100644 --- a/src/factory.h +++ b/src/factory.h @@ -132,10 +132,10 @@ class Factory V8_FINAL { // Allocates and partially initializes an ASCII or TwoByte String. The // characters of the string are uninitialized. Currently used in regexp code // only, where they are pretenured. - Handle NewRawOneByteString( + MaybeHandle NewRawOneByteString( int length, PretenureFlag pretenure = NOT_TENURED); - Handle NewRawTwoByteString( + MaybeHandle NewRawTwoByteString( int length, PretenureFlag pretenure = NOT_TENURED); diff --git a/src/handles.h b/src/handles.h index 77e5e528da..5b5ed1b564 100644 --- a/src/handles.h +++ b/src/handles.h @@ -72,7 +72,8 @@ class MaybeHandle { return Handle(location_); } - INLINE(bool ToHandle(Handle* out)) { + // Convert to a Handle with a type that can be upcasted to. + template INLINE(bool ToHandle(Handle* out)) { if (location_ == NULL) { *out = Handle::null(); return false; @@ -82,6 +83,8 @@ class MaybeHandle { } } + bool is_null() const { return location_ == NULL; } + protected: T** location_; diff --git a/src/json-parser.h b/src/json-parser.h index 4c2b479182..ca21421d44 100644 --- a/src/json-parser.h +++ b/src/json-parser.h @@ -583,14 +583,14 @@ template <> inline Handle NewRawString(Factory* factory, int length, PretenureFlag pretenure) { - return factory->NewRawTwoByteString(length, pretenure); + return factory->NewRawTwoByteString(length, pretenure).ToHandleChecked(); } template <> inline Handle NewRawString(Factory* factory, int length, PretenureFlag pretenure) { - return factory->NewRawOneByteString(length, pretenure); + return factory->NewRawOneByteString(length, pretenure).ToHandleChecked(); } @@ -606,7 +606,6 @@ Handle JsonParser::SlowScanJsonString( int length = Min(max_length, Max(kInitialSpecialStringLength, 2 * count)); Handle seq_string = NewRawString(factory(), length, pretenure_); - ASSERT(!seq_string.is_null()); // Copy prefix into seq_str. SinkChar* dest = seq_string->GetChars(); String::WriteToFlat(*prefix, dest, start, end); @@ -793,8 +792,8 @@ Handle JsonParser::ScanJsonString() { } } while (c0_ != '"'); int length = position_ - beg_pos; - Handle result = factory()->NewRawOneByteString(length, pretenure_); - ASSERT(!result.is_null()); + Handle result = + factory()->NewRawOneByteString(length, pretenure_).ToHandleChecked(); uint8_t* dest = SeqOneByteString::cast(*result)->GetChars(); String::WriteToFlat(*source_, dest, beg_pos, position_); diff --git a/src/json-stringifier.h b/src/json-stringifier.h index 17c6b6f111..a083d48a09 100644 --- a/src/json-stringifier.h +++ b/src/json-stringifier.h @@ -265,8 +265,7 @@ BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate) accumulator_store_ = Handle::cast( factory_->ToObject(factory_->empty_string())); part_length_ = kInitialPartLength; - current_part_ = factory_->NewRawOneByteString(part_length_); - ASSERT(!current_part_.is_null()); + current_part_ = factory_->NewRawOneByteString(part_length_).ToHandleChecked(); tojson_string_ = factory_->toJSON_string(); stack_ = factory_->NewJSArray(8); } @@ -308,18 +307,16 @@ MaybeObject* BasicJsonStringifier::StringifyString(Isolate* isolate, FlattenString(object); ASSERT(object->IsFlat()); if (object->IsOneByteRepresentationUnderneath()) { - Handle result = - isolate->factory()->NewRawOneByteString(worst_case_length); - ASSERT(!result.is_null()); + Handle result = isolate->factory()->NewRawOneByteString( + worst_case_length).ToHandleChecked(); DisallowHeapAllocation no_gc; return StringifyString_( isolate, object->GetFlatContent().ToOneByteVector(), result); } else { - Handle result = - isolate->factory()->NewRawTwoByteString(worst_case_length); - ASSERT(!result.is_null()); + Handle result = isolate->factory()->NewRawTwoByteString( + worst_case_length).ToHandleChecked(); DisallowHeapAllocation no_gc; return StringifyString_( isolate, @@ -742,9 +739,11 @@ void BasicJsonStringifier::Extend() { part_length_ *= kPartLengthGrowthFactor; } if (is_ascii_) { - current_part_ = factory_->NewRawOneByteString(part_length_); + current_part_ = + factory_->NewRawOneByteString(part_length_).ToHandleChecked(); } else { - current_part_ = factory_->NewRawTwoByteString(part_length_); + current_part_ = + factory_->NewRawTwoByteString(part_length_).ToHandleChecked(); } ASSERT(!current_part_.is_null()); current_index_ = 0; @@ -754,7 +753,8 @@ void BasicJsonStringifier::Extend() { void BasicJsonStringifier::ChangeEncoding() { ShrinkCurrentPart(); Accumulate(); - current_part_ = factory_->NewRawTwoByteString(part_length_); + current_part_ = + factory_->NewRawTwoByteString(part_length_).ToHandleChecked(); ASSERT(!current_part_.is_null()); current_index_ = 0; is_ascii_ = false; diff --git a/src/runtime.cc b/src/runtime.cc index fe5d15f6be..4c0536e2b1 100644 --- a/src/runtime.cc +++ b/src/runtime.cc @@ -3434,15 +3434,20 @@ class ReplacementStringBuilder { } - Handle ToString() { + MaybeHandle ToString() { + Isolate* isolate = heap_->isolate(); if (array_builder_.length() == 0) { - return heap_->isolate()->factory()->empty_string(); + return isolate->factory()->empty_string(); } Handle joined_string; if (is_ascii_) { - Handle seq = NewRawOneByteString(character_count_); - RETURN_IF_EMPTY_HANDLE_VALUE(heap_->isolate(), seq, Handle()); + Handle seq; + ASSIGN_RETURN_ON_EXCEPTION( + isolate, seq, + isolate->factory()->NewRawOneByteString(character_count_), + String); + DisallowHeapAllocation no_gc; uint8_t* char_buffer = seq->GetChars(); StringBuilderConcatHelper(*subject_, @@ -3452,8 +3457,12 @@ class ReplacementStringBuilder { joined_string = Handle::cast(seq); } else { // Non-ASCII. - Handle seq = NewRawTwoByteString(character_count_); - RETURN_IF_EMPTY_HANDLE_VALUE(heap_->isolate(), seq, Handle()); + Handle seq; + ASSIGN_RETURN_ON_EXCEPTION( + isolate, seq, + isolate->factory()->NewRawTwoByteString(character_count_), + String); + DisallowHeapAllocation no_gc; uc16* char_buffer = seq->GetChars(); StringBuilderConcatHelper(*subject_, @@ -3476,16 +3485,6 @@ class ReplacementStringBuilder { } private: - Handle NewRawOneByteString(int length) { - return heap_->isolate()->factory()->NewRawOneByteString(length); - } - - - Handle NewRawTwoByteString(int length) { - return heap_->isolate()->factory()->NewRawTwoByteString(length); - } - - void AddElement(Object* element) { ASSERT(element->IsSmi() || element->IsString()); ASSERT(array_builder_.capacity() > array_builder_.length()); @@ -3964,14 +3963,15 @@ MUST_USE_RESULT static MaybeObject* StringReplaceGlobalAtomRegExpWithString( int subject_pos = 0; int result_pos = 0; - Handle result_seq; + MaybeHandle maybe_res; if (ResultSeqString::kHasAsciiEncoding) { - result_seq = isolate->factory()->NewRawOneByteString(result_len); + maybe_res = isolate->factory()->NewRawOneByteString(result_len); } else { - result_seq = isolate->factory()->NewRawTwoByteString(result_len); + maybe_res = isolate->factory()->NewRawTwoByteString(result_len); } - RETURN_IF_EMPTY_HANDLE(isolate, result_seq); - Handle result = Handle::cast(result_seq); + Handle untyped_res; + ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, untyped_res, maybe_res); + Handle result = Handle::cast(untyped_res); for (int i = 0; i < matches; i++) { // Copy non-matched subject content. @@ -4100,8 +4100,8 @@ MUST_USE_RESULT static MaybeObject* StringReplaceGlobalRegExpWithString( capture_count, global_cache.LastSuccessfulMatch()); - Handle result = builder.ToString(); - RETURN_IF_EMPTY_HANDLE(isolate, result); + Handle result; + ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, builder.ToString()); return *result; } @@ -4146,12 +4146,11 @@ MUST_USE_RESULT static MaybeObject* StringReplaceGlobalRegExpWithEmptyString( Handle answer; if (ResultSeqString::kHasAsciiEncoding) { answer = Handle::cast( - isolate->factory()->NewRawOneByteString(new_length)); + isolate->factory()->NewRawOneByteString(new_length).ToHandleChecked()); } else { answer = Handle::cast( - isolate->factory()->NewRawTwoByteString(new_length)); + isolate->factory()->NewRawTwoByteString(new_length).ToHandleChecked()); } - ASSERT(!answer.is_null()); int prev = 0; int position = 0; @@ -6281,10 +6280,12 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_URIEscape) { CONVERT_ARG_HANDLE_CHECKED(String, source, 0); Handle string = FlattenGetString(source); ASSERT(string->IsFlat()); - Handle result = string->IsOneByteRepresentationUnderneath() - ? URIEscape::Escape(isolate, source) - : URIEscape::Escape(isolate, source); - RETURN_IF_EMPTY_HANDLE(isolate, result); + Handle result; + ASSIGN_RETURN_FAILURE_ON_EXCEPTION( + isolate, result, + string->IsOneByteRepresentationUnderneath() + ? URIEscape::Escape(isolate, source) + : URIEscape::Escape(isolate, source)); return *result; } @@ -6595,9 +6596,9 @@ MUST_USE_RESULT static MaybeObject* ConvertCase( // might break in the future if we implement more context and locale // dependent upper/lower conversions. if (s->IsOneByteRepresentationUnderneath()) { + // Same length as input. Handle result = - isolate->factory()->NewRawOneByteString(length); - ASSERT(!result.is_null()); // Same length as input. + isolate->factory()->NewRawOneByteString(length).ToHandleChecked(); DisallowHeapAllocation no_gc; String::FlatContent flat_content = s->GetFlatContent(); ASSERT(flat_content.IsFlat()); @@ -6611,13 +6612,12 @@ MUST_USE_RESULT static MaybeObject* ConvertCase( if (is_ascii) return has_changed_character ? *result : *s; } - Handle result; + Handle result; // Same length as input. if (s->IsOneByteRepresentation()) { - result = isolate->factory()->NewRawOneByteString(length); + result = isolate->factory()->NewRawOneByteString(length).ToHandleChecked(); } else { - result = isolate->factory()->NewRawTwoByteString(length); + result = isolate->factory()->NewRawTwoByteString(length).ToHandleChecked(); } - ASSERT(!result.is_null()); // Same length as input. MaybeObject* maybe = ConvertCaseHelper(isolate, *s, *result, length, mapping); Object* answer; @@ -6627,12 +6627,13 @@ MUST_USE_RESULT static MaybeObject* ConvertCase( ASSERT(answer->IsSmi()); length = Smi::cast(answer)->value(); if (s->IsOneByteRepresentation() && length > 0) { - result = isolate->factory()->NewRawOneByteString(length); + ASSIGN_RETURN_FAILURE_ON_EXCEPTION( + isolate, result, isolate->factory()->NewRawOneByteString(length)); } else { if (length < 0) length = -length; - result = isolate->factory()->NewRawTwoByteString(length); + ASSIGN_RETURN_FAILURE_ON_EXCEPTION( + isolate, result, isolate->factory()->NewRawTwoByteString(length)); } - RETURN_IF_EMPTY_HANDLE(isolate, result); return ConvertCaseHelper(isolate, *s, *result, length, mapping); } @@ -7266,9 +7267,10 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderJoin) { length += increment; } - Handle answer = - isolate->factory()->NewRawTwoByteString(length); - RETURN_IF_EMPTY_HANDLE(isolate, answer); + Handle answer; + ASSIGN_RETURN_FAILURE_ON_EXCEPTION( + isolate, answer, + isolate->factory()->NewRawTwoByteString(length)); DisallowHeapAllocation no_gc; diff --git a/src/uri.h b/src/uri.h index 1e73ddd3d2..58509ce807 100644 --- a/src/uri.h +++ b/src/uri.h @@ -129,9 +129,8 @@ Handle URIUnescape::UnescapeSlow( Handle second_part; ASSERT(unescaped_length <= String::kMaxLength); if (one_byte) { - Handle dest = - isolate->factory()->NewRawOneByteString(unescaped_length); - ASSERT(!dest.is_null()); + Handle dest = isolate->factory()->NewRawOneByteString( + unescaped_length).ToHandleChecked(); DisallowHeapAllocation no_allocation; Vector vector = GetCharVector(string); for (int i = start_index; i < length; dest_position++) { @@ -142,9 +141,8 @@ Handle URIUnescape::UnescapeSlow( } second_part = dest; } else { - Handle dest = - isolate->factory()->NewRawTwoByteString(unescaped_length); - ASSERT(!dest.is_null()); + Handle dest = isolate->factory()->NewRawTwoByteString( + unescaped_length).ToHandleChecked(); DisallowHeapAllocation no_allocation; Vector vector = GetCharVector(string); for (int i = start_index; i < length; dest_position++) { @@ -203,7 +201,7 @@ int URIUnescape::UnescapeChar(Vector vector, class URIEscape : public AllStatic { public: template - static Handle Escape(Isolate* isolate, Handle string); + static MaybeHandle Escape(Isolate* isolate, Handle string); private: static const char kHexChars[17]; @@ -247,7 +245,7 @@ const char URIEscape::kNotEscaped[] = { template -Handle URIEscape::Escape(Isolate* isolate, Handle string) { +MaybeHandle URIEscape::Escape(Isolate* isolate, Handle string) { ASSERT(string->IsFlat()); int escaped_length = 0; int length = string->length(); @@ -273,9 +271,11 @@ Handle URIEscape::Escape(Isolate* isolate, Handle string) { // No length change implies no change. Return original string if no change. if (escaped_length == length) return string; - Handle dest = - isolate->factory()->NewRawOneByteString(escaped_length); - RETURN_IF_EMPTY_HANDLE_VALUE(isolate, dest, Handle()); + Handle dest; + ASSIGN_RETURN_ON_EXCEPTION( + isolate, dest, + isolate->factory()->NewRawOneByteString(escaped_length), + String); int dest_position = 0; { DisallowHeapAllocation no_allocation; diff --git a/test/cctest/test-mementos.cc b/test/cctest/test-mementos.cc index 1dc38f9af5..a377b4a4c6 100644 --- a/test/cctest/test-mementos.cc +++ b/test/cctest/test-mementos.cc @@ -39,7 +39,8 @@ static void SetUpNewSpaceWithPoisonedMementoAtTop() { heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); // Allocate a string, the GC may suspect a memento behind the string. - Handle string = isolate->factory()->NewRawOneByteString(12); + Handle string = + isolate->factory()->NewRawOneByteString(12).ToHandleChecked(); CHECK(*string); // Create an allocation memento behind the string with a garbage allocation