JSCell.h 12.7 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
#include "CallData.h"
27
#include "CallFrame.h"
28
#include "ConstructData.h"
29
#include "Heap.h"
30
#include "JSImmediate.h"
31
#include "JSLock.h"
32 33
#include "JSValue.h"
#include "MarkStack.h"
34
#include "UString.h"
35
#include <wtf/Noncopyable.h>
weinig@apple.com's avatar
weinig@apple.com committed
36

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

39
    class JSGlobalObject;
40
    class Structure;
41

andersca@apple.com's avatar
andersca@apple.com committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55
#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
56 57
    class JSCell {
        WTF_MAKE_NONCOPYABLE(JSCell);
andersca@apple.com's avatar
andersca@apple.com committed
58
#endif
59

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

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

    public:
80
        static PassRefPtr<Structure> createDummyStructure(JSGlobalData&);
81

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

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

92
        // Extracting the value.
93 94
        bool getString(ExecState* exec, UString&) const;
        UString getString(ExecState* exec) const; // null string if not a string
95 96 97 98 99 100 101
        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
102
        // FIXME: remove these methods, can check isNumberCell in JSValue && then call asNumberCell::*.
103 104 105
        virtual bool getUInt32(uint32_t&) const;

        // Basic conversions.
106 107 108 109 110
        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;
111
        virtual JSObject* toObject(ExecState*, JSGlobalObject*) const;
112 113 114

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

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

        // Object operations, with the toObject operation included.
124
        const ClassInfo* classInfo() const;
ggaren@apple.com's avatar
ggaren@apple.com committed
125 126
        virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
        virtual void put(ExecState*, unsigned propertyName, JSValue);
127 128 129 130
        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
131
        virtual JSValue getJSNumber();
132
        void* vptr() { return *reinterpret_cast<void**>(this); }
ap@apple.com's avatar
ap@apple.com committed
133
        void setVPtr(void* vptr) { *reinterpret_cast<void**>(this) = vptr; }
134

135 136 137 138 139 140
        // 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&);

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

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

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

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

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

    inline JSCell::~JSCell()
    {
    }

darin@apple.com's avatar
darin@apple.com committed
171
    inline Structure* JSCell::structure() const
weinig@apple.com's avatar
weinig@apple.com committed
172
    {
darin@apple.com's avatar
darin@apple.com committed
173
        return m_structure;
weinig@apple.com's avatar
weinig@apple.com committed
174 175
    }

176
    inline void JSCell::markChildren(MarkStack&)
177 178 179 180 181
    {
    }

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

ggaren@apple.com's avatar
ggaren@apple.com committed
182
    inline bool JSValue::isString() const
183
    {
184
        return isCell() && asCell()->isString();
185 186
    }

ggaren@apple.com's avatar
ggaren@apple.com committed
187
    inline bool JSValue::isGetterSetter() const
darin@apple.com's avatar
darin@apple.com committed
188
    {
189
        return isCell() && asCell()->isGetterSetter();
darin@apple.com's avatar
darin@apple.com committed
190 191
    }

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

197
    inline bool JSValue::getString(ExecState* exec, UString& s) const
198
    {
199
        return isCell() && asCell()->getString(exec, s);
200 201
    }

202
    inline UString JSValue::getString(ExecState* exec) const
203
    {
204
        return isCell() ? asCell()->getString(exec) : UString();
205 206
    }

207 208 209 210 211
    template <typename Base> UString HandleConverter<Base, Unknown>::getString(ExecState* exec) const
    {
        return jsValue().getString(exec);
    }

ggaren@apple.com's avatar
ggaren@apple.com committed
212
    inline JSObject* JSValue::getObject() const
213
    {
214
        return isCell() ? asCell()->getObject() : 0;
215 216
    }

217
    inline CallType getCallData(JSValue value, CallData& callData)
218
    {
219
        CallType result = value.isCell() ? value.asCell()->getCallData(callData) : CallTypeNone;
220
        ASSERT(result == CallTypeNone || value.isValidCallee());
221
        return result;
222 223
    }

224
    inline ConstructType getConstructData(JSValue value, ConstructData& constructData)
225
    {
226
        ConstructType result = value.isCell() ? value.asCell()->getConstructData(constructData) : ConstructTypeNone;
227
        ASSERT(result == ConstructTypeNone || value.isValidCallee());
228
        return result;
229 230
    }

ggaren@apple.com's avatar
ggaren@apple.com committed
231
    ALWAYS_INLINE bool JSValue::getUInt32(uint32_t& v) const
232
    {
233 234 235 236 237 238 239 240 241 242 243
        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;
244 245
    }

246
#if USE(JSVALUE64)
247
    ALWAYS_INLINE JSCell* JSValue::asCell() const
248 249 250
    {
        ASSERT(isCell());
        return m_ptr;
251
    }
252
#endif // USE(JSVALUE64)
253

ggaren@apple.com's avatar
ggaren@apple.com committed
254
    inline JSValue JSValue::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
255
    {
256
        return isCell() ? asCell()->toPrimitive(exec, preferredType) : asValue();
257 258
    }

ggaren@apple.com's avatar
ggaren@apple.com committed
259
    inline bool JSValue::getPrimitiveNumber(ExecState* exec, double& number, JSValue& value)
260
    {
261 262 263 264 265 266 267 268
        if (isInt32()) {
            number = asInt32();
            value = *this;
            return true;
        }
        if (isDouble()) {
            number = asDouble();
            value = *this;
269 270
            return true;
        }
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
        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;
287 288
    }

ggaren@apple.com's avatar
ggaren@apple.com committed
289
    inline bool JSValue::toBoolean(ExecState* exec) const
290
    {
291 292 293 294 295 296 297
        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.
298 299
    }

ggaren@apple.com's avatar
ggaren@apple.com committed
300
    ALWAYS_INLINE double JSValue::toNumber(ExecState* exec) const
301
    {
302 303 304 305 306 307 308 309 310
        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.
311 312
    }

313
    inline JSValue JSValue::getJSNumber()
mjs@apple.com's avatar
mjs@apple.com committed
314
    {
315 316 317 318 319
        if (isInt32() || isDouble())
            return *this;
        if (isCell())
            return asCell()->getJSNumber();
        return JSValue();
mjs@apple.com's avatar
mjs@apple.com committed
320 321
    }

322
    inline JSObject* JSValue::toObject(ExecState* exec) const
323
    {
324 325 326 327 328 329
        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);
330 331
    }

