JSCallbackObject.h 8.47 KB
Newer Older
ggaren's avatar
ggaren committed
1
/*
2
 * Copyright (C) 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
3
 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
ggaren's avatar
ggaren committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * 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 COMPUTER, 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 COMPUTER, 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. 
 */

#ifndef JSCallbackObject_h
#define JSCallbackObject_h

#include "JSObjectRef.h"
#include "JSValueRef.h"
darin@apple.com's avatar
darin@apple.com committed
32
#include "JSObject.h"
33
#include <wtf/PassOwnPtr.h>
ggaren's avatar
ggaren committed
34

35
namespace JSC {
ggaren's avatar
ggaren committed
36

37
struct JSCallbackObjectData : WeakHandleOwner {
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    JSCallbackObjectData(void* privateData, JSClassRef jsClass)
        : privateData(privateData)
        , jsClass(jsClass)
    {
        JSClassRetain(jsClass);
    }
    
    ~JSCallbackObjectData()
    {
        JSClassRelease(jsClass);
    }
    
    JSValue getPrivateProperty(const Identifier& propertyName) const
    {
        if (!m_privateProperties)
            return JSValue();
        return m_privateProperties->getPrivateProperty(propertyName);
    }
    
57
    void setPrivateProperty(JSGlobalData& globalData, JSCell* owner, const Identifier& propertyName, JSValue value)
58 59
    {
        if (!m_privateProperties)
60
            m_privateProperties = adoptPtr(new JSPrivatePropertyMap);
61
        m_privateProperties->setPrivateProperty(globalData, owner, propertyName, value);
62 63 64 65 66 67 68 69 70
    }
    
    void deletePrivateProperty(const Identifier& propertyName)
    {
        if (!m_privateProperties)
            return;
        m_privateProperties->deletePrivateProperty(propertyName);
    }

71
    void visitChildren(SlotVisitor& visitor)
72 73 74
    {
        if (!m_privateProperties)
            return;
75
        m_privateProperties->visitChildren(visitor);
76 77 78 79 80 81 82
    }

    void* privateData;
    JSClassRef jsClass;
    struct JSPrivatePropertyMap {
        JSValue getPrivateProperty(const Identifier& propertyName) const
        {
83
            PrivatePropertyMap::const_iterator location = m_propertyMap.find(propertyName.impl());
84 85
            if (location == m_propertyMap.end())
                return JSValue();
86
            return location->second.get();
87 88
        }
        
89
        void setPrivateProperty(JSGlobalData& globalData, JSCell* owner, const Identifier& propertyName, JSValue value)
90
        {
91
            WriteBarrier<Unknown> empty;
92
            m_propertyMap.add(propertyName.impl(), empty).iterator->second.set(globalData, owner, value);
93 94 95 96
        }
        
        void deletePrivateProperty(const Identifier& propertyName)
        {
97
            m_propertyMap.remove(propertyName.impl());
98 99
        }

100
        void visitChildren(SlotVisitor& visitor)
101 102
        {
            for (PrivatePropertyMap::iterator ptr = m_propertyMap.begin(); ptr != m_propertyMap.end(); ++ptr) {
103 104
                if (ptr->second)
                    visitor.append(&ptr->second);
105 106 107 108
            }
        }

    private:
109
        typedef HashMap<RefPtr<StringImpl>, WriteBarrier<Unknown>, IdentifierRepHash> PrivatePropertyMap;
110 111 112
        PrivatePropertyMap m_propertyMap;
    };
    OwnPtr<JSPrivatePropertyMap> m_privateProperties;
113
    virtual void finalize(Handle<Unknown>, void*);
114 115 116
};

    
117 118
template <class Parent>
class JSCallbackObject : public Parent {
119
protected:
120
    JSCallbackObject(ExecState*, Structure*, JSClassRef, void* data);
121
    JSCallbackObject(JSGlobalData&, JSClassRef, Structure*);
122

123
    void finishCreation(ExecState*);
124
    void finishCreation(JSGlobalData&);
125

126
public:
127 128
    typedef Parent Base;

129 130
    static JSCallbackObject* create(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, JSClassRef classRef, void* data)
    {
oliver@apple.com's avatar
oliver@apple.com committed
131
        ASSERT_UNUSED(globalObject, !structure->globalObject() || structure->globalObject() == globalObject);
132
        JSCallbackObject* callbackObject = new (NotNull, allocateCell<JSCallbackObject>(*exec->heap())) JSCallbackObject(exec, structure, classRef, data);
133
        callbackObject->finishCreation(exec);
134
        return callbackObject;
135
    }
136 137 138 139 140 141 142
    static JSCallbackObject* create(JSGlobalData& globalData, JSClassRef classRef, Structure* structure)
    {
        JSCallbackObject* callbackObject = new (NotNull, allocateCell<JSCallbackObject>(globalData.heap)) JSCallbackObject(globalData, classRef, structure);
        callbackObject->finishCreation(globalData);
        return callbackObject;
    }

darin@apple.com's avatar
darin@apple.com committed
143 144 145
    void setPrivate(void* data);
    void* getPrivate();

146
    static const ClassInfo s_info;
darin@apple.com's avatar
darin@apple.com committed
147

ggaren@apple.com's avatar
ggaren@apple.com committed
148
    JSClassRef classRef() const { return m_callbackObjectData->jsClass; }
darin@apple.com's avatar
darin@apple.com committed
149 150
    bool inherits(JSClassRef) const;

151
    static Structure* createStructure(JSGlobalData&, JSGlobalObject*, JSValue);
152 153 154 155 156 157
    
