]> git.saurik.com Git - apple/javascriptcore.git/blame - API/JSCallbackObject.h
JavaScriptCore-721.26.tar.gz
[apple/javascriptcore.git] / API / JSCallbackObject.h
CommitLineData
b37bf2e1 1/*
9dae56ea 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
b37bf2e1
A
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"
9dae56ea 32#include "JSObject.h"
b37bf2e1 33
9dae56ea 34namespace JSC {
b37bf2e1 35
4e4e5a6f
A
36struct 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
b37bf2e1 114template <class Base>
9dae56ea 115class JSCallbackObject : public Base {
b37bf2e1 116public:
f9bf01c6 117 JSCallbackObject(ExecState*, NonNullPassRefPtr<Structure>, JSClassRef, void* data);
b37bf2e1
A
118 JSCallbackObject(JSClassRef);
119 virtual ~JSCallbackObject();
120
9dae56ea
A
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
ba379fdc 129 static PassRefPtr<Structure> createStructure(JSValue proto)
9dae56ea 130 {
f9bf01c6 131 return Structure::create(proto, TypeInfo(ObjectType, StructureFlags), Base::AnonymousSlotCount);
9dae56ea 132 }
4e4e5a6f
A
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 }
9dae56ea 148
f9bf01c6
A
149protected:
150 static const unsigned StructureFlags = OverridesGetOwnPropertySlot | ImplementsHasInstance | OverridesHasInstance | OverridesMarkChildren | OverridesGetPropertyNames | Base::StructureFlags;
151
9dae56ea 152private:
b37bf2e1
A
153 virtual UString className() const;
154
155 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
156 virtual bool getOwnPropertySlot(ExecState*, unsigned, PropertySlot&);
f9bf01c6 157 virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
b37bf2e1 158
ba379fdc 159 virtual void put(ExecState*, const Identifier&, JSValue, PutPropertySlot&);
b37bf2e1
A
160
161 virtual bool deleteProperty(ExecState*, const Identifier&);
162 virtual bool deleteProperty(ExecState*, unsigned);
163
ba379fdc 164 virtual bool hasInstance(ExecState* exec, JSValue value, JSValue proto);
b37bf2e1 165
f9bf01c6 166 virtual void getOwnPropertyNames(ExecState*, PropertyNameArray&, EnumerationMode mode = ExcludeDontEnumProperties);
b37bf2e1
A
167
168 virtual double toNumber(ExecState*) const;
169 virtual UString toString(ExecState*) const;
170
9dae56ea
A
171 virtual ConstructType getConstructData(ConstructData&);
172 virtual CallType getCallData(CallData&);
173 virtual const ClassInfo* classInfo() const { return &info; }
b37bf2e1 174
4e4e5a6f
A
175 virtual void markChildren(MarkStack& markStack)
176 {
177 Base::markChildren(markStack);
178 m_callbackObjectData->markChildren(markStack);
179 }
180
b37bf2e1 181 void init(ExecState*);
9dae56ea 182
ba379fdc 183 static JSCallbackObject* asCallbackObject(JSValue);
9dae56ea 184
ba379fdc 185 static JSValue JSC_HOST_CALL call(ExecState*, JSObject* functionObject, JSValue thisValue, const ArgList&);
9dae56ea
A
186 static JSObject* construct(ExecState*, JSObject* constructor, const ArgList&);
187
4e4e5a6f
A
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
9dae56ea 192 OwnPtr<JSCallbackObjectData> m_callbackObjectData;
b37bf2e1
A
193};
194
9dae56ea 195} // namespace JSC
b37bf2e1
A
196
197// include the actual template class implementation
198#include "JSCallbackObjectFunctions.h"
199
200#endif // JSCallbackObject_h