OLD | NEW |
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 4766 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4777 for (; i < length; i++) { | 4777 for (; i < length; i++) { |
4778 hasher.AddCharacterNoIndex(chars[i]); | 4778 hasher.AddCharacterNoIndex(chars[i]); |
4779 } | 4779 } |
4780 } | 4780 } |
4781 return hasher.GetHashField(); | 4781 return hasher.GetHashField(); |
4782 } | 4782 } |
4783 | 4783 |
4784 | 4784 |
4785 uint32_t String::ComputeAndSetHash() { | 4785 uint32_t String::ComputeAndSetHash() { |
4786 // Should only be called if hash code has not yet been computed. | 4786 // Should only be called if hash code has not yet been computed. |
4787 ASSERT(!(hash_field() & kHashComputedMask)); | 4787 ASSERT(!HasHashCode()); |
4788 | 4788 |
4789 const int len = length(); | 4789 const int len = length(); |
4790 | 4790 |
4791 // Compute the hash code. | 4791 // Compute the hash code. |
4792 uint32_t field = 0; | 4792 uint32_t field = 0; |
4793 if (StringShape(this).IsSequentialAscii()) { | 4793 if (StringShape(this).IsSequentialAscii()) { |
4794 field = HashSequentialString(SeqAsciiString::cast(this)->GetChars(), len); | 4794 field = HashSequentialString(SeqAsciiString::cast(this)->GetChars(), len); |
4795 } else if (StringShape(this).IsSequentialTwoByte()) { | 4795 } else if (StringShape(this).IsSequentialTwoByte()) { |
4796 field = HashSequentialString(SeqTwoByteString::cast(this)->GetChars(), len); | 4796 field = HashSequentialString(SeqTwoByteString::cast(this)->GetChars(), len); |
4797 } else { | 4797 } else { |
4798 StringInputBuffer buffer(this); | 4798 StringInputBuffer buffer(this); |
4799 field = ComputeHashField(&buffer, len); | 4799 field = ComputeHashField(&buffer, len); |
4800 } | 4800 } |
4801 | 4801 |
4802 // Store the hash code in the object. | 4802 // Store the hash code in the object. |
4803 set_hash_field(field); | 4803 set_hash_field(field); |
4804 | 4804 |
4805 // Check the hash code is there. | 4805 // Check the hash code is there. |
4806 ASSERT(hash_field() & kHashComputedMask); | 4806 ASSERT(HasHashCode()); |
4807 uint32_t result = field >> kHashShift; | 4807 uint32_t result = field >> kHashShift; |
4808 ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. | 4808 ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
4809 return result; | 4809 return result; |
4810 } | 4810 } |
4811 | 4811 |
4812 | 4812 |
4813 bool String::ComputeArrayIndex(unibrow::CharacterStream* buffer, | 4813 bool String::ComputeArrayIndex(unibrow::CharacterStream* buffer, |
4814 uint32_t* index, | 4814 uint32_t* index, |
4815 int length) { | 4815 int length) { |
4816 if (length == 0 || length > kMaxArrayIndexSize) return false; | 4816 if (length == 0 || length > kMaxArrayIndexSize) return false; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4851 } else { | 4851 } else { |
4852 StringInputBuffer buffer(this); | 4852 StringInputBuffer buffer(this); |
4853 return ComputeArrayIndex(&buffer, index, length()); | 4853 return ComputeArrayIndex(&buffer, index, length()); |
4854 } | 4854 } |
4855 } | 4855 } |
4856 | 4856 |
4857 | 4857 |
4858 static inline uint32_t HashField(uint32_t hash, | 4858 static inline uint32_t HashField(uint32_t hash, |
4859 bool is_array_index, | 4859 bool is_array_index, |
4860 int length = -1) { | 4860 int length = -1) { |
4861 uint32_t result = | 4861 uint32_t result = (hash << String::kHashShift); |
4862 (hash << String::kHashShift) | String::kHashComputedMask; | |
4863 if (is_array_index) { | 4862 if (is_array_index) { |
4864 // For array indexes mix the length into the hash as an array index could | 4863 // For array indexes mix the length into the hash as an array index could |
4865 // be zero. | 4864 // be zero. |
4866 ASSERT(length > 0); | 4865 ASSERT(length > 0); |
4867 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) < | 4866 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) < |
4868 (1 << String::kArrayIndexValueBits)); | 4867 (1 << String::kArrayIndexValueBits)); |
4869 result |= String::kIsArrayIndexMask; | 4868 result |= String::kIsArrayIndexMask; |
4870 result |= length << String::kArrayIndexHashLengthShift; | 4869 result |= length << String::kArrayIndexHashLengthShift; |
4871 } | 4870 } |
4872 return result; | 4871 return result; |
(...skipping 759 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5632 } | 5631 } |
5633 default: | 5632 default: |
5634 UNREACHABLE(); | 5633 UNREACHABLE(); |
5635 break; | 5634 break; |
5636 } | 5635 } |
5637 } | 5636 } |
5638 | 5637 |
5639 // General slow case. | 5638 // General slow case. |
5640 if (len->IsNumber()) { | 5639 if (len->IsNumber()) { |
5641 uint32_t length; | 5640 uint32_t length; |
5642 if (Array::IndexFromObject(len, &length)) { | 5641 if (len->ToArrayIndex(&length)) { |
5643 return SetSlowElements(len); | 5642 return SetSlowElements(len); |
5644 } else { | 5643 } else { |
5645 return ArrayLengthRangeError(); | 5644 return ArrayLengthRangeError(); |
5646 } | 5645 } |
5647 } | 5646 } |
5648 | 5647 |
5649 // len is not a number so make the array size one and | 5648 // len is not a number so make the array size one and |
5650 // set only element to len. | 5649 // set only element to len. |
5651 Object* obj = Heap::AllocateFixedArray(1); | 5650 Object* obj = Heap::AllocateFixedArray(1); |
5652 if (obj->IsFailure()) return obj; | 5651 if (obj->IsFailure()) return obj; |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6056 return value; | 6055 return value; |
6057 } | 6056 } |
6058 } | 6057 } |
6059 | 6058 |
6060 // Check whether there is extra space in fixed array.. | 6059 // Check whether there is extra space in fixed array.. |
6061 if (index < elms_length) { | 6060 if (index < elms_length) { |
6062 elms->set(index, value); | 6061 elms->set(index, value); |
6063 if (IsJSArray()) { | 6062 if (IsJSArray()) { |
6064 // Update the length of the array if needed. | 6063 // Update the length of the array if needed. |
6065 uint32_t array_length = 0; | 6064 uint32_t array_length = 0; |
6066 CHECK(Array::IndexFromObject(JSArray::cast(this)->length(), | 6065 CHECK(JSArray::cast(this)->length()->ToArrayIndex(&array_length)); |
6067 &array_length)); | |
6068 if (index >= array_length) { | 6066 if (index >= array_length) { |
6069 JSArray::cast(this)->set_length(Smi::FromInt(index + 1)); | 6067 JSArray::cast(this)->set_length(Smi::FromInt(index + 1)); |
6070 } | 6068 } |
6071 } | 6069 } |
6072 return value; | 6070 return value; |
6073 } | 6071 } |
6074 | 6072 |
6075 // Allow gap in fast case. | 6073 // Allow gap in fast case. |
6076 if ((index - elms_length) < kMaxGap) { | 6074 if ((index - elms_length) < kMaxGap) { |
6077 // Try allocating extra space. | 6075 // Try allocating extra space. |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6195 JSArray* array = JSArray::cast(this); | 6193 JSArray* array = JSArray::cast(this); |
6196 Object* return_value = array->JSArrayUpdateLengthFromIndex(index, | 6194 Object* return_value = array->JSArrayUpdateLengthFromIndex(index, |
6197 value); | 6195 value); |
6198 if (return_value->IsFailure()) return return_value; | 6196 if (return_value->IsFailure()) return return_value; |
6199 } | 6197 } |
6200 | 6198 |
6201 // Attempt to put this object back in fast case. | 6199 // Attempt to put this object back in fast case. |
6202 if (ShouldConvertToFastElements()) { | 6200 if (ShouldConvertToFastElements()) { |
6203 uint32_t new_length = 0; | 6201 uint32_t new_length = 0; |
6204 if (IsJSArray()) { | 6202 if (IsJSArray()) { |
6205 CHECK(Array::IndexFromObject(JSArray::cast(this)->length(), | 6203 CHECK(JSArray::cast(this)->length()->ToArrayIndex(&new_length)); |
6206 &new_length)); | |
6207 JSArray::cast(this)->set_length(Smi::FromInt(new_length)); | 6204 JSArray::cast(this)->set_length(Smi::FromInt(new_length)); |
6208 } else { | 6205 } else { |
6209 new_length = NumberDictionary::cast(elements())->max_number_key() + 1; | 6206 new_length = NumberDictionary::cast(elements())->max_number_key() + 1; |
6210 } | 6207 } |
6211 Object* obj = Heap::AllocateFixedArrayWithHoles(new_length); | 6208 Object* obj = Heap::AllocateFixedArrayWithHoles(new_length); |
6212 if (obj->IsFailure()) return obj; | 6209 if (obj->IsFailure()) return obj; |
6213 SetFastElements(FixedArray::cast(obj)); | 6210 SetFastElements(FixedArray::cast(obj)); |
6214 #ifdef DEBUG | 6211 #ifdef DEBUG |
6215 if (FLAG_trace_normalization) { | 6212 if (FLAG_trace_normalization) { |
6216 PrintF("Object elements are fast case again:\n"); | 6213 PrintF("Object elements are fast case again:\n"); |
(...skipping 10 matching lines...) Expand all Loading... |
6227 } | 6224 } |
6228 // All possible cases have been handled above. Add a return to avoid the | 6225 // All possible cases have been handled above. Add a return to avoid the |
6229 // complaints from the compiler. | 6226 // complaints from the compiler. |
6230 UNREACHABLE(); | 6227 UNREACHABLE(); |
6231 return Heap::null_value(); | 6228 return Heap::null_value(); |
6232 } | 6229 } |
6233 | 6230 |
6234 | 6231 |
6235 Object* JSArray::JSArrayUpdateLengthFromIndex(uint32_t index, Object* value) { | 6232 Object* JSArray::JSArrayUpdateLengthFromIndex(uint32_t index, Object* value) { |
6236 uint32_t old_len = 0; | 6233 uint32_t old_len = 0; |
6237 CHECK(Array::IndexFromObject(length(), &old_len)); | 6234 CHECK(length()->ToArrayIndex(&old_len)); |
6238 // Check to see if we need to update the length. For now, we make | 6235 // Check to see if we need to update the length. For now, we make |
6239 // sure that the length stays within 32-bits (unsigned). | 6236 // sure that the length stays within 32-bits (unsigned). |
6240 if (index >= old_len && index != 0xffffffff) { | 6237 if (index >= old_len && index != 0xffffffff) { |
6241 Object* len = | 6238 Object* len = |
6242 Heap::NumberFromDouble(static_cast<double>(index) + 1); | 6239 Heap::NumberFromDouble(static_cast<double>(index) + 1); |
6243 if (len->IsFailure()) return len; | 6240 if (len->IsFailure()) return len; |
6244 set_length(len); | 6241 set_length(len); |
6245 } | 6242 } |
6246 return value; | 6243 return value; |
6247 } | 6244 } |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6509 // dictionary, we cannot go back to fast case. | 6506 // dictionary, we cannot go back to fast case. |
6510 if (dictionary->requires_slow_elements()) return false; | 6507 if (dictionary->requires_slow_elements()) return false; |
6511 // An object requiring access checks is never allowed to have fast | 6508 // An object requiring access checks is never allowed to have fast |
6512 // elements. If it had fast elements we would skip security checks. | 6509 // elements. If it had fast elements we would skip security checks. |
6513 if (IsAccessCheckNeeded()) return false; | 6510 if (IsAccessCheckNeeded()) return false; |
6514 // If the dictionary backing storage takes up roughly half as much | 6511 // If the dictionary backing storage takes up roughly half as much |
6515 // space as a fast-case backing storage would the array should have | 6512 // space as a fast-case backing storage would the array should have |
6516 // fast elements. | 6513 // fast elements. |
6517 uint32_t length = 0; | 6514 uint32_t length = 0; |
6518 if (IsJSArray()) { | 6515 if (IsJSArray()) { |
6519 CHECK(Array::IndexFromObject(JSArray::cast(this)->length(), &length)); | 6516 CHECK(JSArray::cast(this)->length()->ToArrayIndex(&length)); |
6520 } else { | 6517 } else { |
6521 length = dictionary->max_number_key(); | 6518 length = dictionary->max_number_key(); |
6522 } | 6519 } |
6523 return static_cast<uint32_t>(dictionary->Capacity()) >= | 6520 return static_cast<uint32_t>(dictionary->Capacity()) >= |
6524 (length / (2 * NumberDictionary::kEntrySize)); | 6521 (length / (2 * NumberDictionary::kEntrySize)); |
6525 } | 6522 } |
6526 | 6523 |
6527 | 6524 |
6528 // Certain compilers request function template instantiation when they | 6525 // Certain compilers request function template instantiation when they |
6529 // see the definition of the other template functions in the | 6526 // see the definition of the other template functions in the |
(...skipping 2181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8711 if (break_point_objects()->IsUndefined()) return 0; | 8708 if (break_point_objects()->IsUndefined()) return 0; |
8712 // Single beak point. | 8709 // Single beak point. |
8713 if (!break_point_objects()->IsFixedArray()) return 1; | 8710 if (!break_point_objects()->IsFixedArray()) return 1; |
8714 // Multiple break points. | 8711 // Multiple break points. |
8715 return FixedArray::cast(break_point_objects())->length(); | 8712 return FixedArray::cast(break_point_objects())->length(); |
8716 } | 8713 } |
8717 #endif | 8714 #endif |
8718 | 8715 |
8719 | 8716 |
8720 } } // namespace v8::internal | 8717 } } // namespace v8::internal |
OLD | NEW |