]> git.saurik.com Git - apple/javascriptcore.git/blame - API/JSCallbackObject.h
JavaScriptCore-903.5.tar.gz
[apple/javascriptcore.git] / API / JSCallbackObject.h
CommitLineData
b37bf2e1 1/*
14957cd0 2 * Copyright (C) 2006, 2007, 2008, 2010 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"
14957cd0 33#include <wtf/PassOwnPtr.h>
b37bf2e1 34
9dae56ea 35namespace JSC {
b37bf2e1 36
14957cd0 37struct JSCallbackObjectData : WeakHandleOwner {
4e4e5a6f
A
38 JSCallbackObjectData(void* privateData, JSClassRef jsClass)
39 : privateData(privateData)
40 , jsClass(jsClass)
41 {
42 JSClassRetain(jsClass);
43 }
44
45 ~JSCallbackObjectData()
46 {
47 JSClassRelease(jsClass);
48 }
49
50 JSValue getPrivateProperty(const Identifier& propertyName) const
51 {
52 if (!m_privateProperties)
53 return JSValue();
54 return m_privateProperties->getPrivateProperty(propertyName);
55 }
56
14957cd0 57 void setPrivateProperty(JSGlobalData& globalData, JSCell* owner, const Identifier& propertyName, JSValue value)
4e4e5a6f
A
58 {
59 if (!m_privateProperties)
14957cd0
A
60 m_privateProperties = adoptPtr(new JSPrivatePropertyMap);
61 m_privateProperties->setPrivateProperty(globalData, owner, propertyName, value);
4e4e5a6f
A
62 }
63
64 void deletePrivateProperty(const Identifier& propertyName)
65 {
66 if (!m_privateProperties)
67 return;
68 m_privateProperties->deletePrivateProperty(propertyName);
69 }
70
14957cd0 71 void visitChildren(SlotVisitor& visitor)
4e4e5a6f
A
72 {
73 if (!m_privateProperties)
74 return;
14957cd0 75 m_privateProperties->visitChildren(visitor);
4e4e5a6f
A
76 }
77
78 void* privateData;
79 JSClassRef jsClass;
80 struct JSPrivatePropertyMap {
81 JSValue getPrivateProperty(const Identifier& propertyName) const
82 {
14957cd0 83 PrivatePropertyMap::const_iterator location = m_propertyMap.find(propertyName.impl());
4e4e5a6f
A
84 if (location == m_propertyMap.end())
85 return JSValue();
14957cd0 86 return location->second.get();
4e4e5a6f
A
87 }
88
14957cd0 89 void setPrivateProperty(JSGlobalData& globalData, JSCell* owner, const Identifier& propertyName, JSValue value)
4e4e5a6f 90 {
14957cd0
A
91 WriteBarrier<Unknown> empty;
92 m_propertyMap.add(propertyName.impl(), empty).first->second.set(globalData, owner, value);
4e4e5a6f
A
93 }
94
95 void deletePrivateProperty(const Identifier& propertyName)
96 {
14957cd0 97 m_propertyMap.remove(propertyName.impl());
4e4e5a6f
A
98 }
99
14957cd0 100 void visitChildren(SlotVisitor& visitor)
4e4e5a6f
A
101 {
102 for (PrivatePropertyMap::iterator ptr = m_propertyMap.begin(); ptr != m_propertyMap.end(); ++ptr) {
103 if (ptr->second)
14957cd0 104 visitor.append(&ptr->second);
4e4e5a6f
A
105 }
106 }
107
108 private:
14957cd0 109 typedef HashMap<RefPtr<StringImpl>, WriteBarrier<Unknown>, IdentifierRepHash> PrivatePropertyMap;
4e4e5a6f
A
110 PrivatePropertyMap m_propertyMap;
111 };
112 OwnPtr<JSPrivatePropertyMap> m_privateProperties;
14957cd0 113 virtual void finalize(Handle<Unknown>, void*);
4e4e5a6f
A
114};
115
116
b37bf2e1 117template <class Base>
9dae56ea 118class JSCallbackObject : public Base {
b37bf2e1 119public:
14957cd0
A
120 JSCallbackObject(ExecState*, JSGlobalObject*, Structure*, JSClassRef, void* data);
121 JSCallbackObject(JSGlobalData&, JSClassRef, Structure*);
b37bf2e1 122
9dae56ea
A
123 void setPrivate(void* data);
124 void* getPrivate();
125
14957cd0 126 static const ClassInfo s_info;
9dae56ea
A
127
128 JSClassRef classRef() const { return m_callbackObjectData->jsClass; }
129 bool inherits(JSClassRef) const;
130
14957cd0 131 static Structure* createStructure(JSGlobalData& globalData, JSValue proto)
9dae56ea 132 {
14957cd0 133 return Structure::create(globalData, proto, TypeInfo(ObjectType, StructureFlags), Base::AnonymousSlotCount, &s_info);
9dae56ea 134 }
4e4e5a6f
A
135
136 JSValue getPrivateProperty(const Identifier& propertyName) const
137 {
138 return m_callbackObjectData->getPrivateProperty(propertyName);
139 }
140
14957cd0 141 void setPrivateProperty(JSGlobalData& globalData, const Identifier& propertyName, JSValue value)
4e4e5a6f 142 {
14957cd0 143 m_callbackObjectData->setPrivateProperty(globalData, this, propertyName, value);
4e4e5a6f
A
144 }
145
146 void deletePrivateProperty(const Identifier& propertyName)
147 {
148 m_callbackObjectData->deletePrivateProperty(propertyName);
149 }
9dae56ea 150
f9bf01c6 151protected:
14957cd0 152 static const unsigned StructureFlags = ProhibitsPropertyCaching | OverridesGetOwnPropertySlot | ImplementsHasInstance | OverridesHasInstance | OverridesVisitChildren | OverridesGetPropertyNames | Base::StructureFlags;
f9bf01c6 153
9dae56ea 154private:
b37bf2e1
A
155 virtual UString className() const;
156
157 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
f9bf01c6 158 virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
b37bf2e1 159
ba379fdc 160 virtual void put(ExecState*, const Identifier&, JSValue, PutPropertySlot&);
b37bf2e1
A
161
162 virtual bool deleteProperty(ExecState*, const Identifier&);
163 virtual bool deleteProperty(ExecState*, unsigned);
164
ba379fdc 165 virtual bool hasInstance(ExecState* exec, JSValue value, JSValue proto);
b37bf2e1 166
f9bf01c6 167 virtual void getOwnPropertyNames(ExecState*, PropertyNameArray&, EnumerationMode mode = ExcludeDontEnumProperties);
b37bf2e1
A
168
169 virtual double toNumber(ExecState*) const;
170 virtual UString toString(ExecState*) const;
171
9dae56ea
A
172 virtual ConstructType getConstructData(ConstructData&);
173 virtual CallType getCallData(CallData&);
b37bf2e1 174
14957cd0 175 virtual void visitChildren(SlotVisitor& visitor)
4e4e5a6f 176 {
14957cd0
A
177 Base::visitChildren(visitor);
178 m_callbackObjectData->visitChildren(visitor);
4e4e5a6f
A
179 }
180
b37bf2e1 181 void init(ExecState*);
9dae56ea 182
ba379fdc 183 static JSCallbackObject* asCallbackObject(JSValue);
9dae56ea 184
14957cd0
A
185 static EncodedJSValue JSC_HOST_CALL call(ExecState*);
186 static EncodedJSValue JSC_HOST_CALL construct(ExecState*);
9dae56ea 187
14957cd0 188 JSValue getStaticValue(ExecState*, const Identifier&);
4e4e5a6f
A
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