Identifier.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (C) 2003, 2006, 2007, 2008, 2009, 2012 Apple Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public License
  15. * along with this library; see the file COPYING.LIB. If not, write to
  16. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. *
  19. */
  20. #ifndef Identifier_h
  21. #define Identifier_h
  22. #include "VM.h"
  23. #include <wtf/ThreadSpecific.h>
  24. #include <wtf/WTFThreadData.h>
  25. #include <wtf/text/CString.h>
  26. #include <wtf/text/WTFString.h>
  27. namespace JSC {
  28. class ExecState;
  29. class Identifier {
  30. friend class Structure;
  31. public:
  32. Identifier() { }
  33. enum EmptyIdentifierFlag { EmptyIdentifier };
  34. Identifier(EmptyIdentifierFlag) : m_string(StringImpl::empty_shared()) { }
  35. // Only to be used with string literals.
  36. template<unsigned charactersCount>
  37. Identifier(ExecState* exec, const char (&characters)[charactersCount]) : m_string(add(exec, characters)) { }
  38. template<unsigned charactersCount>
  39. Identifier(VM* vm, const char (&characters)[charactersCount]) : m_string(add(vm, characters)) { }
  40. Identifier(ExecState* exec, StringImpl* rep) : m_string(add_shared(exec, rep)) { }
  41. Identifier(ExecState* exec, const String& s) : m_string(add_shared(exec, s.impl())) { }
  42. Identifier(VM* vm, const LChar* s, int length) : m_string(add(vm, s, length)) { }
  43. Identifier(VM* vm, const UChar* s, int length) : m_string(add(vm, s, length)) { }
  44. Identifier(VM* vm, StringImpl* rep) : m_string(add_shared(vm, rep))
  45. {
  46. #if ENABLE(DETACHED_JIT)
  47. RELEASE_ASSERT(JITSharedDataMemory::isInRange((uintptr_t)rep));
  48. #endif
  49. }
  50. Identifier(VM* vm, const String& s) : m_string(add_shared(vm, s.impl())) { }
  51. const String& string() const { return m_string; }
  52. StringImpl* impl() const { return m_string.impl(); }
  53. const UChar* characters() const { return m_string.characters(); }
  54. int length() const { return m_string.length(); }
  55. CString ascii() const { return m_string.ascii(); }
  56. static Identifier createLCharFromUChar(VM* vm, const UChar* s, int length) { return Identifier(vm, add8(vm, s, length)); }
  57. JS_EXPORT_PRIVATE static Identifier from(ExecState* exec, unsigned y);
  58. JS_EXPORT_PRIVATE static Identifier from(ExecState* exec, int y);
  59. static Identifier from(ExecState* exec, double y);
  60. static Identifier from(VM*, unsigned y);
  61. static Identifier from(VM*, int y);
  62. static Identifier from(VM*, double y);
  63. bool isNull() const { return m_string.isNull(); }
  64. bool isEmpty() const { return m_string.isEmpty(); }
  65. friend bool operator==(const Identifier&, const Identifier&);
  66. friend bool operator!=(const Identifier&, const Identifier&);
  67. friend bool operator==(const Identifier&, const LChar*);
  68. friend bool operator==(const Identifier&, const char*);
  69. friend bool operator!=(const Identifier&, const LChar*);
  70. friend bool operator!=(const Identifier&, const char*);
  71. static bool equal(const StringImpl*, const LChar*);
  72. static inline bool equal(const StringImpl*a, const char*b) { return Identifier::equal(a, reinterpret_cast<const LChar*>(b)); };
  73. static bool equal(const StringImpl*, const LChar*, unsigned length);
  74. static bool equal(const StringImpl*, const UChar*, unsigned length);
  75. static bool equal(const StringImpl* a, const StringImpl* b) { return ::equal(a, b); }
  76. // Only to be used with string literals.
  77. static PassRefPtr<StringImpl> add(VM*, const char*);
  78. JS_EXPORT_PRIVATE static PassRefPtr<StringImpl> add(ExecState*, const char*);
  79. private:
  80. String m_string;
  81. template <typename CharType>
  82. ALWAYS_INLINE static uint32_t toUInt32FromCharacters(const CharType* characters, unsigned length, bool& ok);
  83. static bool equal(const Identifier& a, const Identifier& b) { return a.m_string.impl() == b.m_string.impl(); }
  84. static bool equal(const Identifier& a, const LChar* b) { return equal(a.m_string.impl(), b); }
  85. template <typename T> static PassRefPtr<StringImpl> add(VM*, const T*, int length);
  86. static PassRefPtr<StringImpl> add8(VM*, const UChar*, int length);
  87. template <typename T> ALWAYS_INLINE static bool canUseSingleCharacterString(T);
  88. static PassRefPtr<StringImpl> add(ExecState* exec, StringImpl* r)
  89. {
  90. #ifndef NDEBUG
  91. checkCurrentIdentifierTable(exec);
  92. #endif
  93. if (r->isIdentifier())
  94. return r;
  95. return addSlowCase(exec, r);
  96. }
  97. static PassRefPtr<StringImpl> add(VM* vm, StringImpl* r)
  98. {
  99. #ifndef NDEBUG
  100. checkCurrentIdentifierTable(vm);
  101. #endif
  102. if (r->isIdentifier())
  103. return r;
  104. return addSlowCase(vm, r);
  105. }
  106. template <typename T>
  107. static PassRefPtr<StringImpl> add_shared(T* t, StringImpl* r)
  108. {
  109. #if ENABLE(DETACHED_JIT)
  110. if (JITSharedDataMemory::isInRange(reinterpret_cast<uintptr_t>(r))) {
  111. return add(t, r);
  112. }
  113. RefPtr<StringImpl> sharedString = r->is8Bit() ?
  114. StringImpl::create(r->characters8(), r->length(), StringImpl::StringImplShared) :
  115. StringImpl::create(r->characters16(), r->length(), StringImpl::StringImplShared);
  116. //sharedString->setIsIdentifier(r->isIdentifier());
  117. //sharedString->setIsAtomic(r->isAtomic());
  118. return add(t, sharedString.get());
  119. #else
  120. return add(t, r);
  121. #endif
  122. }
  123. JS_EXPORT_PRIVATE static PassRefPtr<StringImpl> addSlowCase(ExecState*, StringImpl* r);
  124. JS_EXPORT_PRIVATE static PassRefPtr<StringImpl> addSlowCase(VM*, StringImpl* r);
  125. JS_EXPORT_PRIVATE static void checkCurrentIdentifierTable(ExecState*);
  126. JS_EXPORT_PRIVATE static void checkCurrentIdentifierTable(VM*);
  127. };
  128. template <> ALWAYS_INLINE bool Identifier::canUseSingleCharacterString(LChar)
  129. {
  130. ASSERT(maxSingleCharacterString == 0xff);
  131. return true;
  132. }
  133. template <> ALWAYS_INLINE bool Identifier::canUseSingleCharacterString(UChar c)
  134. {
  135. return (c <= maxSingleCharacterString);
  136. }
  137. template <typename T>
  138. struct CharBuffer {
  139. const T* s;
  140. unsigned int length;
  141. };
  142. template <typename T>
  143. struct IdentifierCharBufferTranslator {
  144. static unsigned hash(const CharBuffer<T>& buf)
  145. {
  146. return StringHasher::computeHashAndMaskTop8Bits(buf.s, buf.length);
  147. }
  148. static bool equal(StringImpl* str, const CharBuffer<T>& buf)
  149. {
  150. return Identifier::equal(str, buf.s, buf.length);
  151. }
  152. static void translate(StringImpl*& location, const CharBuffer<T>& buf, unsigned hash)
  153. {
  154. T* d;
  155. StringImpl* r = StringImpl::createUninitialized(buf.length, d, StringImpl::StringImplShared).leakRef();
  156. for (unsigned i = 0; i != buf.length; i++)
  157. d[i] = buf.s[i];
  158. r->setHash(hash);
  159. location = r;
  160. }
  161. };
  162. template <typename T>
  163. PassRefPtr<StringImpl> Identifier::add(VM* vm, const T* s, int length)
  164. {
  165. if (length == 1) {
  166. T c = s[0];
  167. if (canUseSingleCharacterString(c))
  168. return add(vm, vm->smallStrings.singleCharacterStringRep(c));
  169. }
  170. if (!length)
  171. return StringImpl::empty_shared();
  172. CharBuffer<T> buf = { s, static_cast<unsigned>(length) };
  173. IdentifierTableHashSet::AddResult addResult = vm->identifierTable->add<CharBuffer<T>, IdentifierCharBufferTranslator<T> >(buf);
  174. // If the string is newly-translated, then we need to adopt it.
  175. // The boolean in the pair tells us if that is so.
  176. return addResult.isNewEntry ? adoptRef(*addResult.iterator) : *addResult.iterator;
  177. }
  178. inline bool operator==(const Identifier& a, const Identifier& b)
  179. {
  180. return Identifier::equal(a, b);
  181. }
  182. inline bool operator!=(const Identifier& a, const Identifier& b)
  183. {
  184. return !Identifier::equal(a, b);
  185. }
  186. inline bool operator==(const Identifier& a, const LChar* b)
  187. {
  188. return Identifier::equal(a, b);
  189. }
  190. inline bool operator==(const Identifier& a, const char* b)
  191. {
  192. return Identifier::equal(a, reinterpret_cast<const LChar*>(b));
  193. }
  194. inline bool operator!=(const Identifier& a, const LChar* b)
  195. {
  196. return !Identifier::equal(a, b);
  197. }
  198. inline bool operator!=(const Identifier& a, const char* b)
  199. {
  200. return !Identifier::equal(a, reinterpret_cast<const LChar*>(b));
  201. }
  202. inline bool Identifier::equal(const StringImpl* r, const LChar* s)
  203. {
  204. return WTF::equal(r, s);
  205. }
  206. inline bool Identifier::equal(const StringImpl* r, const LChar* s, unsigned length)
  207. {
  208. return WTF::equal(r, s, length);
  209. }
  210. inline bool Identifier::equal(const StringImpl* r, const UChar* s, unsigned length)
  211. {
  212. return WTF::equal(r, s, length);
  213. }
  214. IdentifierTable* createIdentifierTable();
  215. void deleteIdentifierTable(IdentifierTable*);
  216. struct IdentifierRepHash : PtrHash<RefPtr<StringImpl> > {
  217. static unsigned hash(const RefPtr<StringImpl>& key) { return key->existingHash(); }
  218. static unsigned hash(StringImpl* key) { return key->existingHash(); }
  219. };
  220. struct IdentifierMapIndexHashTraits : HashTraits<int> {
  221. static int emptyValue() { return std::numeric_limits<int>::max(); }
  222. static const bool emptyValueIsZero = false;
  223. };
  224. typedef HashMap<RefPtr<StringImpl>, int, IdentifierRepHash, HashTraits<RefPtr<StringImpl> >, IdentifierMapIndexHashTraits> IdentifierMap;
  225. template<typename U, typename V>
  226. IdentifierTableHashSet::AddResult IdentifierTable::add(U value)
  227. {
  228. IdentifierTableHashSet::AddResult result = m_table.add<U, V>(value);
  229. (*result.iterator)->setIsIdentifier(true);
  230. return result;
  231. }
  232. } // namespace JSC
  233. namespace WTF {
  234. template <> struct VectorTraits<JSC::Identifier> : SimpleClassVectorTraits { };
  235. } // namespace WTF
  236. #endif // Identifier_h