]> git.saurik.com Git - apple/javascriptcore.git/blob - API/JSCallbackObject.h
1cf7a02b256e11521d8a699ec04f462d47bd81cb
[apple/javascriptcore.git] / API / JSCallbackObject.h
1 /*
2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
25 */
26
27 #ifndef JSCallbackObject_h
28 #define JSCallbackObject_h
29
30 #include "JSObjectRef.h"
31 #include "JSValueRef.h"
32 #include "JSObject.h"
33
34 namespace JSC {
35
36 struct JSCallbackObjectData {
37 JSCallbackObjectData(void* privateData, JSClassRef jsClass)
38 : privateData(privateData)
39 , jsClass(jsClass)
40 {
41 JSClassRetain(jsClass);
42 }
43
44 ~JSCallbackObjectData()
45 {
46 JSClassRelease(jsClass);
47 }
48
49 JSValue getPrivateProperty(const Identifier& propertyName) const
50 {
51 if (!m_privateProperties)
52 return JSValue();
53 return m_privateProperties->getPrivateProperty(propertyName);
54 }
55
56 void setPrivateProperty(const Identifier& propertyName, JSValue value)
57 {
58 if (!m_privateProperties)
59 m_privateProperties.set(new JSPrivatePropertyMap);
60 m_privateProperties->setPrivateProperty(propertyName, value);
61 }
62
63 void deletePrivateProperty(const Identifier& propertyName)
64 {
65 if (!m_privateProperties)
66 return;
67 m_privateProperties->deletePrivateProperty(propertyName);
68 }
69
70 void markChildren(MarkStack& markStack)
71 {
72 if (!m_privateProperties)
73 return;
74 m_privateProperties->markChildren(markStack);
75 }
76
77 void* privateData;
78 JSClassRef jsClass;
79 struct JSPrivatePropertyMap {
80 JSValue getPrivateProperty(const Identifier& propertyName) const
81 {
82 PrivatePropertyMap::const_iterator location = m_propertyMap.find(propertyName.ustring().rep());
83 if (location == m_propertyMap.end())
84 return JSValue();
85 return location->second;
86 }
87
88 void setPrivateProperty(const Identifier& propertyName, JSValue value)
89 {
90 m_propertyMap.set(propertyName.ustring().rep(), value);
91 }
92
93 void deletePrivateProperty(const Identifier& propertyName)
94 {
95 m_propertyMap.remove(propertyName.ustring().rep());
96 }
97
98 void markChildren(MarkStack& markStack)
99 {
100 for (PrivatePropertyMap::iterator ptr = m_propertyMap.begin(); ptr != m_propertyMap.end(); ++ptr) {
101 if (ptr->second)
102 markStack.append(ptr->second);
103 }
104 }
105
106 private:
107 typedef HashMap<RefPtr<UString::Rep>, JSValue, IdentifierRepHash> PrivatePropertyMap;
108 PrivatePropertyMap m_propertyMap;
109 };
110 OwnPtr<JSPrivatePropertyMap> m_privateProperties;
111 };
112
113
114 template <class Base>
115 class JSCallbackObject : public Base {
116 public:
117 JSCallbackObject(ExecState*, NonNullPassRefPtr<Structure>, JSClassRef, void* data);
118 JSCallbackObject(JSClassRef);
119 virtual ~JSCallbackObject();
120
121 void setPrivate(void* data);
122 void* getPrivate();
123
124 static const ClassInfo info;
125
126 JSClassRef classRef() const { return m_callbackObjectData->jsClass; }
127 bool inherits(JSClassRef) const;
128
129 static PassRefPtr<Structure> createStructure(JSValue proto)
130 {
131 return Structure::create(proto, TypeInfo(ObjectType, StructureFlags), Base::AnonymousSlotCount);
132 }
133
134 JSValue getPrivateProperty(const Identifier& propertyName) const
135 {
136 return m_callbackObjectData->getPrivateProperty(propertyName);
137 }
138
139 void setPrivateProperty(const Identifier& propertyName, JSValue value)
140 {
141 m_callbackObjectData->setPrivateProperty(propertyName, value);
142 }
143
144 void deletePrivateProperty(const Identifier& propertyName)
145 {
146 m_callbackObjectData->deletePrivateProperty(propertyName);
147 }
148
149 protected:
150 static const unsigned StructureFlags = OverridesGetOwnPropertySlot | ImplementsHasInstance | OverridesHasInstance | OverridesMarkChildren | OverridesGetPropertyNames | Base::StructureFlags;
151
152 private:
153 virtual UString className() const;
154
155 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
156 virtual bool getOwnPropertySlot(ExecState*, unsigned, PropertySlot&);
157 virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
158
159 virtual void put(ExecState*, const Identifier&, JSValue, PutPropertySlot&);
160
161 virtual bool deleteProperty(ExecState*, const Identifier&);
162 virtual bool deleteProperty(ExecState*, unsigned);
163
164 virtual bool hasInstance(ExecState* exec, JSValue value, JSValue proto);
165
166 virtual void getOwnPropertyNames(ExecState*, PropertyNameArray&, EnumerationMode mode = ExcludeDontEnumProperties);
167
168 virtual double toNumber(ExecState*) const;
169 virtual UString toString(ExecState*) const;
170
171 virtual ConstructType getConstructData(ConstructData&);
172 virtual CallType getCallData(CallData&);
173 virtual const ClassInfo* classInfo() const { return &info; }
174
175 virtual void markChildren(MarkStack& markStack)
176 {
177 Base::markChildren(markStack);
178 m_callbackObjectData->markChildren(markStack);
179 }
180
181 void init(ExecState*);
182
183 static JSCallbackObject* asCallbackObject(JSValue);
184
185 static JSValue JSC_HOST_CALL call(ExecState*, JSObject* functionObject, JSValue thisValue, const ArgList&);
186 static JSObject* construct(ExecState*, JSObject* constructor, const ArgList&);
187
188 static JSValue staticValueGetter(ExecState*, JSValue, const Identifier&);
189 static JSValue staticFunctionGetter(ExecState*, JSValue, const Identifier&);
190 static JSValue callbackGetter(ExecState*, JSValue, const Identifier&);
191
192 OwnPtr<JSCallbackObjectData> m_callbackObjectData;
193 };
194
195 } // namespace JSC
196
197 // include the actual template class implementation
198 #include "JSCallbackObjectFunctions.h"
199
200 #endif // JSCallbackObject_h