1 // Copyright 2017 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_OBJECTS_STRING_TABLE_H_ 6 #define V8_OBJECTS_STRING_TABLE_H_ 7 8 #include "src/objects/hash-table.h" 9 10 // Has to be the last include (doesn't have include guards): 11 #include "src/objects/object-macros.h" 12 13 namespace v8 { 14 namespace internal { 15 16 class StringTableKey : public HashTableKey { 17 public: 18 explicit inline StringTableKey(uint32_t hash_field); 19 20 virtual Handle<String> AsHandle(Isolate* isolate) = 0; HashField()21 uint32_t HashField() const { 22 DCHECK_NE(0, hash_field_); 23 return hash_field_; 24 } 25 26 protected: 27 inline void set_hash_field(uint32_t hash_field); 28 29 private: 30 uint32_t hash_field_ = 0; 31 }; 32 33 class StringTableShape : public BaseShape<StringTableKey*> { 34 public: IsMatch(Key key,Object * value)35 static inline bool IsMatch(Key key, Object* value) { 36 return key->IsMatch(value); 37 } 38 Hash(Isolate * isolate,Key key)39 static inline uint32_t Hash(Isolate* isolate, Key key) { return key->Hash(); } 40 41 static inline uint32_t HashForObject(Isolate* isolate, Object* object); 42 43 static inline Handle<Object> AsHandle(Isolate* isolate, Key key); 44 45 static inline int GetMapRootIndex(); 46 47 static const int kPrefixSize = 0; 48 static const int kEntrySize = 1; 49 }; 50 51 class SeqOneByteString; 52 53 // StringTable. 54 // 55 // No special elements in the prefix and the element size is 1 56 // because only the string itself (the key) needs to be stored. 57 class StringTable : public HashTable<StringTable, StringTableShape> { 58 public: 59 // Find string in the string table. If it is not there yet, it is 60 // added. The return value is the string found. 61 V8_EXPORT_PRIVATE static Handle<String> LookupString(Isolate* isolate, 62 Handle<String> key); 63 static Handle<String> LookupKey(Isolate* isolate, StringTableKey* key); 64 static Handle<String> AddKeyNoResize(Isolate* isolate, StringTableKey* key); 65 static String* ForwardStringIfExists(Isolate* isolate, StringTableKey* key, 66 String* string); 67 68 // Shink the StringTable if it's very empty (kMaxEmptyFactor) to avoid the 69 // performance overhead of re-allocating the StringTable over and over again. 70 static Handle<StringTable> CautiousShrink(Isolate* isolate, 71 Handle<StringTable> table); 72 73 // Looks up a string that is equal to the given string and returns 74 // string handle if it is found, or an empty handle otherwise. 75 V8_WARN_UNUSED_RESULT static MaybeHandle<String> LookupTwoCharsStringIfExists( 76 Isolate* isolate, uint16_t c1, uint16_t c2); 77 static Object* LookupStringIfExists_NoAllocate(Isolate* isolate, 78 String* string); 79 80 static void EnsureCapacityForDeserialization(Isolate* isolate, int expected); 81 82 DECL_CAST(StringTable) 83 84 static const int kMaxEmptyFactor = 4; 85 static const int kMinCapacity = 2048; 86 static const int kMinShrinkCapacity = kMinCapacity; 87 88 private: 89 template <bool seq_one_byte> 90 friend class JsonParser; 91 92 DISALLOW_IMPLICIT_CONSTRUCTORS(StringTable); 93 }; 94 95 class StringSetShape : public BaseShape<String*> { 96 public: 97 static inline bool IsMatch(String* key, Object* value); 98 static inline uint32_t Hash(Isolate* isolate, String* key); 99 static inline uint32_t HashForObject(Isolate* isolate, Object* object); 100 101 static const int kPrefixSize = 0; 102 static const int kEntrySize = 1; 103 }; 104 105 class StringSet : public HashTable<StringSet, StringSetShape> { 106 public: 107 static Handle<StringSet> New(Isolate* isolate); 108 static Handle<StringSet> Add(Isolate* isolate, Handle<StringSet> blacklist, 109 Handle<String> name); 110 bool Has(Isolate* isolate, Handle<String> name); 111 112 DECL_CAST(StringSet) 113 }; 114 115 } // namespace internal 116 } // namespace v8 117 118 #include "src/objects/object-macros-undef.h" 119 120 #endif // V8_OBJECTS_STRING_TABLE_H_ 121