Reviewers: Benedikt Meurer,

Description:
Make max size and max length of strings consistent.

[email protected]

Please review this at https://codereview.chromium.org/196133030/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files (+18, -31 lines):
  M src/heap-inl.h
  M src/heap.cc
  M src/hydrogen.cc
  M src/objects.h
  M src/runtime.cc


Index: src/heap-inl.h
diff --git a/src/heap-inl.h b/src/heap-inl.h
index c36a6fd795c0a235066da1013edde6a6d4e072c8..f7055fed557f74a9fa797b1c95909b9d1961eb10 100644
--- a/src/heap-inl.h
+++ b/src/heap-inl.h
@@ -170,7 +170,7 @@ MaybeObject* Heap::AllocateOneByteInternalizedString(Vector<const uint8_t> str,

MaybeObject* Heap::AllocateTwoByteInternalizedString(Vector<const uc16> str,
                                                      uint32_t hash_field) {
-  if (str.length() > SeqTwoByteString::kMaxLength) {
+  if (str.length() > String::kMaxLength) {
     return Failure::OutOfMemoryException(0x3);
   }
   // Compute map and object size.
Index: src/heap.cc
diff --git a/src/heap.cc b/src/heap.cc
index 6790fe9847c6fa52baf5c170fd27cbb149cc5f31..c7ec34e7b84a7aeaec191ffddfee33eb885837b8 100644
--- a/src/heap.cc
+++ b/src/heap.cc
@@ -4972,16 +4972,13 @@ MaybeObject* Heap::AllocateInternalizedStringImpl(
   int size;
   Map* map;

+  if (chars > String::kMaxLength) {
+    return Failure::OutOfMemoryException(0x9);
+  }
   if (is_one_byte) {
-    if (chars > SeqOneByteString::kMaxLength) {
-      return Failure::OutOfMemoryException(0x9);
-    }
     map = ascii_internalized_string_map();
     size = SeqOneByteString::SizeFor(chars);
   } else {
-    if (chars > SeqTwoByteString::kMaxLength) {
-      return Failure::OutOfMemoryException(0xa);
-    }
     map = internalized_string_map();
     size = SeqTwoByteString::SizeFor(chars);
   }
@@ -5047,7 +5044,7 @@ MaybeObject* Heap::AllocateRawOneByteString(int length,

 MaybeObject* Heap::AllocateRawTwoByteString(int length,
                                             PretenureFlag pretenure) {
-  if (length < 0 || length > SeqTwoByteString::kMaxLength) {
+  if (length < 0 || length > String::kMaxLength) {
     return Failure::OutOfMemoryException(0xc);
   }
   int size = SeqTwoByteString::SizeFor(length);
Index: src/hydrogen.cc
diff --git a/src/hydrogen.cc b/src/hydrogen.cc
index cbd53eeaee778c281e6ae565971a4987d41e13ff..f2c9361945be24770c403bcb7ac879f30a05ced9 100644
--- a/src/hydrogen.cc
+++ b/src/hydrogen.cc
@@ -1791,19 +1791,13 @@ HAllocate* HGraphBuilder::BuildAllocate(

 HValue* HGraphBuilder::BuildAddStringLengths(HValue* left_length,
                                              HValue* right_length) {
- // Compute the combined string length. If the result is larger than the max - // supported string length, we bailout to the runtime. This is done implicitly
-  // when converting the result back to a smi in case the max string length
- // equals the max smi value. Otherwise, for platforms with 32-bit smis, we do + // Compute the combined string length and check against max string length.
   HValue* length = AddUncasted<HAdd>(left_length, right_length);
-  STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue);
-  if (String::kMaxLength != Smi::kMaxValue) {
-    IfBuilder if_nooverflow(this);
-    if_nooverflow.If<HCompareNumericAndBranch>(
-        length, Add<HConstant>(String::kMaxLength), Token::LTE);
-    if_nooverflow.Then();
-    if_nooverflow.ElseDeopt("String length exceeds limit");
-  }
+  IfBuilder if_nooverflow(this);
+  if_nooverflow.If<HCompareNumericAndBranch>(
+      length, Add<HConstant>(String::kMaxLength), Token::LTE);
+  if_nooverflow.Then();
+  if_nooverflow.ElseDeopt("String length exceeds limit");
   return length;
 }

Index: src/objects.h
diff --git a/src/objects.h b/src/objects.h
index 85087270bef2f112a5561422b09fbf5df1f18a2c..ea9cc44536fddebe7093df56abdba010d00fec01 100644
--- a/src/objects.h
+++ b/src/objects.h
@@ -8902,7 +8902,7 @@ class String: public Name {
   static const int kEmptyStringHash = kIsNotArrayIndexMask;

   // Maximal string length.
-  static const int kMaxLength = (1 << (32 - 2)) - 1;
+  static const int kMaxLength = (1 << 28) - 16;

   // Max length for computing hash. For strings longer than this limit the
   // string length is used as the hash value.
@@ -9067,6 +9067,7 @@ class SeqOneByteString: public SeqString {
   // Maximal length of a single sequential ASCII string.
// Q.v. String::kMaxLength which is the maximal size of concatenated strings.
   static const int kMaxLength = (kMaxSize - kHeaderSize);
+  STATIC_CHECK((kMaxSize - kHeaderSize) >= String::kMaxLength);

  private:
   DISALLOW_IMPLICIT_CONSTRUCTORS(SeqOneByteString);
@@ -9109,6 +9110,8 @@ class SeqTwoByteString: public SeqString {
   // Maximal length of a single sequential two-byte string.
// Q.v. String::kMaxLength which is the maximal size of concatenated strings. static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t); + STATIC_CHECK(static_cast<int>((kMaxSize - kHeaderSize)/sizeof(uint16_t))
=
+               String::kMaxLength);

  private:
   DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
Index: src/runtime.cc
diff --git a/src/runtime.cc b/src/runtime.cc
index 79d29a02d357f26d472bdecf510ad80a935c89e0..6ea42bd4a9c9915b8d1a248eff906c7540fae95b 100644
--- a/src/runtime.cc
+++ b/src/runtime.cc
@@ -7315,12 +7315,6 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_SparseJoinWithSeparator) {
   // Find total length of join result.
   int string_length = 0;
   bool is_ascii = separator->IsOneByteRepresentation();
-  int max_string_length;
-  if (is_ascii) {
-    max_string_length = SeqOneByteString::kMaxLength;
-  } else {
-    max_string_length = SeqTwoByteString::kMaxLength;
-  }
   bool overflow = false;
   CONVERT_NUMBER_CHECKED(int, elements_length,
                          Int32, elements_array->length());
@@ -7333,10 +7327,9 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_SparseJoinWithSeparator) {
     int length = string->length();
     if (is_ascii && !string->IsOneByteRepresentation()) {
       is_ascii = false;
-      max_string_length = SeqTwoByteString::kMaxLength;
     }
-    if (length > max_string_length ||
-        max_string_length - length < string_length) {
+    if (length > String::kMaxLength ||
+        String::kMaxLength - length < string_length) {
       overflow = true;
       break;
     }
@@ -7346,7 +7339,7 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_SparseJoinWithSeparator) {
   if (!overflow && separator_length > 0) {
     if (array_length <= 0x7fffffffu) {
       int separator_count = static_cast<int>(array_length) - 1;
-      int remaining_length = max_string_length - string_length;
+      int remaining_length = String::kMaxLength - string_length;
       if ((remaining_length / separator_length) >= separator_count) {
         string_length += separator_length * (array_length - 1);
       } else {


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to