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 transitionTo(StructureID*);
155 void removeDirect(const Identifier& propertyName);
156 bool hasCustomProperties() { return !m_structureID->propertyMap().isEmpty(); }
157 bool hasGetterSetterProperties() { return m_structureID->propertyMap().hasGetterSetterProperties(); }
159 void putDirect(const Identifier& propertyName, JSValue* value, unsigned attr = 0);
160 void putDirect(const Identifier& propertyName, JSValue* value, unsigned attr, bool checkReadOnly, PutPropertySlot& slot);
161 void putDirectFunction(ExecState* exec, InternalFunction* function, unsigned attr = 0);
163 // Fast access to known property offsets.
164 JSValue* getDirectOffset(size_t offset) { return m_propertyStorage[offset]; }
165 void putDirectOffset(size_t offset, JSValue* value) { m_propertyStorage[offset] = value; }
167 void fillGetterPropertySlot(PropertySlot&, JSValue** location);
169 virtual void defineGetter(ExecState*, const Identifier& propertyName, JSObject* getterFunction);
170 virtual void defineSetter(ExecState*, const Identifier& propertyName, JSObject* setterFunction);
171 virtual JSValue* lookupGetter(ExecState*, const Identifier& propertyName);
172 virtual JSValue* lookupSetter(ExecState*, const Identifier& propertyName);
174 virtual bool isActivationObject() const { return false; }
175 virtual bool isGlobalObject() const { return false; }
176 virtual bool isVariableObject() const { return false; }
177 virtual bool isWatchdogException() const { return false; }
178 virtual bool isNotAnObjectErrorStub() const { return false; }
180 void allocatePropertyStorage(size_t oldSize, size_t newSize);
181 bool usingInlineStorage() const { return m_propertyStorage == m_inlineStorage; }
183 static const size_t inlineStorageCapacity = 2;
186 bool getOwnPropertySlotForWrite(ExecState*, const Identifier&, PropertySlot&, bool& slotIsWriteable);
189 const HashEntry* findPropertyHashEntry(ExecState*, const Identifier& propertyName) const;
190 StructureID* createInheritorID();
192 RefPtr<StructureID> m_inheritorID;
194 PropertyStorage m_propertyStorage;
195 JSValue* m_inlineStorage[inlineStorageCapacity];
198 JSObject* constructEmptyObject(ExecState*);
200 inline JSObject::JSObject(JSObject* prototype)
201 : JSCell(prototype->inheritorID())
202 , m_propertyStorage(m_inlineStorage)
204 ASSERT(m_structureID);
205 ASSERT(this->prototype());
206 ASSERT(this->prototype()->isNull() || Heap::heap(this) == Heap::heap(this->prototype()));
207 m_structureID->ref(); // ~JSObject balances this ref()
210 inline JSObject::JSObject(PassRefPtr<StructureID> structureID)
211 : JSCell(structureID.releaseRef()) // ~JSObject balances this ref()
212 , m_propertyStorage(m_inlineStorage)
214 ASSERT(m_structureID);
217 inline JSObject::~JSObject()
219 ASSERT(m_structureID);
220 if (m_propertyStorage != m_inlineStorage)
221 delete [] m_propertyStorage;
222 m_structureID->deref();
225 inline JSValue* JSObject::prototype() const
227 return m_structureID->storedPrototype();
230 inline void JSObject::setPrototype(JSValue* prototype)
233 RefPtr<StructureID> newStructureID = StructureID::changePrototypeTransition(m_structureID, prototype);
234 setStructureID(newStructureID.release());
237 inline void JSObject::setStructureID(PassRefPtr<StructureID> structureID)
239 m_structureID->deref();
240 m_structureID = structureID.releaseRef(); // ~JSObject balances this ref()
243 inline StructureID* JSObject::inheritorID()
246 return m_inheritorID.get();
247 return createInheritorID();
250 inline bool JSCell::isObject(const ClassInfo* info) const
252 for (const ClassInfo* ci = classInfo(); ci; ci = ci->parentClass) {
259 // this method is here to be after the inline declaration of JSCell::isObject
260 inline bool JSValue::isObject(const ClassInfo* classInfo) const
262 return !JSImmediate::isImmediate(this) && asCell()->isObject(classInfo);
265 inline JSValue* JSObject::get(ExecState* exec, const Identifier& propertyName) const
267 PropertySlot slot(const_cast<JSObject*>(this));
268 if (const_cast<JSObject*>(this)->getPropertySlot(exec, propertyName, slot))
269 return slot.getValue(exec, propertyName);
271 return jsUndefined();
274 inline JSValue* JSObject::get(ExecState* exec, unsigned propertyName) const
276 PropertySlot slot(const_cast<JSObject*>(this));
277 if (const_cast<JSObject*>(this)->getPropertySlot(exec, propertyName, slot))
278 return slot.getValue(exec, propertyName);
280 return jsUndefined();
283 // It may seem crazy to inline a function this large but it makes a big difference
284 // since this is function very hot in variable lookup
285 inline bool JSObject::getPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
287 JSObject* object = this;
289 if (object->getOwnPropertySlot(exec, propertyName, slot))
292 JSValue* prototype = object->prototype();
293 if (!prototype->isObject())
296 object = static_cast<JSObject*>(prototype);
300 inline bool JSObject::getPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
302 JSObject* object = this;
305 if (object->getOwnPropertySlot(exec, propertyName, slot))
308 JSValue* prototype = object->prototype();
309 if (!prototype->isObject())
312 object = static_cast<JSObject*>(prototype);
318 // It may seem crazy to inline a function this large, especially a virtual function,
319 // but it makes a big difference to property lookup that derived classes can inline their
320 // base class call to this.
321 ALWAYS_INLINE bool JSObject::getOwnPropertySlotForWrite(ExecState* exec, const Identifier& propertyName, PropertySlot& slot, bool& slotIsWriteable)
324 if (JSValue** location = getDirectLocation(propertyName, attributes)) {
325 if (m_structureID->propertyMap().hasGetterSetterProperties() && location[0]->isGetterSetter()) {
326 slotIsWriteable = false;
327 fillGetterPropertySlot(slot, location);
329 slotIsWriteable = !(attributes & ReadOnly);
330 slot.setValueSlot(this, location, offsetForLocation(location));
335 // non-standard Netscape extension
336 if (propertyName == exec->propertyNames().underscoreProto) {
337 slot.setValue(prototype());
338 slotIsWriteable = false;
345 // It may seem crazy to inline a function this large, especially a virtual function,
346 // but it makes a big difference to property lookup that derived classes can inline their
347 // base class call to this.
348 ALWAYS_INLINE bool JSObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
350 if (JSValue** location = getDirectLocation(propertyName)) {
351 if (m_structureID->propertyMap().hasGetterSetterProperties() && location[0]->isGetterSetter())
352 fillGetterPropertySlot(slot, location);
354 slot.setValueSlot(this, location, offsetForLocation(location));
358 // non-standard Netscape extension
359 if (propertyName == exec->propertyNames().underscoreProto) {
360 slot.setValue(prototype());
367 inline void JSObject::putDirect(const Identifier& propertyName, JSValue* value, unsigned attr)
369 PutPropertySlot slot;
370 putDirect(propertyName, value, attr, false, slot);
373 inline void JSObject::putDirect(const Identifier& propertyName, JSValue* value, unsigned attributes, bool checkReadOnly, PutPropertySlot& slot)
375 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
377 if (m_structureID->isDictionary()) {
378 unsigned currentAttributes;
379 size_t offset = m_structureID->propertyMap().getOffset(propertyName, currentAttributes);
380 if (offset != WTF::notFound) {
381 if (checkReadOnly && currentAttributes & ReadOnly)
383 m_propertyStorage[offset] = value;
384 slot.setExistingProperty(this, offset);
388 if (m_structureID->propertyMap().storageSize() == inlineStorageCapacity)
389 allocatePropertyStorage(m_structureID->propertyMap().storageSize(), m_structureID->propertyMap().size());
390 m_structureID->propertyMap().put(propertyName, value, attributes, checkReadOnly, this, slot, m_propertyStorage);
394 unsigned currentAttributes;
395 size_t offset = m_structureID->propertyMap().getOffset(propertyName, currentAttributes);
396 if (offset != WTF::notFound) {
397 if (checkReadOnly && currentAttributes & ReadOnly)
399 m_propertyStorage[offset] = value;
400 slot.setExistingProperty(this, offset);
404 if (m_structureID->propertyMap().storageSize() == inlineStorageCapacity)
405 allocatePropertyStorage(m_structureID->propertyMap().storageSize(), m_structureID->propertyMap().size());
407 RefPtr<StructureID> structureID = StructureID::addPropertyTransition(m_structureID, propertyName, value, attributes, this, slot, m_propertyStorage);
408 slot.setWasTransition(true);
409 setStructureID(structureID.release());
412 inline void JSObject::transitionTo(StructureID* newStructureID)
414 StructureID::transitionTo(m_structureID, newStructureID, this);
415 setStructureID(newStructureID);
418 inline JSValue* JSObject::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
420 return defaultValue(exec, preferredType);
423 inline JSValue* JSValue::get(ExecState* exec, const Identifier& propertyName) const
425 PropertySlot slot(const_cast<JSValue*>(this));
426 return get(exec, propertyName, slot);
429 inline JSValue* JSValue::get(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) const
431 if (UNLIKELY(JSImmediate::isImmediate(this))) {
432 JSObject* prototype = JSImmediate::prototype(this, exec);
433 if (!prototype->getPropertySlot(exec, propertyName, slot))
434 return jsUndefined();
435 return slot.getValue(exec, propertyName);
437 JSCell* cell = static_cast<JSCell*>(const_cast<JSValue*>(this));
439 if (cell->getOwnPropertySlot(exec, propertyName, slot))
440 return slot.getValue(exec, propertyName);
441 ASSERT(cell->isObject());
442 JSValue* prototype = static_cast<JSObject*>(cell)->prototype();
443 if (!prototype->isObject())
444 return jsUndefined();
445 cell = static_cast<JSCell*>(prototype);
449 inline JSValue* JSValue::get(ExecState* exec, unsigned propertyName) const
451 PropertySlot slot(const_cast<JSValue*>(this));
452 return get(exec, propertyName, slot);
455 inline JSValue* JSValue::get(ExecState* exec, unsigned propertyName, PropertySlot& slot) const
457 if (UNLIKELY(JSImmediate::isImmediate(this))) {
458 JSObject* prototype = JSImmediate::prototype(this, exec);
459 if (!prototype->getPropertySlot(exec, propertyName, slot))
460 return jsUndefined();
461 return slot.getValue(exec, propertyName);
463 JSCell* cell = const_cast<JSCell*>(asCell());
465 if (cell->getOwnPropertySlot(exec, propertyName, slot))
466 return slot.getValue(exec, propertyName);
467 ASSERT(cell->isObject());
468 JSValue* prototype = static_cast<JSObject*>(cell)->prototype();
469 if (!prototype->isObject())
470 return jsUndefined();
471 cell = static_cast<JSCell*>(prototype);
475 inline void JSValue::put(ExecState* exec, const Identifier& propertyName, JSValue* value, PutPropertySlot& slot)
477 if (UNLIKELY(JSImmediate::isImmediate(this))) {
478 JSImmediate::toObject(this, exec)->put(exec, propertyName, value, slot);
481 asCell()->put(exec, propertyName, value, slot);
484 inline void JSValue::put(ExecState* exec, unsigned propertyName, JSValue* value)
486 if (UNLIKELY(JSImmediate::isImmediate(this))) {
487 JSImmediate::toObject(this, exec)->put(exec, propertyName, value);
490 asCell()->put(exec, propertyName, value);