Index: src/factory.cc |
diff --git a/src/factory.cc b/src/factory.cc |
index 47f965be7daf1555eb25b1cd1f34971626a9d705..108d94778bf1d89e99641416089400bb0903091b 100644 |
--- a/src/factory.cc |
+++ b/src/factory.cc |
@@ -278,6 +278,7 @@ Handle<String> Factory::InternalizeStringWithKey(StringTableKey* key) { |
MaybeHandle<String> Factory::NewStringFromOneByte(Vector<const uint8_t> string, |
PretenureFlag pretenure) { |
int length = string.length(); |
+ if (length == 0) return empty_string(); |
if (length == 1) return LookupSingleCharacterStringFromCode(string[0]); |
Handle<SeqOneByteString> result; |
ASSIGN_RETURN_ON_EXCEPTION( |
@@ -371,6 +372,7 @@ MaybeHandle<String> Factory::NewStringFromUtf8SubString( |
MaybeHandle<String> Factory::NewStringFromTwoByte(const uc16* string, |
int length, |
PretenureFlag pretenure) { |
+ if (length == 0) return empty_string(); |
if (String::IsOneByte(string, length)) { |
if (length == 1) return LookupSingleCharacterStringFromCode(string[0]); |
Handle<SeqOneByteString> result; |
@@ -455,39 +457,63 @@ Handle<String> Factory::NewInternalizedStringImpl( |
String); |
} |
-MaybeHandle<Map> Factory::InternalizedStringMapForString( |
- Handle<String> string) { |
- // If the string is in new space it cannot be used as internalized. |
- if (isolate()->heap()->InNewSpace(*string)) return MaybeHandle<Map>(); |
+namespace { |
- // Find the corresponding internalized string map for strings. |
+MaybeHandle<Map> GetInternalizedStringMap(Factory* f, Handle<String> string) { |
switch (string->map()->instance_type()) { |
case STRING_TYPE: |
- return internalized_string_map(); |
+ return f->internalized_string_map(); |
case ONE_BYTE_STRING_TYPE: |
- return one_byte_internalized_string_map(); |
+ return f->one_byte_internalized_string_map(); |
case EXTERNAL_STRING_TYPE: |
- return external_internalized_string_map(); |
+ return f->external_internalized_string_map(); |
case EXTERNAL_ONE_BYTE_STRING_TYPE: |
- return external_one_byte_internalized_string_map(); |
+ return f->external_one_byte_internalized_string_map(); |
case EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE: |
- return external_internalized_string_with_one_byte_data_map(); |
+ return f->external_internalized_string_with_one_byte_data_map(); |
case SHORT_EXTERNAL_STRING_TYPE: |
- return short_external_internalized_string_map(); |
+ return f->short_external_internalized_string_map(); |
case SHORT_EXTERNAL_ONE_BYTE_STRING_TYPE: |
- return short_external_one_byte_internalized_string_map(); |
+ return f->short_external_one_byte_internalized_string_map(); |
case SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE: |
- return short_external_internalized_string_with_one_byte_data_map(); |
+ return f->short_external_internalized_string_with_one_byte_data_map(); |
default: return MaybeHandle<Map>(); // No match found. |
} |
} |
+} // namespace |
+ |
+MaybeHandle<Map> Factory::InternalizedStringMapForString( |
+ Handle<String> string) { |
+ // If the string is in new space it cannot be used as internalized. |
+ if (isolate()->heap()->InNewSpace(*string)) return MaybeHandle<Map>(); |
+ |
+ return GetInternalizedStringMap(this, string); |
+} |
+ |
+template <class StringClass> |
+Handle<StringClass> Factory::InternalizeExternalString(Handle<String> string) { |
+ Handle<StringClass> cast_string = Handle<StringClass>::cast(string); |
+ Handle<Map> map = GetInternalizedStringMap(this, string).ToHandleChecked(); |
+ Handle<StringClass> external_string = New<StringClass>(map, OLD_SPACE); |
+ external_string->set_length(cast_string->length()); |
+ external_string->set_hash_field(cast_string->hash_field()); |
+ external_string->set_resource(nullptr); |
+ isolate()->heap()->RegisterExternalString(*external_string); |
+ return external_string; |
+} |
+ |
+template Handle<ExternalOneByteString> |
+ Factory::InternalizeExternalString<ExternalOneByteString>(Handle<String>); |
+template Handle<ExternalTwoByteString> |
+ Factory::InternalizeExternalString<ExternalTwoByteString>(Handle<String>); |
MaybeHandle<SeqOneByteString> Factory::NewRawOneByteString( |
int length, PretenureFlag pretenure) { |
if (length > String::kMaxLength || length < 0) { |
THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), SeqOneByteString); |
} |
+ DCHECK(length > 0); // Use Factory::empty_string() instead. |
CALL_HEAP_FUNCTION( |
isolate(), |
isolate()->heap()->AllocateRawOneByteString(length, pretenure), |
@@ -500,6 +526,7 @@ MaybeHandle<SeqTwoByteString> Factory::NewRawTwoByteString( |
if (length > String::kMaxLength || length < 0) { |
THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), SeqTwoByteString); |
} |
+ DCHECK(length > 0); // Use Factory::empty_string() instead. |
CALL_HEAP_FUNCTION( |
isolate(), |
isolate()->heap()->AllocateRawTwoByteString(length, pretenure), |
@@ -588,6 +615,12 @@ Handle<String> ConcatStringContent(Handle<StringType> result, |
MaybeHandle<String> Factory::NewConsString(Handle<String> left, |
Handle<String> right) { |
+ if (left->IsThinString()) { |
+ left = handle(Handle<ThinString>::cast(left)->actual(), isolate()); |
+ } |
+ if (right->IsThinString()) { |
+ right = handle(Handle<ThinString>::cast(right)->actual(), isolate()); |
+ } |
int left_length = left->length(); |
if (left_length == 0) return right; |
int right_length = right->length(); |