source: webkit/trunk/JavaScriptCore/runtime/JSObject.h@ 53400

Last change on this file since 53400 was 53170, checked in by [email protected], 15 years ago

2010-01-12 Kent Hansen <[email protected]>

Reviewed by Geoffrey Garen.

[ES5] Implement Object.getOwnPropertyNames
https://bugs.webkit.org/show_bug.cgi?id=32242

Add an extra argument to getPropertyNames() and getOwnPropertyNames()
(and all reimplementations thereof) that indicates whether non-enumerable
properties should be added.

  • API/JSCallbackObject.h:
  • API/JSCallbackObjectFunctions.h: (JSC::::getOwnPropertyNames):
  • JavaScriptCore.exp:
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.def:
  • debugger/DebuggerActivation.cpp: (JSC::DebuggerActivation::getOwnPropertyNames):
  • debugger/DebuggerActivation.h:
  • runtime/Arguments.cpp: (JSC::Arguments::getOwnPropertyNames):
  • runtime/Arguments.h:
  • runtime/CommonIdentifiers.h:
  • runtime/JSArray.cpp: (JSC::JSArray::getOwnPropertyNames):
  • runtime/JSArray.h:
  • runtime/JSByteArray.cpp: (JSC::JSByteArray::getOwnPropertyNames):
  • runtime/JSByteArray.h:
  • runtime/JSFunction.cpp: (JSC::JSFunction::getOwnPropertyNames):
  • runtime/JSFunction.h:
  • runtime/JSNotAnObject.cpp: (JSC::JSNotAnObject::getOwnPropertyNames):
  • runtime/JSNotAnObject.h:
  • runtime/JSObject.cpp: (JSC::getClassPropertyNames): (JSC::JSObject::getPropertyNames): (JSC::JSObject::getOwnPropertyNames):
  • runtime/JSObject.h:
  • runtime/JSVariableObject.cpp: (JSC::JSVariableObject::getOwnPropertyNames):
  • runtime/JSVariableObject.h:
  • runtime/ObjectConstructor.cpp: (JSC::ObjectConstructor::ObjectConstructor): (JSC::objectConstructorGetOwnPropertyNames):
  • runtime/RegExpMatchesArray.h: (JSC::RegExpMatchesArray::getOwnPropertyNames):
  • runtime/StringObject.cpp: (JSC::StringObject::getOwnPropertyNames):
  • runtime/StringObject.h:
  • runtime/Structure.cpp: Rename getEnumerablePropertyNames() to getPropertyNames(), which takes an extra argument. (JSC::Structure::getPropertyNames):
  • runtime/Structure.h: (JSC::):

2010-01-12 Kent Hansen <[email protected]>

Reviewed by Geoffrey Garen.

[ES5] Implement Object.getOwnPropertyNames
https://bugs.webkit.org/show_bug.cgi?id=32242

Add new argument to the reimplementation of getOwnPropertyNames().

  • UserObjectImp.cpp: (UserObjectImp::getOwnPropertyNames):
  • UserObjectImp.h:

2010-01-12 Kent Hansen <[email protected]>

Reviewed by Geoffrey Garen.

[ES5] Implement Object.getOwnPropertyNames
https://bugs.webkit.org/show_bug.cgi?id=32242

Add tests for Object.getOwnPropertyNames(o), both standard usage and cross origin.

  • fast/js/Object-getOwnPropertyNames-expected.txt: Added.
  • fast/js/Object-getOwnPropertyNames.html: Added.
  • fast/js/script-tests/Object-getOwnPropertyNames.js: Added.
  • http/tests/security/cross-frame-access-enumeration-expected.txt:
  • http/tests/security/cross-frame-access-enumeration.html:

2010-01-12 Kent Hansen <[email protected]>

Reviewed by Geoffrey Garen.

[ES5] Implement Object.getOwnPropertyNames
https://bugs.webkit.org/show_bug.cgi?id=32242

Add new argument to reimplementations of getPropertyNames()
and getOwnPropertyNames(), and update the JS bindings generator.

