Rubber-stamped by Brady Eidson.
[webbrowser.git] / JavaScriptCore / runtime / Identifier.h
blob2249179cfcfd7bf8305b2f08633a8c163793ae54
1 /*
2 * Copyright (C) 2003, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
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.
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.
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.
21 #ifndef Identifier_h
22 #define Identifier_h
24 #include "JSGlobalData.h"
25 #include "UString.h"
27 namespace JSC {
29 class ExecState;
31 class Identifier {
32 friend class Structure;
33 public:
34 Identifier() { }
36 Identifier(ExecState* exec, const char* s) : _ustring(add(exec, s)) { } // Only to be used with string literals.
37 Identifier(ExecState* exec, const UChar* s, int length) : _ustring(add(exec, s, length)) { }
38 Identifier(ExecState* exec, UString::Rep* rep) : _ustring(add(exec, rep)) { }
39 Identifier(ExecState* exec, const UString& s) : _ustring(add(exec, s.rep())) { }
41 Identifier(JSGlobalData* globalData, const char* s) : _ustring(add(globalData, s)) { } // Only to be used with string literals.
42 Identifier(JSGlobalData* globalData, const UChar* s, int length) : _ustring(add(globalData, s, length)) { }
43 Identifier(JSGlobalData* globalData, UString::Rep* rep) : _ustring(add(globalData, rep)) { }
44 Identifier(JSGlobalData* globalData, const UString& s) : _ustring(add(globalData, s.rep())) { }
46 // Special constructor for cases where we overwrite an object in place.
47 Identifier(PlacementNewAdoptType) : _ustring(PlacementNewAdopt) { }
49 const UString& ustring() const { return _ustring; }
51 const UChar* data() const { return _ustring.data(); }
52 int size() const { return _ustring.size(); }
54 const char* ascii() const { return _ustring.ascii(); }
56 static Identifier from(ExecState* exec, unsigned y) { return Identifier(exec, UString::from(y)); }
57 static Identifier from(ExecState* exec, int y) { return Identifier(exec, UString::from(y)); }
58 static Identifier from(ExecState* exec, double y) { return Identifier(exec, UString::from(y)); }
60 bool isNull() const { return _ustring.isNull(); }
61 bool isEmpty() const { return _ustring.isEmpty(); }
63 uint32_t toUInt32(bool* ok) const { return _ustring.toUInt32(ok); }
64 uint32_t toUInt32(bool* ok, bool tolerateEmptyString) const { return _ustring.toUInt32(ok, tolerateEmptyString); };
65 uint32_t toStrictUInt32(bool* ok) const { return _ustring.toStrictUInt32(ok); }
66 unsigned toArrayIndex(bool* ok) const { return _ustring.toArrayIndex(ok); }
67 double toDouble() const { return _ustring.toDouble(); }
69 friend bool operator==(const Identifier&, const Identifier&);
70 friend bool operator!=(const Identifier&, const Identifier&);
72 friend bool operator==(const Identifier&, const char*);
73 friend bool operator!=(const Identifier&, const char*);
75 static void remove(UString::Rep*);
77 static bool equal(const UString::Rep*, const char*);
78 static bool equal(const UString::Rep*, const UChar*, int length);
79 static bool equal(const UString::Rep* a, const UString::Rep* b) { return JSC::equal(a, b); }
81 static PassRefPtr<UString::Rep> add(ExecState*, const char*); // Only to be used with string literals.
82 static PassRefPtr<UString::Rep> add(JSGlobalData*, const char*); // Only to be used with string literals.
84 private:
85 UString _ustring;
87 static bool equal(const Identifier& a, const Identifier& b) { return a._ustring.rep() == b._ustring.rep(); }
88 static bool equal(const Identifier& a, const char* b) { return equal(a._ustring.rep(), b); }
90 static PassRefPtr<UString::Rep> add(ExecState*, const UChar*, int length);
91 static PassRefPtr<UString::Rep> add(JSGlobalData*, const UChar*, int length);
93 static PassRefPtr<UString::Rep> add(ExecState* exec, UString::Rep* r)
95 if (r->identifierTable()) {
96 #ifndef NDEBUG
97 checkSameIdentifierTable(exec, r);
98 #endif
99 return r;
101 return addSlowCase(exec, r);
103 static PassRefPtr<UString::Rep> add(JSGlobalData* globalData, UString::Rep* r)
105 if (r->identifierTable()) {
106 #ifndef NDEBUG
107 checkSameIdentifierTable(globalData, r);
108 #endif
109 return r;
111 return addSlowCase(globalData, r);
114 static PassRefPtr<UString::Rep> addSlowCase(ExecState*, UString::Rep* r);
115 static PassRefPtr<UString::Rep> addSlowCase(JSGlobalData*, UString::Rep* r);
117 static void checkSameIdentifierTable(ExecState*, UString::Rep*);
118 static void checkSameIdentifierTable(JSGlobalData*, UString::Rep*);
121 inline bool operator==(const Identifier& a, const Identifier& b)
123 return Identifier::equal(a, b);
126 inline bool operator!=(const Identifier& a, const Identifier& b)
128 return !Identifier::equal(a, b);
131 inline bool operator==(const Identifier& a, const char* b)
133 return Identifier::equal(a, b);
136 inline bool operator!=(const Identifier& a, const char* b)
138 return !Identifier::equal(a, b);
141 IdentifierTable* createIdentifierTable();
142 void deleteIdentifierTable(IdentifierTable*);
144 } // namespace JSC
146 #endif // Identifier_h