]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSObject.cpp
JavaScriptCore-1097.13.tar.gz
[apple/javascriptcore.git] / runtime / JSObject.cpp
1 /*
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, 2008, 2009 Apple Inc. All rights reserved.
5 * Copyright (C) 2007 Eric Seidel (eric@webkit.org)
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24 #include "config.h"
25 #include "JSObject.h"
26
27 #include "CopiedSpaceInlineMethods.h"
28 #include "DatePrototype.h"
29 #include "ErrorConstructor.h"
30 #include "GetterSetter.h"
31 #include "JSFunction.h"
32 #include "JSGlobalObject.h"
33 #include "JSGlobalThis.h"
34 #include "Lookup.h"
35 #include "NativeErrorConstructor.h"
36 #include "Nodes.h"
37 #include "ObjectPrototype.h"
38 #include "Operations.h"
39 #include "PropertyDescriptor.h"
40 #include "PropertyNameArray.h"
41 #include <math.h>
42 #include <wtf/Assertions.h>
43
44 namespace JSC {
45
46 ASSERT_CLASS_FITS_IN_CELL(JSObject);
47 ASSERT_CLASS_FITS_IN_CELL(JSNonFinalObject);
48 ASSERT_CLASS_FITS_IN_CELL(JSFinalObject);
49
50 ASSERT_HAS_TRIVIAL_DESTRUCTOR(JSObject);
51 ASSERT_HAS_TRIVIAL_DESTRUCTOR(JSFinalObject);
52
53 const char* StrictModeReadonlyPropertyWriteError = "Attempted to assign to readonly property.";
54
55 const ClassInfo JSObject::s_info = { "Object", 0, 0, 0, CREATE_METHOD_TABLE(JSObject) };
56
57 const ClassInfo JSFinalObject::s_info = { "Object", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSFinalObject) };
58
59 static inline void getClassPropertyNames(ExecState* exec, const ClassInfo* classInfo, PropertyNameArray& propertyNames, EnumerationMode mode)
60 {
61 // Add properties from the static hashtables of properties
62 for (; classInfo; classInfo = classInfo->parentClass) {
63 const HashTable* table = classInfo->propHashTable(exec);
64 if (!table)
65 continue;
66 table->initializeIfNeeded(exec);
67 ASSERT(table->table);
68
69 int hashSizeMask = table->compactSize - 1;
70 const HashEntry* entry = table->table;
71 for (int i = 0; i <= hashSizeMask; ++i, ++entry) {
72 if (entry->key() && (!(entry->attributes() & DontEnum) || (mode == IncludeDontEnumProperties)))
73 propertyNames.add(entry->key());
74 }
75 }
76 }
77
78 void JSObject::visitChildren(JSCell* cell, SlotVisitor& visitor)
79 {
80 JSObject* thisObject = jsCast<JSObject*>(cell);
81 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
82 #if !ASSERT_DISABLED
83 bool wasCheckingForDefaultMarkViolation = visitor.m_isCheckingForDefaultMarkViolation;
84 visitor.m_isCheckingForDefaultMarkViolation = false;
85 #endif
86
87 JSCell::visitChildren(thisObject, visitor);
88
89 PropertyStorage storage = thisObject->propertyStorage();
90 size_t storageSize = thisObject->structure()->propertyStorageSize();
91 if (thisObject->isUsingInlineStorage())
92 visitor.appendValues(storage, storageSize);
93 else {
94 // We have this extra temp here to slake GCC's thirst for the blood of those who dereference type-punned pointers.
95 void* temp = storage;
96 visitor.copyAndAppend(&temp, thisObject->structure()->propertyStorageCapacity() * sizeof(WriteBarrierBase<Unknown>), storage->slot(), storageSize);
97 storage = static_cast<PropertyStorage>(temp);
98 thisObject->m_propertyStorage.set(storage, StorageBarrier::Unchecked);
99 }
100
101 if (thisObject->m_inheritorID)
102 visitor.append(&thisObject->m_inheritorID);
103
104 #if !ASSERT_DISABLED
105 visitor.m_isCheckingForDefaultMarkViolation = wasCheckingForDefaultMarkViolation;
106 #endif
107 }
108
109 UString JSObject::className(const JSObject* object)
110 {
111 const ClassInfo* info = object->classInfo();
112 ASSERT(info);
113 return info->className;
114 }
115
116 bool JSObject::getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, PropertySlot& slot)
117 {
118 JSObject* thisObject = jsCast<JSObject*>(cell);
119 return thisObject->methodTable()->getOwnPropertySlot(thisObject, exec, Identifier::from(exec, propertyName), slot);
120 }
121
122 // ECMA 8.6.2.2
123 void JSObject::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
124 {
125 JSObject* thisObject = jsCast<JSObject*>(cell);
126 ASSERT(value);
127 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
128 JSGlobalData& globalData = exec->globalData();
129
130 // Check if there are any setters or getters in the prototype chain
131 JSValue prototype;
132 if (propertyName != exec->propertyNames().underscoreProto) {
133 for (JSObject* obj = thisObject; !obj->structure()->hasReadOnlyOrGetterSetterPropertiesExcludingProto(); obj = asObject(prototype)) {
134 prototype = obj->prototype();
135 if (prototype.isNull()) {
136 if (!thisObject->putDirectInternal<PutModePut>(globalData, propertyName, value, 0, slot, getJSFunction(value)) && slot.isStrictMode())
137 throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
138 return;
139 }
140 }
141 }
142
143 for (JSObject* obj = thisObject; ; obj = asObject(prototype)) {
144 unsigned attributes;
145 JSCell* specificValue;
146 size_t offset = obj->structure()->get(globalData, propertyName, attributes, specificValue);
147 if (offset != WTF::notFound) {
148 if (attributes & ReadOnly) {
149 if (slot.isStrictMode())
150 throwError(exec, createTypeError(exec, StrictModeReadonlyPropertyWriteError));
151 return;
152 }
153
154 JSValue gs = obj->getDirectOffset(offset);
155 if (gs.isGetterSetter()) {
156 JSObject* setterFunc = asGetterSetter(gs)->setter();
157 if (!setterFunc) {
158 if (slot.isStrictMode())
159 throwError(exec, createTypeError(exec, "setting a property that has only a getter"));
160 return;
161 }
162
163 CallData callData;
164 CallType callType = setterFunc->methodTable()->getCallData(setterFunc, callData);
165 MarkedArgumentBuffer args;
166 args.append(value);
167
168 // If this is WebCore's global object then we need to substitute the shell.
169 call(exec, setterFunc, callType, callData, thisObject->methodTable()->toThisObject(thisObject, exec), args);
170 return;
171 }
172
173 // If there's an existing property on the object or one of its
174 // prototypes it should be replaced, so break here.
175 break;
176 }
177
178 prototype = obj->prototype();
179 if (prototype.isNull())
180 break;
181 }
182
183 if (!thisObject->putDirectInternal<PutModePut>(globalData, propertyName, value, 0, slot, getJSFunction(value)) && slot.isStrictMode())
184 throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
185 return;
186 }
187
188 void JSObject::putByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, JSValue value, bool shouldThrow)
189 {
190 PutPropertySlot slot(shouldThrow);
191 JSObject* thisObject = jsCast<JSObject*>(cell);
192 thisObject->methodTable()->put(thisObject, exec, Identifier::from(exec, propertyName), value, slot);
193 }
194
195 void JSObject::putDirectVirtual(JSObject* object, ExecState* exec, const Identifier& propertyName, JSValue value, unsigned attributes)
196 {
197 ASSERT(!value.isGetterSetter() && !(attributes & Accessor));
198 PutPropertySlot slot;
199 object->putDirectInternal<PutModeDefineOwnProperty>(exec->globalData(), propertyName, value, attributes, slot, getJSFunction(value));
200 }
201
202 bool JSObject::setPrototypeWithCycleCheck(JSGlobalData& globalData, JSValue prototype)
203 {
204 JSValue checkFor = this;
205 if (this->isGlobalObject())
206 checkFor = jsCast<JSGlobalObject*>(this)->globalExec()->thisValue();
207
208 JSValue nextPrototype = prototype;
209 while (nextPrototype && nextPrototype.isObject()) {
210 if (nextPrototype == checkFor)
211 return false;
212 nextPrototype = asObject(nextPrototype)->prototype();
213 }
214 setPrototype(globalData, prototype);
215 return true;
216 }
217
218 bool JSObject::allowsAccessFrom(ExecState* exec)
219 {
220 JSGlobalObject* globalObject = isGlobalThis() ? jsCast<JSGlobalThis*>(this)->unwrappedObject() : this->globalObject();
221 return globalObject->globalObjectMethodTable()->allowsAccessFrom(globalObject, exec);
222 }
223
224 void JSObject::putDirectAccessor(JSGlobalData& globalData, const Identifier& propertyName, JSValue value, unsigned attributes)
225 {
226 ASSERT(value.isGetterSetter() && (attributes & Accessor));
227
228 PutPropertySlot slot;
229 putDirectInternal<PutModeDefineOwnProperty>(globalData, propertyName, value, attributes, slot, getJSFunction(value));
230
231 // putDirect will change our Structure if we add a new property. For
232 // getters and setters, though, we also need to change our Structure
233 // if we override an existing non-getter or non-setter.
234 if (slot.type() != PutPropertySlot::NewProperty)
235 setStructure(globalData, Structure::attributeChangeTransition(globalData, structure(), propertyName, attributes));
236
237 if (attributes & ReadOnly)
238 structure()->setContainsReadOnlyProperties();
239
240 structure()->setHasGetterSetterProperties(propertyName == globalData.propertyNames->underscoreProto);
241 }
242
243 bool JSObject::hasProperty(ExecState* exec, const Identifier& propertyName) const
244 {
245 PropertySlot slot;
246 return const_cast<JSObject*>(this)->getPropertySlot(exec, propertyName, slot);
247 }
248
249 bool JSObject::hasProperty(ExecState* exec, unsigned propertyName) const
250 {
251 PropertySlot slot;
252 return const_cast<JSObject*>(this)->getPropertySlot(exec, propertyName, slot);
253 }
254
255 // ECMA 8.6.2.5
256 bool JSObject::deleteProperty(JSCell* cell, ExecState* exec, const Identifier& propertyName)
257 {
258 JSObject* thisObject = jsCast<JSObject*>(cell);
259
260 if (!thisObject->staticFunctionsReified())
261 thisObject->reifyStaticFunctionsForDelete(exec);
262
263 unsigned attributes;
264 JSCell* specificValue;
265 if (thisObject->structure()->get(exec->globalData(), propertyName, attributes, specificValue) != WTF::notFound) {
266 if (attributes & DontDelete && !exec->globalData().isInDefineOwnProperty())
267 return false;
268 thisObject->removeDirect(exec->globalData(), propertyName);
269 return true;
270 }
271
272 // Look in the static hashtable of properties
273 const HashEntry* entry = thisObject->findPropertyHashEntry(exec, propertyName);
274 if (entry && entry->attributes() & DontDelete && !exec->globalData().isInDefineOwnProperty())
275 return false; // this builtin property can't be deleted
276
277 // FIXME: Should the code here actually do some deletion?
278 return true;
279 }
280
281 bool JSObject::hasOwnProperty(ExecState* exec, const Identifier& propertyName) const
282 {
283 PropertySlot slot;
284 return const_cast<JSObject*>(this)->methodTable()->getOwnPropertySlot(const_cast<JSObject*>(this), exec, propertyName, slot);
285 }
286
287 bool JSObject::deletePropertyByIndex(JSCell* cell, ExecState* exec, unsigned propertyName)
288 {
289 JSObject* thisObject = jsCast<JSObject*>(cell);
290 return thisObject->methodTable()->deleteProperty(thisObject, exec, Identifier::from(exec, propertyName));
291 }
292
293 static ALWAYS_INLINE JSValue callDefaultValueFunction(ExecState* exec, const JSObject* object, const Identifier& propertyName)
294 {
295 JSValue function = object->get(exec, propertyName);
296 CallData callData;
297 CallType callType = getCallData(function, callData);
298 if (callType == CallTypeNone)
299 return exec->exception();
300
301 // Prevent "toString" and "valueOf" from observing execution if an exception
302 // is pending.
303 if (exec->hadException())
304 return exec->exception();
305
306 JSValue result = call(exec, function, callType, callData, const_cast<JSObject*>(object), exec->emptyList());
307 ASSERT(!result.isGetterSetter());
308 if (exec->hadException())
309 return exec->exception();
310 if (result.isObject())
311 return JSValue();
312 return result;
313 }
314
315 bool JSObject::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) const
316 {
317 result = methodTable()->defaultValue(this, exec, PreferNumber);
318 number = result.toNumber(exec);
319 return !result.isString();
320 }
321
322 // ECMA 8.6.2.6
323 JSValue JSObject::defaultValue(const JSObject* object, ExecState* exec, PreferredPrimitiveType hint)
324 {
325 // Must call toString first for Date objects.
326 if ((hint == PreferString) || (hint != PreferNumber && object->prototype() == exec->lexicalGlobalObject()->datePrototype())) {
327 JSValue value = callDefaultValueFunction(exec, object, exec->propertyNames().toString);
328 if (value)
329 return value;
330 value = callDefaultValueFunction(exec, object, exec->propertyNames().valueOf);
331 if (value)
332 return value;
333 } else {
334 JSValue value = callDefaultValueFunction(exec, object, exec->propertyNames().valueOf);
335 if (value)
336 return value;
337 value = callDefaultValueFunction(exec, object, exec->propertyNames().toString);
338 if (value)
339 return value;
340 }
341
342 ASSERT(!exec->hadException());
343
344 return throwError(exec, createTypeError(exec, "No default value"));
345 }
346
347 const HashEntry* JSObject::findPropertyHashEntry(ExecState* exec, const Identifier& propertyName) const
348 {
349 for (const ClassInfo* info = classInfo(); info; info = info->parentClass) {
350 if (const HashTable* propHashTable = info->propHashTable(exec)) {
351 if (const HashEntry* entry = propHashTable->entry(exec, propertyName))
352 return entry;
353 }
354 }
355 return 0;
356 }
357
358 bool JSObject::hasInstance(JSObject*, ExecState* exec, JSValue value, JSValue proto)
359 {
360 if (!value.isObject())
361 return false;
362
363 if (!proto.isObject()) {
364 throwError(exec, createTypeError(exec, "instanceof called on an object with an invalid prototype property."));
365 return false;
366 }
367
368 JSObject* object = asObject(value);
369 while ((object = object->prototype().getObject())) {
370 if (proto == object)
371 return true;
372 }
373 return false;
374 }
375
376 bool JSObject::propertyIsEnumerable(ExecState* exec, const Identifier& propertyName) const
377 {
378 PropertyDescriptor descriptor;
379 if (!const_cast<JSObject*>(this)->methodTable()->getOwnPropertyDescriptor(const_cast<JSObject*>(this), exec, propertyName, descriptor))
380 return false;
381 return descriptor.enumerable();
382 }
383
384 bool JSObject::getPropertySpecificValue(ExecState* exec, const Identifier& propertyName, JSCell*& specificValue) const
385 {
386 unsigned attributes;
387 if (structure()->get(exec->globalData(), propertyName, attributes, specificValue) != WTF::notFound)
388 return true;
389
390 // This could be a function within the static table? - should probably
391 // also look in the hash? This currently should not be a problem, since
392 // we've currently always call 'get' first, which should have populated
393 // the normal storage.
394 return false;
395 }
396
397 void JSObject::getPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
398 {
399 object->methodTable()->getOwnPropertyNames(object, exec, propertyNames, mode);
400
401 if (object->prototype().isNull())
402 return;
403
404 JSObject* prototype = asObject(object->prototype());
405 while(1) {
406 if (prototype->structure()->typeInfo().overridesGetPropertyNames()) {
407 prototype->methodTable()->getPropertyNames(prototype, exec, propertyNames, mode);
408 break;
409 }
410 prototype->methodTable()->getOwnPropertyNames(prototype, exec, propertyNames, mode);
411 JSValue nextProto = prototype->prototype();
412 if (nextProto.isNull())
413 break;
414 prototype = asObject(nextProto);
415 }
416 }
417
418 void JSObject::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
419 {
420 object->structure()->getPropertyNamesFromStructure(exec->globalData(), propertyNames, mode);
421 if (!object->staticFunctionsReified())
422 getClassPropertyNames(exec, object->classInfo(), propertyNames, mode);
423 }
424
425 bool JSObject::toBoolean(ExecState*) const
426 {
427 return true;
428 }
429
430 double JSObject::toNumber(ExecState* exec) const
431 {
432 JSValue primitive = toPrimitive(exec, PreferNumber);
433 if (exec->hadException()) // should be picked up soon in Nodes.cpp
434 return 0.0;
435 return primitive.toNumber(exec);
436 }
437
438 JSString* JSObject::toString(ExecState* exec) const
439 {
440 JSValue primitive = toPrimitive(exec, PreferString);
441 if (exec->hadException())
442 return jsEmptyString(exec);
443 return primitive.toString(exec);
444 }
445
446 JSObject* JSObject::toThisObject(JSCell* cell, ExecState*)
447 {
448 return jsCast<JSObject*>(cell);
449 }
450
451 JSObject* JSObject::unwrappedObject()
452 {
453 if (isGlobalThis())
454 return jsCast<JSGlobalThis*>(this)->unwrappedObject();
455 return this;
456 }
457
458 void JSObject::seal(JSGlobalData& globalData)
459 {
460 if (isSealed(globalData))
461 return;
462 preventExtensions(globalData);
463 setStructure(globalData, Structure::sealTransition(globalData, structure()));
464 }
465
466 void JSObject::freeze(JSGlobalData& globalData)
467 {
468 if (isFrozen(globalData))
469 return;
470 preventExtensions(globalData);
471 setStructure(globalData, Structure::freezeTransition(globalData, structure()));
472 }
473
474 void JSObject::preventExtensions(JSGlobalData& globalData)
475 {
476 if (isJSArray(this))
477 asArray(this)->enterDictionaryMode(globalData);
478 if (isExtensible())
479 setStructure(globalData, Structure::preventExtensionsTransition(globalData, structure()));
480 }
481
482 // This presently will flatten to an uncachable dictionary; this is suitable
483 // for use in delete, we may want to do something different elsewhere.
484 void JSObject::reifyStaticFunctionsForDelete(ExecState* exec)
485 {
486 ASSERT(!staticFunctionsReified());
487 JSGlobalData& globalData = exec->globalData();
488
489 // If this object's ClassInfo has no static properties, then nothing to reify!
490 // We can safely set the flag to avoid the expensive check again in the future.
491 if (!classInfo()->hasStaticProperties()) {
492 structure()->setStaticFunctionsReified();
493 return;
494 }
495
496 if (!structure()->isUncacheableDictionary())
497 setStructure(globalData, Structure::toUncacheableDictionaryTransition(globalData, structure()));
498
499 for (const ClassInfo* info = classInfo(); info; info = info->parentClass) {
500 const HashTable* hashTable = info->propHashTable(globalObject()->globalExec());
501 if (!hashTable)
502 continue;
503 PropertySlot slot;
504 for (HashTable::ConstIterator iter = hashTable->begin(globalData); iter != hashTable->end(globalData); ++iter) {
505 if (iter->attributes() & Function)
506 setUpStaticFunctionSlot(globalObject()->globalExec(), *iter, this, Identifier(&globalData, iter->key()), slot);
507 }
508 }
509
510 structure()->setStaticFunctionsReified();
511 }
512
513 void JSObject::removeDirect(JSGlobalData& globalData, const Identifier& propertyName)
514 {
515 if (structure()->get(globalData, propertyName) == WTF::notFound)
516 return;
517
518 size_t offset;
519 if (structure()->isUncacheableDictionary()) {
520 offset = structure()->removePropertyWithoutTransition(globalData, propertyName);
521 if (offset != WTF::notFound)
522 putUndefinedAtDirectOffset(offset);
523 return;
524 }
525
526 setStructure(globalData, Structure::removePropertyTransition(globalData, structure(), propertyName, offset));
527 if (offset != WTF::notFound)
528 putUndefinedAtDirectOffset(offset);
529 }
530
531 NEVER_INLINE void JSObject::fillGetterPropertySlot(PropertySlot& slot, WriteBarrierBase<Unknown>* location)
532 {
533 if (JSObject* getterFunction = asGetterSetter(location->get())->getter()) {
534 if (!structure()->isDictionary())
535 slot.setCacheableGetterSlot(this, getterFunction, offsetForLocation(location));
536 else
537 slot.setGetterSlot(getterFunction);
538 } else
539 slot.setUndefined();
540 }
541
542 Structure* JSObject::createInheritorID(JSGlobalData& globalData)
543 {
544 JSGlobalObject* globalObject;
545 if (isGlobalThis())
546 globalObject = static_cast<JSGlobalThis*>(this)->unwrappedObject();
547 else
548 globalObject = structure()->globalObject();
549 ASSERT(globalObject);
550 m_inheritorID.set(globalData, this, createEmptyObjectStructure(globalData, globalObject, this));
551 ASSERT(m_inheritorID->isEmpty());
552 return m_inheritorID.get();
553 }
554
555 PropertyStorage JSObject::growPropertyStorage(JSGlobalData& globalData, size_t oldSize, size_t newSize)
556 {
557 ASSERT(newSize > oldSize);
558
559 // It's important that this function not rely on structure(), since
560 // we might be in the middle of a transition.
561
562 PropertyStorage oldPropertyStorage = m_propertyStorage.get();
563 PropertyStorage newPropertyStorage = 0;
564
565 if (isUsingInlineStorage()) {
566 // We have this extra temp here to slake GCC's thirst for the blood of those who dereference type-punned pointers.
567 void* temp = newPropertyStorage;
568 if (!globalData.heap.tryAllocateStorage(sizeof(WriteBarrierBase<Unknown>) * newSize, &temp))
569 CRASH();
570 newPropertyStorage = static_cast<PropertyStorage>(temp);
571
572 for (unsigned i = 0; i < oldSize; ++i)
573 newPropertyStorage[i] = oldPropertyStorage[i];
574 } else {
575 // We have this extra temp here to slake GCC's thirst for the blood of those who dereference type-punned pointers.
576 void* temp = oldPropertyStorage;
577 if (!globalData.heap.tryReallocateStorage(&temp, sizeof(WriteBarrierBase<Unknown>) * oldSize, sizeof(WriteBarrierBase<Unknown>) * newSize))
578 CRASH();
579 newPropertyStorage = static_cast<PropertyStorage>(temp);
580 }
581
582 ASSERT(newPropertyStorage);
583 return newPropertyStorage;
584 }
585
586 bool JSObject::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
587 {
588 unsigned attributes = 0;
589 JSCell* cell = 0;
590 size_t offset = object->structure()->get(exec->globalData(), propertyName, attributes, cell);
591 if (offset == WTF::notFound)
592 return false;
593 descriptor.setDescriptor(object->getDirectOffset(offset), attributes);
594 return true;
595 }
596
597 bool JSObject::getPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
598 {
599 JSObject* object = this;
600 while (true) {
601 if (object->methodTable()->getOwnPropertyDescriptor(object, exec, propertyName, descriptor))
602 return true;
603 JSValue prototype = object->prototype();
604 if (!prototype.isObject())
605 return false;
606 object = asObject(prototype);
607 }
608 }
609
610 static bool putDescriptor(ExecState* exec, JSObject* target, const Identifier& propertyName, PropertyDescriptor& descriptor, unsigned attributes, const PropertyDescriptor& oldDescriptor)
611 {
612 if (descriptor.isGenericDescriptor() || descriptor.isDataDescriptor()) {
613 if (descriptor.isGenericDescriptor() && oldDescriptor.isAccessorDescriptor()) {
614 GetterSetter* accessor = GetterSetter::create(exec);
615 if (oldDescriptor.getterPresent())
616 accessor->setGetter(exec->globalData(), oldDescriptor.getterObject());
617 if (oldDescriptor.setterPresent())
618 accessor->setSetter(exec->globalData(), oldDescriptor.setterObject());
619 target->putDirectAccessor(exec->globalData(), propertyName, accessor, attributes | Accessor);
620 return true;
621 }
622 JSValue newValue = jsUndefined();
623 if (descriptor.value())
624 newValue = descriptor.value();
625 else if (oldDescriptor.value())
626 newValue = oldDescriptor.value();
627 target->putDirect(exec->globalData(), propertyName, newValue, attributes & ~Accessor);
628 if (attributes & ReadOnly)
629 target->structure()->setContainsReadOnlyProperties();
630 return true;
631 }
632 attributes &= ~ReadOnly;
633 GetterSetter* accessor = GetterSetter::create(exec);
634
635 if (descriptor.getterPresent())
636 accessor->setGetter(exec->globalData(), descriptor.getterObject());
637 else if (oldDescriptor.getterPresent())
638 accessor->setGetter(exec->globalData(), oldDescriptor.getterObject());
639 if (descriptor.setterPresent())
640 accessor->setSetter(exec->globalData(), descriptor.setterObject());
641 else if (oldDescriptor.setterPresent())
642 accessor->setSetter(exec->globalData(), oldDescriptor.setterObject());
643
644 target->putDirectAccessor(exec->globalData(), propertyName, accessor, attributes | Accessor);
645 return true;
646 }
647
648 class DefineOwnPropertyScope {
649 public:
650 DefineOwnPropertyScope(ExecState* exec)
651 : m_globalData(exec->globalData())
652 {
653 m_globalData.setInDefineOwnProperty(true);
654 }
655
656 ~DefineOwnPropertyScope()
657 {
658 m_globalData.setInDefineOwnProperty(false);
659 }
660
661 private:
662 JSGlobalData& m_globalData;
663 };
664
665 bool JSObject::defineOwnProperty(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor, bool throwException)
666 {
667 // Track on the globaldata that we're in define property.
668 // Currently DefineOwnProperty uses delete to remove properties when they are being replaced
669 // (particularly when changing attributes), however delete won't allow non-configurable (i.e.
670 // DontDelete) properties to be deleted. For now, we can use this flag to make this work.
671 DefineOwnPropertyScope scope(exec);
672
673 // If we have a new property we can just put it on normally
674 PropertyDescriptor current;
675 if (!object->methodTable()->getOwnPropertyDescriptor(object, exec, propertyName, current)) {
676 // unless extensions are prevented!
677 if (!object->isExtensible()) {
678 if (throwException)
679 throwError(exec, createTypeError(exec, "Attempting to define property on object that is not extensible."));
680 return false;
681 }
682 PropertyDescriptor oldDescriptor;
683 oldDescriptor.setValue(jsUndefined());
684 return putDescriptor(exec, object, propertyName, descriptor, descriptor.attributes(), oldDescriptor);
685 }
686
687 if (descriptor.isEmpty())
688 return true;
689
690 if (current.equalTo(exec, descriptor))
691 return true;
692
693 // Filter out invalid changes
694 if (!current.configurable()) {
695 if (descriptor.configurable()) {
696 if (throwException)
697 throwError(exec, createTypeError(exec, "Attempting to configurable attribute of unconfigurable property."));
698 return false;
699 }
700 if (descriptor.enumerablePresent() && descriptor.enumerable() != current.enumerable()) {
701 if (throwException)
702 throwError(exec, createTypeError(exec, "Attempting to change enumerable attribute of unconfigurable property."));
703 return false;
704 }
705 }
706
707 // A generic descriptor is simply changing the attributes of an existing property
708 if (descriptor.isGenericDescriptor()) {
709 if (!current.attributesEqual(descriptor)) {
710 object->methodTable()->deleteProperty(object, exec, propertyName);
711 return putDescriptor(exec, object, propertyName, descriptor, descriptor.attributesOverridingCurrent(current), current);
712 }
713 return true;
714 }
715
716 // Changing between a normal property or an accessor property
717 if (descriptor.isDataDescriptor() != current.isDataDescriptor()) {
718 if (!current.configurable()) {
719 if (throwException)
720 throwError(exec, createTypeError(exec, "Attempting to change access mechanism for an unconfigurable property."));
721 return false;
722 }
723 object->methodTable()->deleteProperty(object, exec, propertyName);
724 return putDescriptor(exec, object, propertyName, descriptor, descriptor.attributesOverridingCurrent(current), current);
725 }
726
727 // Changing the value and attributes of an existing property
728 if (descriptor.isDataDescriptor()) {
729 if (!current.configurable()) {
730 if (!current.writable() && descriptor.writable()) {
731 if (throwException)
732 throwError(exec, createTypeError(exec, "Attempting to change writable attribute of unconfigurable property."));
733 return false;
734 }
735 if (!current.writable()) {
736 if (descriptor.value() && !sameValue(exec, current.value(), descriptor.value())) {
737 if (throwException)
738 throwError(exec, createTypeError(exec, "Attempting to change value of a readonly property."));
739 return false;
740 }
741 }
742 }
743 if (current.attributesEqual(descriptor) && !descriptor.value())
744 return true;
745 object->methodTable()->deleteProperty(object, exec, propertyName);
746 return putDescriptor(exec, object, propertyName, descriptor, descriptor.attributesOverridingCurrent(current), current);
747 }
748
749 // Changing the accessor functions of an existing accessor property
750 ASSERT(descriptor.isAccessorDescriptor());
751 if (!current.configurable()) {
752 if (descriptor.setterPresent() && !(current.setterPresent() && JSValue::strictEqual(exec, current.setter(), descriptor.setter()))) {
753 if (throwException)
754 throwError(exec, createTypeError(exec, "Attempting to change the setter of an unconfigurable property."));
755 return false;
756 }
757 if (descriptor.getterPresent() && !(current.getterPresent() && JSValue::strictEqual(exec, current.getter(), descriptor.getter()))) {
758 if (throwException)
759 throwError(exec, createTypeError(exec, "Attempting to change the getter of an unconfigurable property."));
760 return false;
761 }
762 }
763 JSValue accessor = object->getDirect(exec->globalData(), propertyName);
764 if (!accessor)
765 return false;
766 GetterSetter* getterSetter = asGetterSetter(accessor);
767 if (descriptor.setterPresent())
768 getterSetter->setSetter(exec->globalData(), descriptor.setterObject());
769 if (descriptor.getterPresent())
770 getterSetter->setGetter(exec->globalData(), descriptor.getterObject());
771 if (current.attributesEqual(descriptor))
772 return true;
773 object->methodTable()->deleteProperty(object, exec, propertyName);
774 unsigned attrs = descriptor.attributesOverridingCurrent(current);
775 object->putDirectAccessor(exec->globalData(), propertyName, getterSetter, attrs | Accessor);
776 return true;
777 }
778
779 JSObject* throwTypeError(ExecState* exec, const UString& message)
780 {
781 return throwError(exec, createTypeError(exec, message));
782 }
783
784 } // namespace JSC