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 "JSLock.h"
|
---|
42 | #include <new> // for placement new
|
---|
43 | #include <string.h> // for strlen
|
---|
44 | #include <wtf/Assertions.h>
|
---|
45 | #include <wtf/FastMalloc.h>
|
---|
46 | #include <wtf/HashSet.h>
|
---|
47 |
|
---|
48 | namespace WTF {
|
---|
49 |
|
---|
50 | template<typename T> struct DefaultHash;
|
---|
51 | template<typename T> struct StrHash;
|
---|
52 |
|
---|
53 | template<> struct StrHash<KJS::UString::Rep *> {
|
---|
54 | static unsigned hash(const KJS::UString::Rep *key) { return key->hash(); }
|
---|
55 | static bool equal(const KJS::UString::Rep *a, const KJS::UString::Rep *b) { return KJS::Identifier::equal(a, b); }
|
---|
56 | };
|
---|
57 |
|
---|
58 | template<> struct DefaultHash<KJS::UString::Rep *> {
|
---|
59 | typedef StrHash<KJS::UString::Rep *> Hash;
|
---|
60 | };
|
---|
61 |
|
---|
62 | }
|
---|
63 |
|
---|
64 | namespace KJS {
|
---|
65 |
|
---|
66 | typedef HashSet<UString::Rep *> IdentifierTable;
|
---|
67 | static IdentifierTable *table;
|
---|
68 |
|
---|
69 | static inline IdentifierTable& identifierTable()
|
---|
70 | {
|
---|
71 | ASSERT(JSLock::lockCount() > 0);
|
---|
72 |
|
---|
73 | if (!table)
|
---|
74 | table = new IdentifierTable;
|
---|
75 | return *table;
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | bool Identifier::equal(const UString::Rep *r, const char *s)
|
---|
80 | {
|
---|
81 | int length = r->len;
|
---|
82 | const UChar *d = r->data();
|
---|
83 | for (int i = 0; i != length; ++i)
|
---|
84 | if (d[i].uc != (unsigned char)s[i])
|
---|
85 | return false;
|
---|
86 | return s[length] == 0;
|
---|
87 | }
|
---|
88 |
|
---|
89 | bool Identifier::equal(const UString::Rep *r, const UChar *s, int length)
|
---|
90 | {
|
---|
91 | if (r->len != length)
|
---|
92 | return false;
|
---|
93 | const UChar *d = r->data();
|
---|
94 | for (int i = 0; i != length; ++i)
|
---|
95 | if (d[i].uc != s[i].uc)
|
---|
96 | return false;
|
---|
97 | return true;
|
---|
98 | }
|
---|
99 |
|
---|
100 | bool Identifier::equal(const UString::Rep *r, const UString::Rep *b)
|
---|
101 | {
|
---|
102 | int length = r->len;
|
---|
103 | if (length != b->len)
|
---|
104 | return false;
|
---|
105 | const UChar *d = r->data();
|
---|
106 | const UChar *s = b->data();
|
---|
107 | for (int i = 0; i != length; ++i)
|
---|
108 | if (d[i].uc != s[i].uc)
|
---|
109 | return false;
|
---|
110 | return true;
|
---|
111 | }
|
---|
112 |
|
---|
113 | struct CStringTranslator
|
---|
114 | {
|
---|
115 | static unsigned hash(const char *c)
|
---|
116 | {
|
---|
117 | return UString::Rep::computeHash(c);
|
---|
118 | }
|
---|
119 |
|
---|
120 | static bool equal(UString::Rep *r, const char *s)
|
---|
121 | {
|
---|
122 | return Identifier::equal(r, s);
|
---|
123 | }
|
---|
124 |
|
---|
125 | static void translate(UString::Rep*& location, const char *c, unsigned hash)
|
---|
126 | {
|
---|
127 | size_t length = strlen(c);
|
---|
128 | UChar *d = static_cast<UChar *>(fastMalloc(sizeof(UChar) * length));
|
---|
129 | for (size_t i = 0; i != length; i++)
|
---|
130 | d[i] = c[i];
|
---|
131 |
|
---|
132 | UString::Rep *r = UString::Rep::create(d, static_cast<int>(length)).releaseRef();
|
---|
133 | r->isIdentifier = 1;
|
---|
134 | r->rc = 0;
|
---|
135 | r->_hash = hash;
|
---|
136 |
|
---|
137 | location = r;
|
---|
138 | }
|
---|
139 | };
|
---|
140 |
|
---|
141 | PassRefPtr<UString::Rep> Identifier::add(const char *c)
|
---|
142 | {
|
---|
143 | if (!c)
|
---|
144 | return &UString::Rep::null;
|
---|
145 | size_t length = strlen(c);
|
---|
146 | if (length == 0)
|
---|
147 | return &UString::Rep::empty;
|
---|
148 |
|
---|
149 | return *identifierTable().add<const char *, CStringTranslator>(c).first;
|
---|
150 | }
|
---|
151 |
|
---|
152 | struct UCharBuffer {
|
---|
153 | const UChar *s;
|
---|
154 | unsigned int length;
|
---|
155 | };
|
---|
156 |
|
---|
157 | struct UCharBufferTranslator
|
---|
158 | {
|
---|
159 | static unsigned hash(const UCharBuffer& buf)
|
---|
160 | {
|
---|
161 | return UString::Rep::computeHash(buf.s, buf.length);
|
---|
162 | }
|
---|
163 |
|
---|
164 | static bool equal(UString::Rep *str, const UCharBuffer& buf)
|
---|
165 | {
|
---|
166 | return Identifier::equal(str, buf.s, buf.length);
|
---|
167 | }
|
---|
168 |
|
---|
169 | static void translate(UString::Rep *& location, const UCharBuffer& buf, unsigned hash)
|
---|
170 | {
|
---|
171 | UChar *d = static_cast<UChar *>(fastMalloc(sizeof(UChar) * buf.length));
|
---|
172 | for (unsigned i = 0; i != buf.length; i++)
|
---|
173 | d[i] = buf.s[i];
|
---|
174 |
|
---|
175 | UString::Rep *r = UString::Rep::create(d, buf.length).releaseRef();
|
---|
176 | r->isIdentifier = 1;
|
---|
177 | r->rc = 0;
|
---|
178 | r->_hash = hash;
|
---|
179 |
|
---|
180 | location = r;
|
---|
181 | }
|
---|
182 | };
|
---|
183 |
|
---|
184 | PassRefPtr<UString::Rep> Identifier::add(const UChar *s, int length)
|
---|
185 | {
|
---|
186 | if (length == 0)
|
---|
187 | return &UString::Rep::empty;
|
---|
188 |
|
---|
189 | UCharBuffer buf = {s, length};
|
---|
190 | return *identifierTable().add<UCharBuffer, UCharBufferTranslator>(buf).first;
|
---|
191 | }
|
---|
192 |
|
---|
193 | PassRefPtr<UString::Rep> Identifier::add(UString::Rep *r)
|
---|
194 | {
|
---|
195 | if (r->isIdentifier)
|
---|
196 | return r;
|
---|
197 |
|
---|
198 | if (r->len == 0)
|
---|
199 | return &UString::Rep::empty;
|
---|
200 |
|
---|
201 | UString::Rep *result = *identifierTable().add(r).first;
|
---|
202 | if (result == r)
|
---|
203 | r->isIdentifier = true;
|
---|
204 | return result;
|
---|
205 | }
|
---|
206 |
|
---|
207 | void Identifier::remove(UString::Rep *r)
|
---|
208 | {
|
---|
209 | identifierTable().remove(r);
|
---|
210 | }
|
---|
211 |
|
---|
212 | // Global constants for property name strings.
|
---|
213 |
|
---|
214 | #if !AVOID_STATIC_CONSTRUCTORS
|
---|
215 | // Define an Identifier in the normal way.
|
---|
216 | #define DEFINE_GLOBAL(name, string) extern const Identifier name(string);
|
---|
217 | #else
|
---|
218 | // Define an Identifier-sized array of pointers to avoid static initialization.
|
---|
219 | // Use an array of pointers instead of an array of char in case there is some alignment issue.
|
---|
220 | #define DEFINE_GLOBAL(name, string) \
|
---|
221 | void * name[(sizeof(Identifier) + sizeof(void *) - 1) / sizeof(void *)];
|
---|
222 | #endif
|
---|
223 |
|
---|
224 | const char * const nullCString = 0;
|
---|
225 |
|
---|
226 | DEFINE_GLOBAL(nullIdentifier, nullCString)
|
---|
227 | DEFINE_GLOBAL(specialPrototypePropertyName, "__proto__")
|
---|
228 |
|
---|
229 | #define DEFINE_PROPERTY_NAME_GLOBAL(name) DEFINE_GLOBAL(name ## PropertyName, #name)
|
---|
230 | KJS_IDENTIFIER_EACH_PROPERTY_NAME_GLOBAL(DEFINE_PROPERTY_NAME_GLOBAL)
|
---|
231 |
|
---|
232 | void Identifier::init()
|
---|
233 | {
|
---|
234 | #if AVOID_STATIC_CONSTRUCTORS
|
---|
235 | static bool initialized;
|
---|
236 | if (!initialized) {
|
---|
237 | // Use placement new to initialize the globals.
|
---|
238 |
|
---|
239 | new (&nullIdentifier) Identifier(nullCString);
|
---|
240 | new (&specialPrototypePropertyName) Identifier("__proto__");
|
---|
241 |
|
---|
242 | #define PLACEMENT_NEW_PROPERTY_NAME_GLOBAL(name) new(&name ## PropertyName) Identifier(#name);
|
---|
243 | KJS_IDENTIFIER_EACH_PROPERTY_NAME_GLOBAL(PLACEMENT_NEW_PROPERTY_NAME_GLOBAL)
|
---|
244 |
|
---|
245 | initialized = true;
|
---|
246 | }
|
---|
247 | #endif
|
---|
248 | }
|
---|
249 |
|
---|
250 | } // namespace KJS
|
---|