Test: fast/js/Object-getOwnPropertyNames.html

  • bindings/js/JSDOMWindowCustom.cpp: (WebCore::JSDOMWindow::getPropertyNames): (WebCore::JSDOMWindow::getOwnPropertyNames):
  • bindings/js/JSDOMWindowShell.cpp: (WebCore::JSDOMWindowShell::getPropertyNames): (WebCore::JSDOMWindowShell::getOwnPropertyNames):
  • bindings/js/JSDOMWindowShell.h:
  • bindings/js/JSHistoryCustom.cpp: (WebCore::JSHistory::getOwnPropertyNames):
  • bindings/js/JSLocationCustom.cpp: (WebCore::JSLocation::getOwnPropertyNames):
  • bindings/js/JSQuarantinedObjectWrapper.cpp: (WebCore::JSQuarantinedObjectWrapper::getPropertyNames): (WebCore::JSQuarantinedObjectWrapper::getOwnPropertyNames):
  • bindings/js/JSQuarantinedObjectWrapper.h:
  • bindings/js/JSStorageCustom.cpp: (WebCore::JSStorage::getOwnPropertyNames):
  • bindings/scripts/CodeGeneratorJS.pm:
  • bridge/runtime_array.cpp: (JSC::RuntimeArray::getOwnPropertyNames):
  • bridge/runtime_array.h:
  • bridge/runtime_object.cpp: (JSC::RuntimeObjectImp::getPropertyNames): (JSC::RuntimeObjectImp::getOwnPropertyNames):
  • bridge/runtime_object.h:
  • Property svn:eol-style set to native
