2 * Copyright (C) 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef JSCallbackObject_h
28 #define JSCallbackObject_h
30 #include "JSObjectRef.h"
31 #include "JSValueRef.h"
33 #include <wtf/PassOwnPtr.h>
37 struct JSCallbackObjectData
: WeakHandleOwner
{
38 JSCallbackObjectData(void* privateData
, JSClassRef jsClass
)
39 : privateData(privateData
)
42 JSClassRetain(jsClass
);
45 ~JSCallbackObjectData()
47 JSClassRelease(jsClass
);
50 JSValue
getPrivateProperty(const Identifier
& propertyName
) const
52 if (!m_privateProperties
)
54 return m_privateProperties
->getPrivateProperty(propertyName
);
57 void setPrivateProperty(VM
& vm
, JSCell
* owner
, const Identifier
& propertyName
, JSValue value
)
59 if (!m_privateProperties
)
60 m_privateProperties
= adoptPtr(new JSPrivatePropertyMap
);
61 m_privateProperties
->setPrivateProperty(vm
, owner
, propertyName
, value
);
64 void deletePrivateProperty(const Identifier
& propertyName
)
66 if (!m_privateProperties
)
68 m_privateProperties
->deletePrivateProperty(propertyName
);
71 void visitChildren(SlotVisitor
& visitor
)
73 if (!m_privateProperties
)
75 m_privateProperties
->visitChildren(visitor
);
80 struct JSPrivatePropertyMap
{
81 JSValue
getPrivateProperty(const Identifier
& propertyName
) const
83 PrivatePropertyMap::const_iterator location
= m_propertyMap
.find(propertyName
.impl());
84 if (location
== m_propertyMap
.end())
86 return location
->value
.get();
89 void setPrivateProperty(VM
& vm
, JSCell
* owner
, const Identifier
& propertyName
, JSValue value
)
91 WriteBarrier
<Unknown
> empty
;
92 m_propertyMap
.add(propertyName
.impl(), empty
).iterator
->value
.set(vm
, owner
, value
);
95 void deletePrivateProperty(const Identifier
& propertyName
)
97 m_propertyMap
.remove(propertyName
.impl());
100 void visitChildren(SlotVisitor
& visitor
)
102 for (PrivatePropertyMap::iterator ptr
= m_propertyMap
.begin(); ptr
!= m_propertyMap
.end(); ++ptr
) {
104 visitor
.append(&ptr
->value
);
109 typedef HashMap
<RefPtr
<StringImpl
>, WriteBarrier
<Unknown
>, IdentifierRepHash
> PrivatePropertyMap
;
110 PrivatePropertyMap m_propertyMap
;
112 OwnPtr
<JSPrivatePropertyMap
> m_privateProperties
;
113 virtual void finalize(Handle
<Unknown
>, void*);
117 template <class Parent
>
118 class JSCallbackObject
: public Parent
{
120 JSCallbackObject(ExecState
*, Structure
*, JSClassRef
, void* data
);
121 JSCallbackObject(VM
&, JSClassRef
, Structure
*);
123 void finishCreation(ExecState
*);
124 void finishCreation(VM
&);
129 static JSCallbackObject
* create(ExecState
* exec
, JSGlobalObject
* globalObject
, Structure
* structure
, JSClassRef classRef
, void* data
)
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
;
136 static JSCallbackObject
<Parent
>* create(VM
&, JSClassRef
, Structure
*);
138 static const bool needsDestruction
;
139 static void destroy(JSCell
* cell
)
141 static_cast<JSCallbackObject
*>(cell
)->JSCallbackObject::~JSCallbackObject();
144 void setPrivate(void* data
);
147 static const ClassInfo s_info
;
149 JSClassRef
classRef() const { return m_callbackObjectData
->jsClass
; }
150 bool inherits(JSClassRef
) const;
152 static Structure
* createStructure(VM
&, JSGlobalObject
*, JSValue
);
154 JSValue
getPrivateProperty(const Identifier
& propertyName
) const
156 return m_callbackObjectData
->getPrivateProperty(propertyName
);
159 void setPrivateProperty(VM
& vm
, const Identifier
& propertyName
, JSValue value
)
161 m_callbackObjectData
->setPrivateProperty(vm
, this, propertyName
, value
);
164 void deletePrivateProperty(const Identifier
& propertyName
)
166 m_callbackObjectData
->deletePrivateProperty(propertyName
);
169 using Parent::methodTable
;
172 static const unsigned StructureFlags
= ProhibitsPropertyCaching
| OverridesGetOwnPropertySlot
| InterceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero
| ImplementsHasInstance
| OverridesHasInstance
| OverridesVisitChildren
| OverridesGetPropertyNames
| Parent::StructureFlags
;
175 static String
className(const JSObject
*);
177 static JSValue
defaultValue(const JSObject
*, ExecState
*, PreferredPrimitiveType
);
179 static bool getOwnPropertySlot(JSCell
*, ExecState
*, PropertyName
, PropertySlot
&);
180 static bool getOwnPropertySlotByIndex(JSCell
*, ExecState
*, unsigned propertyName
, PropertySlot
&);
181 static bool getOwnPropertyDescriptor(JSObject
*, ExecState
*, PropertyName
, PropertyDescriptor
&);
183 static void put(JSCell
*, ExecState
*, PropertyName
, JSValue
, PutPropertySlot
&);
184 static void putByIndex(JSCell
*, ExecState
*, unsigned, JSValue
, bool shouldThrow
);
186 static bool deleteProperty(JSCell
*, ExecState
*, PropertyName
);
187 static bool deletePropertyByIndex(JSCell
*, ExecState
*, unsigned);
189 static bool customHasInstance(JSObject
*, ExecState
*, JSValue
);
191 static void getOwnNonIndexPropertyNames(JSObject
*, ExecState
*, PropertyNameArray
&, EnumerationMode
);
193 static ConstructType
getConstructData(JSCell
*, ConstructData
&);
194 static CallType
getCallData(JSCell
*, CallData
&);
196 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 ASSERT(thisObject
->Parent::structure()->typeInfo().overridesVisitChildren());
202 Parent::visitChildren(thisObject
, visitor
);
203 thisObject
->m_callbackObjectData
->visitChildren(visitor
);
206 void init(ExecState
*);
208 static JSCallbackObject
* asCallbackObject(JSValue
);
210 static EncodedJSValue JSC_HOST_CALL
call(ExecState
*);
211 static EncodedJSValue JSC_HOST_CALL
construct(ExecState
*);
213 JSValue
getStaticValue(ExecState
*, PropertyName
);
214 static JSValue
staticFunctionGetter(ExecState
*, JSValue
, PropertyName
);
215 static JSValue
callbackGetter(ExecState
*, JSValue
, PropertyName
);
217 OwnPtr
<JSCallbackObjectData
> m_callbackObjectData
;
222 // include the actual template class implementation
223 #include "JSCallbackObjectFunctions.h"
225 #endif // JSCallbackObject_h