JSObject.h 17.9 KB
Newer Older
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, 2007, 2008 Apple Inc. All rights reserved.
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
mjs's avatar
mjs committed
18
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
 *  Boston, MA 02110-1301, USA.
20
 *
21 22
 */

weinig@apple.com's avatar
weinig@apple.com committed
23 24
#ifndef JSObject_h
#define JSObject_h
25

weinig@apple.com's avatar
weinig@apple.com committed
26
#include "ArgList.h"
weinig@apple.com's avatar
weinig@apple.com committed
27
#include "ClassInfo.h"
mjs's avatar
mjs committed
28
#include "CommonIdentifiers.h"
ggaren@apple.com's avatar
ggaren@apple.com committed
29
#include "ExecState.h"
weinig@apple.com's avatar
weinig@apple.com committed
30
#include "JSNumberCell.h"
darin@apple.com's avatar
darin@apple.com committed
31 32
#include "PropertyMap.h"
#include "PropertySlot.h"
ggaren@apple.com's avatar
ggaren@apple.com committed
33
#include "PutPropertySlot.h"
darin@apple.com's avatar
darin@apple.com committed
34
#include "ScopeChain.h"
weinig@apple.com's avatar
weinig@apple.com committed
35
#include "StructureID.h"
36

37
namespace JSC {
38

weinig@apple.com's avatar
weinig@apple.com committed
39 40
    class InternalFunction;
    class PropertyNameArray;
weinig@apple.com's avatar
weinig@apple.com committed
41
    class StructureID;
weinig@apple.com's avatar
weinig@apple.com committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55
    struct HashEntry;
    struct HashTable;

    // ECMA 262-3 8.6.1
    // Property attributes
    enum Attribute {
        None         = 0,
        ReadOnly     = 1 << 1,  // property can be only read, not written
        DontEnum     = 1 << 2,  // property doesn't appear in (for .. in ..)
        DontDelete   = 1 << 3,  // property can't be deleted
        Function     = 1 << 4,  // property is a function - only used by static hashtables
    };

    class JSObject : public JSCell {
ggaren@apple.com's avatar
ggaren@apple.com committed
56
        friend class BatchedTransitionOptimizer;
57
        friend class CTI;
ggaren@apple.com's avatar
ggaren@apple.com committed
58

weinig@apple.com's avatar
weinig@apple.com committed
59
    public:
darin@apple.com's avatar
darin@apple.com committed
60
        explicit JSObject(PassRefPtr<StructureID>);
weinig@apple.com's avatar
weinig@apple.com committed
61 62 63

        virtual void mark();

mrowe@apple.com's avatar
mrowe@apple.com committed
64 65 66 67
        // The inline virtual destructor cannot be the first virtual function declared
        // in the class as it results in the vtable being generated as a weak symbol
        virtual ~JSObject();

darin@apple.com's avatar
darin@apple.com committed
68
        bool inherits(const ClassInfo* classInfo) const { return JSCell::isObject(classInfo); }
weinig@apple.com's avatar
weinig@apple.com committed
69 70 71

        JSValue* prototype() const;
        void setPrototype(JSValue* prototype);
weinig@apple.com's avatar
weinig@apple.com committed
72
        
ggaren@apple.com's avatar
ggaren@apple.com committed
73
        void setStructureID(PassRefPtr<StructureID>);
ggaren@apple.com's avatar
ggaren@apple.com committed
74
        StructureID* inheritorID();
weinig@apple.com's avatar
weinig@apple.com committed
75

weinig@apple.com's avatar
weinig@apple.com committed
76 77
        PropertyStorage& propertyStorage() { return m_propertyStorage; }

weinig@apple.com's avatar
weinig@apple.com committed
78 79 80 81 82 83 84 85 86 87 88
        virtual UString className() const;

        JSValue* get(ExecState*, const Identifier& propertyName) const;
        JSValue* get(ExecState*, unsigned propertyName) const;

