JSCell.h 13.5 KB
Newer Older
weinig@apple.com's avatar
weinig@apple.com committed
1 2 3
/*
 *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
 *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
4
 *  Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009 Apple Inc. All rights reserved.
weinig@apple.com's avatar
weinig@apple.com committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *  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
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

#ifndef JSCell_h
#define JSCell_h

26 27
#include "CallData.h"
#include "ConstructData.h"
28
#include "Heap.h"
29
#include "JSImmediate.h"
30
#include "JSLock.h"
31 32 33 34
#include "JSValue.h"
#include "MarkStack.h"
#include "Structure.h"
#include <wtf/Noncopyable.h>
weinig@apple.com's avatar
weinig@apple.com committed
35

36
namespace JSC {
weinig@apple.com's avatar
weinig@apple.com committed
37

38 39
    class JSGlobalObject;

andersca@apple.com's avatar
andersca@apple.com committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53
#if COMPILER(MSVC)
    // If WTF_MAKE_NONCOPYABLE is applied to JSCell we end up with a bunch of
    // undefined references to the JSCell copy constructor and assignment operator
    // when linking JavaScriptCore.
    class MSVCBugWorkaround {
        WTF_MAKE_NONCOPYABLE(MSVCBugWorkaround);

    protected:
        MSVCBugWorkaround() { }
        ~MSVCBugWorkaround() { }
    };

    class JSCell : MSVCBugWorkaround {
#else
54 55
    class JSCell {
        WTF_MAKE_NONCOPYABLE(JSCell);
andersca@apple.com's avatar
andersca@apple.com committed
56
#endif
57

58
        friend class ExecutableBase;
59
        friend class GetterSetter;
60
        friend class Heap;
61 62 63
        friend class JSObject;
        friend class JSPropertyNameIterator;
        friend class JSString;
ggaren@apple.com's avatar
ggaren@apple.com committed
64
        friend class JSValue;
65
        friend class JSAPIValueWrapper;
oliver@apple.com's avatar
oliver@apple.com committed
66
        friend class JSZombie;
ap@apple.com's avatar
ap@apple.com committed
67
        friend class JSGlobalData;
68
        friend class MarkedSpace;
69
        friend class MarkedBlock;
70
        friend class ScopeChainNode;
71
        friend class StructureChain;
72

73
    private:
darin@apple.com's avatar
darin@apple.com committed
74
        explicit JSCell(Structure*);
75 76 77
        virtual ~JSCell();

    public:
78
        static PassRefPtr<Structure> createDummyStructure(JSGlobalData& globalData)
79
        {
80
            return Structure::create(globalData, jsNull(), TypeInfo(UnspecifiedType), AnonymousSlotCount, 0);
81 82
        }

83
        // Querying the type.
84 85
        bool isString() const;
        bool isObject() const;
darin@apple.com's avatar
darin@apple.com committed
86
        virtual bool isGetterSetter() const;
87
        bool inherits(const ClassInfo*) const;
88
        virtual bool isAPIValueWrapper() const { return false; }
89
        virtual bool isPropertyNameIterator() const { return false; }
90

darin@apple.com's avatar
darin@apple.com committed
91
        Structure* structure() const;
weinig@apple.com's avatar
weinig@apple.com committed
92

93
        // Extracting the value.
94 95
        bool getString(ExecState* exec, UString&) const;
        UString getString(ExecState* exec) const; // null string if not a string
96 97 98 99 100 101 102
        JSObject* getObject(); // NULL if not an object
        const JSObject* getObject() const; // NULL if not an object
        
        virtual CallType getCallData(CallData&);
        virtual ConstructType getConstructData(ConstructData&);

        // Extracting integer values.
ggaren@apple.com's avatar
ggaren@apple.com committed
103
        // FIXME: remove these methods, can check isNumberCell in JSValue && then call asNumberCell::*.
104 105 106
        virtual bool getUInt32(uint32_t&) const;

        // Basic conversions.
107 108 109 110 111
        virtual JSValue toPrimitive(ExecState*, PreferredPrimitiveType) const;
        virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue&);
        virtual bool toBoolean(ExecState*) const;
        virtual double toNumber(ExecState*) const;
        virtual UString toString(ExecState*) const;
112
        virtual JSObject* toObject(ExecState*, JSGlobalObject*) const;
113 114 115

        // Garbage collection.
        void* operator new(size_t, ExecState*);
darin@apple.com's avatar
darin@apple.com committed
116
        void* operator new(size_t, JSGlobalData*);
117
        void* operator new(size_t, void* placementNewDestination) { return placementNewDestination; }
118 119

        virtual void markChildren(MarkStack&);
oliver@apple.com's avatar
oliver@apple.com committed
120 121 122
#if ENABLE(JSC_ZOMBIES)
        virtual bool isZombie() const { return false; }
#endif
123 124

        // Object operations, with the toObject operation included.
125
        const ClassInfo* classInfo() const { return m_structure->classInfo(); }
ggaren@apple.com's avatar
ggaren@apple.com committed
126 127
        virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
        virtual void put(ExecState*, unsigned propertyName, JSValue);
128 129 130 131
        virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
        virtual bool deleteProperty(ExecState*, unsigned propertyName);

        virtual JSObject* toThisObject(ExecState*) const;
ggaren@apple.com's avatar
ggaren@apple.com committed
132
        virtual JSValue getJSNumber();
133
        void* vptr() { return *reinterpret_cast<void**>(this); }
ap@apple.com's avatar
ap@apple.com committed
134
        void setVPtr(void* vptr) { *reinterpret_cast<void**>(this) = vptr; }
135

136 137 138 139 140 141
        // FIXME: Rename getOwnPropertySlot to virtualGetOwnPropertySlot, and
        // fastGetOwnPropertySlot to getOwnPropertySlot. Callers should always
        // call this function, not its slower virtual counterpart. (For integer
        // property names, we want a similar interface with appropriate optimizations.)
        bool fastGetOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);

142 143 144 145 146 147 148 149 150 151
        static ptrdiff_t structureOffset()
        {
            return OBJECT_OFFSETOF(JSCell, m_structure);
        }

        Structure* const * addressOfStructure() const
        {
            return &m_structure;
        }

152 153 154
    protected:
        static const unsigned AnonymousSlotCount = 0;

155
    private:
darin@apple.com's avatar
darin@apple.com committed
156
        // Base implementation; for non-object classes implements getPropertySlot.
157 158
        virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
        virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
ggaren@apple.com's avatar
ggaren@apple.com committed
159
        
darin@apple.com's avatar
darin@apple.com committed
160
        Structure* m_structure;
161 162
    };

darin@apple.com's avatar
darin@apple.com committed
163 164
    inline JSCell::JSCell(Structure* structure)
        : m_structure(structure)
165 166 167 168 169 170 171
    {
    }

    inline JSCell::~JSCell()
    {
    }

172 173
    inline bool JSCell::isObject() const
    {
darin@apple.com's avatar
darin@apple.com committed
174
        return m_structure->typeInfo().type() == ObjectType;
175 176 177 178
    }

    inline bool JSCell::isString() const
    {
darin@apple.com's avatar
darin@apple.com committed
179
        return m_structure->typeInfo().type() == StringType;
180 181
    }

darin@apple.com's avatar
darin@apple.com committed
182
    inline Structure* JSCell::structure() const
weinig@apple.com's avatar
weinig@apple.com committed
183
    {
darin@apple.com's avatar
darin@apple.com committed
184
        return m_structure;
weinig@apple.com's avatar
weinig@apple.com committed
185 186
    }

187
    inline void JSCell::markChildren(MarkStack&)
188 189 190 191 192
    {
    }

    // --- JSValue inlines ----------------------------

ggaren@apple.com's avatar
ggaren@apple.com committed
193
    inline bool JSValue::isString() const
194
    {
195
        return isCell() && asCell()->isString();
196 197
    }

ggaren@apple.com's avatar
ggaren@apple.com committed
198
    inline bool JSValue::isGetterSetter() const
darin@apple.com's avatar
darin@apple.com committed
199
    {
200
        return isCell() && asCell()->isGetterSetter();
darin@apple.com's avatar
darin@apple.com committed
201 202
    }

ggaren@apple.com's avatar
ggaren@apple.com committed
203
    inline bool JSValue::isObject() const
204
    {
205
        return isCell() && asCell()->isObject();
206 207
    }

208
    inline bool JSValue::getString(ExecState* exec, UString& s) const
209
    {
210
        return isCell() && asCell()->getString(exec, s);
211 212
    }

213
    inline UString JSValue::getString(ExecState* exec) const
214
    {
215
        return isCell() ? asCell()->getString(exec) : UString();
216 217
    }

218 219 220 221 222
    template <typename Base> UString HandleConverter<Base, Unknown>::getString(ExecState* exec) const
    {
        return jsValue().getString(exec);
    }

ggaren@apple.com's avatar
ggaren@apple.com committed
223
    inline JSObject* JSValue::getObject() const
224
    {
225
        return isCell() ? asCell()->getObject() : 0;
226 227
    }

228
    inline CallType getCallData(JSValue value, CallData& callData)
229
    {
230
        CallType result = value.isCell() ? value.asCell()->getCallData(callData) : CallTypeNone;
231
        ASSERT(result == CallTypeNone || value.isValidCallee());
232
        return result;
233 234
    }

235
    inline ConstructType getConstructData(JSValue value, ConstructData& constructData)
236
    {
237
        ConstructType result = value.isCell() ? value.asCell()->getConstructData(constructData) : ConstructTypeNone;
238
        ASSERT(result == ConstructTypeNone || value.isValidCallee());
239
        return result;
240 241
    }

ggaren@apple.com's avatar
ggaren@apple.com committed
242
    ALWAYS_INLINE bool JSValue::getUInt32(uint32_t& v) const
243
    {
244 245 246 247 248 249 250 251 252 253 254
        if (isInt32()) {
            int32_t i = asInt32();
            v = static_cast<uint32_t>(i);
            return i >= 0;
        }
        if (isDouble()) {
            double d = asDouble();
            v = static_cast<uint32_t>(d);
            return v == d;
        }
        return false;
255 256
    }

257
#if USE(JSVALUE64)
258
    ALWAYS_INLINE JSCell* JSValue::asCell() const
259 260 261
    {
        ASSERT(isCell());
        return m_ptr;
262
    }
263
#endif // USE(JSVALUE64)
264

ggaren@apple.com's avatar
ggaren@apple.com committed
265
    inline JSValue JSValue::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
266
    {
267
        return isCell() ? asCell()->toPrimitive(exec, preferredType) : asValue();
268 269
    }

ggaren@apple.com's avatar
ggaren@apple.com committed
270
    inline bool JSValue::getPrimitiveNumber(ExecState* exec, double& number, JSValue& value)
271
    {
272 273 274 275 276 277 278 279
        if (isInt32()) {
            number = asInt32();
            value = *this;
            return true;
        }
        if (isDouble()) {
            number = asDouble();
            value = *this;
280 281
            return true;
        }
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
        if (isCell())
            return asCell()->getPrimitiveNumber(exec, number, value);
        if (isTrue()) {
            number = 1.0;
            value = *this;
            return true;
        }
        if (isFalse() || isNull()) {
            number = 0.0;
            value = *this;
            return true;
        }
        ASSERT(isUndefined());
        number = nonInlineNaN();
        value = *this;
        return true;
298 299
    }

ggaren@apple.com's avatar
ggaren@apple.com committed
300
    inline bool JSValue::toBoolean(ExecState* exec) const
301
    {
302 303 304 305 306 307 308
        if (isInt32())
            return asInt32() != 0;
        if (isDouble())
            return asDouble() > 0.0 || asDouble() < 0.0; // false for NaN
        if (isCell())
            return asCell()->toBoolean(exec);
        return isTrue(); // false, null, and undefined all convert to false.
309 310
    }

ggaren@apple.com's avatar
ggaren@apple.com committed
311
    ALWAYS_INLINE double JSValue::toNumber(ExecState* exec) const
312
    {
313 314 315 316 317 318 319 320 321
        if (isInt32())
            return asInt32();
        if (isDouble())
            return asDouble();
        if (isCell())
            return asCell()->toNumber(exec);
        if (isTrue())
            return 1.0;
        return isUndefined() ? nonInlineNaN() : 0; // null and false both convert to 0.
322 323
    }

324
    inline bool JSValue::needsThisConversion() const
325
    {
326 327 328
        if (UNLIKELY(!isCell()))
            return true;
        return asCell()->structure()->typeInfo().needsThisConversion();
329 330
    }

331
    inline JSValue JSValue::getJSNumber()
mjs@apple.com's avatar
mjs@apple.com committed
332
    {
333 334 335 336 337
        if (isInt32() || isDouble())
            return *this;
        if (isCell())
            return asCell()->getJSNumber();
        return JSValue();
mjs@apple.com's avatar
mjs@apple.com committed
338 339
    }

340
    inline JSObject* JSValue::toObject(ExecState* exec) const
341
    {
342 343 344 345 346 347
        return isCell() ? asCell()->toObject(exec, exec->lexicalGlobalObject()) : toObjectSlowCase(exec, exec->lexicalGlobalObject());
    }

    inline JSObject* JSValue::toObject(ExecState* exec, JSGlobalObject* globalObject) const
    {
        return isCell() ? asCell()->toObject(exec, globalObject) : toObjectSlowCase(exec, globalObject);
348 349
    }

350
    inline JSObject* JSValue::toThisObject(ExecState* exec) const
351
    {
352
        return isCell() ? asCell()->toThisObject(exec) : toThisObjectSlowCase(exec);
353
    }
354 355
    
    ALWAYS_INLINE void MarkStack::internalAppend(JSCell* cell)
356
    {
357
        ASSERT(!m_isCheckingForDefaultMarkViolation);
358
        ASSERT(cell);
359
        if (Heap::testAndSetMarked(cell))
360 361 362 363
            return;
        if (cell->structure()->typeInfo().type() >= CompoundType)
            m_values.append(cell);
    }
364 365 366 367 368 369 370 371 372 373

    inline Heap* Heap::heap(JSValue v)
    {
        if (!v.isCell())
            return 0;
        return heap(v.asCell());
    }

    inline Heap* Heap::heap(JSCell* c)
    {
374
        return MarkedSpace::heap(c);
375
    }
oliver@apple.com's avatar
oliver@apple.com committed
376 377 378 379 380 381 382
    
#if ENABLE(JSC_ZOMBIES)
    inline bool JSValue::isZombie() const
    {
        return isCell() && asCell() && asCell()->isZombie();
    }
#endif
383

384
    inline void* MarkedBlock::allocate()
385
    {
386 387 388 389
        while (m_nextAtom < m_endAtom) {
            if (!m_marks.testAndSet(m_nextAtom)) {
                JSCell* cell = reinterpret_cast<JSCell*>(&atoms()[m_nextAtom]);
                m_nextAtom += m_atomsPerCell;
390 391 392
                cell->~JSCell();
                return cell;
            }
393
            m_nextAtom += m_atomsPerCell;
394
        }
395

396 397
        return 0;
    }
398
    
399
    inline MarkedSpace::SizeClass& MarkedSpace::sizeClassFor(size_t bytes)
400
    {
401 402 403 404
        ASSERT(bytes && bytes < maxCellSize);
        if (bytes < preciseCutoff)
            return m_preciseSizeClasses[(bytes - 1) / preciseStep];
        return m_impreciseSizeClasses[(bytes - 1) / impreciseStep];
405 406 407 408 409 410 411
    }

    inline void* MarkedSpace::allocate(size_t bytes)
    {
        SizeClass& sizeClass = sizeClassFor(bytes);
        return allocateFromSizeClass(sizeClass);
    }
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    
    inline void* Heap::allocate(size_t bytes)
    {
        ASSERT(globalData()->identifierTable == wtfThreadData().currentIdentifierTable());
        ASSERT(JSLock::lockCount() > 0);
        ASSERT(JSLock::currentThreadIsHoldingLock());
        ASSERT(bytes <= MarkedSpace::maxCellSize);
        ASSERT(m_operationInProgress == NoOperation);

        m_operationInProgress = Allocation;
        void* result = m_markedSpace.allocate(bytes);
        m_operationInProgress = NoOperation;
        if (result)
            return result;

        return allocateSlowCase(bytes);
    }

    inline void* JSCell::operator new(size_t size, JSGlobalData* globalData)
    {
        return globalData->heap.allocate(size);
    }

    inline void* JSCell::operator new(size_t size, ExecState* exec)
    {
        return exec->heap()->allocate(size);
    }
439

440
} // namespace JSC
weinig@apple.com's avatar
weinig@apple.com committed
441 442

#endif // JSCell_h