]> git.saurik.com Git - apple/javascriptcore.git/blob - API/JSCallbackObject.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / API / JSCallbackObject.h
1 /*
2 * Copyright (C) 2006, 2007, 2008, 2010 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 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 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 WTF_MAKE_FAST_ALLOCATED;
38 public:
39 JSCallbackObjectData(void* privateData, JSClassRef jsClass)
40 : privateData(privateData)
41 , jsClass(jsClass)
42 {
43 JSClassRetain(jsClass);
44 }
45
46 ~JSCallbackObjectData()
47 {
48 JSClassRelease(jsClass);
49 }
50
51 JSValue getPrivateProperty(const Identifier& propertyName) const
52 {
53 if (!m_privateProperties)
54 return JSValue();
55 return m_privateProperties->getPrivateProperty(propertyName);
56 }
57
58 void setPrivateProperty(VM& vm, JSCell* owner, const Identifier& propertyName, JSValue value)
59 {
60 if (!m_privateProperties)
61 m_privateProperties = std::make_unique<JSPrivatePropertyMap>();
62 m_privateProperties->setPrivateProperty(vm, owner, propertyName, value);
63 }
64
65 void deletePrivateProperty(const Identifier& propertyName)
66 {
67 if (!m_privateProperties)
68 return;
69 m_privateProperties->deletePrivateProperty(propertyName);
70 }
71
72 void visitChildren(SlotVisitor& visitor)
73 {
74 if (!m_privateProperties)
75 return;
76 m_privateProperties->visitChildren(visitor);
77 }
78
79 void* privateData;
80 JSClassRef jsClass;
81 struct JSPrivatePropertyMap {
82 JSValue getPrivateProperty(const Identifier& propertyName) const
83 {
84 PrivatePropertyMap::const_iterator location = m_propertyMap.find(propertyName.impl());
85 if (location == m_propertyMap.end())
86 return JSValue();
87 return location->value.get();
88 }
89
90 void setPrivateProperty(VM& vm, JSCell* owner, const Identifier& propertyName, JSValue value)
91 {
92 WriteBarrier<Unknown> empty;
93 m_propertyMap.add(propertyName.impl(), empty).iterator->value.set(vm, owner, value);
94 }
95
96 void deletePrivateProperty(const Identifier& propertyName)
97 {
98 m_propertyMap.remove(propertyName.impl());
99 }
100
101 void visitChildren(SlotVisitor& visitor)
102 {
103 for (PrivatePropertyMap::iterator ptr = m_propertyMap.begin(); ptr != m_propertyMap.end(); ++ptr) {
104 if (ptr->value)
105 visitor.append(&ptr->value);
106 }
107 }
108
109 private:
110 typedef HashMap<RefPtr<UniquedStringImpl>, WriteBarrier<Unknown>, IdentifierRepHash> PrivatePropertyMap;
111 PrivatePropertyMap m_propertyMap;
112 };
113 std::unique_ptr<JSPrivatePropertyMap> m_privateProperties;
114 };
115
116
117 template <class Parent>
118 class JSCallbackObject : public Parent {
119 protected:
120 JSCallbackObject(ExecState*, Structure*, JSClassRef, void* data);
121 JSCallbackObject(VM&, JSClassRef, Structure*);
122
123 void finishCreation(ExecState*);
124 void finishCreation(VM&);
125
126 public:
127 typedef Parent Base;
128 static const unsigned StructureFlags = Base::StructureFlags | ProhibitsPropertyCaching | OverridesGetOwnPropertySlot | InterceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero | ImplementsHasInstance | OverridesHasInstance | OverridesGetPropertyNames | TypeOfShouldCallGetCallData;
129
130 ~JSCallbackObject();
131
132 static JSCallbackObject* create(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, JSClassRef classRef, void* data)
133 {
134 ASSERT_UNUSED(globalObject, !structure->globalObject() || structure->globalObject() == globalObject);
135 JSCallbackObject* callbackObject = new (NotNull, allocateCell<JSCallbackObject>(*exec->heap())) JSCallbackObject(exec, structure, classRef, data);
136 callbackObject->finishCreation(exec);
137 return callbackObject;
138 }
139 static JSCallbackObject<Parent>* create(VM&, JSClassRef, Structure*);
140
141 static const bool needsDestruction;
142 static void destroy(JSCell* cell)
143 {
144 static_cast<JSCallbackObject*>(cell)->JSCallbackObject::~JSCallbackObject();
145 }
146
147 void setPrivate(void* data);
148 void* getPrivate();
149
150 DECLARE_INFO;
151
152 JSClassRef classRef() const { return m_callbackObjectData->jsClass; }
153 bool inherits(JSClassRef) const;
154
155 static Structure* createStructure(VM&, JSGlobalObject*, JSValue);
156
157 JSValue getPrivateProperty(const Identifier& propertyName) const
158 {
159 return m_callbackObjectData->getPrivateProperty(propertyName);
160 }
161
162 void setPrivateProperty(VM& vm, const Identifier& propertyName, JSValue value)
163 {
164 m_callbackObjectData->setPrivateProperty(vm, this, propertyName, value);
165 }
166
167 void deletePrivateProperty(const Identifier& propertyName)
168 {
169 m_callbackObjectData->deletePrivateProperty(propertyName);
170 }
171
172 using Parent::methodTable;
173
174 private:
175 static String className(const JSObject*);
176
177 static JSValue defaultValue(const JSObject*, ExecState*, PreferredPrimitiveType);
178
179 static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
180 static bool getOwnPropertySlotByIndex(JSObject*, ExecState*, unsigned propertyName, PropertySlot&);
181
182 static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
183 static void putByIndex(JSCell*, ExecState*, unsigned, JSValue, bool shouldThrow);
184
185 static bool deleteProperty(JSCell*, ExecState*, PropertyName);
186 static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned);
187
188 static bool customHasInstance(JSObject*, ExecState*, JSValue);
189
190 static void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
191
192 static ConstructType getConstructData(JSCell*, ConstructData&);
193 static CallType getCallData(JSCell*, CallData&);
194
195 static void visitChildren(JSCell* cell, SlotVisitor& visitor)
196 {
197 JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell);
198 ASSERT_GC_OBJECT_INHERITS((static_cast<Parent*>(thisObject)), JSCallbackObject<Parent>::info());
199 Parent::visitChildren(thisObject, visitor);
200 thisObject->m_callbackObjectData->visitChildren(visitor);
201 }
202
203 void init(ExecState*);
204
205 static JSCallbackObject* asCallbackObject(JSValue);
206 static JSCallbackObject* asCallbackObject(EncodedJSValue);
207
208 static EncodedJSValue JSC_HOST_CALL call(ExecState*);
209 static EncodedJSValue JSC_HOST_CALL construct(ExecState*);
210
211 JSValue getStaticValue(ExecState*, PropertyName);
212 static EncodedJSValue staticFunctionGetter(ExecState*, JSObject*, EncodedJSValue, PropertyName);
213 static EncodedJSValue callbackGetter(ExecState*, JSObject*, EncodedJSValue, PropertyName);
214
215 std::unique_ptr<JSCallbackObjectData> m_callbackObjectData;
216 };
217
218 } // namespace JSC
219
220 // include the actual template class implementation
221 #include "JSCallbackObjectFunctions.h"
222
223 #endif // JSCallbackObject_h