    JSValue getPrivateProperty(const Identifier& propertyName) const
    {
        return m_callbackObjectData->getPrivateProperty(propertyName);
    }
    
158
    void setPrivateProperty(JSGlobalData& globalData, const Identifier& propertyName, JSValue value)
159
    {
160
        m_callbackObjectData->setPrivateProperty(globalData, this, propertyName, value);
161 162 163 164 165 166
    }
    
    void deletePrivateProperty(const Identifier& propertyName)
    {
        m_callbackObjectData->deletePrivateProperty(propertyName);
    }
mjs@apple.com's avatar
mjs@apple.com committed
167

168 169
    using Parent::methodTable;

170
protected:
171
    static const unsigned StructureFlags = ProhibitsPropertyCaching | OverridesGetOwnPropertySlot | InterceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero | ImplementsHasInstance | OverridesHasInstance | OverridesVisitChildren | OverridesGetPropertyNames | Parent::StructureFlags;
172

darin@apple.com's avatar
darin@apple.com committed
173
private:
174
    static String className(const JSObject*);
ggaren's avatar
ggaren committed
175

176 177
    static void destroy(JSCell*);

178 179
    static JSValue defaultValue(const JSObject*, ExecState*, PreferredPrimitiveType);

180
    static bool getOwnPropertySlot(JSCell*, ExecState*, PropertyName, PropertySlot&);
181
    static bool getOwnPropertySlotByIndex(JSCell*, ExecState*, unsigned propertyName, PropertySlot&);
182
    static bool getOwnPropertyDescriptor(JSObject*, ExecState*, PropertyName, PropertyDescriptor&);
ggaren's avatar
ggaren committed
183
    
184
    static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
ggaren's avatar
ggaren committed
185

186
    static bool deleteProperty(JSCell*, ExecState*, PropertyName);
187
    static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned);
ggaren's avatar
ggaren committed
188

189
    static bool customHasInstance(JSObject*, ExecState*, JSValue);
mjs's avatar
mjs committed
190

191
    static void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
ggaren's avatar
ggaren committed
192

193
    static ConstructType getConstructData(JSCell*, ConstructData&);
194
    static CallType getCallData(JSCell*, CallData&);
ggaren's avatar
ggaren committed
195

196 197
    static void visitChildren(JSCell* cell, SlotVisitor& visitor)
    {
198
        JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell);
199
        ASSERT_GC_OBJECT_INHERITS((static_cast<Parent*>(thisObject)), &JSCallbackObject<Parent>::s_info);
200
        COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
201 202 203
        ASSERT(thisObject->Parent::structure()->typeInfo().overridesVisitChildren());
        Parent::visitChildren(thisObject, visitor);
        thisObject->m_callbackObjectData->visitChildren(visitor);
204 205
    }

ggaren@apple.com's avatar
ggaren@apple.com committed
206
    void init(ExecState*);
darin@apple.com's avatar
darin@apple.com committed
207
 
ggaren@apple.com's avatar
ggaren@apple.com committed
208
    static JSCallbackObject* asCallbackObject(JSValue);
darin@apple.com's avatar
darin@apple.com committed
209
 
210
    static EncodedJSValue JSC_HOST_CALL call(ExecState*);
211
    static EncodedJSValue JSC_HOST_CALL construct(ExecState*);
darin@apple.com's avatar
darin@apple.com committed
212
   
213 214 215
    JSValue getStaticValue(ExecState*, PropertyName);
    static JSValue staticFunctionGetter(ExecState*, JSValue, PropertyName);
    static JSValue callbackGetter(ExecState*, JSValue, PropertyName);
ggaren@apple.com's avatar
ggaren@apple.com committed
216 217

    OwnPtr<JSCallbackObjectData> m_callbackObjectData;
ggaren's avatar
ggaren committed
218 219
};

220
} // namespace JSC
ggaren's avatar
ggaren committed
221

222 223 224
// include the actual template class implementation
#include "JSCallbackObjectFunctions.h"

ggaren's avatar
ggaren committed
225
#endif // JSCallbackObject_h