JSCellInlines.h 6.07 KB
Newer Older
1
/*
2
 * Copyright (C) 2012, 2013 Apple Inc. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

26 27 28
#ifndef JSCellInlines_h
#define JSCellInlines_h

29
#include "CallFrame.h"
30
#include "DeferGC.h"
31
#include "Handle.h"
32
#include "JSCell.h"
33 34 35
#include "JSObject.h"
#include "JSString.h"
#include "Structure.h"
36
#include <wtf/CompilationThread.h>
37 38 39 40 41

namespace JSC {

inline JSCell::JSCell(CreatingEarlyCellTag)
{
42
    ASSERT(!isCompilationThread());
43 44
}

45 46
inline JSCell::JSCell(VM& vm, Structure* structure)
    : m_structure(vm, this, structure)
47
{
48
    ASSERT(!isCompilationThread());
49 50
}

51
inline void JSCell::finishCreation(VM& vm)
52 53
{
#if ENABLE(GC_VALIDATION)
54 55
    ASSERT(vm.isInitializingObject());
    vm.setInitializingObjectClass(0);
56
#else
57
    UNUSED_PARAM(vm);
58 59 60 61
#endif
    ASSERT(m_structure);
}

62
inline void JSCell::finishCreation(VM& vm, Structure* structure, CreatingEarlyCellTag)
63 64
{
#if ENABLE(GC_VALIDATION)
65 66
    ASSERT(vm.isInitializingObject());
    vm.setInitializingObjectClass(0);
67 68
    if (structure)
#endif
69
        m_structure.setEarlyValue(vm, this, structure);
70
    // Very first set of allocations won't have a real structure.
71
    ASSERT(m_structure || !vm.structureStructure);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
}

inline Structure* JSCell::structure() const
{
    return m_structure.get();
}

inline void JSCell::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
    MARK_LOG_PARENT(visitor, cell);

    visitor.append(&cell->m_structure);
}

template<typename T>
void* allocateCell(Heap& heap, size_t size)
{
89
    ASSERT(!DisallowGC::isGCDisallowedOnCurrentThread());
90 91 92 93
    ASSERT(size >= sizeof(T));
    JSCell* result = 0;
    if (T::needsDestruction && T::hasImmortalStructure)
        result = static_cast<JSCell*>(heap.allocateWithImmortalStructureDestructor(size));
94
    else if (T::needsDestruction)
95 96 97
        result = static_cast<JSCell*>(heap.allocateWithNormalDestructor(size));
    else 
        result = static_cast<JSCell*>(heap.allocateWithoutDestructor(size));
98 99 100 101
#if ENABLE(GC_VALIDATION)
    ASSERT(!heap.vm()->isInitializingObject());
    heap.vm()->setInitializingObjectClass(T::info());
#endif
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    result->clearStructure();
    return result;
}
    
template<typename T>
void* allocateCell(Heap& heap)
{
    return allocateCell<T>(heap, sizeof(T));
}
    
inline bool isZapped(const JSCell* cell)
{
    return cell->isZapped();
}

inline bool JSCell::isObject() const
{
    return m_structure->isObject();
}

inline bool JSCell::isString() const
{
    return m_structure->typeInfo().type() == StringType;
}

inline bool JSCell::isGetterSetter() const
{
    return m_structure->typeInfo().type() == GetterSetterType;
}

inline bool JSCell::isProxy() const
{
    return structure()->typeInfo().type() == ProxyType;
}

inline bool JSCell::isAPIValueWrapper() const
{
    return m_structure->typeInfo().type() == APIValueWrapperType;
}

142
inline void JSCell::setStructure(VM& vm, Structure* structure)
143 144 145 146 147 148
{
    ASSERT(structure->typeInfo().overridesVisitChildren() == this->structure()->typeInfo().overridesVisitChildren());
    ASSERT(structure->classInfo() == m_structure->classInfo());
    ASSERT(!m_structure
        || m_structure->transitionWatchpointSetHasBeenInvalidated()
        || m_structure.get() == structure);
149
    m_structure.set(vm, this, structure);
150 151
}

152 153 154 155 156
inline const MethodTable* JSCell::methodTableForDestruction() const
{
    return &classInfo()->methodTable;
}

157 158
inline const MethodTable* JSCell::methodTable() const
{
159 160 161
    if (Structure* rootStructure = m_structure->structure())
        RELEASE_ASSERT(rootStructure == rootStructure->structure());

162 163 164 165 166 167 168 169 170 171 172 173
    return &classInfo()->methodTable;
}

inline bool JSCell::inherits(const ClassInfo* info) const
{
    return classInfo()->isSubClassOf(info);
}

// Fast call to get a property where we may not yet have converted the string to an
// identifier. The first time we perform a property access with a given string, try
// performing the property map lookup without forming an identifier. We detect this
// case by checking whether the hash has yet been set for this string.
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
174 175 176 177
ALWAYS_INLINE JSValue JSCell::fastGetOwnProperty(ExecState* exec, const String& name)
{
    if (!structure()->typeInfo().overridesGetOwnPropertySlot() && !structure()->hasGetterSetterProperties()) {
        PropertyOffset offset = name.impl()->hasHash()
178 179
            ? structure()->get(exec->vm(), Identifier(exec, name))
            : structure()->get(exec->vm(), name);
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
180 181 182 183
        if (offset != invalidOffset)
            return asObject(this)->locationForOffset(offset)->get();
    }
    return JSValue();
184 185 186 187 188 189 190 191 192
}

inline bool JSCell::toBoolean(ExecState* exec) const
{
    if (isString()) 
        return static_cast<const JSString*>(this)->toBoolean();
    return !structure()->masqueradesAsUndefined(exec->lexicalGlobalObject());
}

193 194 195 196 197 198 199
inline TriState JSCell::pureToBoolean() const
{
    if (isString()) 
        return static_cast<const JSString*>(this)->toBoolean() ? TrueTriState : FalseTriState;
    return MixedTriState;
}

200 201
} // namespace JSC

202
#endif // JSCellInlines_h