        bool getPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
        bool getPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);

        virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
        virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);

weinig@apple.com's avatar
weinig@apple.com committed
89
        virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, PutPropertySlot&);
weinig@apple.com's avatar
weinig@apple.com committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103
        virtual void put(ExecState*, unsigned propertyName, JSValue* value);

        virtual void putWithAttributes(ExecState*, const Identifier& propertyName, JSValue* value, unsigned attributes);
        virtual void putWithAttributes(ExecState*, unsigned propertyName, JSValue* value, unsigned attributes);

        bool propertyIsEnumerable(ExecState*, const Identifier& propertyName) const;

        bool hasProperty(ExecState*, const Identifier& propertyName) const;
        bool hasProperty(ExecState*, unsigned propertyName) const;
        bool hasOwnProperty(ExecState*, const Identifier& propertyName) const;

        virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
        virtual bool deleteProperty(ExecState*, unsigned propertyName);

darin@apple.com's avatar
darin@apple.com committed
104
        virtual JSValue* defaultValue(ExecState*, PreferredPrimitiveType) const;
weinig@apple.com's avatar
weinig@apple.com committed
105

106
        virtual bool hasInstance(ExecState*, JSValue*, JSValue* prototypeProperty);
weinig@apple.com's avatar
weinig@apple.com committed
107 108 109

        virtual void getPropertyNames(ExecState*, PropertyNameArray&);

darin@apple.com's avatar
darin@apple.com committed
110
        virtual JSValue* toPrimitive(ExecState*, PreferredPrimitiveType = NoPreference) const;
weinig@apple.com's avatar
weinig@apple.com committed
111
        virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue*& value);
mjs@apple.com's avatar
mjs@apple.com committed
112
        virtual bool toBoolean(ExecState*) const;
weinig@apple.com's avatar
weinig@apple.com committed
113 114 115 116 117 118 119 120 121 122
        virtual double toNumber(ExecState*) const;
        virtual UString toString(ExecState*) const;
        virtual JSObject* toObject(ExecState*) const;

        virtual JSObject* toThisObject(ExecState*) const;
        virtual JSGlobalObject* toGlobalObject(ExecState*) const;

        virtual bool getPropertyAttributes(ExecState*, const Identifier& propertyName, unsigned& attributes) const;

        // This get function only looks at the property map.
weinig@apple.com's avatar
weinig@apple.com committed
123 124
        JSValue* getDirect(const Identifier& propertyName) const
        {
125 126
            size_t offset = m_structureID->propertyMap().getOffset(propertyName);
            return offset != WTF::notFound ? m_propertyStorage[offset] : 0;
weinig@apple.com's avatar
weinig@apple.com committed
127 128 129 130
        }

        JSValue** getDirectLocation(const Identifier& propertyName)
        {
131 132
            size_t offset = m_structureID->propertyMap().getOffset(propertyName);
            return offset != WTF::notFound ? locationForOffset(offset) : 0;
weinig@apple.com's avatar
weinig@apple.com committed
133 134
        }

135
        JSValue** getDirectLocation(const Identifier& propertyName, unsigned& attributes)
weinig@apple.com's avatar
weinig@apple.com committed
136
        {
137
            size_t offset = m_structureID->propertyMap().getOffset(propertyName, attributes);
138
            return offset != WTF::notFound ? locationForOffset(offset) : 0;
weinig@apple.com's avatar
weinig@apple.com committed
139 140 141 142
        }

        size_t offsetForLocation(JSValue** location)
        {
143
            return location - m_propertyStorage;
weinig@apple.com's avatar
weinig@apple.com committed
144 145
        }

146 147 148 149 150
        JSValue** locationForOffset(size_t offset)
        {
            return &m_propertyStorage[offset];
        }

151 152
        void transitionTo(StructureID*);

weinig@apple.com's avatar
weinig@apple.com committed
153
        void removeDirect(const Identifier& propertyName);
