string-table.h 4.08 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_OBJECTS_STRING_TABLE_H_
#define V8_OBJECTS_STRING_TABLE_H_

#include "src/objects/hash-table.h"
9
#include "src/roots.h"
10 11 12 13 14 15 16

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
namespace internal {

17
class StringTableKey : public HashTableKey {
18
 public:
19 20 21 22 23
  explicit inline StringTableKey(uint32_t hash_field);

  virtual Handle<String> AsHandle(Isolate* isolate) = 0;
  uint32_t HashField() const {
    DCHECK_NE(0, hash_field_);
24 25
    return hash_field_;
  }
26 27 28

 protected:
  inline void set_hash_field(uint32_t hash_field);
29 30 31

 private:
  uint32_t hash_field_ = 0;
32 33 34 35
};

class StringTableShape : public BaseShape<StringTableKey*> {
 public:
36
  static inline bool IsMatch(Key key, Object value) {
37 38 39
    return key->IsMatch(value);
  }

40
  static inline uint32_t Hash(Isolate* isolate, Key key) { return key->Hash(); }
41

42
  static inline uint32_t HashForObject(ReadOnlyRoots roots, Object object);
43

44
  static inline Handle<Object> AsHandle(Isolate* isolate, Key key);
45

46
  static inline RootIndex GetMapRootIndex();
47

48 49 50 51 52 53 54 55 56 57
  static const int kPrefixSize = 0;
  static const int kEntrySize = 1;
};

class SeqOneByteString;

// StringTable.
//
// No special elements in the prefix and the element size is 1
// because only the string itself (the key) needs to be stored.
58
class StringTable : public HashTable<StringTable, StringTableShape> {
59 60 61 62 63
 public:
  // Find string in the string table. If it is not there yet, it is
  // added. The return value is the string found.
  V8_EXPORT_PRIVATE static Handle<String> LookupString(Isolate* isolate,
                                                       Handle<String> key);
64
  static Handle<String> LookupKey(Isolate* isolate, StringTableKey* key);
65
  static Handle<String> AddKeyNoResize(Isolate* isolate, StringTableKey* key);
66 67
  static String ForwardStringIfExists(Isolate* isolate, StringTableKey* key,
                                      String string);
68

69 70
  // Shink the StringTable if it's very empty (kMaxEmptyFactor) to avoid the
  // performance overhead of re-allocating the StringTable over and over again.
71 72
  static Handle<StringTable> CautiousShrink(Isolate* isolate,
                                            Handle<StringTable> table);
73

74 75
  // Looks up a string that is equal to the given string and returns
  // string handle if it is found, or an empty handle otherwise.
76
  V8_WARN_UNUSED_RESULT static MaybeHandle<String> LookupTwoCharsStringIfExists(
77
      Isolate* isolate, uint16_t c1, uint16_t c2);
78 79 80
  // {raw_string} must be a tagged String pointer.
  // Returns a tagged pointer: either an internalized string, or a Smi
  // sentinel.
81 82
  V8_EXPORT_PRIVATE static Address LookupStringIfExists_NoAllocate(
      Isolate* isolate, Address raw_string);
83 84 85

  static void EnsureCapacityForDeserialization(Isolate* isolate, int expected);

86
  DECL_CAST(StringTable)
87

88
  static const int kMaxEmptyFactor = 4;
89 90 91
  static const int kMinCapacity = 2048;
  static const int kMinShrinkCapacity = kMinCapacity;

92 93 94 95
 private:
  template <bool seq_one_byte>
  friend class JsonParser;

96
  OBJECT_CONSTRUCTORS(StringTable, HashTable<StringTable, StringTableShape>);
97 98
};

99
class StringSetShape : public BaseShape<String> {
100
 public:
101
  static inline bool IsMatch(String key, Object value);
102
  static inline uint32_t Hash(Isolate* isolate, String key);
103
  static inline uint32_t HashForObject(ReadOnlyRoots roots, Object object);
104 105 106 107 108

  static const int kPrefixSize = 0;
  static const int kEntrySize = 1;
};

109
class StringSet : public HashTable<StringSet, StringSetShape> {
110 111
 public:
  static Handle<StringSet> New(Isolate* isolate);
112
  static Handle<StringSet> Add(Isolate* isolate, Handle<StringSet> blacklist,
113
                               Handle<String> name);
114
  bool Has(Isolate* isolate, Handle<String> name);
115

116
  DECL_CAST(StringSet)
117
  OBJECT_CONSTRUCTORS(StringSet, HashTable<StringSet, StringSetShape>);
118 119 120 121 122 123 124 125
};

}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_STRING_TABLE_H_