2 * Copyright (C) 2006, 2007, 2008 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"
36 struct JSCallbackObjectData
{
37 JSCallbackObjectData(void* privateData
, JSClassRef jsClass
)
38 : privateData(privateData
)
41 JSClassRetain(jsClass
);
44 ~JSCallbackObjectData()
46 JSClassRelease(jsClass
);
49 JSValue
getPrivateProperty(const Identifier
& propertyName
) const
51 if (!m_privateProperties
)
53 return m_privateProperties
->getPrivateProperty(propertyName
);
56 void setPrivateProperty(const Identifier
& propertyName
, JSValue value
)
58 if (!m_privateProperties
)
59 m_privateProperties
.set(new JSPrivatePropertyMap
);
60 m_privateProperties
->setPrivateProperty(propertyName
, value
);
63 void deletePrivateProperty(const Identifier
& propertyName
)
65 if (!m_privateProperties
)
67 m_privateProperties
->deletePrivateProperty(propertyName
);
70 void markChildren(MarkStack
& markStack
)
72 if (!m_privateProperties
)
74 m_privateProperties
->markChildren(markStack
);
79 struct JSPrivatePropertyMap
{
80 JSValue
getPrivateProperty(const Identifier
& propertyName
) const
82 PrivatePropertyMap::const_iterator location
= m_propertyMap
.find(propertyName
.ustring().rep());
83 if (location
== m_propertyMap
.end())
85 return location
->second
;
88 void setPrivateProperty(const Identifier
& propertyName
, JSValue value
)
90 m_propertyMap
.set(propertyName
.ustring().rep(), value
);
93 void deletePrivateProperty(const Identifier
& propertyName
)
95 m_propertyMap
.remove(propertyName
.ustring().rep());
98 void markChildren(MarkStack
& markStack
)
100 for (PrivatePropertyMap::iterator ptr
= m_propertyMap
.begin(); ptr
!= m_propertyMap
.end(); ++ptr
) {
102 markStack
.append(ptr
->second
);
107 typedef HashMap
<RefPtr
<UString::Rep
>, JSValue
, IdentifierRepHash
> PrivatePropertyMap
;
108 PrivatePropertyMap m_propertyMap
;
110 OwnPtr
<JSPrivatePropertyMap
> m_privateProperties
;
114 template <class Base
>
115 class JSCallbackObject
: public Base
{
117 JSCallbackObject(ExecState
*, NonNullPassRefPtr
<Structure
>, JSClassRef
, void* data
);
118 JSCallbackObject(JSClassRef
);
119 virtual ~JSCallbackObject();
121 void setPrivate(void* data
);
124 static const ClassInfo info
;
126 JSClassRef
classRef() const { return m_callbackObjectData
->jsClass
; }
127 bool inherits(JSClassRef
) const;
129 static PassRefPtr
<Structure
> createStructure(JSValue proto
)
131 return Structure::create(proto
, TypeInfo(ObjectType
, StructureFlags
), Base::AnonymousSlotCount
);
134 JSValue
getPrivateProperty(const Identifier
& propertyName
) const
136 return m_callbackObjectData
->getPrivateProperty(propertyName
);
139 void setPrivateProperty(const Identifier
& propertyName
, JSValue value
)
141 m_callbackObjectData
->setPrivateProperty(propertyName
, value
);
144 void deletePrivateProperty(const Identifier
& propertyName
)
146 m_callbackObjectData
->deletePrivateProperty(propertyName
);
150 static const unsigned StructureFlags
= OverridesGetOwnPropertySlot
| ImplementsHasInstance
| OverridesHasInstance
| OverridesMarkChildren
| OverridesGetPropertyNames
| Base::StructureFlags
;
153 virtual UString
className() const;
155 virtual bool getOwnPropertySlot(ExecState
*, const Identifier
&, PropertySlot
&);
156 virtual bool getOwnPropertySlot(ExecState
*, unsigned, PropertySlot
&);
157 virtual bool getOwnPropertyDescriptor(ExecState
*, const Identifier
&, PropertyDescriptor
&);
159 virtual void put(ExecState
*, const Identifier
&, JSValue
, PutPropertySlot
&);
161 virtual bool deleteProperty(ExecState
*, const Identifier
&);
162 virtual bool deleteProperty(ExecState
*, unsigned);
164 virtual bool hasInstance(ExecState
* exec
, JSValue value
, JSValue proto
);
166 virtual void getOwnPropertyNames(ExecState
*, PropertyNameArray
&, EnumerationMode mode
= ExcludeDontEnumProperties
);
168 virtual double toNumber(ExecState
*) const;
169 virtual UString
toString(ExecState
*) const;
171 virtual ConstructType
getConstructData(ConstructData
&);
172 virtual CallType
getCallData(CallData
&);
173 virtual const ClassInfo
* classInfo() const { return &info
; }
175 virtual void markChildren(MarkStack
& markStack
)
177 Base::markChildren(markStack
);
178 m_callbackObjectData
->markChildren(markStack
);
181 void init(ExecState
*);
183 static JSCallbackObject
* asCallbackObject(JSValue
);
185 static JSValue JSC_HOST_CALL
call(ExecState
*, JSObject
* functionObject
, JSValue thisValue
, const ArgList
&);
186 static JSObject
* construct(ExecState
*, JSObject
* constructor
, const ArgList
&);
188 static JSValue
staticValueGetter(ExecState
*, JSValue
, const Identifier
&);
189 static JSValue
staticFunctionGetter(ExecState
*, JSValue
, const Identifier
&);
190 static JSValue
callbackGetter(ExecState
*, JSValue
, const Identifier
&);
192 OwnPtr
<JSCallbackObjectData
> m_callbackObjectData
;
197 // include the actual template class implementation
198 #include "JSCallbackObjectFunctions.h"
200 #endif // JSCallbackObject_h