File size: 27.4 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, 2009 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 "CallFrame.h"
30#include "JSCell.h"
31#include "JSNumberCell.h"
32#include "MarkStack.h"
33#include "PropertySlot.h"
34#include "PutPropertySlot.h"
35#include "ScopeChain.h"
36#include "Structure.h"
37#include "JSGlobalData.h"
38#include <wtf/StdLibExtras.h>
39
40namespace JSC {
41
42 inline JSCell* getJSFunction(JSGlobalData& globalData, JSValue value)
43 {
44 if (value.isCell() && (value.asCell()->vptr() == globalData.jsFunctionVPtr))
45 return value.asCell();
46 return 0;
47 }
48
49 class HashEntry;
50 class InternalFunction;
51 class PropertyDescriptor;
52 class PropertyNameArray;
53 class Structure;
54 struct HashTable;
55
56 // ECMA 262-3 8.6.1
57 // Property attributes
58 enum Attribute {
59 None = 0,
60 ReadOnly = 1 << 1, // property can be only read, not written
61 DontEnum = 1 << 2, // property doesn't appear in (for .. in ..)
62 DontDelete = 1 << 3, // property can't be deleted
63 Function = 1 << 4, // property is a function - only used by static hashtables
64 Getter = 1 << 5, // property is a getter
65 Setter = 1 << 6 // property is a setter
66 };
67
68 typedef EncodedJSValue* PropertyStorage;
69 typedef const EncodedJSValue* ConstPropertyStorage;
70
71 class JSObject : public JSCell {
72 friend class BatchedTransitionOptimizer;
73 friend class JIT;
74 friend class JSCell;
75
76 public:
77 explicit JSObject(NonNullPassRefPtr<Structure>);
78
79 virtual void markChildren(MarkStack&);
80 ALWAYS_INLINE void markChildrenDirect(MarkStack& markStack);
81
82 // The inline virtual destructor cannot be the first virtual function declared
83 // in the class as it results in the vtable being generated as a weak symbol
84 virtual ~JSObject();
85
86 JSValue prototype() const;
87 void setPrototype(JSValue prototype);
88
89 void setStructure(NonNullPassRefPtr<Structure>);
90 Structure* inheritorID();
91
92 virtual UString className() const;
93
94 JSValue get(ExecState*, const Identifier& propertyName) const;
95 JSValue get(ExecState*, unsigned propertyName) const;
96
97 bool getPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
98 bool getPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
99 bool getPropertyDescriptor(ExecState*, const Identifier& propertyName, PropertyDescriptor&);
100
101 virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
102 virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
103 virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
104
105 virtual void put(ExecState*, const Identifier& propertyName, JSValue value, PutPropertySlot&);
106 virtual void put(ExecState*, unsigned propertyName, JSValue value);
107
108 virtual void putWithAttributes(ExecState*, const Identifier& propertyName, JSValue value, unsigned attributes, bool checkReadOnly, PutPropertySlot& slot);
109 virtual void putWithAttributes(ExecState*, const Identifier& propertyName, JSValue value, unsigned attributes);
110 virtual void putWithAttributes(ExecState*, unsigned propertyName, JSValue value, unsigned attributes);
111
112 bool propertyIsEnumerable(ExecState*, const Identifier& propertyName) const;
113
114 bool hasProperty(ExecState*, const Identifier& propertyName) const;
115 bool hasProperty(ExecState*, unsigned propertyName) const;
116 bool hasOwnProperty(ExecState*, const Identifier& propertyName) const;
117
118 virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
119 virtual bool deleteProperty(ExecState*, unsigned propertyName);
120
121 virtual JSValue defaultValue(ExecState*, PreferredPrimitiveType) const;
122
123 virtual bool hasInstance(ExecState*, JSValue, JSValue prototypeProperty);
124
125 virtual void getPropertyNames(ExecState*, PropertyNameArray&, EnumerationMode mode = ExcludeDontEnumProperties);
126 virtual void getOwnPropertyNames(ExecState*, PropertyNameArray&, EnumerationMode mode = ExcludeDontEnumProperties);
127
128 virtual JSValue toPrimitive(ExecState*, PreferredPrimitiveType = NoPreference) const;
129 virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue& value);
130 virtual bool toBoolean(ExecState*) const;
131 virtual double toNumber(ExecState*) const;
132 virtual UString toString(ExecState*) const;
133 virtual JSObject* toObject(ExecState*) const;
134
135 virtual JSObject* toThisObject(ExecState*) const;
136 virtual JSObject* unwrappedObject();
137
138 bool getPropertySpecificValue(ExecState* exec, const Identifier& propertyName, JSCell*& specificFunction) const;
139
140 // This get function only looks at the property map.
141 JSValue getDirect(const Identifier& propertyName) const
142 {
143 size_t offset = m_structure->get(propertyName);
144 return offset != WTF::notFound ? getDirectOffset(offset) : JSValue();
145 }
146
147 JSValue* getDirectLocation(const Identifier& propertyName)
148 {
149 size_t offset = m_structure->get(propertyName);
150 return offset != WTF::notFound ? locationForOffset(offset) : 0;
151 }
152
153 JSValue* getDirectLocation(const Identifier& propertyName, unsigned& attributes)
154 {
155 JSCell* specificFunction;
156 size_t offset = m_structure->get(propertyName, attributes, specificFunction);
157 return offset != WTF::notFound ? locationForOffset(offset) : 0;
158 }
159
160 size_t offsetForLocation(JSValue* location) const
161 {
162 return location - reinterpret_cast<const JSValue*>(propertyStorage());
163 }
164
165 void transitionTo(Structure*);
166
167 void removeDirect(const Identifier& propertyName);
168 bool hasCustomProperties() { return !m_structure->isEmpty(); }
169 bool hasGetterSetterProperties() { return m_structure->hasGetterSetterProperties(); }
170
171 void putDirect(const Identifier& propertyName, JSValue value, unsigned attr, bool checkReadOnly, PutPropertySlot& slot);
172 void putDirect(const Identifier& propertyName, JSValue value, unsigned attr = 0);
173
174 void putDirectFunction(const Identifier& propertyName, JSCell* value, unsigned attr = 0);
175 void putDirectFunction(const Identifier& propertyName, JSCell* value, unsigned attr, bool checkReadOnly, PutPropertySlot& slot);
176 void putDirectFunction(ExecState* exec, InternalFunction* function, unsigned attr = 0);
177
178 void putDirectWithoutTransition(const Identifier& propertyName, JSValue value, unsigned attr = 0);
179 void putDirectFunctionWithoutTransition(const Identifier& propertyName, JSCell* value, unsigned attr = 0);
180 void putDirectFunctionWithoutTransition(ExecState* exec, InternalFunction* function, unsigned attr = 0);
181
182 // Fast access to known property offsets.
183 JSValue getDirectOffset(size_t offset) const { return JSValue::decode(propertyStorage()[offset]); }
184 void putDirectOffset(size_t offset, JSValue value) { propertyStorage()[offset] = JSValue::encode(value); }
185
186 void fillGetterPropertySlot(PropertySlot&, JSValue* location);
187
188 virtual void defineGetter(ExecState*, const Identifier& propertyName, JSObject* getterFunction, unsigned attributes = 0);
189 virtual void defineSetter(ExecState*, const Identifier& propertyName, JSObject* setterFunction, unsigned attributes = 0);
190 virtual JSValue lookupGetter(ExecState*, const Identifier& propertyName);
191 virtual JSValue lookupSetter(ExecState*, const Identifier& propertyName);
192 virtual bool defineOwnProperty(ExecState*, const Identifier& propertyName, PropertyDescriptor&, bool shouldThrow);
193
194 virtual bool isGlobalObject() const { return false; }
195 virtual bool isVariableObject() const { return false; }
196 virtual bool isActivationObject() const { return false; }
197 virtual bool isWatchdogException() const { return false; }
198 virtual bool isNotAnObjectErrorStub() const { return false; }
199
200 void allocatePropertyStorage(size_t oldSize, size_t newSize);
201 void allocatePropertyStorageInline(size_t oldSize, size_t newSize);
202 bool isUsingInlineStorage() const { return m_structure->isUsingInlineStorage(); }
203
204 static const unsigned inlineStorageCapacity = sizeof(EncodedJSValue) == 2 * sizeof(void*) ? 4 : 3;
205 static const unsigned nonInlineBaseStorageCapacity = 16;
206
207 static PassRefPtr<Structure> createStructure(JSValue prototype)
208 {
209 return Structure::create(prototype, TypeInfo(ObjectType, StructureFlags));
210 }
211
212 void flattenDictionaryObject()
213 {
214 m_structure->flattenDictionaryStructure(this);
215 }
216
217 protected:
218 static const unsigned StructureFlags = 0;
219
220 void addAnonymousSlots(unsigned count);
221 void putAnonymousValue(unsigned index, JSValue value)
222 {
223 *locationForOffset(index) = value;
224 }
225 JSValue getAnonymousValue(unsigned index)
226 {
227 return *locationForOffset(index);
228 }
229
230 private:
231 // Nobody should ever ask any of these questions on something already known to be a JSObject.
232 using JSCell::isAPIValueWrapper;
233 using JSCell::isGetterSetter;
234 using JSCell::toObject;
235 void getObject();
236 void getString(ExecState* exec);
237 void isObject();
238 void isString();
239#if USE(JSVALUE32)
240 void isNumber();
241#endif
242
243 ConstPropertyStorage propertyStorage() const { return (isUsingInlineStorage() ? m_inlineStorage : m_externalStorage); }
244 PropertyStorage propertyStorage() { return (isUsingInlineStorage() ? m_inlineStorage : m_externalStorage); }
245
246 const JSValue* locationForOffset(size_t offset) const
247 {
248 return reinterpret_cast<const JSValue*>(&propertyStorage()[offset]);
249 }
250
251 JSValue* locationForOffset(size_t offset)
252 {
253 return reinterpret_cast<JSValue*>(&propertyStorage()[offset]);
254 }
255
256 void putDirectInternal(const Identifier& propertyName, JSValue value, unsigned attr, bool checkReadOnly, PutPropertySlot& slot, JSCell*);
257 void putDirectInternal(JSGlobalData&, const Identifier& propertyName, JSValue value, unsigned attr, bool checkReadOnly, PutPropertySlot& slot);
258 void putDirectInternal(JSGlobalData&, const Identifier& propertyName, JSValue value, unsigned attr = 0);
259
260 bool inlineGetOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
261
262 const HashEntry* findPropertyHashEntry(ExecState*, const Identifier& propertyName) const;
263 Structure* createInheritorID();
264
265 union {
266 PropertyStorage m_externalStorage;
267 EncodedJSValue m_inlineStorage[inlineStorageCapacity];
268 };
269
270 RefPtr<Structure> m_inheritorID;
271 };
272
273inline JSObject* asObject(JSCell* cell)
274{
275 ASSERT(cell->isObject());
276 return static_cast<JSObject*>(cell);
277}
278
279inline JSObject* asObject(JSValue value)
280{
281 return asObject(value.asCell());
282}
283
284inline JSObject::JSObject(NonNullPassRefPtr<Structure> structure)
285 : JSCell(structure.releaseRef()) // ~JSObject balances this ref()
286{
287 ASSERT(m_structure->propertyStorageCapacity() == inlineStorageCapacity);
288 ASSERT(m_structure->isEmpty());
289 ASSERT(prototype().isNull() || Heap::heap(this) == Heap::heap(prototype()));
290#if USE(JSVALUE64) || USE(JSVALUE32_64)
291 ASSERT(OBJECT_OFFSETOF(JSObject, m_inlineStorage) % sizeof(double) == 0);
292#endif
293}
294
295inline JSObject::~JSObject()
296{
297 ASSERT(m_structure);
298 if (!isUsingInlineStorage())
299 delete [] m_externalStorage;
300 m_structure->deref();
301}
302
303inline JSValue JSObject::prototype() const
304{
305 return m_structure->storedPrototype();
306}
307
308inline void JSObject::setPrototype(JSValue prototype)
309{
310 ASSERT(prototype);
311 RefPtr<Structure> newStructure = Structure::changePrototypeTransition(m_structure, prototype);
312 setStructure(newStructure.release());
313}
314
315inline void JSObject::setStructure(NonNullPassRefPtr<Structure> structure)
316{
317 m_structure->deref();
318 m_structure = structure.releaseRef(); // ~JSObject balances this ref()
319}
320
321inline Structure* JSObject::inheritorID()
322{
323 if (m_inheritorID)
324 return m_inheritorID.get();
325 return createInheritorID();
326}
327
328inline bool Structure::isUsingInlineStorage() const
329{
330 return (propertyStorageCapacity() == JSObject::inlineStorageCapacity);
331}
332
333inline bool JSCell::inherits(const ClassInfo* info) const
334{
335 for (const ClassInfo* ci = classInfo(); ci; ci = ci->parentClass) {
336 if (ci == info)
337 return true;
338 }
339 return false;
340}
341
342// this method is here to be after the inline declaration of JSCell::inherits
343inline bool JSValue::inherits(const ClassInfo* classInfo) const
344{
345 return isCell() && asCell()->inherits(classInfo);
346}
347
348ALWAYS_INLINE bool JSObject::inlineGetOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
349{
350 if (JSValue* location = getDirectLocation(propertyName)) {
351 if (m_structure->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
367// It may seem crazy to inline a function this large, especially a virtual function,
368// but it makes a big difference to property lookup that derived classes can inline their
369// base class call to this.
370ALWAYS_INLINE bool JSObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
371{
372 return inlineGetOwnPropertySlot(exec, propertyName, slot);
373}
374
375ALWAYS_INLINE bool JSCell::fastGetOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
376{
377 if (!structure()->typeInfo().overridesGetOwnPropertySlot())
378 return asObject(this)->inlineGetOwnPropertySlot(exec, propertyName, slot);
379 return getOwnPropertySlot(exec, propertyName, slot);
380}
381
382// It may seem crazy to inline a function this large but it makes a big difference
383// since this is function very hot in variable lookup
384inline bool JSObject::getPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
385{
386 JSObject* object = this;
387 while (true) {
388 if (object->fastGetOwnPropertySlot(exec, propertyName, slot))
389 return true;
390 JSValue prototype = object->prototype();
391 if (!prototype.isObject())
392 return false;
393 object = asObject(prototype);
394 }
395}
396
397inline bool JSObject::getPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
398{
399 JSObject* object = this;
400 while (true) {
401 if (object->getOwnPropertySlot(exec, propertyName, slot))
402 return true;
403 JSValue prototype = object->prototype();
404 if (!prototype.isObject())
405 return false;
406 object = asObject(prototype);
407 }
408}
409
410inline JSValue JSObject::get(ExecState* exec, const Identifier& propertyName) const
411{
412 PropertySlot slot(this);
413 if (const_cast<JSObject*>(this)->getPropertySlot(exec, propertyName, slot))
414 return slot.getValue(exec, propertyName);
415
416 return jsUndefined();
417}
418
419inline JSValue JSObject::get(ExecState* exec, unsigned propertyName) const
420{
421 PropertySlot slot(this);
422 if (const_cast<JSObject*>(this)->getPropertySlot(exec, propertyName, slot))
423 return slot.getValue(exec, propertyName);
424
425 return jsUndefined();
426}
427
428inline void JSObject::putDirectInternal(const Identifier& propertyName, JSValue value, unsigned attributes, bool checkReadOnly, PutPropertySlot& slot, JSCell* specificFunction)
429{
430 ASSERT(value);
431 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
432
433 if (m_structure->isDictionary()) {
434 unsigned currentAttributes;
435 JSCell* currentSpecificFunction;
436 size_t offset = m_structure->get(propertyName, currentAttributes, currentSpecificFunction);
437 if (offset != WTF::notFound) {
438 if (currentSpecificFunction && (specificFunction != currentSpecificFunction))
439 m_structure->despecifyDictionaryFunction(propertyName);
440 if (checkReadOnly && currentAttributes & ReadOnly)
441 return;
442 putDirectOffset(offset, value);
443 if (!specificFunction && !currentSpecificFunction)
444 slot.setExistingProperty(this, offset);
445 return;
446 }
447
448 size_t currentCapacity = m_structure->propertyStorageCapacity();
449 offset = m_structure->addPropertyWithoutTransition(propertyName, attributes, specificFunction);
450 if (currentCapacity != m_structure->propertyStorageCapacity())
451 allocatePropertyStorage(currentCapacity, m_structure->propertyStorageCapacity());
452
453 ASSERT(offset < m_structure->propertyStorageCapacity());
454 putDirectOffset(offset, value);
455 // See comment on setNewProperty call below.
456 if (!specificFunction)
457 slot.setNewProperty(this, offset);
458 return;
459 }
460
461 size_t offset;
462 size_t currentCapacity = m_structure->propertyStorageCapacity();
463 if (RefPtr<Structure> structure = Structure::addPropertyTransitionToExistingStructure(m_structure, propertyName, attributes, specificFunction, offset)) {
464 if (currentCapacity != structure->propertyStorageCapacity())
465 allocatePropertyStorage(currentCapacity, structure->propertyStorageCapacity());
466
467 ASSERT(offset < structure->propertyStorageCapacity());
468 setStructure(structure.release());
469 putDirectOffset(offset, value);
470 // See comment on setNewProperty call below.
471 if (!specificFunction)
472 slot.setNewProperty(this, offset);
473 return;
474 }
475
476 unsigned currentAttributes;
477 JSCell* currentSpecificFunction;
478 offset = m_structure->get(propertyName, currentAttributes, currentSpecificFunction);
479 if (offset != WTF::notFound) {
480 if (checkReadOnly && currentAttributes & ReadOnly)
481 return;
482
483 if (currentSpecificFunction && (specificFunction != currentSpecificFunction)) {
484 setStructure(Structure::despecifyFunctionTransition(m_structure, propertyName));
485 putDirectOffset(offset, value);
486 // Function transitions are not currently cachable, so leave the slot in an uncachable state.
487 return;
488 }
489 putDirectOffset(offset, value);
490 slot.setExistingProperty(this, offset);
491 return;
492 }
493
494 // If we have a specific function, we may have got to this point if there is
495 // already a transition with the correct property name and attributes, but
496 // specialized to a different function. In this case we just want to give up
497 // and despecialize the transition.
498 // In this case we clear the value of specificFunction which will result
499 // in us adding a non-specific transition, and any subsequent lookup in
500 // Structure::addPropertyTransitionToExistingStructure will just use that.
501 if (specificFunction && m_structure->hasTransition(propertyName, attributes))
502 specificFunction = 0;
503
504 RefPtr<Structure> structure = Structure::addPropertyTransition(m_structure, propertyName, attributes, specificFunction, offset);
505
506 if (currentCapacity != structure->propertyStorageCapacity())
507 allocatePropertyStorage(currentCapacity, structure->propertyStorageCapacity());
508
509 ASSERT(offset < structure->propertyStorageCapacity());
510 setStructure(structure.release());
511 putDirectOffset(offset, value);
512 // Function transitions are not currently cachable, so leave the slot in an uncachable state.
513 if (!specificFunction)
514 slot.setNewProperty(this, offset);
515}
516
517inline void JSObject::putDirectInternal(JSGlobalData& globalData, const Identifier& propertyName, JSValue value, unsigned attributes, bool checkReadOnly, PutPropertySlot& slot)
518{
519 ASSERT(value);
520 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
521
522 putDirectInternal(propertyName, value, attributes, checkReadOnly, slot, getJSFunction(globalData, value));
523}
524
525inline void JSObject::putDirectInternal(JSGlobalData& globalData, const Identifier& propertyName, JSValue value, unsigned attributes)
526{
527 PutPropertySlot slot;
528 putDirectInternal(propertyName, value, attributes, false, slot, getJSFunction(globalData, value));
529}
530
531inline void JSObject::addAnonymousSlots(unsigned count)
532{
533 size_t currentCapacity = m_structure->propertyStorageCapacity();
534 RefPtr<Structure> structure = Structure::addAnonymousSlotsTransition(m_structure, count);
535
536 if (currentCapacity != structure->propertyStorageCapacity())
537 allocatePropertyStorage(currentCapacity, structure->propertyStorageCapacity());
538
539 setStructure(structure.release());
540}
541
542inline void JSObject::putDirect(const Identifier& propertyName, JSValue value, unsigned attributes, bool checkReadOnly, PutPropertySlot& slot)
543{
544 ASSERT(value);
545 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
546
547 putDirectInternal(propertyName, value, attributes, checkReadOnly, slot, 0);
548}
549
550inline void JSObject::putDirect(const Identifier& propertyName, JSValue value, unsigned attributes)
551{
552 PutPropertySlot slot;
553 putDirectInternal(propertyName, value, attributes, false, slot, 0);
554}
555
556inline void JSObject::putDirectFunction(const Identifier& propertyName, JSCell* value, unsigned attributes, bool checkReadOnly, PutPropertySlot& slot)
557{
558 putDirectInternal(propertyName, value, attributes, checkReadOnly, slot, value);
559}
560
561inline void JSObject::putDirectFunction(const Identifier& propertyName, JSCell* value, unsigned attr)
562{
563 PutPropertySlot slot;
564 putDirectInternal(propertyName, value, attr, false, slot, value);
565}
566
567inline void JSObject::putDirectWithoutTransition(const Identifier& propertyName, JSValue value, unsigned attributes)
568{
569 size_t currentCapacity = m_structure->propertyStorageCapacity();
570 size_t offset = m_structure->addPropertyWithoutTransition(propertyName, attributes, 0);
571 if (currentCapacity != m_structure->propertyStorageCapacity())
572 allocatePropertyStorage(currentCapacity, m_structure->propertyStorageCapacity());
573 putDirectOffset(offset, value);
574}
575
576inline void JSObject::putDirectFunctionWithoutTransition(const Identifier& propertyName, JSCell* value, unsigned attributes)
577{
578 size_t currentCapacity = m_structure->propertyStorageCapacity();
579 size_t offset = m_structure->addPropertyWithoutTransition(propertyName, attributes, value);
580 if (currentCapacity != m_structure->propertyStorageCapacity())
581 allocatePropertyStorage(currentCapacity, m_structure->propertyStorageCapacity());
582 putDirectOffset(offset, value);
583}
584
585inline void JSObject::transitionTo(Structure* newStructure)
586{
587 if (m_structure->propertyStorageCapacity() != newStructure->propertyStorageCapacity())
588 allocatePropertyStorage(m_structure->propertyStorageCapacity(), newStructure->propertyStorageCapacity());
589 setStructure(newStructure);
590}
591
592inline JSValue JSObject::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
593{
594 return defaultValue(exec, preferredType);
595}
596
597inline JSValue JSValue::get(ExecState* exec, const Identifier& propertyName) const
598{
599 PropertySlot slot(asValue());
600 return get(exec, propertyName, slot);
601}
602
603inline JSValue JSValue::get(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) const
604{
605 if (UNLIKELY(!isCell())) {
606 JSObject* prototype = synthesizePrototype(exec);
607 if (propertyName == exec->propertyNames().underscoreProto)
608 return prototype;
609 if (!prototype->getPropertySlot(exec, propertyName, slot))
610 return jsUndefined();
611 return slot.getValue(exec, propertyName);
612 }
613 JSCell* cell = asCell();
614 while (true) {
615 if (cell->fastGetOwnPropertySlot(exec, propertyName, slot))
616 return slot.getValue(exec, propertyName);
617 JSValue prototype = asObject(cell)->prototype();
618 if (!prototype.isObject())
619 return jsUndefined();
620 cell = asObject(prototype);
621 }
622}
623
624inline JSValue JSValue::get(ExecState* exec, unsigned propertyName) const
625{
626 PropertySlot slot(asValue());
627 return get(exec, propertyName, slot);
628}
629
630inline JSValue JSValue::get(ExecState* exec, unsigned propertyName, PropertySlot& slot) const
631{
632 if (UNLIKELY(!isCell())) {
633 JSObject* prototype = synthesizePrototype(exec);
634 if (!prototype->getPropertySlot(exec, propertyName, slot))
635 return jsUndefined();
636 return slot.getValue(exec, propertyName);
637 }
638 JSCell* cell = const_cast<JSCell*>(asCell());
639 while (true) {
640 if (cell->getOwnPropertySlot(exec, propertyName, slot))
641 return slot.getValue(exec, propertyName);
642 JSValue prototype = asObject(cell)->prototype();
643 if (!prototype.isObject())
644 return jsUndefined();
645 cell = prototype.asCell();
646 }
647}
648
649inline void JSValue::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
650{
651 if (UNLIKELY(!isCell())) {
652 synthesizeObject(exec)->put(exec, propertyName, value, slot);
653 return;
654 }
655 asCell()->put(exec, propertyName, value, slot);
656}
657
658inline void JSValue::put(ExecState* exec, unsigned propertyName, JSValue value)
659{
660 if (UNLIKELY(!isCell())) {
661 synthesizeObject(exec)->put(exec, propertyName, value);
662 return;
663 }
664 asCell()->put(exec, propertyName, value);
665}
666
667ALWAYS_INLINE void JSObject::allocatePropertyStorageInline(size_t oldSize, size_t newSize)
668{
669 ASSERT(newSize > oldSize);
670
671 // It's important that this function not rely on m_structure, since
672 // we might be in the middle of a transition.
673 bool wasInline = (oldSize == JSObject::inlineStorageCapacity);
674
675 PropertyStorage oldPropertyStorage = (wasInline ? m_inlineStorage : m_externalStorage);
676 PropertyStorage newPropertyStorage = new EncodedJSValue[newSize];
677
678 for (unsigned i = 0; i < oldSize; ++i)
679 newPropertyStorage[i] = oldPropertyStorage[i];
680
681 if (!wasInline)
682 delete [] oldPropertyStorage;
683
684 m_externalStorage = newPropertyStorage;
685}
686
687ALWAYS_INLINE void JSObject::markChildrenDirect(MarkStack& markStack)
688{
689 JSCell::markChildren(markStack);
690
691 markStack.append(prototype());
692
693 PropertyStorage storage = propertyStorage();
694 size_t storageSize = m_structure->propertyStorageSize();
695 markStack.appendValues(reinterpret_cast<JSValue*>(storage), storageSize);
696}
697
698} // namespace JSC
699
700#endif // JSObject_h
Note: See TracBrowser for help on using the repository browser.