source: webkit/trunk/JavaScriptCore/kjs/JSObject.h@ 36693

Last change on this file since 36693 was 36436, checked in by [email protected], 17 years ago

2008-09-15 Maciej Stachowiak <[email protected]>

Reviewed by Cameron Zwarich.


  • fix layout test failure introduced by fix for 20849


(The failing test was fast/js/delete-then-put.html)

  • kjs/JSObject.cpp: (JSC::JSObject::removeDirect): Clear enumeration cache in the dictionary case.
  • kjs/JSObject.h: (JSC::JSObject::putDirect): Ditto.
  • kjs/StructureID.h: (JSC::StructureID::clearEnumerationCache): Inline to handle the clear.
  • Property svn:eol-style set to native
File size: 18.2 KB
Line 
1/*
2 * Copyright (C) 1999-2001 Harri Porten ([email protected])
3 * Copyright (C) 2001 Peter Kelly ([email protected])
4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
5 *
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.
10 *
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.
15 *
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.
20 *
21 */
22
23#ifndef JSObject_h
24#define JSObject_h
25
26#include "ArgList.h"
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"
36
37namespace JSC {
38
39 class InternalFunction;
40 class PropertyNameArray;
41 class StructureID;
42 struct HashEntry;
43 struct HashTable;
44
45 // ECMA 262-3 8.6.1
46 // Property attributes
47 enum Attribute {
48 None = 0,
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
53 };
54
55 class JSObject : public JSCell {
56 friend class BatchedTransitionOptimizer;
57 friend class CTI;
58
59 public:
60 JSObject(PassRefPtr<StructureID>);
61 JSObject(JSObject* prototype);
62
63 virtual void mark();
64
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
67 virtual ~JSObject();
68
69 bool inherits(const ClassInfo* classInfo) const { return JSCell::isObject(classInfo); }
70
71 JSValue* prototype() const;
72 void setPrototype(JSValue* prototype);
73
74 void setStructureID(PassRefPtr<StructureID>);
75 StructureID* inheritorID();
76
77 PropertyStorage& propertyStorage() { return m_propertyStorage; }
78
79 virtual UString className() const;
80
81 JSValue* get(ExecState*, const Identifier& propertyName) const;
82 JSValue* get(ExecState*, unsigned propertyName) const;
83
84 bool getPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
85 bool getPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
86
87 virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
88 virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
89
90 virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, PutPropertySlot&);
91 virtual void put(ExecState*, unsigned propertyName, JSValue* value);
92
93 virtual void putWithAttributes(ExecState*, const Identifier& propertyName, JSValue* value, unsigned attributes);
94 virtual void putWithAttributes(ExecState*, unsigned propertyName, JSValue* value, unsigned attributes);
95
96 bool propertyIsEnumerable(ExecState*, const Identifier& propertyName) const;
97
98 bool hasProperty(ExecState*, const Identifier& propertyName) const;
99 bool hasProperty(ExecState*, unsigned propertyName) const;
100 bool hasOwnProperty(ExecState*, const Identifier& propertyName) const;
101
102 virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
103 virtual bool deleteProperty(ExecState*, unsigned propertyName);
104
105 virtual JSValue* defaultValue(ExecState*, PreferredPrimitiveType) const;
106
107 virtual bool implementsHasInstance() const;
108 virtual bool hasInstance(ExecState*, JSValue*, JSValue* prototypeProperty);
109
110 virtual void getPropertyNames(ExecState*, PropertyNameArray&);
111
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;
118
119 virtual JSObject* toThisObject(ExecState*) const;
120 virtual JSGlobalObject* toGlobalObject(ExecState*) const;
121
122 virtual bool getPropertyAttributes(ExecState*, const Identifier& propertyName, unsigned& attributes) const;
123
124 // This get function only looks at the property map.
125 JSValue* getDirect(const Identifier& propertyName) const
126 {
127 size_t offset = m_structureID->propertyMap().getOffset(propertyName);
128 return offset != WTF::notFound ? m_propertyStorage[offset] : 0;
129 }
130
131 JSValue** getDirectLocation(const Identifier& propertyName)
132 {
133 size_t offset = m_structureID->propertyMap().getOffset(propertyName);
134 return offset != WTF::notFound ? locationForOffset(offset) : 0;
135 }
136
137 JSValue** getDirectLocation(const Identifier& propertyName, unsigned& attributes)
138 {
139 size_t offset = m_structureID->propertyMap().getOffset(propertyName, attributes);
140 return offset != WTF::notFound ? locationForOffset(offset) : 0;
141 }
142
143 size_t offsetForLocation(JSValue** location)
144 {
145 return location - m_propertyStorage;
146 }
147
148 JSValue** locationForOffset(size_t offset)
149 {
150 return &m_propertyStorage[offset];
151 }
152
153 void transitionTo(StructureID*);
154
155 void removeDirect(const Identifier& propertyName);
156 bool hasCustomProperties() { return !m_structureID->propertyMap().isEmpty(); }
157 bool hasGetterSetterProperties() { return m_structureID->propertyMap().hasGetterSetterProperties(); }
158
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);
162
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; }
166
167 void fillGetterPropertySlot(PropertySlot&, JSValue** location);
168
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);
173
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; }
179
180 void allocatePropertyStorage(size_t oldSize, size_t newSize);
181 bool usingInlineStorage() const { return m_propertyStorage == m_inlineStorage; }
182
183 static const size_t inlineStorageCapacity = 2;
184
185 protected:
186 bool getOwnPropertySlotForWrite(ExecState*, const Identifier&, PropertySlot&, bool& slotIsWriteable);
187
188 private:
189 const HashEntry* findPropertyHashEntry(ExecState*, const Identifier& propertyName) const;
190 StructureID* createInheritorID();
191
192 RefPtr<StructureID> m_inheritorID;
193
194 PropertyStorage m_propertyStorage;
195 JSValue* m_inlineStorage[inlineStorageCapacity];
196 };
197
198 JSObject* constructEmptyObject(ExecState*);
199
200inline JSObject::JSObject(JSObject* prototype)
201 : JSCell(prototype->inheritorID())
202 , m_propertyStorage(m_inlineStorage)
203{
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()
208}
209
210inline JSObject::JSObject(PassRefPtr<StructureID> structureID)
211 : JSCell(structureID.releaseRef()) // ~JSObject balances this ref()
212 , m_propertyStorage(m_inlineStorage)
213{
214 ASSERT(m_structureID);
215}
216
217inline JSObject::~JSObject()
218{
219 ASSERT(m_structureID);
220 if (m_propertyStorage != m_inlineStorage)
221 delete [] m_propertyStorage;
222 m_structureID->deref();
223}
224
225inline JSValue* JSObject::prototype() const
226{
227 return m_structureID->storedPrototype();
228}
229
230inline void JSObject::setPrototype(JSValue* prototype)
231{
232 ASSERT(prototype);
233 RefPtr<StructureID> newStructureID = StructureID::changePrototypeTransition(m_structureID, prototype);
234 setStructureID(newStructureID.release());
235}
236
237inline void JSObject::setStructureID(PassRefPtr<StructureID> structureID)
238{
239 m_structureID->deref();
240 m_structureID = structureID.releaseRef(); // ~JSObject balances this ref()
241}
242
243inline StructureID* JSObject::inheritorID()
244{
245 if (m_inheritorID)
246 return m_inheritorID.get();
247 return createInheritorID();
248}
249
250inline bool JSCell::isObject(const ClassInfo* info) const
251{
252 for (const ClassInfo* ci = classInfo(); ci; ci = ci->parentClass) {
253 if (ci == info)
254 return true;
255 }
256 return false;
257}
258
259// this method is here to be after the inline declaration of JSCell::isObject
260inline bool JSValue::isObject(const ClassInfo* classInfo) const
261{
262 return !JSImmediate::isImmediate(this) && asCell()->isObject(classInfo);
263}
264
265inline JSValue* JSObject::get(ExecState* exec, const Identifier& propertyName) const
266{
267 PropertySlot slot(const_cast<JSObject*>(this));
268 if (const_cast<JSObject*>(this)->getPropertySlot(exec, propertyName, slot))
269 return slot.getValue(exec, propertyName);
270
271 return jsUndefined();
272}
273
274inline JSValue* JSObject::get(ExecState* exec, unsigned propertyName) const
275{
276 PropertySlot slot(const_cast<JSObject*>(this));
277 if (const_cast<JSObject*>(this)->getPropertySlot(exec, propertyName, slot))
278 return slot.getValue(exec, propertyName);
279
280 return jsUndefined();
281}
282
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
285inline bool JSObject::getPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
286{
287 JSObject* object = this;
288 while (true) {
289 if (object->getOwnPropertySlot(exec, propertyName, slot))
290 return true;
291
292 JSValue* prototype = object->prototype();
293 if (!prototype->isObject())
294 return false;
295
296 object = static_cast<JSObject*>(prototype);
297 }
298}
299
300inline bool JSObject::getPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
301{
302 JSObject* object = this;
303
304 while (true) {
305 if (object->getOwnPropertySlot(exec, propertyName, slot))
306 return true;
307
308 JSValue* prototype = object->prototype();
309 if (!prototype->isObject())
310 break;
311
312 object = static_cast<JSObject*>(prototype);
313 }
314
315 return false;
316}
317
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.
321ALWAYS_INLINE bool JSObject::getOwnPropertySlotForWrite(ExecState* exec, const Identifier& propertyName, PropertySlot& slot, bool& slotIsWriteable)
322{
323 unsigned attributes;
324 if (JSValue** location = getDirectLocation(propertyName, attributes)) {
325 if (m_structureID->propertyMap().hasGetterSetterProperties() && location[0]->isGetterSetter()) {
326 slotIsWriteable = false;
327 fillGetterPropertySlot(slot, location);
328 } else {
329 slotIsWriteable = !(attributes & ReadOnly);
330 slot.setValueSlot(this, location, offsetForLocation(location));
331 }
332 return true;
333 }
334
335 // non-standard Netscape extension
336 if (propertyName == exec->propertyNames().underscoreProto) {
337 slot.setValue(prototype());
338 slotIsWriteable = false;
339 return true;
340 }
341
342 return false;
343}
344
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.
348ALWAYS_INLINE bool JSObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
349{
350 if (JSValue** location = getDirectLocation(propertyName)) {
351 if (m_structureID->propertyMap().hasGetterSetterProperties() && location[0]->isGetterSetter())
352 fillGetterPropertySlot(slot, location);
353 else
354 slot.setValueSlot(this, location, offsetForLocation(location));
355 return true;
356 }
357
358 // non-standard Netscape extension
359 if (propertyName == exec->propertyNames().underscoreProto) {
360 slot.setValue(prototype());
361 return true;
362 }
363
364 return false;
365}
366
367inline void JSObject::putDirect(const Identifier& propertyName, JSValue* value, unsigned attr)
368{
369 PutPropertySlot slot;
370 putDirect(propertyName, value, attr, false, slot);
371}
372
373inline void JSObject::putDirect(const Identifier& propertyName, JSValue* value, unsigned attributes, bool checkReadOnly, PutPropertySlot& slot)
374{
375 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
376
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)
382 return;
383 m_propertyStorage[offset] = value;
384 slot.setExistingProperty(this, offset);
385 return;
386 }
387
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);
391 m_structureID->clearEnumerationCache();
392 return;
393 }
394
395 unsigned currentAttributes;
396 size_t offset = m_structureID->propertyMap().getOffset(propertyName, currentAttributes);
397 if (offset != WTF::notFound) {
398 if (checkReadOnly && currentAttributes & ReadOnly)
399 return;
400 m_propertyStorage[offset] = value;
401 slot.setExistingProperty(this, offset);
402 return;
403 }
404
405 if (m_structureID->propertyMap().storageSize() == inlineStorageCapacity)
406 allocatePropertyStorage(m_structureID->propertyMap().storageSize(), m_structureID->propertyMap().size());
407
408 RefPtr<StructureID> structureID = StructureID::addPropertyTransition(m_structureID, propertyName, value, attributes, this, slot, m_propertyStorage);
409 slot.setWasTransition(true);
410 setStructureID(structureID.release());
411}
412
413inline void JSObject::transitionTo(StructureID* newStructureID)
414{
415 StructureID::transitionTo(m_structureID, newStructureID, this);
416 setStructureID(newStructureID);
417}
418
419inline JSValue* JSObject::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
420{
421 return defaultValue(exec, preferredType);
422}
423
424inline JSValue* JSValue::get(ExecState* exec, const Identifier& propertyName) const
425{
426 PropertySlot slot(const_cast<JSValue*>(this));
427 return get(exec, propertyName, slot);
428}
429
430inline JSValue* JSValue::get(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) const
431{
432 if (UNLIKELY(JSImmediate::isImmediate(this))) {
433 JSObject* prototype = JSImmediate::prototype(this, exec);
434 if (!prototype->getPropertySlot(exec, propertyName, slot))
435 return jsUndefined();
436 return slot.getValue(exec, propertyName);
437 }
438 JSCell* cell = static_cast<JSCell*>(const_cast<JSValue*>(this));
439 while (true) {
440 if (cell->getOwnPropertySlot(exec, propertyName, slot))
441 return slot.getValue(exec, propertyName);
442 ASSERT(cell->isObject());
443 JSValue* prototype = static_cast<JSObject*>(cell)->prototype();
444 if (!prototype->isObject())
445 return jsUndefined();
446 cell = static_cast<JSCell*>(prototype);
447 }
448}
449
450inline JSValue* JSValue::get(ExecState* exec, unsigned propertyName) const
451{
452 PropertySlot slot(const_cast<JSValue*>(this));
453 return get(exec, propertyName, slot);
454}
455
456inline JSValue* JSValue::get(ExecState* exec, unsigned propertyName, PropertySlot& slot) const
457{
458 if (UNLIKELY(JSImmediate::isImmediate(this))) {
459 JSObject* prototype = JSImmediate::prototype(this, exec);
460 if (!prototype->getPropertySlot(exec, propertyName, slot))
461 return jsUndefined();
462 return slot.getValue(exec, propertyName);
463 }
464 JSCell* cell = const_cast<JSCell*>(asCell());
465 while (true) {
466 if (cell->getOwnPropertySlot(exec, propertyName, slot))
467 return slot.getValue(exec, propertyName);
468 ASSERT(cell->isObject());
469 JSValue* prototype = static_cast<JSObject*>(cell)->prototype();
470 if (!prototype->isObject())
471 return jsUndefined();
472 cell = static_cast<JSCell*>(prototype);
473 }
474}
475
476inline void JSValue::put(ExecState* exec, const Identifier& propertyName, JSValue* value, PutPropertySlot& slot)
477{
478 if (UNLIKELY(JSImmediate::isImmediate(this))) {
479 JSImmediate::toObject(this, exec)->put(exec, propertyName, value, slot);
480 return;
481 }
482 asCell()->put(exec, propertyName, value, slot);
483}
484
485inline void JSValue::put(ExecState* exec, unsigned propertyName, JSValue* value)
486{
487 if (UNLIKELY(JSImmediate::isImmediate(this))) {
488 JSImmediate::toObject(this, exec)->put(exec, propertyName, value);
489 return;
490 }
491 asCell()->put(exec, propertyName, value);
492}
493
494} // namespace JSC
495
496#endif // JSObject_h
Note: See TracBrowser for help on using the repository browser.