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 {
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(const Identifier& propertyName, JSValue value)
59 if (!m_privateProperties)
60 m_privateProperties = adoptPtr(new JSPrivatePropertyMap);
61 m_privateProperties->setPrivateProperty(propertyName, value);
64 void deletePrivateProperty(const Identifier& propertyName)
66 if (!m_privateProperties)
68 m_privateProperties->deletePrivateProperty(propertyName);
71 void markChildren(MarkStack& markStack)
73 if (!m_privateProperties)
75 m_privateProperties->markChildren(markStack);
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->second;
89 void setPrivateProperty(const Identifier& propertyName, JSValue value)
91 m_propertyMap.set(propertyName.impl(), value);
94 void deletePrivateProperty(const Identifier& propertyName)
96 m_propertyMap.remove(propertyName.impl());
99 void markChildren(MarkStack& markStack)
101 for (PrivatePropertyMap::iterator ptr = m_propertyMap.begin(); ptr != m_propertyMap.end(); ++ptr) {
103 markStack.append(ptr->second);
108 typedef HashMap<RefPtr<StringImpl>, JSValue, IdentifierRepHash> PrivatePropertyMap;
109 PrivatePropertyMap m_propertyMap;
111 OwnPtr<JSPrivatePropertyMap> m_privateProperties;
115 template <class Base>
116 class JSCallbackObject : public Base {
118 JSCallbackObject(ExecState*, JSGlobalObject*, NonNullPassRefPtr<Structure>, JSClassRef, void* data);
119 JSCallbackObject(JSClassRef, NonNullPassRefPtr<Structure>);
120 virtual ~JSCallbackObject();
122 void setPrivate(void* data);
125 static const ClassInfo info;
127 JSClassRef classRef() const { return m_callbackObjectData->jsClass; }
128 bool inherits(JSClassRef) const;
130 static PassRefPtr<Structure> createStructure(JSValue proto)
132 return Structure::create(proto, TypeInfo(ObjectType, StructureFlags), Base::AnonymousSlotCount);
135 JSValue getPrivateProperty(const Identifier& propertyName) const
137 return m_callbackObjectData->getPrivateProperty(propertyName);
140 void setPrivateProperty(const Identifier& propertyName, JSValue value)
142 m_callbackObjectData->setPrivateProperty(propertyName, value);
145 void deletePrivateProperty(const Identifier& propertyName)
147 m_callbackObjectData->deletePrivateProperty(propertyName);
151 static const unsigned StructureFlags = OverridesGetOwnPropertySlot | ImplementsHasInstance | OverridesHasInstance | OverridesMarkChildren | OverridesGetPropertyNames | Base::StructureFlags;
154 virtual UString className() const;
156 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
157 virtual bool getOwnPropertySlot(ExecState*, unsigned, PropertySlot&);
158 virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
160 virtual void put(ExecState*, const Identifier&, JSValue, PutPropertySlot&);
162 virtual bool deleteProperty(ExecState*, const Identifier&);
163 virtual bool deleteProperty(ExecState*, unsigned);
165 virtual bool hasInstance(ExecState* exec, JSValue value, JSValue proto);
167 virtual void getOwnPropertyNames(ExecState*, PropertyNameArray&, EnumerationMode mode = ExcludeDontEnumProperties);
169 virtual double toNumber(ExecState*) const;
170 virtual UString toString(ExecState*) const;
172 virtual ConstructType getConstructData(ConstructData&);
173 virtual CallType getCallData(CallData&);
174 virtual const ClassInfo* classInfo() const { return &info; }
176 virtual void markChildren(MarkStack& markStack)
178 Base::markChildren(markStack);
179 m_callbackObjectData->markChildren(markStack);
182 void init(ExecState*);
184 static JSCallbackObject* asCallbackObject(JSValue);
186 static EncodedJSValue JSC_HOST_CALL call(ExecState*);
187 static EncodedJSValue JSC_HOST_CALL construct(ExecState*);
189 static JSValue staticValueGetter(ExecState*, JSValue, const Identifier&);
190 static JSValue staticFunctionGetter(ExecState*, JSValue, const Identifier&);
191 static JSValue callbackGetter(ExecState*, JSValue, const Identifier&);
193 OwnPtr<JSCallbackObjectData> m_callbackObjectData;
198 // include the actual template class implementation
199 #include "JSCallbackObjectFunctions.h"
201 #endif // JSCallbackObject_h