source: webkit/trunk/JavaScriptCore/kjs/identifier.cpp@ 13093

Last change on this file since 13093 was 13093, checked in by mjs, 19 years ago
  • now fix mac build again
  • kjs/identifier.cpp:
  • Property svn:eol-style set to native
File size: 6.8 KB
Line 
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 2003 Apple Computer, Inc
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#include "config.h"
23// For JavaScriptCore we need to avoid having static constructors.
24// Our strategy is to declare the global objects with a different type (initialized to 0)
25// and then use placement new to initialize the global objects later. This is not completely
26// portable, and it would be good to figure out a 100% clean way that still avoids code that
27// runs at init time.
28
29#if !PLATFORM(WIN_OS) // can't get this to compile on Visual C++ yet
30#define AVOID_STATIC_CONSTRUCTORS 1
31#else
32#define AVOID_STATIC_CONSTRUCTORS 0
33#endif
34
35#if AVOID_STATIC_CONSTRUCTORS
36#define KJS_IDENTIFIER_HIDE_GLOBALS 1
37#endif
38
39#include "identifier.h"
40
41#include <kxmlcore/FastMalloc.h>
42#include <kxmlcore/HashSet.h>
43#include <string.h> // for strlen
44#include <new> // for placement new
45
46namespace KXMLCore {
47
48 template<typename T> class DefaultHash;
49 template<typename T> class StrHash;
50
51 template<> struct StrHash<KJS::UString::Rep *> {
52 static unsigned hash(const KJS::UString::Rep *key) { return key->hash(); }
53 static bool equal(const KJS::UString::Rep *a, const KJS::UString::Rep *b) { return KJS::Identifier::equal(a, b); }
54 };
55
56 template<> struct DefaultHash<KJS::UString::Rep *> {
57 typedef StrHash<KJS::UString::Rep *> Hash;
58 };
59
60}
61
62namespace KJS {
63
64typedef HashSet<UString::Rep *> IdentifierTable;
65static IdentifierTable *table;
66
67static inline IdentifierTable& identifierTable()
68{
69 if (!table)
70 table = new IdentifierTable;
71 return *table;
72}
73
74
75bool Identifier::equal(const UString::Rep *r, const char *s)
76{
77 int length = r->len;
78 const UChar *d = r->data();
79 for (int i = 0; i != length; ++i)
80 if (d[i].uc != (unsigned char)s[i])
81 return false;
82 return s[length] == 0;
83}
84
85bool Identifier::equal(const UString::Rep *r, const UChar *s, int length)
86{
87 if (r->len != length)
88 return false;
89 const UChar *d = r->data();
90 for (int i = 0; i != length; ++i)
91 if (d[i].uc != s[i].uc)
92 return false;
93 return true;
94}
95
96bool Identifier::equal(const UString::Rep *r, const UString::Rep *b)
97{
98 int length = r->len;
99 if (length != b->len)
100 return false;
101 const UChar *d = r->data();
102 const UChar *s = b->data();
103 for (int i = 0; i != length; ++i)
104 if (d[i].uc != s[i].uc)
105 return false;
106 return true;
107}
108
109struct CStringTranslator
110{
111 static unsigned hash(const char *c)
112 {
113 return UString::Rep::computeHash(c);
114 }
115
116 static bool equal(UString::Rep *r, const char *s)
117 {
118 return Identifier::equal(r, s);
119 }
120
121 static void translate(UString::Rep*& location, const char *c, unsigned hash)
122 {
123 int length = strlen(c);
124 UChar *d = static_cast<UChar *>(fastMalloc(sizeof(UChar) * length));
125 for (int i = 0; i != length; i++)
126 d[i] = c[i];
127
128 UString::Rep *r = UString::Rep::create(d, length).release();
129 r->isIdentifier = 1;
130 r->rc = 0;
131 r->_hash = hash;
132
133 location = r;
134 }
135};
136
137PassRefPtr<UString::Rep> Identifier::add(const char *c)
138{
139 if (!c)
140 return &UString::Rep::null;
141 int length = strlen(c);
142 if (length == 0)
143 return &UString::Rep::empty;
144
145 return *identifierTable().add<const char *, CStringTranslator>(c).first;
146}
147
148struct UCharBuffer {
149 const UChar *s;
150 unsigned int length;
151};
152
153struct UCharBufferTranslator
154{
155 static unsigned hash(const UCharBuffer& buf)
156 {
157 return UString::Rep::computeHash(buf.s, buf.length);
158 }
159
160 static bool equal(UString::Rep *str, const UCharBuffer& buf)
161 {
162 return Identifier::equal(str, buf.s, buf.length);
163 }
164
165 static void translate(UString::Rep *& location, const UCharBuffer& buf, unsigned hash)
166 {
167 UChar *d = static_cast<UChar *>(fastMalloc(sizeof(UChar) * buf.length));
168 for (unsigned i = 0; i != buf.length; i++)
169 d[i] = buf.s[i];
170
171 UString::Rep *r = UString::Rep::create(d, buf.length).release();
172 r->isIdentifier = 1;
173 r->rc = 0;
174 r->_hash = hash;
175
176 location = r;
177 }
178};
179
180PassRefPtr<UString::Rep> Identifier::add(const UChar *s, int length)
181{
182 if (length == 0)
183 return &UString::Rep::empty;
184
185 UCharBuffer buf = {s, length};
186 return *identifierTable().add<UCharBuffer, UCharBufferTranslator>(buf).first;
187}
188
189PassRefPtr<UString::Rep> Identifier::add(UString::Rep *r)
190{
191 if (r->isIdentifier)
192 return r;
193
194 if (r->len == 0)
195 return &UString::Rep::empty;
196
197 UString::Rep *result = *identifierTable().add(r).first;
198 if (result == r)
199 r->isIdentifier = true;
200 return result;
201}
202
203void Identifier::remove(UString::Rep *r)
204{
205 identifierTable().remove(r);
206}
207
208// Global constants for property name strings.
209
210#if !AVOID_STATIC_CONSTRUCTORS
211 // Define an Identifier in the normal way.
212 #define DEFINE_GLOBAL(name, string) extern const Identifier name(string);
213#else
214 // Define an Identifier-sized array of pointers to avoid static initialization.
215 // Use an array of pointers instead of an array of char in case there is some alignment issue.
216 #define DEFINE_GLOBAL(name, string) \
217 void * name[(sizeof(Identifier) + sizeof(void *) - 1) / sizeof(void *)];
218#endif
219
220const char * const nullCString = 0;
221
222DEFINE_GLOBAL(nullIdentifier, nullCString)
223DEFINE_GLOBAL(specialPrototypePropertyName, "__proto__")
224
225#define DEFINE_PROPERTY_NAME_GLOBAL(name) DEFINE_GLOBAL(name ## PropertyName, #name)
226KJS_IDENTIFIER_EACH_PROPERTY_NAME_GLOBAL(DEFINE_PROPERTY_NAME_GLOBAL)
227
228void Identifier::init()
229{
230#if AVOID_STATIC_CONSTRUCTORS
231 static bool initialized;
232 if (!initialized) {
233 // Use placement new to initialize the globals.
234
235 new (&nullIdentifier) Identifier(nullCString);
236 new (&specialPrototypePropertyName) Identifier("__proto__");
237
238 #define PLACEMENT_NEW_PROPERTY_NAME_GLOBAL(name) new(&name ## PropertyName) Identifier(#name);
239 KJS_IDENTIFIER_EACH_PROPERTY_NAME_GLOBAL(PLACEMENT_NEW_PROPERTY_NAME_GLOBAL)
240
241 initialized = true;
242 }
243#endif
244}
245
246} // namespace KJS
Note: See TracBrowser for help on using the repository browser.