2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
27 #include "ClassInfo.h"
28 #include "CommonIdentifiers.h"
29 #include "ExecState.h"
30 #include "JSNumberCell.h"
31 #include "PropertyMap.h"
32 #include "PropertySlot.h"
33 #include "PutPropertySlot.h"
34 #include "ScopeChain.h"
35 #include "StructureID.h"
39 class InternalFunction;
40 class PropertyNameArray;
46 // Property attributes
49 ReadOnly = 1 << 1, // property can be only read, not written
50 DontEnum = 1 << 2, // property doesn't appear in (for .. in ..)
51 DontDelete = 1 << 3, // property can't be deleted
52 Function = 1 << 4, // property is a function - only used by static hashtables
55 class JSObject : public JSCell {
56 friend class BatchedTransitionOptimizer;
60 JSObject(PassRefPtr<StructureID>);
61 JSObject(JSObject* prototype);
65 // The inline virtual destructor cannot be the first virtual function declared
66 // in the class as it results in the vtable being generated as a weak symbol
69 bool inherits(const ClassInfo* classInfo) const { return JSCell::isObject(classInfo); }
71 JSValue* prototype() const;
72 void setPrototype(JSValue* prototype);
74 void setStructureID(PassRefPtr<StructureID>);
75 StructureID* inheritorID();
77 PropertyStorage& propertyStorage() { return m_propertyStorage; }
79 virtual UString className() const;
81 JSValue* get(ExecState*, const Identifier& propertyName) const;
82 JSValue* get(ExecState*, unsigned propertyName) const;
84 bool getPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
85 bool getPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
87 virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
88 virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
90 virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, PutPropertySlot&);
91 virtual void put(ExecState*, unsigned propertyName, JSValue* value);
93 virtual void putWithAttributes(ExecState*, const Identifier& propertyName, JSValue* value, unsigned attributes);
94 virtual void putWithAttributes(ExecState*, unsigned propertyName, JSValue* value, unsigned attributes);
96 bool propertyIsEnumerable(ExecState*, const Identifier& propertyName) const;
98 bool hasProperty(ExecState*, const Identifier& propertyName) const;
99 bool hasProperty(ExecState*, unsigned propertyName) const;
100 bool hasOwnProperty(ExecState*, const Identifier& propertyName) const;
102 virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
103 virtual bool deleteProperty(ExecState*, unsigned propertyName);
105 virtual JSValue* defaultValue(ExecState*, PreferredPrimitiveType) const;
107 virtual bool implementsHasInstance() const;
108 virtual bool hasInstance(ExecState*, JSValue*);
110 virtual void getPropertyNames(ExecState*, PropertyNameArray&);
112 virtual JSValue* toPrimitive(ExecState*, PreferredPrimitiveType = NoPreference) const;
113 virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue*& value);
114 virtual bool toBoolean(ExecState*) const;
115 virtual double toNumber(ExecState*) const;
116 virtual UString toString(ExecState*) const;
117 virtual JSObject* toObject(ExecState*) const;
119 virtual JSObject* toThisObject(ExecState*) const;
120 virtual JSGlobalObject* toGlobalObject(ExecState*) const;
122 virtual bool getPropertyAttributes(ExecState*, const Identifier& propertyName, unsigned& attributes) const;
124 // This get function only looks at the property map.
125 JSValue* getDirect(const Identifier& propertyName) const
127 size_t offset = m_structureID->propertyMap().getOffset(propertyName);
128 return offset != WTF::notFound ? m_propertyStorage[offset] : 0;
131 JSValue** getDirectLocation(const Identifier& propertyName)
133 size_t offset = m_structureID->propertyMap().getOffset(propertyName);
134 return offset != WTF::notFound ? locationForOffset(offset) : 0;
137 JSValue** getDirectLocation(const Identifier& propertyName, unsigned& attributes)
139 size_t offset = m_structureID->propertyMap().getOffset(propertyName, attributes);
140 return offset != WTF::notFound ? locationForOffset(offset) : 0;
143 size_t offsetForLocation(JSValue** location)
145 return location - m_propertyStorage;
148 JSValue** locationForOffset(size_t offset)
150 return &m_propertyStorage[offset];
153 void removeDirect(const Identifier& propertyName);
154 bool hasCustomProperties() { return !m_structureID->propertyMap().isEmpty(); }
155 bool hasGetterSetterProperties() { return m_structureID->propertyMap().hasGetterSetterProperties(); }
157 void putDirect(const Identifier& propertyName, JSValue* value, unsigned attr = 0);
158 void putDirect(const Identifier& propertyName, JSValue* value, unsigned attr, bool checkReadOnly, PutPropertySlot& slot);
159 void putDirectFunction(ExecState* exec, InternalFunction* function, unsigned attr = 0);
161 // Fast access to known property offsets.
162 JSValue* getDirectOffset(size_t offset) { return m_propertyStorage[offset]; }
163 void putDirectOffset(size_t offset, JSValue* value) { m_propertyStorage[offset] = value; }
165 void fillGetterPropertySlot(PropertySlot&, JSValue** location);
167 virtual void defineGetter(ExecState*, const Identifier& propertyName, JSObject* getterFunction);
168 virtual void defineSetter(ExecState*, const Identifier& propertyName, JSObject* setterFunction);
169 virtual JSValue* lookupGetter(ExecState*, const Identifier& propertyName);
170 virtual JSValue* lookupSetter(ExecState*, const Identifier& propertyName);
172 virtual bool isActivationObject() const { return false; }
173 virtual bool isGlobalObject() const { return false; }
174 virtual bool isVariableObject() const { return false; }
175 virtual bool isWatchdogException() const { return false; }
176 virtual bool isNotAnObjectErrorStub() const { return false; }
178 void allocatePropertyStorage(size_t oldSize, size_t newSize);
179 bool usingInlineStorage() const { return m_propertyStorage == m_inlineStorage; }
182 bool getOwnPropertySlotForWrite(ExecState*, const Identifier&, PropertySlot&, bool& slotIsWriteable);
185 const HashEntry* findPropertyHashEntry(ExecState*, const Identifier& propertyName) const;
186 StructureID* createInheritorID();
188 static const size_t inlineStorageCapacity = 2;
190 RefPtr<StructureID> m_inheritorID;
192 PropertyStorage m_propertyStorage;
193 JSValue* m_inlineStorage[inlineStorageCapacity];
196 JSObject* constructEmptyObject(ExecState*);
198 inline JSObject::JSObject(JSObject* prototype)
199 : JSCell(prototype->inheritorID())
200 , m_propertyStorage(m_inlineStorage)
202 ASSERT(m_structureID);
203 ASSERT(this->prototype());
204 ASSERT(this->prototype()->isNull() || Heap::heap(this) == Heap::heap(this->prototype()));
205 m_structureID->ref(); // ~JSObject balances this ref()
208 inline JSObject::JSObject(PassRefPtr<StructureID> structureID)
209 : JSCell(structureID.releaseRef()) // ~JSObject balances this ref()
210 , m_propertyStorage(m_inlineStorage)
212 ASSERT(m_structureID);
215 inline JSObject::~JSObject()
217 ASSERT(m_structureID);
218 if (m_propertyStorage != m_inlineStorage)
219 delete [] m_propertyStorage;
220 m_structureID->deref();
223 inline JSValue* JSObject::prototype() const
225 return m_structureID->storedPrototype();
228 inline void JSObject::setPrototype(JSValue* prototype)
231 RefPtr<StructureID> newStructureID = StructureID::changePrototypeTransition(m_structureID, prototype);
232 setStructureID(newStructureID.release());
235 inline void JSObject::setStructureID(PassRefPtr<StructureID> structureID)
237 m_structureID->deref();
238 m_structureID = structureID.releaseRef(); // ~JSObject balances this ref()
241 inline StructureID* JSObject::inheritorID()
244 return m_inheritorID.get();
245 return createInheritorID();
248 inline bool JSCell::isObject(const ClassInfo* info) const
250 for (const ClassInfo* ci = classInfo(); ci; ci = ci->parentClass) {
257 // this method is here to be after the inline declaration of JSCell::isObject
258 inline bool JSValue::isObject(const ClassInfo* classInfo) const
260 return !JSImmediate::isImmediate(this) && asCell()->isObject(classInfo);
263 inline JSValue* JSObject::get(ExecState* exec, const Identifier& propertyName) const
265 PropertySlot slot(const_cast<JSObject*>(this));
266 if (const_cast<JSObject*>(this)->getPropertySlot(exec, propertyName, slot))
267 return slot.getValue(exec, propertyName);
269 return jsUndefined();
272 inline JSValue* JSObject::get(ExecState* exec, unsigned propertyName) const
274 PropertySlot slot(const_cast<JSObject*>(this));
275 if (const_cast<JSObject*>(this)->getPropertySlot(exec, propertyName, slot))
276 return slot.getValue(exec, propertyName);
278 return jsUndefined();
281 // It may seem crazy to inline a function this large but it makes a big difference
282 // since this is function very hot in variable lookup
283 inline bool JSObject::getPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
285 JSObject* object = this;
287 if (object->getOwnPropertySlot(exec, propertyName, slot))
290 JSValue* prototype = object->prototype();
291 if (!prototype->isObject())
294 object = static_cast<JSObject*>(prototype);
298 inline bool JSObject::getPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
300 JSObject* object = this;
303 if (object->getOwnPropertySlot(exec, propertyName, slot))
306 JSValue* prototype = object->prototype();
307 if (!prototype->isObject())
310 object = static_cast<JSObject*>(prototype);
316 // It may seem crazy to inline a function this large, especially a virtual function,
317 // but it makes a big difference to property lookup that derived classes can inline their
318 // base class call to this.
319 ALWAYS_INLINE bool JSObject::getOwnPropertySlotForWrite(ExecState* exec, const Identifier& propertyName, PropertySlot& slot, bool& slotIsWriteable)
322 if (JSValue** location = getDirectLocation(propertyName, attributes)) {
323 if (m_structureID->propertyMap().hasGetterSetterProperties() && location[0]->isGetterSetter()) {
324 slotIsWriteable = false;
325 fillGetterPropertySlot(slot, location);
327 slotIsWriteable = !(attributes & ReadOnly);
328 slot.setValueSlot(this, location, offsetForLocation(location));
333 // non-standard Netscape extension
334 if (propertyName == exec->propertyNames().underscoreProto) {
335 slot.setValue(prototype());
336 slotIsWriteable = false;
343 // It may seem crazy to inline a function this large, especially a virtual function,
344 // but it makes a big difference to property lookup that derived classes can inline their
345 // base class call to this.
346 ALWAYS_INLINE bool JSObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
348 if (JSValue** location = getDirectLocation(propertyName)) {
349 if (m_structureID->propertyMap().hasGetterSetterProperties() && location[0]->isGetterSetter())
350 fillGetterPropertySlot(slot, location);
352 slot.setValueSlot(this, location, offsetForLocation(location));
356 // non-standard Netscape extension
357 if (propertyName == exec->propertyNames().underscoreProto) {
358 slot.setValue(prototype());
365 inline void JSObject::putDirect(const Identifier& propertyName, JSValue* value, unsigned attr)
367 PutPropertySlot slot;
368 putDirect(propertyName, value, attr, false, slot);
371 inline void JSObject::putDirect(const Identifier& propertyName, JSValue* value, unsigned attributes, bool checkReadOnly, PutPropertySlot& slot)
373 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
375 if (m_structureID->isDictionary()) {
376 unsigned currentAttributes;
377 size_t offset = m_structureID->propertyMap().getOffset(propertyName, currentAttributes);
378 if (offset != WTF::notFound) {
379 if (checkReadOnly && currentAttributes & ReadOnly)
381 m_propertyStorage[offset] = value;
382 slot.setExistingProperty(this, offset);
386 if (m_structureID->propertyMap().storageSize() == inlineStorageCapacity)
387 allocatePropertyStorage(m_structureID->propertyMap().storageSize(), m_structureID->propertyMap().size());
388 m_structureID->propertyMap().put(propertyName, value, attributes, checkReadOnly, this, slot, m_propertyStorage);
392 unsigned currentAttributes;
393 size_t offset = m_structureID->propertyMap().getOffset(propertyName, currentAttributes);
394 if (offset != WTF::notFound) {
395 if (checkReadOnly && currentAttributes & ReadOnly)
397 m_propertyStorage[offset] = value;
398 slot.setExistingProperty(this, offset);
402 if (m_structureID->propertyMap().storageSize() == inlineStorageCapacity)
403 allocatePropertyStorage(m_structureID->propertyMap().storageSize(), m_structureID->propertyMap().size());
405 RefPtr<StructureID> structureID = StructureID::addPropertyTransition(m_structureID, propertyName, value, attributes, this, slot, m_propertyStorage);
406 setStructureID(structureID.release());
409 inline JSValue* JSObject::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
411 return defaultValue(exec, preferredType);
414 inline JSValue* JSValue::get(ExecState* exec, const Identifier& propertyName) const
416 PropertySlot slot(const_cast<JSValue*>(this));
417 return get(exec, propertyName, slot);
420 inline JSValue* JSValue::get(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) const
422 if (UNLIKELY(JSImmediate::isImmediate(this))) {
423 JSObject* prototype = JSImmediate::prototype(this, exec);
424 if (!prototype->getPropertySlot(exec, propertyName, slot))
425 return jsUndefined();
426 return slot.getValue(exec, propertyName);
428 JSCell* cell = static_cast<JSCell*>(const_cast<JSValue*>(this));
430 if (cell->getOwnPropertySlot(exec, propertyName, slot))
431 return slot.getValue(exec, propertyName);
432 ASSERT(cell->isObject());
433 JSValue* prototype = static_cast<JSObject*>(cell)->prototype();
434 if (!prototype->isObject())
435 return jsUndefined();
436 cell = static_cast<JSCell*>(prototype);
440 inline JSValue* JSValue::get(ExecState* exec, unsigned propertyName) const
442 PropertySlot slot(const_cast<JSValue*>(this));
443 return get(exec, propertyName, slot);
446 inline JSValue* JSValue::get(ExecState* exec, unsigned propertyName, PropertySlot& slot) const
448 if (UNLIKELY(JSImmediate::isImmediate(this))) {
449 JSObject* prototype = JSImmediate::prototype(this, exec);
450 if (!prototype->getPropertySlot(exec, propertyName, slot))
451 return jsUndefined();
452 return slot.getValue(exec, propertyName);
454 JSCell* cell = const_cast<JSCell*>(asCell());
456 if (cell->getOwnPropertySlot(exec, propertyName, slot))
457 return slot.getValue(exec, propertyName);
458 ASSERT(cell->isObject());
459 JSValue* prototype = static_cast<JSObject*>(cell)->prototype();
460 if (!prototype->isObject())
461 return jsUndefined();
462 cell = static_cast<JSCell*>(prototype);
466 inline void JSValue::put(ExecState* exec, const Identifier& propertyName, JSValue* value, PutPropertySlot& slot)
468 if (UNLIKELY(JSImmediate::isImmediate(this))) {
469 JSImmediate::toObject(this, exec)->put(exec, propertyName, value, slot);
472 asCell()->put(exec, propertyName, value, slot);
475 inline void JSValue::put(ExecState* exec, unsigned propertyName, JSValue* value)
477 if (UNLIKELY(JSImmediate::isImmediate(this))) {
478 JSImmediate::toObject(this, exec)->put(exec, propertyName, value);
481 asCell()->put(exec, propertyName, value);