]> git.saurik.com Git - apple/javascriptcore.git/blobdiff - runtime/Identifier.h
JavaScriptCore-7600.1.4.16.1.tar.gz
[apple/javascriptcore.git] / runtime / Identifier.h
index 631cf42f7edfb4f3ffeca857fe88cbe94e14e615..50a253fd36e6423fa0f9b2499f52638b06fb9942 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (C) 2003, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
+ *  Copyright (C) 2003, 2006, 2007, 2008, 2009, 2012 Apple Inc. All rights reserved.
  *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
 #ifndef Identifier_h
 #define Identifier_h
 
-#include "JSGlobalData.h"
-#include "UString.h"
+#include "VM.h"
+#include <wtf/ThreadSpecific.h>
+#include <wtf/WTFThreadData.h>
+#include <wtf/text/CString.h>
+#include <wtf/text/WTFString.h>
 
 namespace JSC {
 
@@ -32,90 +35,113 @@ namespace JSC {
         friend class Structure;
     public:
         Identifier() { }
+        enum EmptyIdentifierFlag { EmptyIdentifier };
+        Identifier(EmptyIdentifierFlag) : m_string(StringImpl::empty()) { ASSERT(m_string.impl()->isAtomic()); }
 
-        Identifier(ExecState* exec, const char* s) : _ustring(add(exec, s)) { } // Only to be used with string literals.
-        Identifier(ExecState* exec, const UChar* s, int length) : _ustring(add(exec, s, length)) { }
-        Identifier(ExecState* exec, UString::Rep* rep) : _ustring(add(exec, rep)) { } 
-        Identifier(ExecState* exec, const UString& s) : _ustring(add(exec, s.rep())) { }
+        // Only to be used with string literals.
+        template<unsigned charactersCount>
+        Identifier(ExecState* exec, const char (&characters)[charactersCount]) : m_string(add(exec, characters)) { ASSERT(m_string.impl()->isAtomic()); }
+        template<unsigned charactersCount>
+        Identifier(VM* vm, const char (&characters)[charactersCount]) : m_string(add(vm, characters)) { ASSERT(m_string.impl()->isAtomic()); }
 
-        Identifier(JSGlobalData* globalData, const char* s) : _ustring(add(globalData, s)) { } // Only to be used with string literals.
-        Identifier(JSGlobalData* globalData, const UChar* s, int length) : _ustring(add(globalData, s, length)) { }
-        Identifier(JSGlobalData* globalData, UString::Rep* rep) : _ustring(add(globalData, rep)) { } 
-        Identifier(JSGlobalData* globalData, const UString& s) : _ustring(add(globalData, s.rep())) { }
+        Identifier(ExecState* exec, StringImpl* rep) : m_string(add(exec, rep)) { ASSERT(m_string.impl()->isAtomic()); }
+        Identifier(ExecState* exec, const String& s) : m_string(add(exec, s.impl())) { ASSERT(m_string.impl()->isAtomic()); }
 
-        // Special constructor for cases where we overwrite an object in place.
-        Identifier(PlacementNewAdoptType) : _ustring(PlacementNewAdopt) { }
-        
-        const UString& ustring() const { return _ustring; }
-        
-        const UChar* data() const { return _ustring.data(); }
-        int size() const { return _ustring.size(); }
-        
-        const char* ascii() const { return _ustring.ascii(); }
+        Identifier(VM* vm, const LChar* s, int length) : m_string(add(vm, s, length)) { ASSERT(m_string.impl()->isAtomic()); }
+        Identifier(VM* vm, const UChar* s, int length) : m_string(add(vm, s, length)) { ASSERT(m_string.impl()->isAtomic()); }
+        Identifier(VM* vm, StringImpl* rep) : m_string(add(vm, rep)) { ASSERT(m_string.impl()->isAtomic()); }
+        Identifier(VM* vm, const String& s) : m_string(add(vm, s.impl())) { ASSERT(m_string.impl()->isAtomic()); }
+
+        const String& string() const { return m_string; }
+        StringImpl* impl() const { return m_string.impl(); }
         
-        static Identifier from(ExecState* exec, unsigned y) { return Identifier(exec, UString::from(y)); }
+        int length() const { return m_string.length(); }
         
-        bool isNull() const { return _ustring.isNull(); }
-        bool isEmpty() const { return _ustring.isEmpty(); }
+        CString ascii() const { return m_string.ascii(); }
+        CString utf8() const { return m_string.utf8(); }
         
-        uint32_t toUInt32(bool* ok) const { return _ustring.toUInt32(ok); }
-        uint32_t toUInt32(bool* ok, bool tolerateEmptyString) const { return _ustring.toUInt32(ok, tolerateEmptyString); };
-        uint32_t toStrictUInt32(bool* ok) const { return _ustring.toStrictUInt32(ok); }
-        unsigned toArrayIndex(bool* ok) const { return _ustring.toArrayIndex(ok); }
-        double toDouble() const { return _ustring.toDouble(); }
+        static Identifier from(const PrivateName& name)
+        {
+            Identifier result;
+            result.m_string = name.uid();
+            return result;
+        }
+
+        static Identifier createLCharFromUChar(VM* vm, const UChar* s, int length) { return Identifier(vm, add8(vm, s, length)); }
+
+        JS_EXPORT_PRIVATE static Identifier from(ExecState* exec, unsigned y);
+        JS_EXPORT_PRIVATE static Identifier from(ExecState* exec, int y);
+        static Identifier from(ExecState* exec, double y);
+        static Identifier from(VM*, unsigned y);
+        static Identifier from(VM*, int y);
+        static Identifier from(VM*, double y);
+
+        bool isNull() const { return m_string.isNull(); }
+        bool isEmpty() const { return m_string.isEmpty(); }
         
         friend bool operator==(const Identifier&, const Identifier&);
         friend bool operator!=(const Identifier&, const Identifier&);
 
+        friend bool operator==(const Identifier&, const LChar*);
         friend bool operator==(const Identifier&, const char*);
+        friend bool operator!=(const Identifier&, const LChar*);
         friend bool operator!=(const Identifier&, const char*);
     
-        static void remove(UString::Rep*);
+        static bool equal(const StringImpl*, const LChar*);
+        static inline bool equal(const StringImpl*a, const char*b) { return Identifier::equal(a, reinterpret_cast<const LChar*>(b)); };
+        static bool equal(const StringImpl*, const LChar*, unsigned length);
+        static bool equal(const StringImpl*, const UChar*, unsigned length);
+        static bool equal(const StringImpl* a, const StringImpl* b) { return ::equal(a, b); }
 
-        static bool equal(const UString::Rep*, const char*);
-        static bool equal(const UString::Rep*, const UChar*, int length);
-        static bool equal(const UString::Rep* a, const UString::Rep* b) { return JSC::equal(a, b); }
-
-        static PassRefPtr<UString::Rep> add(ExecState*, const char*); // Only to be used with string literals.
-        static PassRefPtr<UString::Rep> add(JSGlobalData*, const char*); // Only to be used with string literals.
+        // Only to be used with string literals.
+        JS_EXPORT_PRIVATE static PassRef<StringImpl> add(VM*, const char*);
+        JS_EXPORT_PRIVATE static PassRef<StringImpl> add(ExecState*, const char*);
 
     private:
-        UString _ustring;
-        
-        static bool equal(const Identifier& a, const Identifier& b) { return a._ustring.rep() == b._ustring.rep(); }
-        static bool equal(const Identifier& a, const char* b) { return equal(a._ustring.rep(), b); }
+        String m_string;
 
-        static PassRefPtr<UString::Rep> add(ExecState*, const UChar*, int length);
-        static PassRefPtr<UString::Rep> add(JSGlobalData*, const UChar*, int length);
+        template <typename CharType>
+        ALWAYS_INLINE static uint32_t toUInt32FromCharacters(const CharType* characters, unsigned length, bool& ok);
 
-        static PassRefPtr<UString::Rep> add(ExecState* exec, UString::Rep* r)
-        {
-            if (r->identifierTable()) {
-#ifndef NDEBUG
-                checkSameIdentifierTable(exec, r);
-#endif
-                return r;
-            }
-            return addSlowCase(exec, r);
-        }
-        static PassRefPtr<UString::Rep> add(JSGlobalData* globalData, UString::Rep* r)
-        {
-            if (r->identifierTable()) {
-#ifndef NDEBUG
-                checkSameIdentifierTable(globalData, r);
-#endif
-                return r;
-            }
-            return addSlowCase(globalData, r);
-        }
+        static bool equal(const Identifier& a, const Identifier& b) { return a.m_string.impl() == b.m_string.impl(); }
+        static bool equal(const Identifier& a, const LChar* b) { return equal(a.m_string.impl(), b); }
+
+        template <typename T> static PassRef<StringImpl> add(VM*, const T*, int length);
+        static PassRef<StringImpl> add8(VM*, const UChar*, int length);
+        template <typename T> ALWAYS_INLINE static bool canUseSingleCharacterString(T);
 
-        static PassRefPtr<UString::Rep> addSlowCase(ExecState*, UString::Rep* r);
-        static PassRefPtr<UString::Rep> addSlowCase(JSGlobalData*, UString::Rep* r);
+        static PassRef<StringImpl> add(ExecState*, StringImpl*);
+        static PassRef<StringImpl> add(VM*, StringImpl*);
 
-        static void checkSameIdentifierTable(ExecState*, UString::Rep*);
-        static void checkSameIdentifierTable(JSGlobalData*, UString::Rep*);
+        JS_EXPORT_PRIVATE static void checkCurrentAtomicStringTable(ExecState*);
+        JS_EXPORT_PRIVATE static void checkCurrentAtomicStringTable(VM*);
     };
-    
+
+    template <> ALWAYS_INLINE bool Identifier::canUseSingleCharacterString(LChar)
+    {
+        ASSERT(maxSingleCharacterString == 0xff);
+        return true;
+    }
+
+    template <> ALWAYS_INLINE bool Identifier::canUseSingleCharacterString(UChar c)
+    {
+        return (c <= maxSingleCharacterString);
+    }
+
+    template <typename T>
+    PassRef<StringImpl> Identifier::add(VM* vm, const T* s, int length)
+    {
+        if (length == 1) {
+            T c = s[0];
+            if (canUseSingleCharacterString(c))
+                return *vm->smallStrings.singleCharacterStringRep(c);
+        }
+        if (!length)
+            return *StringImpl::empty();
+        
+        return *AtomicString::add(s, length);
+    }
+
     inline bool operator==(const Identifier& a, const Identifier& b)
     {
         return Identifier::equal(a, b);
@@ -126,19 +152,60 @@ namespace JSC {
         return !Identifier::equal(a, b);
     }
 
-    inline bool operator==(const Identifier& a, const char* b)
+    inline bool operator==(const Identifier& a, const LChar* b)
     {
         return Identifier::equal(a, b);
     }
 
-    inline bool operator!=(const Identifier& a, const char* b)
+    inline bool operator==(const Identifier& a, const char* b)
+    {
+        return Identifier::equal(a, reinterpret_cast<const LChar*>(b));
+    }
+    
+    inline bool operator!=(const Identifier& a, const LChar* b)
     {
         return !Identifier::equal(a, b);
     }
 
-    IdentifierTable* createIdentifierTable();
-    void deleteIdentifierTable(IdentifierTable*);
+    inline bool operator!=(const Identifier& a, const char* b)
+    {
+        return !Identifier::equal(a, reinterpret_cast<const LChar*>(b));
+    }
+    
+    inline bool Identifier::equal(const StringImpl* r, const LChar* s)
+    {
+        return WTF::equal(r, s);
+    }
+
+    inline bool Identifier::equal(const StringImpl* r, const LChar* s, unsigned length)
+    {
+        return WTF::equal(r, s, length);
+    }
+
+    inline bool Identifier::equal(const StringImpl* r, const UChar* s, unsigned length)
+    {
+        return WTF::equal(r, s, length);
+    }
+    
+    struct IdentifierRepHash : PtrHash<RefPtr<StringImpl>> {
+        static unsigned hash(const RefPtr<StringImpl>& key) { return key->existingHash(); }
+        static unsigned hash(StringImpl* key) { return key->existingHash(); }
+    };
+
+    struct IdentifierMapIndexHashTraits : HashTraits<int> {
+        static int emptyValue() { return std::numeric_limits<int>::max(); }
+        static const bool emptyValueIsZero = false;
+    };
+
+    typedef HashMap<RefPtr<StringImpl>, int, IdentifierRepHash, HashTraits<RefPtr<StringImpl>>, IdentifierMapIndexHashTraits> IdentifierMap;
+    typedef HashMap<StringImpl*, int, IdentifierRepHash, HashTraits<StringImpl*>, IdentifierMapIndexHashTraits> BorrowedIdentifierMap;
 
 } // namespace JSC
 
+namespace WTF {
+
+template <> struct VectorTraits<JSC::Identifier> : SimpleClassVectorTraits { };
+
+} // namespace WTF
+
 #endif // Identifier_h