]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/Identifier.h
JavaScriptCore-721.26.tar.gz
[apple/javascriptcore.git] / runtime / Identifier.h
1 /*
2 * Copyright (C) 2003, 2006, 2007, 2008, 2009 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
21 #ifndef Identifier_h
22 #define Identifier_h
23
24 #include "JSGlobalData.h"
25 #include "ThreadSpecific.h"
26 #include "UString.h"
27
28 namespace JSC {
29
30 class ExecState;
31
32 class Identifier {
33 friend class Structure;
34 public:
35 Identifier() { }
36
37 Identifier(ExecState* exec, const char* s) : _ustring(add(exec, s)) { } // Only to be used with string literals.
38 Identifier(ExecState* exec, const UChar* s, int length) : _ustring(add(exec, s, length)) { }
39 Identifier(ExecState* exec, UString::Rep* rep) : _ustring(add(exec, rep)) { }
40 Identifier(ExecState* exec, const UString& s) : _ustring(add(exec, s.rep())) { }
41
42 Identifier(JSGlobalData* globalData, const char* s) : _ustring(add(globalData, s)) { } // Only to be used with string literals.
43 Identifier(JSGlobalData* globalData, const UChar* s, int length) : _ustring(add(globalData, s, length)) { }
44 Identifier(JSGlobalData* globalData, UString::Rep* rep) : _ustring(add(globalData, rep)) { }
45 Identifier(JSGlobalData* globalData, const UString& s) : _ustring(add(globalData, s.rep())) { }
46
47 // Special constructor for cases where we overwrite an object in place.
48 Identifier(PlacementNewAdoptType) : _ustring(PlacementNewAdopt) { }
49
50 const UString& ustring() const { return _ustring; }
51
52 const UChar* data() const { return _ustring.data(); }
53 int size() const { return _ustring.size(); }
54
55 const char* ascii() const { return _ustring.ascii(); }
56
57 static Identifier from(ExecState* exec, unsigned y);
58 static Identifier from(ExecState* exec, int y);
59 static Identifier from(ExecState* exec, double y);
60
61 bool isNull() const { return _ustring.isNull(); }
62 bool isEmpty() const { return _ustring.isEmpty(); }
63
64 uint32_t toUInt32(bool* ok) const { return _ustring.toUInt32(ok); }
65 uint32_t toUInt32(bool* ok, bool tolerateEmptyString) const { return _ustring.toUInt32(ok, tolerateEmptyString); };
66 uint32_t toStrictUInt32(bool* ok) const { return _ustring.toStrictUInt32(ok); }
67 unsigned toArrayIndex(bool* ok) const { return _ustring.toArrayIndex(ok); }
68 double toDouble() const { return _ustring.toDouble(); }
69
70 friend bool operator==(const Identifier&, const Identifier&);
71 friend bool operator!=(const Identifier&, const Identifier&);
72
73 friend bool operator==(const Identifier&, const char*);
74 friend bool operator!=(const Identifier&, const char*);
75
76 static bool equal(const UString::Rep*, const char*);
77 static bool equal(const UString::Rep*, const UChar*, unsigned length);
78 static bool equal(const UString::Rep* a, const UString::Rep* b) { return ::equal(a, b); }
79
80 static PassRefPtr<UString::Rep> add(ExecState*, const char*); // Only to be used with string literals.
81 static PassRefPtr<UString::Rep> add(JSGlobalData*, const char*); // Only to be used with string literals.
82
83 private:
84 UString _ustring;
85
86 static bool equal(const Identifier& a, const Identifier& b) { return a._ustring.rep() == b._ustring.rep(); }
87 static bool equal(const Identifier& a, const char* b) { return equal(a._ustring.rep(), b); }
88
89 static PassRefPtr<UString::Rep> add(ExecState*, const UChar*, int length);
90 static PassRefPtr<UString::Rep> add(JSGlobalData*, const UChar*, int length);
91
92 static PassRefPtr<UString::Rep> add(ExecState* exec, UString::Rep* r)
93 {
94 #ifndef NDEBUG
95 checkCurrentIdentifierTable(exec);
96 #endif
97 if (r->isIdentifier())
98 return r;
99 return addSlowCase(exec, r);
100 }
101 static PassRefPtr<UString::Rep> add(JSGlobalData* globalData, UString::Rep* r)
102 {
103 #ifndef NDEBUG
104 checkCurrentIdentifierTable(globalData);
105 #endif
106 if (r->isIdentifier())
107 return r;
108 return addSlowCase(globalData, r);
109 }
110
111 static PassRefPtr<UString::Rep> addSlowCase(ExecState*, UString::Rep* r);
112 static PassRefPtr<UString::Rep> addSlowCase(JSGlobalData*, UString::Rep* r);
113
114 static void checkCurrentIdentifierTable(ExecState*);
115 static void checkCurrentIdentifierTable(JSGlobalData*);
116 };
117
118 inline bool operator==(const Identifier& a, const Identifier& b)
119 {
120 return Identifier::equal(a, b);
121 }
122
123 inline bool operator!=(const Identifier& a, const Identifier& b)
124 {
125 return !Identifier::equal(a, b);
126 }
127
128 inline bool operator==(const Identifier& a, const char* b)
129 {
130 return Identifier::equal(a, b);
131 }
132
133 inline bool operator!=(const Identifier& a, const char* b)
134 {
135 return !Identifier::equal(a, b);
136 }
137
138 IdentifierTable* createIdentifierTable();
139 void deleteIdentifierTable(IdentifierTable*);
140
141 } // namespace JSC
142
143 #endif // Identifier_h