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