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
This commit is contained in:
yangguo@chromium.org 2014-04-03 12:30:08 +00:00
parent 5c0d85163c
commit 893f413434
8 changed files with 98 additions and 88 deletions

View File

@ -255,8 +255,8 @@ Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
}
Handle<SeqOneByteString> Factory::NewRawOneByteString(int length,
PretenureFlag pretenure) {
MaybeHandle<SeqOneByteString> Factory::NewRawOneByteString(
int length, PretenureFlag pretenure) {
CALL_HEAP_FUNCTION(
isolate(),
isolate()->heap()->AllocateRawOneByteString(length, pretenure),
@ -264,8 +264,8 @@ Handle<SeqOneByteString> Factory::NewRawOneByteString(int length,
}
Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length,
PretenureFlag pretenure) {
MaybeHandle<SeqTwoByteString> Factory::NewRawTwoByteString(
int length, PretenureFlag pretenure) {
CALL_HEAP_FUNCTION(
isolate(),
isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
@ -298,13 +298,15 @@ static inline Handle<String> MakeOrFindTwoCharacterString(Isolate* isolate,
if (static_cast<unsigned>(c1 | c2) <= String::kMaxOneByteCharCodeU) {
// We can do this.
ASSERT(IsPowerOf2(String::kMaxOneByteCharCodeU + 1)); // because of this.
Handle<SeqOneByteString> str = isolate->factory()->NewRawOneByteString(2);
Handle<SeqOneByteString> str =
isolate->factory()->NewRawOneByteString(2).ToHandleChecked();
uint8_t* dest = str->GetChars();
dest[0] = static_cast<uint8_t>(c1);
dest[1] = static_cast<uint8_t>(c2);
return str;
} else {
Handle<SeqTwoByteString> str = isolate->factory()->NewRawTwoByteString(2);
Handle<SeqTwoByteString> str =
isolate->factory()->NewRawTwoByteString(2).ToHandleChecked();
uc16* dest = str->GetChars();
dest[0] = c1;
dest[1] = c2;
@ -380,7 +382,8 @@ Handle<String> Factory::NewConsString(Handle<String> left,
STATIC_ASSERT(ConsString::kMinLength <= String::kMaxLength);
if (is_one_byte) {
Handle<SeqOneByteString> result = NewRawOneByteString(length);
Handle<SeqOneByteString> result =
NewRawOneByteString(length).ToHandleChecked();
DisallowHeapAllocation no_gc;
uint8_t* dest = result->GetChars();
// Copy left part.
@ -397,8 +400,10 @@ Handle<String> Factory::NewConsString(Handle<String> left,
}
return (is_one_byte_data_in_two_byte_string)
? ConcatStringContent<uint8_t>(NewRawOneByteString(length), left, right)
: ConcatStringContent<uc16>(NewRawTwoByteString(length), left, right);
? ConcatStringContent<uint8_t>(
NewRawOneByteString(length).ToHandleChecked(), left, right)
: ConcatStringContent<uc16>(
NewRawTwoByteString(length).ToHandleChecked(), left, right);
}
Handle<ConsString> result = NewRawConsString(
@ -422,10 +427,10 @@ Handle<String> Factory::NewFlatConcatString(Handle<String> first,
int total_length = first->length() + second->length();
if (first->IsOneByteRepresentation() && second->IsOneByteRepresentation()) {
return ConcatStringContent<uint8_t>(
NewRawOneByteString(total_length), first, second);
NewRawOneByteString(total_length).ToHandleChecked(), first, second);
} else {
return ConcatStringContent<uc16>(
NewRawTwoByteString(total_length), first, second);
NewRawTwoByteString(total_length).ToHandleChecked(), first, second);
}
}
@ -463,15 +468,15 @@ Handle<String> Factory::NewProperSubString(Handle<String> str,
if (!FLAG_string_slices || length < SlicedString::kMinLength) {
if (str->IsOneByteRepresentation()) {
Handle<SeqOneByteString> result = NewRawOneByteString(length);
ASSERT(!result.is_null());
Handle<SeqOneByteString> result =
NewRawOneByteString(length).ToHandleChecked();
uint8_t* dest = result->GetChars();
DisallowHeapAllocation no_gc;
String::WriteToFlat(*str, dest, begin, end);
return result;
} else {
Handle<SeqTwoByteString> result = NewRawTwoByteString(length);
ASSERT(!result.is_null());
Handle<SeqTwoByteString> result =
NewRawTwoByteString(length).ToHandleChecked();
uc16* dest = result->GetChars();
DisallowHeapAllocation no_gc;
String::WriteToFlat(*str, dest, begin, end);

View File

@ -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<SeqOneByteString> NewRawOneByteString(
MaybeHandle<SeqOneByteString> NewRawOneByteString(
int length,
PretenureFlag pretenure = NOT_TENURED);
Handle<SeqTwoByteString> NewRawTwoByteString(
MaybeHandle<SeqTwoByteString> NewRawTwoByteString(
int length,
PretenureFlag pretenure = NOT_TENURED);

View File

@ -72,7 +72,8 @@ class MaybeHandle {
return Handle<T>(location_);
}
INLINE(bool ToHandle(Handle<T>* out)) {
// Convert to a Handle with a type that can be upcasted to.
template <class S> INLINE(bool ToHandle(Handle<S>* out)) {
if (location_ == NULL) {
*out = Handle<T>::null();
return false;
@ -82,6 +83,8 @@ class MaybeHandle {
}
}
bool is_null() const { return location_ == NULL; }
protected:
T** location_;

View File

@ -583,14 +583,14 @@ template <>
inline Handle<SeqTwoByteString> NewRawString(Factory* factory,
int length,
PretenureFlag pretenure) {
return factory->NewRawTwoByteString(length, pretenure);
return factory->NewRawTwoByteString(length, pretenure).ToHandleChecked();
}
template <>
inline Handle<SeqOneByteString> NewRawString(Factory* factory,
int length,
PretenureFlag pretenure) {
return factory->NewRawOneByteString(length, pretenure);
return factory->NewRawOneByteString(length, pretenure).ToHandleChecked();
}
@ -606,7 +606,6 @@ Handle<String> JsonParser<seq_ascii>::SlowScanJsonString(
int length = Min(max_length, Max(kInitialSpecialStringLength, 2 * count));
Handle<StringType> seq_string =
NewRawString<StringType>(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<String> JsonParser<seq_ascii>::ScanJsonString() {
}
} while (c0_ != '"');
int length = position_ - beg_pos;
Handle<String> result = factory()->NewRawOneByteString(length, pretenure_);
ASSERT(!result.is_null());
Handle<String> result =
factory()->NewRawOneByteString(length, pretenure_).ToHandleChecked();
uint8_t* dest = SeqOneByteString::cast(*result)->GetChars();
String::WriteToFlat(*source_, dest, beg_pos, position_);

View File

@ -265,8 +265,7 @@ BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate)
accumulator_store_ = Handle<JSValue>::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<String> result =
isolate->factory()->NewRawOneByteString(worst_case_length);
ASSERT(!result.is_null());
Handle<String> result = isolate->factory()->NewRawOneByteString(
worst_case_length).ToHandleChecked();
DisallowHeapAllocation no_gc;
return StringifyString_<SeqOneByteString>(
isolate,
object->GetFlatContent().ToOneByteVector(),
result);
} else {
Handle<String> result =
isolate->factory()->NewRawTwoByteString(worst_case_length);
ASSERT(!result.is_null());
Handle<String> result = isolate->factory()->NewRawTwoByteString(
worst_case_length).ToHandleChecked();
DisallowHeapAllocation no_gc;
return StringifyString_<SeqTwoByteString>(
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;

View File

@ -3434,15 +3434,20 @@ class ReplacementStringBuilder {
}
Handle<String> ToString() {
MaybeHandle<String> ToString() {
Isolate* isolate = heap_->isolate();
if (array_builder_.length() == 0) {
return heap_->isolate()->factory()->empty_string();
return isolate->factory()->empty_string();
}
Handle<String> joined_string;
if (is_ascii_) {
Handle<SeqOneByteString> seq = NewRawOneByteString(character_count_);
RETURN_IF_EMPTY_HANDLE_VALUE(heap_->isolate(), seq, Handle<String>());
Handle<SeqOneByteString> 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<String>::cast(seq);
} else {
// Non-ASCII.
Handle<SeqTwoByteString> seq = NewRawTwoByteString(character_count_);
RETURN_IF_EMPTY_HANDLE_VALUE(heap_->isolate(), seq, Handle<String>());
Handle<SeqTwoByteString> 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<SeqOneByteString> NewRawOneByteString(int length) {
return heap_->isolate()->factory()->NewRawOneByteString(length);
}
Handle<SeqTwoByteString> 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<String> result_seq;
MaybeHandle<SeqString> 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<ResultSeqString> result = Handle<ResultSeqString>::cast(result_seq);
Handle<SeqString> untyped_res;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, untyped_res, maybe_res);
Handle<ResultSeqString> result = Handle<ResultSeqString>::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<String> result = builder.ToString();
RETURN_IF_EMPTY_HANDLE(isolate, result);
Handle<String> result;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, builder.ToString());
return *result;
}
@ -4146,12 +4146,11 @@ MUST_USE_RESULT static MaybeObject* StringReplaceGlobalRegExpWithEmptyString(
Handle<ResultSeqString> answer;
if (ResultSeqString::kHasAsciiEncoding) {
answer = Handle<ResultSeqString>::cast(
isolate->factory()->NewRawOneByteString(new_length));
isolate->factory()->NewRawOneByteString(new_length).ToHandleChecked());
} else {
answer = Handle<ResultSeqString>::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> string = FlattenGetString(source);
ASSERT(string->IsFlat());
Handle<String> result = string->IsOneByteRepresentationUnderneath()
? URIEscape::Escape<uint8_t>(isolate, source)
: URIEscape::Escape<uc16>(isolate, source);
RETURN_IF_EMPTY_HANDLE(isolate, result);
Handle<String> result;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, result,
string->IsOneByteRepresentationUnderneath()
? URIEscape::Escape<uint8_t>(isolate, source)
: URIEscape::Escape<uc16>(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<SeqOneByteString> 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<SeqString> result;
Handle<SeqString> 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<SeqTwoByteString> answer =
isolate->factory()->NewRawTwoByteString(length);
RETURN_IF_EMPTY_HANDLE(isolate, answer);
Handle<SeqTwoByteString> answer;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, answer,
isolate->factory()->NewRawTwoByteString(length));
DisallowHeapAllocation no_gc;

View File

@ -129,9 +129,8 @@ Handle<String> URIUnescape::UnescapeSlow(
Handle<String> second_part;
ASSERT(unescaped_length <= String::kMaxLength);
if (one_byte) {
Handle<SeqOneByteString> dest =
isolate->factory()->NewRawOneByteString(unescaped_length);
ASSERT(!dest.is_null());
Handle<SeqOneByteString> dest = isolate->factory()->NewRawOneByteString(
unescaped_length).ToHandleChecked();
DisallowHeapAllocation no_allocation;
Vector<const Char> vector = GetCharVector<Char>(string);
for (int i = start_index; i < length; dest_position++) {
@ -142,9 +141,8 @@ Handle<String> URIUnescape::UnescapeSlow(
}
second_part = dest;
} else {
Handle<SeqTwoByteString> dest =
isolate->factory()->NewRawTwoByteString(unescaped_length);
ASSERT(!dest.is_null());
Handle<SeqTwoByteString> dest = isolate->factory()->NewRawTwoByteString(
unescaped_length).ToHandleChecked();
DisallowHeapAllocation no_allocation;
Vector<const Char> vector = GetCharVector<Char>(string);
for (int i = start_index; i < length; dest_position++) {
@ -203,7 +201,7 @@ int URIUnescape::UnescapeChar(Vector<const Char> vector,
class URIEscape : public AllStatic {
public:
template<typename Char>
static Handle<String> Escape(Isolate* isolate, Handle<String> string);
static MaybeHandle<String> Escape(Isolate* isolate, Handle<String> string);
private:
static const char kHexChars[17];
@ -247,7 +245,7 @@ const char URIEscape::kNotEscaped[] = {
template<typename Char>
Handle<String> URIEscape::Escape(Isolate* isolate, Handle<String> string) {
MaybeHandle<String> URIEscape::Escape(Isolate* isolate, Handle<String> string) {
ASSERT(string->IsFlat());
int escaped_length = 0;
int length = string->length();
@ -273,9 +271,11 @@ Handle<String> URIEscape::Escape(Isolate* isolate, Handle<String> string) {
// No length change implies no change. Return original string if no change.
if (escaped_length == length) return string;
Handle<SeqOneByteString> dest =
isolate->factory()->NewRawOneByteString(escaped_length);
RETURN_IF_EMPTY_HANDLE_VALUE(isolate, dest, Handle<String>());
Handle<SeqOneByteString> dest;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, dest,
isolate->factory()->NewRawOneByteString(escaped_length),
String);
int dest_position = 0;
{ DisallowHeapAllocation no_allocation;

View File

@ -39,7 +39,8 @@ static void SetUpNewSpaceWithPoisonedMementoAtTop() {
heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask);
// Allocate a string, the GC may suspect a memento behind the string.
Handle<SeqOneByteString> string = isolate->factory()->NewRawOneByteString(12);
Handle<SeqOneByteString> string =
isolate->factory()->NewRawOneByteString(12).ToHandleChecked();
CHECK(*string);
// Create an allocation memento behind the string with a garbage allocation