weinig@apple.com's avatar
weinig@apple.com committed
154 155
        bool hasCustomProperties() { return !m_structureID->propertyMap().isEmpty(); }
        bool hasGetterSetterProperties() { return m_structureID->propertyMap().hasGetterSetterProperties(); }
weinig@apple.com's avatar
weinig@apple.com committed
156 157 158 159

        void putDirect(const Identifier& propertyName, JSValue* value, unsigned attr = 0);
        void putDirect(const Identifier& propertyName, JSValue* value, unsigned attr, bool checkReadOnly, PutPropertySlot& slot);
        void putDirectFunction(ExecState* exec, InternalFunction* function, unsigned attr = 0);
weinig@apple.com's avatar
weinig@apple.com committed
160

weinig@apple.com's avatar
weinig@apple.com committed
161
        // Fast access to known property offsets.
weinig@apple.com's avatar
weinig@apple.com committed
162 163
        JSValue* getDirectOffset(size_t offset) { return m_propertyStorage[offset]; }
        void putDirectOffset(size_t offset, JSValue* value) { m_propertyStorage[offset] = value; }
weinig@apple.com's avatar
weinig@apple.com committed
164 165 166 167 168 169 170 171 172 173 174

        void fillGetterPropertySlot(PropertySlot&, JSValue** location);

        virtual void defineGetter(ExecState*, const Identifier& propertyName, JSObject* getterFunction);
        virtual void defineSetter(ExecState*, const Identifier& propertyName, JSObject* setterFunction);
        virtual JSValue* lookupGetter(ExecState*, const Identifier& propertyName);
        virtual JSValue* lookupSetter(ExecState*, const Identifier& propertyName);

        virtual bool isGlobalObject() const { return false; }
        virtual bool isVariableObject() const { return false; }
        virtual bool isWatchdogException() const { return false; }
175
        virtual bool isNotAnObjectErrorStub() const { return false; }
weinig@apple.com's avatar
weinig@apple.com committed
176

177 178 179
        void allocatePropertyStorage(size_t oldSize, size_t newSize);
        bool usingInlineStorage() const { return m_propertyStorage == m_inlineStorage; }

180 181
        static const size_t inlineStorageCapacity = 2;

mjs@apple.com's avatar
mjs@apple.com committed
182 183
        static PassRefPtr<StructureID> createStructureID(JSValue* proto) { return StructureID::create(proto, TypeInfo(ObjectType)); }

weinig@apple.com's avatar
weinig@apple.com committed
184 185 186 187 188
    protected:
        bool getOwnPropertySlotForWrite(ExecState*, const Identifier&, PropertySlot&, bool& slotIsWriteable);

    private:
        const HashEntry* findPropertyHashEntry(ExecState*, const Identifier& propertyName) const;
ggaren@apple.com's avatar
ggaren@apple.com committed
189
        StructureID* createInheritorID();
weinig@apple.com's avatar
weinig@apple.com committed
190 191

        RefPtr<StructureID> m_inheritorID;
192 193 194

        PropertyStorage m_propertyStorage;        
        JSValue* m_inlineStorage[inlineStorageCapacity];
weinig@apple.com's avatar
weinig@apple.com committed
195
    };
196

weinig@apple.com's avatar
weinig@apple.com committed
197
  JSObject* constructEmptyObject(ExecState*);
darin's avatar
darin committed
198

weinig@apple.com's avatar
weinig@apple.com committed
199 200
inline JSObject::JSObject(PassRefPtr<StructureID> structureID)
    : JSCell(structureID.releaseRef()) // ~JSObject balances this ref()
201
    , m_propertyStorage(m_inlineStorage)
weinig@apple.com's avatar
weinig@apple.com committed
202 203
{
    ASSERT(m_structureID);
darin@apple.com's avatar
darin@apple.com committed
204
    ASSERT(prototype()->isNull() || Heap::heap(this) == Heap::heap(prototype()));
darin's avatar
darin committed
205 206
}