332
    inline JSObject* JSValue::toThisObject(ExecState* exec) const
333
    {
334
        return isCell() ? asCell()->toThisObject(exec) : toThisObjectSlowCase(exec);
335
    }
336 337 338 339 340 341 342 343 344 345

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

    inline Heap* Heap::heap(JSCell* c)
    {
346
        return MarkedSpace::heap(c);
347
    }
oliver@apple.com's avatar
oliver@apple.com committed
348 349 350 351 352 353 354
    
#if ENABLE(JSC_ZOMBIES)
    inline bool JSValue::isZombie() const
    {
        return isCell() && asCell() && asCell()->isZombie();
    }
#endif
355

356
    inline void* MarkedBlock::allocate()
357
    {
358 359 360 361
        while (m_nextAtom < m_endAtom) {
            if (!m_marks.testAndSet(m_nextAtom)) {
                JSCell* cell = reinterpret_cast<JSCell*>(&atoms()[m_nextAtom]);
                m_nextAtom += m_atomsPerCell;
362 363 364
                cell->~JSCell();
                return cell;
            }
365
            m_nextAtom += m_atomsPerCell;
366
        }
367

368 369
        return 0;
    }
370
    
371
    inline MarkedSpace::SizeClass& MarkedSpace::sizeClassFor(size_t bytes)
372
    {
373 374 375 376
        ASSERT(bytes && bytes < maxCellSize);
        if (bytes < preciseCutoff)
            return m_preciseSizeClasses[(bytes - 1) / preciseStep];
        return m_impreciseSizeClasses[(bytes - 1) / impreciseStep];
377 378 379 380 381 382 383
    }

    inline void* MarkedSpace::allocate(size_t bytes)
    {
        SizeClass& sizeClass = sizeClassFor(bytes);
        return allocateFromSizeClass(sizeClass);
    }
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
    
    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);
    }
411

412
} // namespace JSC
weinig@apple.com's avatar
weinig@apple.com committed
413 414

#endif // JSCell_h