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 "Intrinsic.h"
26 #include "Identifier.h"
27 #include "JSGlobalObject.h"
28 #include "PropertySlot.h"
30 #include <wtf/Assertions.h>
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
42 // FIXME: There is no reason this get function can't be simpler.
43 // ie. typedef JSValue (*GetFunction)(ExecState*, JSObject* baseObject)
44 typedef PropertySlot::GetValueFunc GetFunction
;
45 typedef void (*PutFunction
)(ExecState
*, JSObject
* baseObject
, JSValue value
);
48 WTF_MAKE_FAST_ALLOCATED
;
50 void initialize(StringImpl
* key
, unsigned char attributes
, intptr_t v1
, intptr_t v2
, Intrinsic intrinsic
)
53 m_attributes
= attributes
;
54 m_u
.store
.value1
= v1
;
55 m_u
.store
.value2
= v2
;
56 m_u
.function
.intrinsic
= intrinsic
;
60 void setKey(StringImpl
* key
) { m_key
= key
; }
61 StringImpl
* key() const { return m_key
; }
63 unsigned char attributes() const { return m_attributes
; }
65 Intrinsic
intrinsic() const
67 ASSERT(m_attributes
& Function
);
68 return m_u
.function
.intrinsic
;
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
); }
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
; }
77 intptr_t lexerValue() const { ASSERT(!m_attributes
); return m_u
.lexer
.value
; }
79 void setNext(HashEntry
*next
) { m_next
= next
; }
80 HashEntry
* next() const { return m_next
; }
84 unsigned char m_attributes
; // JSObject attributes
92 NativeFunction functionValue
;
93 intptr_t length
; // number of arguments for function
112 int compactHashSizeMask
;
114 const HashTableValue
* values
; // Fixed values generated by script.
115 mutable const HashEntry
* table
; // Table allocated at runtime.
117 ALWAYS_INLINE
void initializeIfNeeded(JSGlobalData
* globalData
) const
120 createTable(globalData
);
123 ALWAYS_INLINE
void initializeIfNeeded(ExecState
* exec
) const
126 createTable(&exec
->globalData());
129 JS_EXPORT_PRIVATE
void deleteTable() const;
131 // Find an entry in the table, and return the entry.
132 ALWAYS_INLINE
const HashEntry
* entry(JSGlobalData
* globalData
, const Identifier
& identifier
) const
134 initializeIfNeeded(globalData
);
135 return entry(identifier
);
138 ALWAYS_INLINE
const HashEntry
* entry(ExecState
* exec
, const Identifier
& identifier
) const
140 initializeIfNeeded(exec
);
141 return entry(identifier
);
144 class ConstIterator
{
146 ConstIterator(const HashTable
* table
, int position
)
148 , m_position(position
)
153 const HashEntry
* operator->()
155 return &m_table
->table
[m_position
];
158 const HashEntry
* operator*()
160 return &m_table
->table
[m_position
];
163 bool operator!=(const ConstIterator
& other
)
165 ASSERT(m_table
== other
.m_table
);
166 return m_position
!= other
.m_position
;
169 ConstIterator
& operator++()
171 ASSERT(m_position
< m_table
->compactSize
);
178 void skipInvalidKeys()
180 ASSERT(m_position
<= m_table
->compactSize
);
181 while (m_position
< m_table
->compactSize
&& !m_table
->table
[m_position
].key())
183 ASSERT(m_position
<= m_table
->compactSize
);
186 const HashTable
* m_table
;
190 ConstIterator
begin(JSGlobalData
& globalData
) const
192 initializeIfNeeded(&globalData
);
193 return ConstIterator(this, 0);
195 ConstIterator
end(JSGlobalData
& globalData
) const
197 initializeIfNeeded(&globalData
);
198 return ConstIterator(this, compactSize
);
202 ALWAYS_INLINE
const HashEntry
* entry(const Identifier
& identifier
) const
206 const HashEntry
* entry
= &table
[identifier
.impl()->existingHash() & compactHashSizeMask
];
212 if (entry
->key() == identifier
.impl())
214 entry
= entry
->next();
220 // Convert the hash table keys to identifiers.
221 JS_EXPORT_PRIVATE
void createTable(JSGlobalData
*) const;
224 JS_EXPORT_PRIVATE
bool setUpStaticFunctionSlot(ExecState
*, const HashEntry
*, JSObject
* thisObject
, const Identifier
& propertyName
, PropertySlot
&);
227 * This method does it all (looking in the hashtable, checking for function
228 * overrides, creating the function or retrieving from cache, calling
229 * getValueProperty in case of a non-function property, forwarding to parent if
232 template <class ThisImp
, class ParentImp
>
233 inline bool getStaticPropertySlot(ExecState
* exec
, const HashTable
* table
, ThisImp
* thisObj
, const Identifier
& propertyName
, PropertySlot
& slot
)
235 const HashEntry
* entry
= table
->entry(exec
, propertyName
);
237 if (!entry
) // not found, forward to parent
238 return ParentImp::getOwnPropertySlot(thisObj
, exec
, propertyName
, slot
);
240 if (entry
->attributes() & Function
)
241 return setUpStaticFunctionSlot(exec
, entry
, thisObj
, propertyName
, slot
);
243 slot
.setCacheableCustom(thisObj
, entry
->propertyGetter());
247 template <class ThisImp
, class ParentImp
>
248 inline bool getStaticPropertyDescriptor(ExecState
* exec
, const HashTable
* table
, ThisImp
* thisObj
, const Identifier
& propertyName
, PropertyDescriptor
& descriptor
)
250 const HashEntry
* entry
= table
->entry(exec
, propertyName
);
252 if (!entry
) // not found, forward to parent
253 return ParentImp::getOwnPropertyDescriptor(thisObj
, exec
, propertyName
, descriptor
);
256 if (entry
->attributes() & Function
) {
257 bool present
= setUpStaticFunctionSlot(exec
, entry
, thisObj
, propertyName
, slot
);
259 descriptor
.setDescriptor(slot
.getValue(exec
, propertyName
), entry
->attributes());
263 slot
.setCustom(thisObj
, entry
->propertyGetter());
264 descriptor
.setDescriptor(slot
.getValue(exec
, propertyName
), entry
->attributes());
269 * Simplified version of getStaticPropertySlot in case there are only functions.
270 * Using this instead of getStaticPropertySlot allows 'this' to avoid implementing
271 * a dummy getValueProperty.
273 template <class ParentImp
>
274 inline bool getStaticFunctionSlot(ExecState
* exec
, const HashTable
* table
, JSObject
* thisObj
, const Identifier
& propertyName
, PropertySlot
& slot
)
276 if (ParentImp::getOwnPropertySlot(thisObj
, exec
, propertyName
, slot
))
279 const HashEntry
* entry
= table
->entry(exec
, propertyName
);
283 return setUpStaticFunctionSlot(exec
, entry
, thisObj
, propertyName
, slot
);
287 * Simplified version of getStaticPropertyDescriptor in case there are only functions.
288 * Using this instead of getStaticPropertyDescriptor allows 'this' to avoid implementing
289 * a dummy getValueProperty.
291 template <class ParentImp
>
292 inline bool getStaticFunctionDescriptor(ExecState
* exec
, const HashTable
* table
, JSObject
* thisObj
, const Identifier
& propertyName
, PropertyDescriptor
& descriptor
)
294 if (ParentImp::getOwnPropertyDescriptor(static_cast<ParentImp
*>(thisObj
), exec
, propertyName
, descriptor
))
297 const HashEntry
* entry
= table
->entry(exec
, propertyName
);
302 bool present
= setUpStaticFunctionSlot(exec
, entry
, thisObj
, propertyName
, slot
);
304 descriptor
.setDescriptor(slot
.getValue(exec
, propertyName
), entry
->attributes());
309 * Simplified version of getStaticPropertySlot in case there are no functions, only "values".
310 * Using this instead of getStaticPropertySlot removes the need for a FuncImp class.
312 template <class ThisImp
, class ParentImp
>
313 inline bool getStaticValueSlot(ExecState
* exec
, const HashTable
* table
, ThisImp
* thisObj
, const Identifier
& propertyName
, PropertySlot
& slot
)
315 const HashEntry
* entry
= table
->entry(exec
, propertyName
);
317 if (!entry
) // not found, forward to parent
318 return ParentImp::getOwnPropertySlot(thisObj
, exec
, propertyName
, slot
);
320 ASSERT(!(entry
->attributes() & Function
));
322 slot
.setCacheableCustom(thisObj
, entry
->propertyGetter());
327 * Simplified version of getStaticPropertyDescriptor in case there are no functions, only "values".
328 * Using this instead of getStaticPropertyDescriptor removes the need for a FuncImp class.
330 template <class ThisImp
, class ParentImp
>
331 inline bool getStaticValueDescriptor(ExecState
* exec
, const HashTable
* table
, ThisImp
* thisObj
, const Identifier
& propertyName
, PropertyDescriptor
& descriptor
)
333 const HashEntry
* entry
= table
->entry(exec
, propertyName
);
335 if (!entry
) // not found, forward to parent
336 return ParentImp::getOwnPropertyDescriptor(thisObj
, exec
, propertyName
, descriptor
);
338 ASSERT(!(entry
->attributes() & Function
));
340 slot
.setCustom(thisObj
, entry
->propertyGetter());
341 descriptor
.setDescriptor(slot
.getValue(exec
, propertyName
), entry
->attributes());
346 * This one is for "put".
347 * It looks up a hash entry for the property to be set. If an entry
348 * is found it sets the value and returns true, else it returns false.
350 template <class ThisImp
>
351 inline bool lookupPut(ExecState
* exec
, const Identifier
& propertyName
, JSValue value
, const HashTable
* table
, ThisImp
* thisObj
, bool shouldThrow
= false)
353 const HashEntry
* entry
= table
->entry(exec
, propertyName
);
358 // If this is a function put it as an override property.
359 if (entry
->attributes() & Function
)
360 thisObj
->putDirect(exec
->globalData(), propertyName
, value
);
361 else if (!(entry
->attributes() & ReadOnly
))
362 entry
->propertyPutter()(exec
, thisObj
, value
);
363 else if (shouldThrow
)
364 throwTypeError(exec
, StrictModeReadonlyPropertyWriteError
);
370 * This one is for "put".
371 * It calls lookupPut<ThisImp>() to set the value. If that call
372 * returns false (meaning no entry in the hash table was found),
373 * then it calls put() on the ParentImp class.
375 template <class ThisImp
, class ParentImp
>
376 inline void lookupPut(ExecState
* exec
, const Identifier
& propertyName
, JSValue value
, const HashTable
* table
, ThisImp
* thisObj
, PutPropertySlot
& slot
)
378 if (!lookupPut
<ThisImp
>(exec
, propertyName
, value
, table
, thisObj
, slot
.isStrictMode()))
379 ParentImp::put(thisObj
, exec
, propertyName
, value
, slot
); // not found: forward to parent