weinig@apple.com's avatar
weinig@apple.com committed
207
inline JSObject::~JSObject()
darin's avatar
darin committed
208
{
weinig@apple.com's avatar
weinig@apple.com committed
209
    ASSERT(m_structureID);
210 211
    if (m_propertyStorage != m_inlineStorage)
        delete [] m_propertyStorage;
weinig@apple.com's avatar
weinig@apple.com committed
212
    m_structureID->deref();
darin's avatar
darin committed
213 214
}

weinig@apple.com's avatar
weinig@apple.com committed
215
inline JSValue* JSObject::prototype() const
darin's avatar
darin committed
216
{
217
    return m_structureID->storedPrototype();
darin's avatar
darin committed
218 219
}

weinig@apple.com's avatar
weinig@apple.com committed
220
inline void JSObject::setPrototype(JSValue* prototype)
darin's avatar
darin committed
221
{
weinig@apple.com's avatar
weinig@apple.com committed
222
    ASSERT(prototype);
weinig@apple.com's avatar
weinig@apple.com committed
223 224 225 226 227 228 229 230 231 232
    RefPtr<StructureID> newStructureID = StructureID::changePrototypeTransition(m_structureID, prototype);
    setStructureID(newStructureID.release());
}

inline void JSObject::setStructureID(PassRefPtr<StructureID> structureID)
{
    m_structureID->deref();
    m_structureID = structureID.releaseRef(); // ~JSObject balances this ref()
}

ggaren@apple.com's avatar
ggaren@apple.com committed
233
inline StructureID* JSObject::inheritorID()
weinig@apple.com's avatar
weinig@apple.com committed
234 235 236 237
{
    if (m_inheritorID)
        return m_inheritorID.get();
    return createInheritorID();
darin's avatar
darin committed
238 239
}

darin@apple.com's avatar
darin@apple.com committed
240
inline bool JSCell::isObject(const ClassInfo* info) const
darin's avatar
darin committed
241
{
darin@apple.com's avatar
darin@apple.com committed
242
    for (const ClassInfo* ci = classInfo(); ci; ci = ci->parentClass) {
darin's avatar
darin committed
243 244
        if (ci == info)
            return true;
darin@apple.com's avatar
darin@apple.com committed
245
    }
darin's avatar
darin committed
246 247
    return false;
}
248

mjs's avatar
mjs committed
249
// this method is here to be after the inline declaration of JSCell::isObject
darin@apple.com's avatar
darin@apple.com committed
250
inline bool JSValue::isObject(const ClassInfo* classInfo) const
mjs's avatar
mjs committed
251
{
darin@apple.com's avatar
darin@apple.com committed
252
    return !JSImmediate::isImmediate(this) && asCell()->isObject(classInfo);
mjs's avatar
mjs committed
253 254
}

weinig@apple.com's avatar
weinig@apple.com committed
255
inline JSValue* JSObject::get(ExecState* exec, const Identifier& propertyName) const
256
{
weinig@apple.com's avatar
weinig@apple.com committed
257 258 259
    PropertySlot slot(const_cast<JSObject*>(this));
    if (const_cast<JSObject*>(this)->getPropertySlot(exec, propertyName, slot))
        return slot.getValue(exec, propertyName);
260
    
weinig@apple.com's avatar
weinig@apple.com committed
261
    return jsUndefined();
262 263
}

weinig@apple.com's avatar
weinig@apple.com committed
264
inline JSValue* JSObject::get(ExecState* exec, unsigned propertyName) const
265
{
weinig@apple.com's avatar
weinig@apple.com committed
266 267 268 269 270
    PropertySlot slot(const_cast<JSObject*>(this));
    if (const_cast<JSObject*>(this)->getPropertySlot(exec, propertyName, slot))
        return slot.getValue(exec, propertyName);

    return jsUndefined();
271 272
}

