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 4665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4676 for (; i < length; i++) { | 4676 for (; i < length; i++) { |
4677 hasher.AddCharacterNoIndex(chars[i]); | 4677 hasher.AddCharacterNoIndex(chars[i]); |
4678 } | 4678 } |
4679 } | 4679 } |
4680 return hasher.GetHashField(); | 4680 return hasher.GetHashField(); |
4681 } | 4681 } |
4682 | 4682 |
4683 | 4683 |
4684 uint32_t String::ComputeAndSetHash() { | 4684 uint32_t String::ComputeAndSetHash() { |
4685 // Should only be called if hash code has not yet been computed. | 4685 // Should only be called if hash code has not yet been computed. |
4686 ASSERT(!HasHashCode()); | 4686 ASSERT(!(hash_field() & kHashComputedMask)); |
4687 | 4687 |
4688 const int len = length(); | 4688 const int len = length(); |
4689 | 4689 |
4690 // Compute the hash code. | 4690 // Compute the hash code. |
4691 uint32_t field = 0; | 4691 uint32_t field = 0; |
4692 if (StringShape(this).IsSequentialAscii()) { | 4692 if (StringShape(this).IsSequentialAscii()) { |
4693 field = HashSequentialString(SeqAsciiString::cast(this)->GetChars(), len); | 4693 field = HashSequentialString(SeqAsciiString::cast(this)->GetChars(), len); |
4694 } else if (StringShape(this).IsSequentialTwoByte()) { | 4694 } else if (StringShape(this).IsSequentialTwoByte()) { |
4695 field = HashSequentialString(SeqTwoByteString::cast(this)->GetChars(), len); | 4695 field = HashSequentialString(SeqTwoByteString::cast(this)->GetChars(), len); |
4696 } else { | 4696 } else { |
4697 StringInputBuffer buffer(this); | 4697 StringInputBuffer buffer(this); |
4698 field = ComputeHashField(&buffer, len); | 4698 field = ComputeHashField(&buffer, len); |
4699 } | 4699 } |
4700 | 4700 |
4701 // Store the hash code in the object. | 4701 // Store the hash code in the object. |
4702 set_hash_field(field); | 4702 set_hash_field(field); |
4703 | 4703 |
4704 // Check the hash code is there. | 4704 // Check the hash code is there. |
4705 ASSERT(HasHashCode()); | 4705 ASSERT(hash_field() & kHashComputedMask); |
4706 uint32_t result = field >> kHashShift; | 4706 uint32_t result = field >> kHashShift; |
4707 ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. | 4707 ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
4708 return result; | 4708 return result; |
4709 } | 4709 } |
4710 | 4710 |
4711 | 4711 |
4712 bool String::ComputeArrayIndex(unibrow::CharacterStream* buffer, | 4712 bool String::ComputeArrayIndex(unibrow::CharacterStream* buffer, |
4713 uint32_t* index, | 4713 uint32_t* index, |
4714 int length) { | 4714 int length) { |
4715 if (length == 0 || length > kMaxArrayIndexSize) return false; | 4715 if (length == 0 || length > kMaxArrayIndexSize) return false; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4750 } else { | 4750 } else { |
4751 StringInputBuffer buffer(this); | 4751 StringInputBuffer buffer(this); |
4752 return ComputeArrayIndex(&buffer, index, length()); | 4752 return ComputeArrayIndex(&buffer, index, length()); |
4753 } | 4753 } |
4754 } | 4754 } |
4755 | 4755 |
4756 | 4756 |
4757 static inline uint32_t HashField(uint32_t hash, | 4757 static inline uint32_t HashField(uint32_t hash, |
4758 bool is_array_index, | 4758 bool is_array_index, |
4759 int length = -1) { | 4759 int length = -1) { |
4760 uint32_t result = (hash << String::kHashShift); | 4760 uint32_t result = |
| 4761 (hash << String::kHashShift) | String::kHashComputedMask; |
4761 if (is_array_index) { | 4762 if (is_array_index) { |
4762 // For array indexes mix the length into the hash as an array index could | 4763 // For array indexes mix the length into the hash as an array index could |
4763 // be zero. | 4764 // be zero. |
4764 ASSERT(length > 0); | 4765 ASSERT(length > 0); |
4765 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) < | 4766 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) < |
4766 (1 << String::kArrayIndexValueBits)); | 4767 (1 << String::kArrayIndexValueBits)); |
4767 result |= String::kIsArrayIndexMask; | 4768 result |= String::kIsArrayIndexMask; |
4768 result |= length << String::kArrayIndexHashLengthShift; | 4769 result |= length << String::kArrayIndexHashLengthShift; |
4769 } | 4770 } |
4770 return result; | 4771 return result; |
(...skipping 774 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5545 } | 5546 } |
5546 default: | 5547 default: |
5547 UNREACHABLE(); | 5548 UNREACHABLE(); |
5548 break; | 5549 break; |
5549 } | 5550 } |
5550 } | 5551 } |
5551 | 5552 |
5552 // General slow case. | 5553 // General slow case. |
5553 if (len->IsNumber()) { | 5554 if (len->IsNumber()) { |
5554 uint32_t length; | 5555 uint32_t length; |
5555 if (len->ToArrayIndex(&length)) { | 5556 if (Array::IndexFromObject(len, &length)) { |
5556 return SetSlowElements(len); | 5557 return SetSlowElements(len); |
5557 } else { | 5558 } else { |
5558 return ArrayLengthRangeError(); | 5559 return ArrayLengthRangeError(); |
5559 } | 5560 } |
5560 } | 5561 } |
5561 | 5562 |
5562 // len is not a number so make the array size one and | 5563 // len is not a number so make the array size one and |
5563 // set only element to len. | 5564 // set only element to len. |
5564 Object* obj = Heap::AllocateFixedArray(1); | 5565 Object* obj = Heap::AllocateFixedArray(1); |
5565 if (obj->IsFailure()) return obj; | 5566 if (obj->IsFailure()) return obj; |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5868 return SetPropertyWithDefinedSetter(JSFunction::cast(setter), value); | 5869 return SetPropertyWithDefinedSetter(JSFunction::cast(setter), value); |
5869 } | 5870 } |
5870 } | 5871 } |
5871 | 5872 |
5872 // Check whether there is extra space in fixed array.. | 5873 // Check whether there is extra space in fixed array.. |
5873 if (index < elms_length) { | 5874 if (index < elms_length) { |
5874 elms->set(index, value); | 5875 elms->set(index, value); |
5875 if (IsJSArray()) { | 5876 if (IsJSArray()) { |
5876 // Update the length of the array if needed. | 5877 // Update the length of the array if needed. |
5877 uint32_t array_length = 0; | 5878 uint32_t array_length = 0; |
5878 CHECK(JSArray::cast(this)->length()->ToArrayIndex(&array_length)); | 5879 CHECK(Array::IndexFromObject(JSArray::cast(this)->length(), |
| 5880 &array_length)); |
5879 if (index >= array_length) { | 5881 if (index >= array_length) { |
5880 JSArray::cast(this)->set_length(Smi::FromInt(index + 1)); | 5882 JSArray::cast(this)->set_length(Smi::FromInt(index + 1)); |
5881 } | 5883 } |
5882 } | 5884 } |
5883 return value; | 5885 return value; |
5884 } | 5886 } |
5885 | 5887 |
5886 // Allow gap in fast case. | 5888 // Allow gap in fast case. |
5887 if ((index - elms_length) < kMaxGap) { | 5889 if ((index - elms_length) < kMaxGap) { |
5888 // Try allocating extra space. | 5890 // Try allocating extra space. |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6019 JSArray* array = JSArray::cast(this); | 6021 JSArray* array = JSArray::cast(this); |
6020 Object* return_value = array->JSArrayUpdateLengthFromIndex(index, | 6022 Object* return_value = array->JSArrayUpdateLengthFromIndex(index, |
6021 value); | 6023 value); |
6022 if (return_value->IsFailure()) return return_value; | 6024 if (return_value->IsFailure()) return return_value; |
6023 } | 6025 } |
6024 | 6026 |
6025 // Attempt to put this object back in fast case. | 6027 // Attempt to put this object back in fast case. |
6026 if (ShouldConvertToFastElements()) { | 6028 if (ShouldConvertToFastElements()) { |
6027 uint32_t new_length = 0; | 6029 uint32_t new_length = 0; |
6028 if (IsJSArray()) { | 6030 if (IsJSArray()) { |
6029 CHECK(JSArray::cast(this)->length()->ToArrayIndex(&new_length)); | 6031 CHECK(Array::IndexFromObject(JSArray::cast(this)->length(), |
| 6032 &new_length)); |
6030 JSArray::cast(this)->set_length(Smi::FromInt(new_length)); | 6033 JSArray::cast(this)->set_length(Smi::FromInt(new_length)); |
6031 } else { | 6034 } else { |
6032 new_length = NumberDictionary::cast(elements())->max_number_key() + 1; | 6035 new_length = NumberDictionary::cast(elements())->max_number_key() + 1; |
6033 } | 6036 } |
6034 Object* obj = Heap::AllocateFixedArrayWithHoles(new_length); | 6037 Object* obj = Heap::AllocateFixedArrayWithHoles(new_length); |
6035 if (obj->IsFailure()) return obj; | 6038 if (obj->IsFailure()) return obj; |
6036 SetFastElements(FixedArray::cast(obj)); | 6039 SetFastElements(FixedArray::cast(obj)); |
6037 #ifdef DEBUG | 6040 #ifdef DEBUG |
6038 if (FLAG_trace_normalization) { | 6041 if (FLAG_trace_normalization) { |
6039 PrintF("Object elements are fast case again:\n"); | 6042 PrintF("Object elements are fast case again:\n"); |
(...skipping 10 matching lines...) Expand all Loading... |
6050 } | 6053 } |
6051 // All possible cases have been handled above. Add a return to avoid the | 6054 // All possible cases have been handled above. Add a return to avoid the |
6052 // complaints from the compiler. | 6055 // complaints from the compiler. |
6053 UNREACHABLE(); | 6056 UNREACHABLE(); |
6054 return Heap::null_value(); | 6057 return Heap::null_value(); |
6055 } | 6058 } |
6056 | 6059 |
6057 | 6060 |
6058 Object* JSArray::JSArrayUpdateLengthFromIndex(uint32_t index, Object* value) { | 6061 Object* JSArray::JSArrayUpdateLengthFromIndex(uint32_t index, Object* value) { |
6059 uint32_t old_len = 0; | 6062 uint32_t old_len = 0; |
6060 CHECK(length()->ToArrayIndex(&old_len)); | 6063 CHECK(Array::IndexFromObject(length(), &old_len)); |
6061 // Check to see if we need to update the length. For now, we make | 6064 // Check to see if we need to update the length. For now, we make |
6062 // sure that the length stays within 32-bits (unsigned). | 6065 // sure that the length stays within 32-bits (unsigned). |
6063 if (index >= old_len && index != 0xffffffff) { | 6066 if (index >= old_len && index != 0xffffffff) { |
6064 Object* len = | 6067 Object* len = |
6065 Heap::NumberFromDouble(static_cast<double>(index) + 1); | 6068 Heap::NumberFromDouble(static_cast<double>(index) + 1); |
6066 if (len->IsFailure()) return len; | 6069 if (len->IsFailure()) return len; |
6067 set_length(len); | 6070 set_length(len); |
6068 } | 6071 } |
6069 return value; | 6072 return value; |
6070 } | 6073 } |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6344 // dictionary, we cannot go back to fast case. | 6347 // dictionary, we cannot go back to fast case. |
6345 if (dictionary->requires_slow_elements()) return false; | 6348 if (dictionary->requires_slow_elements()) return false; |
6346 // An object requiring access checks is never allowed to have fast | 6349 // An object requiring access checks is never allowed to have fast |
6347 // elements. If it had fast elements we would skip security checks. | 6350 // elements. If it had fast elements we would skip security checks. |
6348 if (IsAccessCheckNeeded()) return false; | 6351 if (IsAccessCheckNeeded()) return false; |
6349 // If the dictionary backing storage takes up roughly half as much | 6352 // If the dictionary backing storage takes up roughly half as much |
6350 // space as a fast-case backing storage would the array should have | 6353 // space as a fast-case backing storage would the array should have |
6351 // fast elements. | 6354 // fast elements. |
6352 uint32_t length = 0; | 6355 uint32_t length = 0; |
6353 if (IsJSArray()) { | 6356 if (IsJSArray()) { |
6354 CHECK(JSArray::cast(this)->length()->ToArrayIndex(&length)); | 6357 CHECK(Array::IndexFromObject(JSArray::cast(this)->length(), &length)); |
6355 } else { | 6358 } else { |
6356 length = dictionary->max_number_key(); | 6359 length = dictionary->max_number_key(); |
6357 } | 6360 } |
6358 return static_cast<uint32_t>(dictionary->Capacity()) >= | 6361 return static_cast<uint32_t>(dictionary->Capacity()) >= |
6359 (length / (2 * NumberDictionary::kEntrySize)); | 6362 (length / (2 * NumberDictionary::kEntrySize)); |
6360 } | 6363 } |
6361 | 6364 |
6362 | 6365 |
6363 // Certain compilers request function template instantiation when they | 6366 // Certain compilers request function template instantiation when they |
6364 // see the definition of the other template functions in the | 6367 // see the definition of the other template functions in the |
(...skipping 2181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8546 if (break_point_objects()->IsUndefined()) return 0; | 8549 if (break_point_objects()->IsUndefined()) return 0; |
8547 // Single beak point. | 8550 // Single beak point. |
8548 if (!break_point_objects()->IsFixedArray()) return 1; | 8551 if (!break_point_objects()->IsFixedArray()) return 1; |
8549 // Multiple break points. | 8552 // Multiple break points. |
8550 return FixedArray::cast(break_point_objects())->length(); | 8553 return FixedArray::cast(break_point_objects())->length(); |
8551 } | 8554 } |
8552 #endif | 8555 #endif |
8553 | 8556 |
8554 | 8557 |
8555 } } // namespace v8::internal | 8558 } } // namespace v8::internal |
OLD | NEW |