2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
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.
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.
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
24 #include "CallFrame.h"
25 #include "Identifier.h"
26 #include "JSGlobalObject.h"
28 #include "PropertySlot.h"
30 #include <wtf/Assertions.h>
32 // Bug #26843: Work around Metrowerks compiler bug
34 #define JSC_CONST_HASHTABLE
36 #define JSC_CONST_HASHTABLE const
40 // Hash table generated by the create_hash_table script.
41 struct HashTableValue
{
42 const char* key
; // property name
43 unsigned char attributes
; // JSObject attributes
47 ThunkGenerator generator
;
51 // FIXME: There is no reason this get function can't be simpler.
52 // ie. typedef JSValue (*GetFunction)(ExecState*, JSObject* baseObject)
53 typedef PropertySlot::GetValueFunc GetFunction
;
54 typedef void (*PutFunction
)(ExecState
*, JSObject
* baseObject
, JSValue value
);
57 WTF_MAKE_FAST_ALLOCATED
;
59 void initialize(StringImpl
* key
, unsigned char attributes
, intptr_t v1
, intptr_t v2
61 , ThunkGenerator generator
= 0
66 m_attributes
= attributes
;
67 m_u
.store
.value1
= v1
;
68 m_u
.store
.value2
= v2
;
70 m_u
.function
.generator
= generator
;
75 void setKey(StringImpl
* key
) { m_key
= key
; }
76 StringImpl
* key() const { return m_key
; }
78 unsigned char attributes() const { return m_attributes
; }
80 #if ENABLE(JIT) && ENABLE(JIT_OPTIMIZE_NATIVE_CALL)
81 ThunkGenerator
generator() const { ASSERT(m_attributes
& Function
); return m_u
.function
.generator
; }
83 NativeFunction
function() const { ASSERT(m_attributes
& Function
); return m_u
.function
.functionValue
; }
84 unsigned char functionLength() const { ASSERT(m_attributes
& Function
); return static_cast<unsigned char>(m_u
.function
.length
); }
86 GetFunction
propertyGetter() const { ASSERT(!(m_attributes
& Function
)); return m_u
.property
.get
; }
87 PutFunction
propertyPutter() const { ASSERT(!(m_attributes
& Function
)); return m_u
.property
.put
; }
89 intptr_t lexerValue() const { ASSERT(!m_attributes
); return m_u
.lexer
.value
; }
91 void setNext(HashEntry
*next
) { m_next
= next
; }
92 HashEntry
* next() const { return m_next
; }
96 unsigned char m_attributes
; // JSObject attributes
104 NativeFunction functionValue
;
105 intptr_t length
; // number of arguments for function
107 ThunkGenerator generator
;
126 int compactHashSizeMask
;
128 const HashTableValue
* values
; // Fixed values generated by script.
129 mutable const HashEntry
* table
; // Table allocated at runtime.
131 ALWAYS_INLINE
void initializeIfNeeded(JSGlobalData
* globalData
) const
134 createTable(globalData
);
137 ALWAYS_INLINE
void initializeIfNeeded(ExecState
* exec
) const
140 createTable(&exec
->globalData());
143 void deleteTable() const;
145 // Find an entry in the table, and return the entry.
146 ALWAYS_INLINE
const HashEntry
* entry(JSGlobalData
* globalData
, const Identifier
& identifier
) const
148 initializeIfNeeded(globalData
);
149 return entry(identifier
);
152 ALWAYS_INLINE
const HashEntry
* entry(ExecState
* exec
, const Identifier
& identifier
) const
154 initializeIfNeeded(exec
);
155 return entry(identifier
);
159 ALWAYS_INLINE
const HashEntry
* entry(const Identifier
& identifier
) const
163 const HashEntry
* entry
= &table
[identifier
.impl()->existingHash() & compactHashSizeMask
];
169 if (entry
->key() == identifier
.impl())
171 entry
= entry
->next();
177 // Convert the hash table keys to identifiers.
178 void createTable(JSGlobalData
*) const;
181 void setUpStaticFunctionSlot(ExecState
*, const HashEntry
*, JSObject
* thisObject
, const Identifier
& propertyName
, PropertySlot
&);
184 * This method does it all (looking in the hashtable, checking for function
185 * overrides, creating the function or retrieving from cache, calling
186 * getValueProperty in case of a non-function property, forwarding to parent if
189 template <class ThisImp
, class ParentImp
>
190 inline bool getStaticPropertySlot(ExecState
* exec
, const HashTable
* table
, ThisImp
* thisObj
, const Identifier
& propertyName
, PropertySlot
& slot
)
192 const HashEntry
* entry
= table
->entry(exec
, propertyName
);
194 if (!entry
) // not found, forward to parent
195 return thisObj
->ParentImp::getOwnPropertySlot(exec
, propertyName
, slot
);
197 if (entry
->attributes() & Function
)
198 setUpStaticFunctionSlot(exec
, entry
, thisObj
, propertyName
, slot
);
200 slot
.setCacheableCustom(thisObj
, entry
->propertyGetter());
205 template <class ThisImp
, class ParentImp
>
206 inline bool getStaticPropertyDescriptor(ExecState
* exec
, const HashTable
* table
, ThisImp
* thisObj
, const Identifier
& propertyName
, PropertyDescriptor
& descriptor
)
208 const HashEntry
* entry
= table
->entry(exec
, propertyName
);
210 if (!entry
) // not found, forward to parent
211 return thisObj
->ParentImp::getOwnPropertyDescriptor(exec
, propertyName
, descriptor
);
214 if (entry
->attributes() & Function
)
215 setUpStaticFunctionSlot(exec
, entry
, thisObj
, propertyName
, slot
);
217 slot
.setCustom(thisObj
, entry
->propertyGetter());
219 descriptor
.setDescriptor(slot
.getValue(exec
, propertyName
), entry
->attributes());
224 * Simplified version of getStaticPropertySlot in case there are only functions.
225 * Using this instead of getStaticPropertySlot allows 'this' to avoid implementing
226 * a dummy getValueProperty.
228 template <class ParentImp
>
229 inline bool getStaticFunctionSlot(ExecState
* exec
, const HashTable
* table
, JSObject
* thisObj
, const Identifier
& propertyName
, PropertySlot
& slot
)
231 if (static_cast<ParentImp
*>(thisObj
)->ParentImp::getOwnPropertySlot(exec
, propertyName
, slot
))
234 const HashEntry
* entry
= table
->entry(exec
, propertyName
);
238 setUpStaticFunctionSlot(exec
, entry
, thisObj
, propertyName
, slot
);
243 * Simplified version of getStaticPropertyDescriptor in case there are only functions.
244 * Using this instead of getStaticPropertyDescriptor allows 'this' to avoid implementing
245 * a dummy getValueProperty.
247 template <class ParentImp
>
248 inline bool getStaticFunctionDescriptor(ExecState
* exec
, const HashTable
* table
, JSObject
* thisObj
, const Identifier
& propertyName
, PropertyDescriptor
& descriptor
)
250 if (static_cast<ParentImp
*>(thisObj
)->ParentImp::getOwnPropertyDescriptor(exec
, propertyName
, descriptor
))
253 const HashEntry
* entry
= table
->entry(exec
, propertyName
);
258 setUpStaticFunctionSlot(exec
, entry
, thisObj
, propertyName
, slot
);
259 descriptor
.setDescriptor(slot
.getValue(exec
, propertyName
), entry
->attributes());
264 * Simplified version of getStaticPropertySlot in case there are no functions, only "values".
265 * Using this instead of getStaticPropertySlot removes the need for a FuncImp class.
267 template <class ThisImp
, class ParentImp
>
268 inline bool getStaticValueSlot(ExecState
* exec
, const HashTable
* table
, ThisImp
* thisObj
, const Identifier
& propertyName
, PropertySlot
& slot
)
270 const HashEntry
* entry
= table
->entry(exec
, propertyName
);
272 if (!entry
) // not found, forward to parent
273 return thisObj
->ParentImp::getOwnPropertySlot(exec
, propertyName
, slot
);
275 ASSERT(!(entry
->attributes() & Function
));
277 slot
.setCacheableCustom(thisObj
, entry
->propertyGetter());
282 * Simplified version of getStaticPropertyDescriptor in case there are no functions, only "values".
283 * Using this instead of getStaticPropertyDescriptor removes the need for a FuncImp class.
285 template <class ThisImp
, class ParentImp
>
286 inline bool getStaticValueDescriptor(ExecState
* exec
, const HashTable
* table
, ThisImp
* thisObj
, const Identifier
& propertyName
, PropertyDescriptor
& descriptor
)
288 const HashEntry
* entry
= table
->entry(exec
, propertyName
);
290 if (!entry
) // not found, forward to parent
291 return thisObj
->ParentImp::getOwnPropertyDescriptor(exec
, propertyName
, descriptor
);
293 ASSERT(!(entry
->attributes() & Function
));
295 slot
.setCustom(thisObj
, entry
->propertyGetter());
296 descriptor
.setDescriptor(slot
.getValue(exec
, propertyName
), entry
->attributes());
301 * This one is for "put".
302 * It looks up a hash entry for the property to be set. If an entry
303 * is found it sets the value and returns true, else it returns false.
305 template <class ThisImp
>
306 inline bool lookupPut(ExecState
* exec
, const Identifier
& propertyName
, JSValue value
, const HashTable
* table
, ThisImp
* thisObj
)
308 const HashEntry
* entry
= table
->entry(exec
, propertyName
);
313 if (entry
->attributes() & Function
) { // function: put as override property
314 if (LIKELY(value
.isCell()))
315 thisObj
->putDirectFunction(exec
->globalData(), propertyName
, value
.asCell());
317 thisObj
->putDirect(exec
->globalData(), propertyName
, value
);
318 } else if (!(entry
->attributes() & ReadOnly
))
319 entry
->propertyPutter()(exec
, thisObj
, value
);
325 * This one is for "put".
326 * It calls lookupPut<ThisImp>() to set the value. If that call
327 * returns false (meaning no entry in the hash table was found),
328 * then it calls put() on the ParentImp class.
330 template <class ThisImp
, class ParentImp
>
331 inline void lookupPut(ExecState
* exec
, const Identifier
& propertyName
, JSValue value
, const HashTable
* table
, ThisImp
* thisObj
, PutPropertySlot
& slot
)
333 if (!lookupPut
<ThisImp
>(exec
, propertyName
, value
, table
, thisObj
))
334 thisObj
->ParentImp::put(exec
, propertyName
, value
, slot
); // not found: forward to parent