darin's avatar
darin committed
273 274
// It may seem crazy to inline a function this large but it makes a big difference
// since this is function very hot in variable lookup
weinig@apple.com's avatar
weinig@apple.com committed
275
inline bool JSObject::getPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
darin's avatar
darin committed
276
{
weinig@apple.com's avatar
weinig@apple.com committed
277
    JSObject* object = this;
darin's avatar
darin committed
278 279 280 281
    while (true) {
        if (object->getOwnPropertySlot(exec, propertyName, slot))
            return true;

weinig@apple.com's avatar
weinig@apple.com committed
282
        JSValue* prototype = object->prototype();
weinig@apple.com's avatar
weinig@apple.com committed
283
        if (!prototype->isObject())
darin's avatar
darin committed
284 285
            return false;

weinig@apple.com's avatar
weinig@apple.com committed
286
        object = static_cast<JSObject*>(prototype);
darin's avatar
darin committed
287 288 289
    }
}

weinig@apple.com's avatar
weinig@apple.com committed
290
inline bool JSObject::getPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
291
{
weinig@apple.com's avatar
weinig@apple.com committed
292 293 294 295 296 297
    JSObject* object = this;

    while (true) {
        if (object->getOwnPropertySlot(exec, propertyName, slot))
            return true;

weinig@apple.com's avatar
weinig@apple.com committed
298
        JSValue* prototype = object->prototype();
weinig@apple.com's avatar
weinig@apple.com committed
299 300 301 302 303 304 305
        if (!prototype->isObject())
            break;

        object = static_cast<JSObject*>(prototype);
    }

    return false;
306 307 308 309 310 311 312
}

// It may seem crazy to inline a function this large, especially a virtual function,
// but it makes a big difference to property lookup that derived classes can inline their
// base class call to this.
ALWAYS_INLINE bool JSObject::getOwnPropertySlotForWrite(ExecState* exec, const Identifier& propertyName, PropertySlot& slot, bool& slotIsWriteable)
{
313 314
    unsigned attributes;
    if (JSValue** location = getDirectLocation(propertyName, attributes)) {
weinig@apple.com's avatar
weinig@apple.com committed
315
        if (m_structureID->propertyMap().hasGetterSetterProperties() && location[0]->isGetterSetter()) {
316 317
            slotIsWriteable = false;
            fillGetterPropertySlot(slot, location);
318 319
        } else {
            slotIsWriteable = !(attributes & ReadOnly);
weinig@apple.com's avatar
weinig@apple.com committed
320
            slot.setValueSlot(this, location, offsetForLocation(location));
321
        }
322 323 324 325 326
        return true;
    }

    // non-standard Netscape extension
    if (propertyName == exec->propertyNames().underscoreProto) {
weinig@apple.com's avatar
weinig@apple.com committed
327
        slot.setValue(prototype());
andersca@apple.com's avatar
andersca@apple.com committed
328
        slotIsWriteable = false;
329 330 331 332 333 334
        return true;
    }

    return false;
}

darin's avatar
darin committed
335 336 337
// It may seem crazy to inline a function this large, especially a virtual function,
// but it makes a big difference to property lookup that derived classes can inline their
// base class call to this.
mjs's avatar
mjs committed
338
ALWAYS_INLINE bool JSObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
darin's avatar
darin committed
339
{
weinig@apple.com's avatar
weinig@apple.com committed
340
    if (JSValue** location = getDirectLocation(propertyName)) {
weinig@apple.com's avatar
weinig@apple.com committed
341
        if (m_structureID->propertyMap().hasGetterSetterProperties() && location[0]->isGetterSetter())
mjs's avatar
mjs committed
342 343
            fillGetterPropertySlot(slot, location);
        else
weinig@apple.com's avatar
weinig@apple.com committed
344
            slot.setValueSlot(this, location, offsetForLocation(location));
darin's avatar
darin committed
345 346 347 348
        return true;
    }

    // non-standard Netscape extension
mjs's avatar
mjs committed
349
    if (propertyName == exec->propertyNames().underscoreProto) {
weinig@apple.com's avatar
weinig@apple.com committed
350
        slot.setValue(prototype());
darin's avatar
darin committed
351 352 353 354 355 356
        return true;
    }

    return false;
}

