]>
Commit | Line | Data |
---|---|---|
9dae56ea A |
1 | /* |
2 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) | |
3 | * Copyright (C) 2003, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. | |
4 | * | |
5 | * This library is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU Lesser 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 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | * | |
19 | */ | |
20 | ||
21 | #ifndef Lookup_h | |
22 | #define Lookup_h | |
23 | ||
24 | #include "CallFrame.h" | |
6fe7ccc8 | 25 | #include "Intrinsic.h" |
9dae56ea | 26 | #include "Identifier.h" |
9dae56ea | 27 | #include "JSGlobalObject.h" |
9dae56ea A |
28 | #include "PropertySlot.h" |
29 | #include <stdio.h> | |
30 | #include <wtf/Assertions.h> | |
31 | ||
9dae56ea | 32 | namespace JSC { |
9dae56ea A |
33 | // Hash table generated by the create_hash_table script. |
34 | struct HashTableValue { | |
35 | const char* key; // property name | |
36 | unsigned char attributes; // JSObject attributes | |
37 | intptr_t value1; | |
38 | intptr_t value2; | |
6fe7ccc8 | 39 | Intrinsic intrinsic; |
9dae56ea A |
40 | }; |
41 | ||
42 | // FIXME: There is no reason this get function can't be simpler. | |
ba379fdc | 43 | // ie. typedef JSValue (*GetFunction)(ExecState*, JSObject* baseObject) |
9dae56ea | 44 | typedef PropertySlot::GetValueFunc GetFunction; |
ba379fdc | 45 | typedef void (*PutFunction)(ExecState*, JSObject* baseObject, JSValue value); |
9dae56ea | 46 | |
14957cd0 A |
47 | class HashEntry { |
48 | WTF_MAKE_FAST_ALLOCATED; | |
9dae56ea | 49 | public: |
6fe7ccc8 | 50 | void initialize(StringImpl* key, unsigned char attributes, intptr_t v1, intptr_t v2, Intrinsic intrinsic) |
9dae56ea A |
51 | { |
52 | m_key = key; | |
53 | m_attributes = attributes; | |
54 | m_u.store.value1 = v1; | |
55 | m_u.store.value2 = v2; | |
93a37866 | 56 | m_intrinsic = intrinsic; |
9dae56ea | 57 | m_next = 0; |
9dae56ea A |
58 | } |
59 | ||
14957cd0 A |
60 | void setKey(StringImpl* key) { m_key = key; } |
61 | StringImpl* key() const { return m_key; } | |
9dae56ea A |
62 | |
63 | unsigned char attributes() const { return m_attributes; } | |
64 | ||
6fe7ccc8 A |
65 | Intrinsic intrinsic() const |
66 | { | |
67 | ASSERT(m_attributes & Function); | |
93a37866 | 68 | return m_intrinsic; |
6fe7ccc8 A |
69 | } |
70 | ||
9dae56ea A |
71 | NativeFunction function() const { ASSERT(m_attributes & Function); return m_u.function.functionValue; } |
72 | unsigned char functionLength() const { ASSERT(m_attributes & Function); return static_cast<unsigned char>(m_u.function.length); } | |
73 | ||
74 | GetFunction propertyGetter() const { ASSERT(!(m_attributes & Function)); return m_u.property.get; } | |
75 | PutFunction propertyPutter() const { ASSERT(!(m_attributes & Function)); return m_u.property.put; } | |
76 | ||
77 | intptr_t lexerValue() const { ASSERT(!m_attributes); return m_u.lexer.value; } | |
78 | ||
9dae56ea A |
79 | void setNext(HashEntry *next) { m_next = next; } |
80 | HashEntry* next() const { return m_next; } | |
9dae56ea A |
81 | |
82 | private: | |
14957cd0 | 83 | StringImpl* m_key; |
9dae56ea | 84 | unsigned char m_attributes; // JSObject attributes |
93a37866 | 85 | Intrinsic m_intrinsic; |
9dae56ea A |
86 | |
87 | union { | |
88 | struct { | |
89 | intptr_t value1; | |
90 | intptr_t value2; | |
91 | } store; | |
92 | struct { | |
93 | NativeFunction functionValue; | |
94 | intptr_t length; // number of arguments for function | |
95 | } function; | |
96 | struct { | |
97 | GetFunction get; | |
98 | PutFunction put; | |
99 | } property; | |
100 | struct { | |
101 | intptr_t value; | |
102 | intptr_t unused; | |
103 | } lexer; | |
104 | } m_u; | |
105 | ||
9dae56ea | 106 | HashEntry* m_next; |
9dae56ea A |
107 | }; |
108 | ||
109 | struct HashTable { | |
ba379fdc | 110 | |
9dae56ea A |
111 | int compactSize; |
112 | int compactHashSizeMask; | |
ba379fdc | 113 | |
9dae56ea A |
114 | const HashTableValue* values; // Fixed values generated by script. |
115 | mutable const HashEntry* table; // Table allocated at runtime. | |
116 | ||
93a37866 A |
117 | ALWAYS_INLINE HashTable copy() const |
118 | { | |
119 | // Don't copy dynamic table since it's thread specific. | |
120 | HashTable result = { compactSize, compactHashSizeMask, values, 0 }; | |
121 | return result; | |
122 | } | |
123 | ||
124 | ALWAYS_INLINE void initializeIfNeeded(VM* vm) const | |
9dae56ea A |
125 | { |
126 | if (!table) | |
93a37866 | 127 | createTable(vm); |
9dae56ea A |
128 | } |
129 | ||
130 | ALWAYS_INLINE void initializeIfNeeded(ExecState* exec) const | |
131 | { | |
132 | if (!table) | |
93a37866 | 133 | createTable(&exec->vm()); |
9dae56ea A |
134 | } |
135 | ||
6fe7ccc8 | 136 | JS_EXPORT_PRIVATE void deleteTable() const; |
9dae56ea A |
137 | |
138 | // Find an entry in the table, and return the entry. | |
93a37866 | 139 | ALWAYS_INLINE const HashEntry* entry(VM* vm, PropertyName identifier) const |
9dae56ea | 140 | { |
93a37866 | 141 | initializeIfNeeded(vm); |
9dae56ea A |
142 | return entry(identifier); |
143 | } | |
144 | ||
93a37866 | 145 | ALWAYS_INLINE const HashEntry* entry(ExecState* exec, PropertyName identifier) const |
9dae56ea A |
146 | { |
147 | initializeIfNeeded(exec); | |
148 | return entry(identifier); | |
149 | } | |
150 | ||
6fe7ccc8 A |
151 | class ConstIterator { |
152 | public: | |
153 | ConstIterator(const HashTable* table, int position) | |
154 | : m_table(table) | |
155 | , m_position(position) | |
156 | { | |
157 | skipInvalidKeys(); | |
158 | } | |
159 | ||
160 | const HashEntry* operator->() | |
161 | { | |
162 | return &m_table->table[m_position]; | |
163 | } | |
164 | ||
165 | const HashEntry* operator*() | |
166 | { | |
167 | return &m_table->table[m_position]; | |
168 | } | |
169 | ||
170 | bool operator!=(const ConstIterator& other) | |
171 | { | |
172 | ASSERT(m_table == other.m_table); | |
173 | return m_position != other.m_position; | |
174 | } | |
175 | ||
176 | ConstIterator& operator++() | |
177 | { | |
178 | ASSERT(m_position < m_table->compactSize); | |
179 | ++m_position; | |
180 | skipInvalidKeys(); | |
181 | return *this; | |
182 | } | |
183 | ||
184 | private: | |
185 | void skipInvalidKeys() | |
186 | { | |
187 | ASSERT(m_position <= m_table->compactSize); | |
188 | while (m_position < m_table->compactSize && !m_table->table[m_position].key()) | |
189 | ++m_position; | |
190 | ASSERT(m_position <= m_table->compactSize); | |
191 | } | |
192 | ||
193 | const HashTable* m_table; | |
194 | int m_position; | |
195 | }; | |
196 | ||
93a37866 | 197 | ConstIterator begin(VM& vm) const |
6fe7ccc8 | 198 | { |
93a37866 | 199 | initializeIfNeeded(&vm); |
6fe7ccc8 A |
200 | return ConstIterator(this, 0); |
201 | } | |
93a37866 | 202 | ConstIterator end(VM& vm) const |
6fe7ccc8 | 203 | { |
93a37866 | 204 | initializeIfNeeded(&vm); |
6fe7ccc8 A |
205 | return ConstIterator(this, compactSize); |
206 | } | |
207 | ||
9dae56ea | 208 | private: |
93a37866 | 209 | ALWAYS_INLINE const HashEntry* entry(PropertyName propertyName) const |
9dae56ea | 210 | { |
93a37866 A |
211 | StringImpl* impl = propertyName.publicName(); |
212 | if (!impl) | |
213 | return 0; | |
214 | ||
9dae56ea A |
215 | ASSERT(table); |
216 | ||
93a37866 | 217 | const HashEntry* entry = &table[impl->existingHash() & compactHashSizeMask]; |
9dae56ea A |
218 | |
219 | if (!entry->key()) | |
220 | return 0; | |
221 | ||
222 | do { | |
93a37866 | 223 | if (entry->key() == impl) |
9dae56ea A |
224 | return entry; |
225 | entry = entry->next(); | |
226 | } while (entry); | |
227 | ||
228 | return 0; | |
9dae56ea A |
229 | } |
230 | ||
231 | // Convert the hash table keys to identifiers. | |
93a37866 | 232 | JS_EXPORT_PRIVATE void createTable(VM*) const; |
9dae56ea A |
233 | }; |
234 | ||
93a37866 | 235 | JS_EXPORT_PRIVATE bool setUpStaticFunctionSlot(ExecState*, const HashEntry*, JSObject* thisObject, PropertyName, PropertySlot&); |
9dae56ea A |
236 | |
237 | /** | |
238 | * This method does it all (looking in the hashtable, checking for function | |
239 | * overrides, creating the function or retrieving from cache, calling | |
240 | * getValueProperty in case of a non-function property, forwarding to parent if | |
241 | * unknown property). | |
242 | */ | |
243 | template <class ThisImp, class ParentImp> | |
93a37866 | 244 | inline bool getStaticPropertySlot(ExecState* exec, const HashTable* table, ThisImp* thisObj, PropertyName propertyName, PropertySlot& slot) |
9dae56ea A |
245 | { |
246 | const HashEntry* entry = table->entry(exec, propertyName); | |
247 | ||
248 | if (!entry) // not found, forward to parent | |
6fe7ccc8 | 249 | return ParentImp::getOwnPropertySlot(thisObj, exec, propertyName, slot); |
9dae56ea A |
250 | |
251 | if (entry->attributes() & Function) | |
6fe7ccc8 | 252 | return setUpStaticFunctionSlot(exec, entry, thisObj, propertyName, slot); |
9dae56ea | 253 | |
6fe7ccc8 | 254 | slot.setCacheableCustom(thisObj, entry->propertyGetter()); |
9dae56ea A |
255 | return true; |
256 | } | |
257 | ||
f9bf01c6 | 258 | template <class ThisImp, class ParentImp> |
93a37866 | 259 | inline bool getStaticPropertyDescriptor(ExecState* exec, const HashTable* table, ThisImp* thisObj, PropertyName propertyName, PropertyDescriptor& descriptor) |
f9bf01c6 A |
260 | { |
261 | const HashEntry* entry = table->entry(exec, propertyName); | |
262 | ||
263 | if (!entry) // not found, forward to parent | |
6fe7ccc8 | 264 | return ParentImp::getOwnPropertyDescriptor(thisObj, exec, propertyName, descriptor); |
f9bf01c6 A |
265 | |
266 | PropertySlot slot; | |
6fe7ccc8 A |
267 | if (entry->attributes() & Function) { |
268 | bool present = setUpStaticFunctionSlot(exec, entry, thisObj, propertyName, slot); | |
269 | if (present) | |
270 | descriptor.setDescriptor(slot.getValue(exec, propertyName), entry->attributes()); | |
271 | return present; | |
272 | } | |
f9bf01c6 | 273 | |
6fe7ccc8 | 274 | slot.setCustom(thisObj, entry->propertyGetter()); |
f9bf01c6 A |
275 | descriptor.setDescriptor(slot.getValue(exec, propertyName), entry->attributes()); |
276 | return true; | |
277 | } | |
278 | ||
9dae56ea A |
279 | /** |
280 | * Simplified version of getStaticPropertySlot in case there are only functions. | |
281 | * Using this instead of getStaticPropertySlot allows 'this' to avoid implementing | |
282 | * a dummy getValueProperty. | |
283 | */ | |
284 | template <class ParentImp> | |
93a37866 | 285 | inline bool getStaticFunctionSlot(ExecState* exec, const HashTable* table, JSObject* thisObj, PropertyName propertyName, PropertySlot& slot) |
9dae56ea | 286 | { |
6fe7ccc8 | 287 | if (ParentImp::getOwnPropertySlot(thisObj, exec, propertyName, slot)) |
9dae56ea A |
288 | return true; |
289 | ||
290 | const HashEntry* entry = table->entry(exec, propertyName); | |
291 | if (!entry) | |
292 | return false; | |
293 | ||
6fe7ccc8 | 294 | return setUpStaticFunctionSlot(exec, entry, thisObj, propertyName, slot); |
9dae56ea | 295 | } |
f9bf01c6 A |
296 | |
297 | /** | |
298 | * Simplified version of getStaticPropertyDescriptor in case there are only functions. | |
299 | * Using this instead of getStaticPropertyDescriptor allows 'this' to avoid implementing | |
300 | * a dummy getValueProperty. | |
301 | */ | |
302 | template <class ParentImp> | |
93a37866 | 303 | inline bool getStaticFunctionDescriptor(ExecState* exec, const HashTable* table, JSObject* thisObj, PropertyName propertyName, PropertyDescriptor& descriptor) |
f9bf01c6 | 304 | { |
6fe7ccc8 | 305 | if (ParentImp::getOwnPropertyDescriptor(static_cast<ParentImp*>(thisObj), exec, propertyName, descriptor)) |
f9bf01c6 A |
306 | return true; |
307 | ||
308 | const HashEntry* entry = table->entry(exec, propertyName); | |
309 | if (!entry) | |
310 | return false; | |
311 | ||
312 | PropertySlot slot; | |
6fe7ccc8 A |
313 | bool present = setUpStaticFunctionSlot(exec, entry, thisObj, propertyName, slot); |
314 | if (present) | |
315 | descriptor.setDescriptor(slot.getValue(exec, propertyName), entry->attributes()); | |
316 | return present; | |
f9bf01c6 | 317 | } |
9dae56ea A |
318 | |
319 | /** | |
320 | * Simplified version of getStaticPropertySlot in case there are no functions, only "values". | |
321 | * Using this instead of getStaticPropertySlot removes the need for a FuncImp class. | |
322 | */ | |
323 | template <class ThisImp, class ParentImp> | |
93a37866 | 324 | inline bool getStaticValueSlot(ExecState* exec, const HashTable* table, ThisImp* thisObj, PropertyName propertyName, PropertySlot& slot) |
9dae56ea A |
325 | { |
326 | const HashEntry* entry = table->entry(exec, propertyName); | |
327 | ||
328 | if (!entry) // not found, forward to parent | |
6fe7ccc8 | 329 | return ParentImp::getOwnPropertySlot(thisObj, exec, propertyName, slot); |
9dae56ea A |
330 | |
331 | ASSERT(!(entry->attributes() & Function)); | |
332 | ||
4e4e5a6f | 333 | slot.setCacheableCustom(thisObj, entry->propertyGetter()); |
9dae56ea A |
334 | return true; |
335 | } | |
336 | ||
f9bf01c6 A |
337 | /** |
338 | * Simplified version of getStaticPropertyDescriptor in case there are no functions, only "values". | |
339 | * Using this instead of getStaticPropertyDescriptor removes the need for a FuncImp class. | |
340 | */ | |
341 | template <class ThisImp, class ParentImp> | |
93a37866 | 342 | inline bool getStaticValueDescriptor(ExecState* exec, const HashTable* table, ThisImp* thisObj, PropertyName propertyName, PropertyDescriptor& descriptor) |
f9bf01c6 A |
343 | { |
344 | const HashEntry* entry = table->entry(exec, propertyName); | |
345 | ||
346 | if (!entry) // not found, forward to parent | |
6fe7ccc8 | 347 | return ParentImp::getOwnPropertyDescriptor(thisObj, exec, propertyName, descriptor); |
f9bf01c6 A |
348 | |
349 | ASSERT(!(entry->attributes() & Function)); | |
350 | PropertySlot slot; | |
351 | slot.setCustom(thisObj, entry->propertyGetter()); | |
352 | descriptor.setDescriptor(slot.getValue(exec, propertyName), entry->attributes()); | |
353 | return true; | |
354 | } | |
355 | ||
93a37866 A |
356 | template <class ThisImp> |
357 | inline void putEntry(ExecState* exec, const HashEntry* entry, PropertyName propertyName, JSValue value, ThisImp* thisObj, bool shouldThrow = false) | |
358 | { | |
359 | // If this is a function put it as an override property. | |
360 | if (entry->attributes() & Function) | |
361 | thisObj->putDirect(exec->vm(), propertyName, value); | |
362 | else if (!(entry->attributes() & ReadOnly)) | |
363 | entry->propertyPutter()(exec, thisObj, value); | |
364 | else if (shouldThrow) | |
365 | throwTypeError(exec, StrictModeReadonlyPropertyWriteError); | |
366 | } | |
367 | ||
9dae56ea A |
368 | /** |
369 | * This one is for "put". | |
370 | * It looks up a hash entry for the property to be set. If an entry | |
371 | * is found it sets the value and returns true, else it returns false. | |
372 | */ | |
373 | template <class ThisImp> | |
93a37866 | 374 | inline bool lookupPut(ExecState* exec, PropertyName propertyName, JSValue value, const HashTable* table, ThisImp* thisObj, bool shouldThrow = false) |
9dae56ea A |
375 | { |
376 | const HashEntry* entry = table->entry(exec, propertyName); | |
6fe7ccc8 | 377 | |
9dae56ea A |
378 | if (!entry) |
379 | return false; | |
380 | ||
93a37866 | 381 | putEntry<ThisImp>(exec, entry, propertyName, value, thisObj, shouldThrow); |
9dae56ea A |
382 | return true; |
383 | } | |
384 | ||
385 | /** | |
386 | * This one is for "put". | |
387 | * It calls lookupPut<ThisImp>() to set the value. If that call | |
388 | * returns false (meaning no entry in the hash table was found), | |
389 | * then it calls put() on the ParentImp class. | |
390 | */ | |
391 | template <class ThisImp, class ParentImp> | |
93a37866 | 392 | inline void lookupPut(ExecState* exec, PropertyName propertyName, JSValue value, const HashTable* table, ThisImp* thisObj, PutPropertySlot& slot) |
9dae56ea | 393 | { |
6fe7ccc8 A |
394 | if (!lookupPut<ThisImp>(exec, propertyName, value, table, thisObj, slot.isStrictMode())) |
395 | ParentImp::put(thisObj, exec, propertyName, value, slot); // not found: forward to parent | |
9dae56ea A |
396 | } |
397 | ||
398 | } // namespace JSC | |
399 | ||
400 | #endif // Lookup_h |