OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_OBJECTS_H_ | 5 #ifndef V8_OBJECTS_H_ |
6 #define V8_OBJECTS_H_ | 6 #define V8_OBJECTS_H_ |
7 | 7 |
8 #include <iosfwd> | 8 #include <iosfwd> |
9 | 9 |
10 #include "src/allocation.h" | 10 #include "src/allocation.h" |
(...skipping 2454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2465 // Gives access to raw memory which stores the array's data. | 2465 // Gives access to raw memory which stores the array's data. |
2466 inline Object** data_start(); | 2466 inline Object** data_start(); |
2467 | 2467 |
2468 inline void FillWithHoles(int from, int to); | 2468 inline void FillWithHoles(int from, int to); |
2469 | 2469 |
2470 // Shrink length and insert filler objects. | 2470 // Shrink length and insert filler objects. |
2471 void Shrink(int length); | 2471 void Shrink(int length); |
2472 | 2472 |
2473 enum KeyFilter { ALL_KEYS, NON_SYMBOL_KEYS }; | 2473 enum KeyFilter { ALL_KEYS, NON_SYMBOL_KEYS }; |
2474 | 2474 |
2475 // Add the elements of a JSArray to this FixedArray. | |
2476 MUST_USE_RESULT static MaybeHandle<FixedArray> AddKeysFromArrayLike( | |
2477 Handle<FixedArray> content, Handle<JSObject> array, | |
2478 KeyFilter filter = ALL_KEYS); | |
2479 | |
2480 // Computes the union of keys and return the result. | |
2481 // Used for implementing "for (n in object) { }" | |
2482 MUST_USE_RESULT static MaybeHandle<FixedArray> UnionOfKeys( | |
2483 Handle<FixedArray> first, | |
2484 Handle<FixedArray> second); | |
2485 | |
2486 // Copy a sub array from the receiver to dest. | 2475 // Copy a sub array from the receiver to dest. |
2487 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len); | 2476 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len); |
2488 | 2477 |
2489 // Garbage collection support. | 2478 // Garbage collection support. |
2490 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; } | 2479 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; } |
2491 | 2480 |
2492 // Code Generation support. | 2481 // Code Generation support. |
2493 static int OffsetOfElementAt(int index) { return SizeFor(index); } | 2482 static int OffsetOfElementAt(int index) { return SizeFor(index); } |
2494 | 2483 |
2495 // Garbage collection support. | 2484 // Garbage collection support. |
(...skipping 1158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3654 static Handle<Derived> EnsureGrowable(Handle<Derived> table); | 3643 static Handle<Derived> EnsureGrowable(Handle<Derived> table); |
3655 | 3644 |
3656 // Returns an OrderedHashTable (possibly |table|) that's shrunken | 3645 // Returns an OrderedHashTable (possibly |table|) that's shrunken |
3657 // if possible. | 3646 // if possible. |
3658 static Handle<Derived> Shrink(Handle<Derived> table); | 3647 static Handle<Derived> Shrink(Handle<Derived> table); |
3659 | 3648 |
3660 // Returns a new empty OrderedHashTable and records the clearing so that | 3649 // Returns a new empty OrderedHashTable and records the clearing so that |
3661 // exisiting iterators can be updated. | 3650 // exisiting iterators can be updated. |
3662 static Handle<Derived> Clear(Handle<Derived> table); | 3651 static Handle<Derived> Clear(Handle<Derived> table); |
3663 | 3652 |
| 3653 // Returns a true if the OrderedHashTable contains the key |
| 3654 static bool HasKey(Handle<Derived> table, Handle<Object> key); |
| 3655 |
3664 int NumberOfElements() { | 3656 int NumberOfElements() { |
3665 return Smi::cast(get(kNumberOfElementsIndex))->value(); | 3657 return Smi::cast(get(kNumberOfElementsIndex))->value(); |
3666 } | 3658 } |
3667 | 3659 |
3668 int NumberOfDeletedElements() { | 3660 int NumberOfDeletedElements() { |
3669 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value(); | 3661 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value(); |
3670 } | 3662 } |
3671 | 3663 |
3672 int UsedCapacity() { return NumberOfElements() + NumberOfDeletedElements(); } | 3664 int UsedCapacity() { return NumberOfElements() + NumberOfDeletedElements(); } |
3673 | 3665 |
3674 int NumberOfBuckets() { | 3666 int NumberOfBuckets() { |
3675 return Smi::cast(get(kNumberOfBucketsIndex))->value(); | 3667 return Smi::cast(get(kNumberOfBucketsIndex))->value(); |
3676 } | 3668 } |
3677 | 3669 |
3678 // Returns an index into |this| for the given entry. | 3670 // Returns an index into |this| for the given entry. |
3679 int EntryToIndex(int entry) { | 3671 int EntryToIndex(int entry) { |
3680 return kHashTableStartIndex + NumberOfBuckets() + (entry * kEntrySize); | 3672 return kHashTableStartIndex + NumberOfBuckets() + (entry * kEntrySize); |
3681 } | 3673 } |
3682 | 3674 |
| 3675 int HashToBucket(int hash) { return hash & (NumberOfBuckets() - 1); } |
| 3676 |
| 3677 int HashToEntry(int hash) { |
| 3678 int bucket = HashToBucket(hash); |
| 3679 Object* entry = this->get(kHashTableStartIndex + bucket); |
| 3680 return Smi::cast(entry)->value(); |
| 3681 } |
| 3682 |
| 3683 int KeyToFirstEntry(Object* key) { |
| 3684 Object* hash = key->GetHash(); |
| 3685 // If the object does not have an identity hash, it was never used as a key |
| 3686 if (hash->IsUndefined()) return kNotFound; |
| 3687 return HashToEntry(Smi::cast(hash)->value()); |
| 3688 } |
| 3689 |
| 3690 int NextChainEntry(int entry) { |
| 3691 Object* next_entry = get(EntryToIndex(entry) + kChainOffset); |
| 3692 return Smi::cast(next_entry)->value(); |
| 3693 } |
| 3694 |
3683 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); } | 3695 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); } |
3684 | 3696 |
3685 bool IsObsolete() { | 3697 bool IsObsolete() { |
3686 return !get(kNextTableIndex)->IsSmi(); | 3698 return !get(kNextTableIndex)->IsSmi(); |
3687 } | 3699 } |
3688 | 3700 |
3689 // The next newer table. This is only valid if the table is obsolete. | 3701 // The next newer table. This is only valid if the table is obsolete. |
3690 Derived* NextTable() { | 3702 Derived* NextTable() { |
3691 return Derived::cast(get(kNextTableIndex)); | 3703 return Derived::cast(get(kNextTableIndex)); |
3692 } | 3704 } |
(...skipping 26 matching lines...) Expand all Loading... |
3719 static const int kEntrySize = entrysize + 1; | 3731 static const int kEntrySize = entrysize + 1; |
3720 static const int kChainOffset = entrysize; | 3732 static const int kChainOffset = entrysize; |
3721 | 3733 |
3722 static const int kLoadFactor = 2; | 3734 static const int kLoadFactor = 2; |
3723 | 3735 |
3724 // NumberOfDeletedElements is set to kClearedTableSentinel when | 3736 // NumberOfDeletedElements is set to kClearedTableSentinel when |
3725 // the table is cleared, which allows iterator transitions to | 3737 // the table is cleared, which allows iterator transitions to |
3726 // optimize that case. | 3738 // optimize that case. |
3727 static const int kClearedTableSentinel = -1; | 3739 static const int kClearedTableSentinel = -1; |
3728 | 3740 |
3729 private: | 3741 protected: |
3730 static Handle<Derived> Rehash(Handle<Derived> table, int new_capacity); | 3742 static Handle<Derived> Rehash(Handle<Derived> table, int new_capacity); |
3731 | 3743 |
3732 void SetNumberOfBuckets(int num) { | 3744 void SetNumberOfBuckets(int num) { |
3733 set(kNumberOfBucketsIndex, Smi::FromInt(num)); | 3745 set(kNumberOfBucketsIndex, Smi::FromInt(num)); |
3734 } | 3746 } |
3735 | 3747 |
3736 void SetNumberOfElements(int num) { | 3748 void SetNumberOfElements(int num) { |
3737 set(kNumberOfElementsIndex, Smi::FromInt(num)); | 3749 set(kNumberOfElementsIndex, Smi::FromInt(num)); |
3738 } | 3750 } |
3739 | 3751 |
(...skipping 21 matching lines...) Expand all Loading... |
3761 }; | 3773 }; |
3762 | 3774 |
3763 | 3775 |
3764 class JSSetIterator; | 3776 class JSSetIterator; |
3765 | 3777 |
3766 | 3778 |
3767 class OrderedHashSet: public OrderedHashTable< | 3779 class OrderedHashSet: public OrderedHashTable< |
3768 OrderedHashSet, JSSetIterator, 1> { | 3780 OrderedHashSet, JSSetIterator, 1> { |
3769 public: | 3781 public: |
3770 DECLARE_CAST(OrderedHashSet) | 3782 DECLARE_CAST(OrderedHashSet) |
| 3783 |
| 3784 static Handle<OrderedHashSet> Add(Handle<OrderedHashSet> table, |
| 3785 Handle<Object> value); |
3771 }; | 3786 }; |
3772 | 3787 |
3773 | 3788 |
3774 class JSMapIterator; | 3789 class JSMapIterator; |
3775 | 3790 |
3776 | 3791 |
3777 class OrderedHashMap | 3792 class OrderedHashMap |
3778 : public OrderedHashTable<OrderedHashMap, JSMapIterator, 2> { | 3793 : public OrderedHashTable<OrderedHashMap, JSMapIterator, 2> { |
3779 public: | 3794 public: |
3780 DECLARE_CAST(OrderedHashMap) | 3795 DECLARE_CAST(OrderedHashMap) |
(...skipping 6689 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
10470 static inline int set(int value, int bit_position, bool v) { | 10485 static inline int set(int value, int bit_position, bool v) { |
10471 if (v) { | 10486 if (v) { |
10472 value |= (1 << bit_position); | 10487 value |= (1 << bit_position); |
10473 } else { | 10488 } else { |
10474 value &= ~(1 << bit_position); | 10489 value &= ~(1 << bit_position); |
10475 } | 10490 } |
10476 return value; | 10491 return value; |
10477 } | 10492 } |
10478 }; | 10493 }; |
10479 | 10494 |
| 10495 |
| 10496 class KeyAccumulator final BASE_EMBEDDED { |
| 10497 public: |
| 10498 explicit KeyAccumulator(Isolate* isolate) : isolate_(isolate), length_(0) {} |
| 10499 |
| 10500 void AddKey(Handle<Object> key, int check_limit); |
| 10501 void AddKeys(Handle<FixedArray> array, FixedArray::KeyFilter filter); |
| 10502 void AddKeys(Handle<JSObject> array, FixedArray::KeyFilter filter); |
| 10503 void PrepareForComparisons(int count); |
| 10504 Handle<FixedArray> GetKeys(); |
| 10505 |
| 10506 int GetLength() { return length_; } |
| 10507 |
| 10508 private: |
| 10509 void EnsureCapacity(int capacity); |
| 10510 void Grow(); |
| 10511 |
| 10512 Isolate* isolate_; |
| 10513 Handle<FixedArray> keys_; |
| 10514 Handle<OrderedHashSet> set_; |
| 10515 int length_; |
| 10516 DISALLOW_COPY_AND_ASSIGN(KeyAccumulator); |
| 10517 }; |
10480 } } // namespace v8::internal | 10518 } } // namespace v8::internal |
10481 | 10519 |
10482 #endif // V8_OBJECTS_H_ | 10520 #endif // V8_OBJECTS_H_ |
OLD | NEW |