weinig@apple.com's avatar
weinig@apple.com committed
357
inline void JSObject::putDirect(const Identifier& propertyName, JSValue* value, unsigned attr)
358
{
weinig@apple.com's avatar
weinig@apple.com committed
359 360
    PutPropertySlot slot;
    putDirect(propertyName, value, attr, false, slot);
361 362
}

weinig@apple.com's avatar
weinig@apple.com committed
363
inline void JSObject::putDirect(const Identifier& propertyName, JSValue* value, unsigned attributes, bool checkReadOnly, PutPropertySlot& slot)
364
{
weinig@apple.com's avatar
weinig@apple.com committed
365
    ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
weinig@apple.com's avatar
weinig@apple.com committed
366 367

     if (m_structureID->isDictionary()) {
368 369 370 371 372 373 374 375 376 377 378 379
         unsigned currentAttributes;
         size_t offset = m_structureID->propertyMap().getOffset(propertyName, currentAttributes);
         if (offset != WTF::notFound) {
             if (checkReadOnly && currentAttributes & ReadOnly)
                 return;
             m_propertyStorage[offset] = value;
             slot.setExistingProperty(this, offset);
             return;
         }

         if (m_structureID->propertyMap().storageSize() == inlineStorageCapacity)
             allocatePropertyStorage(m_structureID->propertyMap().storageSize(), m_structureID->propertyMap().size());
weinig@apple.com's avatar
weinig@apple.com committed
380
         m_structureID->propertyMap().put(propertyName, value, attributes, checkReadOnly, this, slot, m_propertyStorage);
381
         m_structureID->clearEnumerationCache();
weinig@apple.com's avatar
weinig@apple.com committed
382 383
         return;
     }
384

385 386 387 388 389 390 391 392 393
    unsigned currentAttributes;
    size_t offset = m_structureID->propertyMap().getOffset(propertyName, currentAttributes);
    if (offset != WTF::notFound) {
        if (checkReadOnly && currentAttributes & ReadOnly)
            return;
        m_propertyStorage[offset] = value;
        slot.setExistingProperty(this, offset);
        return;
    }
394

395 396 397 398
     if (m_structureID->propertyMap().storageSize() == inlineStorageCapacity)
         allocatePropertyStorage(m_structureID->propertyMap().storageSize(), m_structureID->propertyMap().size());

     RefPtr<StructureID> structureID = StructureID::addPropertyTransition(m_structureID, propertyName, value, attributes, this, slot, m_propertyStorage);
399
     slot.setWasTransition(true);
weinig@apple.com's avatar
weinig@apple.com committed
400
     setStructureID(structureID.release());
401 402
}

403 404 405 406 407 408
inline void JSObject::transitionTo(StructureID* newStructureID)
{
    StructureID::transitionTo(m_structureID, newStructureID, this);
    setStructureID(newStructureID);
}

darin@apple.com's avatar
darin@apple.com committed
409
inline JSValue* JSObject::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
ggaren's avatar
ggaren committed
410 411 412 413
{
    return defaultValue(exec, preferredType);
}

darin@apple.com's avatar
darin@apple.com committed
414
inline JSValue* JSValue::get(ExecState* exec, const Identifier& propertyName) const
weinig@apple.com's avatar
weinig@apple.com committed
415 416 417 418 419 420
{
    PropertySlot slot(const_cast<JSValue*>(this));
    return get(exec, propertyName, slot);
}

