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