inline JSValue* JSValue::get(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) const
darin@apple.com's avatar
darin@apple.com committed
421 422
{
    if (UNLIKELY(JSImmediate::isImmediate(this))) {
423 424
        JSObject* prototype = JSImmediate::prototype(this, exec);
        if (!prototype->getPropertySlot(exec, propertyName, slot))
darin@apple.com's avatar
darin@apple.com committed
425 426 427 428 429 430 431 432
            return jsUndefined();
        return slot.getValue(exec, propertyName);
    }
    JSCell* cell = static_cast<JSCell*>(const_cast<JSValue*>(this));
    while (true) {
        if (cell->getOwnPropertySlot(exec, propertyName, slot))
            return slot.getValue(exec, propertyName);
        ASSERT(cell->isObject());
weinig@apple.com's avatar
weinig@apple.com committed
433 434
        JSValue* prototype = static_cast<JSObject*>(cell)->prototype();
        if (!prototype->isObject())
darin@apple.com's avatar
darin@apple.com committed
435
            return jsUndefined();
weinig@apple.com's avatar
weinig@apple.com committed
436
        cell = static_cast<JSCell*>(prototype);
darin@apple.com's avatar
darin@apple.com committed
437 438 439 440
    }
}

inline JSValue* JSValue::get(ExecState* exec, unsigned propertyName) const
weinig@apple.com's avatar
weinig@apple.com committed
441 442 443 444 445 446
{
    PropertySlot slot(const_cast<JSValue*>(this));
    return get(exec, propertyName, slot);
}

inline JSValue* JSValue::get(ExecState* exec, unsigned propertyName, PropertySlot& slot) const
darin@apple.com's avatar
darin@apple.com committed
447 448
{
    if (UNLIKELY(JSImmediate::isImmediate(this))) {
449 450
        JSObject* prototype = JSImmediate::prototype(this, exec);
        if (!prototype->getPropertySlot(exec, propertyName, slot))
darin@apple.com's avatar
darin@apple.com committed
451 452 453 454 455 456 457 458
            return jsUndefined();
        return slot.getValue(exec, propertyName);
    }
    JSCell* cell = const_cast<JSCell*>(asCell());
    while (true) {
        if (cell->getOwnPropertySlot(exec, propertyName, slot))
            return slot.getValue(exec, propertyName);
        ASSERT(cell->isObject());
weinig@apple.com's avatar
weinig@apple.com committed
459 460
        JSValue* prototype = static_cast<JSObject*>(cell)->prototype();
        if (!prototype->isObject())
darin@apple.com's avatar
darin@apple.com committed
461
            return jsUndefined();
weinig@apple.com's avatar
weinig@apple.com committed
462
        cell = static_cast<JSCell*>(prototype);
darin@apple.com's avatar
darin@apple.com committed
463 464 465
    }
}

weinig@apple.com's avatar
weinig@apple.com committed
466
inline void JSValue::put(ExecState* exec, const Identifier& propertyName, JSValue* value, PutPropertySlot& slot)
darin@apple.com's avatar
darin@apple.com committed
467 468
{
    if (UNLIKELY(JSImmediate::isImmediate(this))) {
weinig@apple.com's avatar
weinig@apple.com committed
469
        JSImmediate::toObject(this, exec)->put(exec, propertyName, value, slot);
darin@apple.com's avatar
darin@apple.com committed
470 471
        return;
    }
weinig@apple.com's avatar
weinig@apple.com committed
472
    asCell()->put(exec, propertyName, value, slot);
darin@apple.com's avatar
darin@apple.com committed
473 474 475 476 477 478 479 480 481 482 483
}

inline void JSValue::put(ExecState* exec, unsigned propertyName, JSValue* value)
{
    if (UNLIKELY(JSImmediate::isImmediate(this))) {
        JSImmediate::toObject(this, exec)->put(exec, propertyName, value);
        return;
    }
    asCell()->put(exec, propertyName, value);
}

484
} // namespace JSC
485

weinig@apple.com's avatar
weinig@apple.com committed
486
#endif // JSObject_h