ba1e7e531bc89ad23cf0106395d72b8a94dd1d17
[WebKit-https.git] / Source / WebCore / bridge / runtime_object.cpp
1 /*
2  * Copyright (C) 2003, 2008, 2009 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "runtime_object.h"
28
29 #include "JSDOMBinding.h"
30 #include "runtime_method.h"
31 #include <runtime/Error.h>
32
33 using namespace WebCore;
34
35 namespace JSC {
36 namespace Bindings {
37
38 const ClassInfo RuntimeObject::s_info = { "RuntimeObject", &JSNonFinalObject::s_info, 0, 0 };
39
40 RuntimeObject::RuntimeObject(ExecState*, JSGlobalObject* globalObject, Structure* structure, PassRefPtr<Instance> instance)
41     : JSNonFinalObject(globalObject->globalData(), structure)
42     , m_instance(instance)
43 {
44     ASSERT(structure->globalObject() == globalObject);
45     finishCreation(globalObject);
46 }
47
48 void RuntimeObject::finishCreation(JSGlobalObject* globalObject)
49 {
50     Base::finishCreation(globalObject->globalData());
51     ASSERT(inherits(&s_info));
52 }
53
54 RuntimeObject::~RuntimeObject()
55 {
56     ASSERT(!m_instance);
57 }
58
59 void RuntimeObject::invalidate()
60 {
61     ASSERT(m_instance);
62     if (m_instance)
63         m_instance->willInvalidateRuntimeObject();
64     m_instance = 0;
65 }
66
67 JSValue RuntimeObject::fallbackObjectGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
68 {
69     RuntimeObject* thisObj = static_cast<RuntimeObject*>(asObject(slotBase));
70     RefPtr<Instance> instance = thisObj->m_instance;
71
72     if (!instance)
73         return throwInvalidAccessError(exec);
74     
75     instance->begin();
76
77     Class *aClass = instance->getClass();
78     JSValue result = aClass->fallbackObject(exec, instance.get(), propertyName);
79
80     instance->end();
81             
82     return result;
83 }
84
85 JSValue RuntimeObject::fieldGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
86 {    
87     RuntimeObject* thisObj = static_cast<RuntimeObject*>(asObject(slotBase));
88     RefPtr<Instance> instance = thisObj->m_instance;
89
90     if (!instance)
91         return throwInvalidAccessError(exec);
92     
93     instance->begin();
94
95     Class *aClass = instance->getClass();
96     Field* aField = aClass->fieldNamed(propertyName, instance.get());
97     JSValue result = aField->valueFromInstance(exec, instance.get());
98     
99     instance->end();
100             
101     return result;
102 }
103
104 JSValue RuntimeObject::methodGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
105 {
106     RuntimeObject* thisObj = static_cast<RuntimeObject*>(asObject(slotBase));
107     RefPtr<Instance> instance = thisObj->m_instance;
108
109     if (!instance)
110         return throwInvalidAccessError(exec);
111     
112     instance->begin();
113
114     JSValue method = instance->getMethod(exec, propertyName);
115
116     instance->end();
117             
118     return method;
119 }
120
121 bool RuntimeObject::getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot)
122 {
123     if (!m_instance) {
124         throwInvalidAccessError(exec);
125         return false;
126     }
127     
128     RefPtr<Instance> instance = m_instance;
129
130     instance->begin();
131     
132     Class *aClass = instance->getClass();
133     
134     if (aClass) {
135         // See if the instance has a field with the specified name.
136         Field *aField = aClass->fieldNamed(propertyName, instance.get());
137         if (aField) {
138             slot.setCustom(this, fieldGetter);
139             instance->end();
140             return true;
141         } else {
142             // Now check if a method with specified name exists, if so return a function object for
143             // that method.
144             MethodList methodList = aClass->methodsNamed(propertyName, instance.get());
145             if (methodList.size() > 0) {
146                 slot.setCustom(this, methodGetter);
147                 
148                 instance->end();
149                 return true;
150             }
151         }
152
153         // Try a fallback object.
154         if (!aClass->fallbackObject(exec, instance.get(), propertyName).isUndefined()) {
155             slot.setCustom(this, fallbackObjectGetter);
156             instance->end();
157             return true;
158         }
159     }
160         
161     instance->end();
162     
163     return instance->getOwnPropertySlot(this, exec, propertyName, slot);
164 }
165
166 bool RuntimeObject::getOwnPropertyDescriptor(ExecState *exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
167 {
168     if (!m_instance) {
169         throwInvalidAccessError(exec);
170         return false;
171     }
172     
173     RefPtr<Instance> instance = m_instance;
174     instance->begin();
175     
176     Class *aClass = instance->getClass();
177     
178     if (aClass) {
179         // See if the instance has a field with the specified name.
180         Field *aField = aClass->fieldNamed(propertyName, instance.get());
181         if (aField) {
182             PropertySlot slot;
183             slot.setCustom(this, fieldGetter);
184             instance->end();
185             descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete);
186             return true;
187         } else {
188             // Now check if a method with specified name exists, if so return a function object for
189             // that method.
190             MethodList methodList = aClass->methodsNamed(propertyName, instance.get());
191             if (methodList.size() > 0) {
192                 PropertySlot slot;
193                 slot.setCustom(this, methodGetter);
194                 instance->end();
195                 descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete | ReadOnly);
196                 return true;
197             }
198         }
199         
200         // Try a fallback object.
201         if (!aClass->fallbackObject(exec, instance.get(), propertyName).isUndefined()) {
202             PropertySlot slot;
203             slot.setCustom(this, fallbackObjectGetter);
204             instance->end();
205             descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete | ReadOnly | DontEnum);
206             return true;
207         }
208     }
209     
210     instance->end();
211     
212     return instance->getOwnPropertyDescriptor(this, exec, propertyName, descriptor);
213 }
214
215 void RuntimeObject::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
216 {
217     if (!m_instance) {
218         throwInvalidAccessError(exec);
219         return;
220     }
221     
222     RefPtr<Instance> instance = m_instance;
223     instance->begin();
224
225     // Set the value of the property.
226     Field *aField = instance->getClass()->fieldNamed(propertyName, instance.get());
227     if (aField)
228         aField->setValueToInstance(exec, instance.get(), value);
229     else if (!instance->setValueOfUndefinedField(exec, propertyName, value))
230         instance->put(this, exec, propertyName, value, slot);
231
232     instance->end();
233 }
234
235 bool RuntimeObject::deleteProperty(ExecState*, const Identifier&)
236 {
237     // Can never remove a property of a RuntimeObject.
238     return false;
239 }
240
241 JSValue RuntimeObject::defaultValue(ExecState* exec, PreferredPrimitiveType hint) const
242 {
243     if (!m_instance)
244         return throwInvalidAccessError(exec);
245     
246     RefPtr<Instance> instance = m_instance;
247
248     instance->begin();
249     JSValue result = instance->defaultValue(exec, hint);
250     instance->end();
251     return result;
252 }
253
254 static EncodedJSValue JSC_HOST_CALL callRuntimeObject(ExecState* exec)
255 {
256     ASSERT(exec->callee()->inherits(&RuntimeObject::s_info));
257     RefPtr<Instance> instance(static_cast<RuntimeObject*>(exec->callee())->getInternalInstance());
258     instance->begin();
259     JSValue result = instance->invokeDefaultMethod(exec);
260     instance->end();
261     return JSValue::encode(result);
262 }
263
264 CallType RuntimeObject::getCallData(CallData& callData)
265 {
266     if (!m_instance)
267         return CallTypeNone;
268     
269     RefPtr<Instance> instance = m_instance;
270     if (!instance->supportsInvokeDefaultMethod())
271         return CallTypeNone;
272     
273     callData.native.function = callRuntimeObject;
274     return CallTypeHost;
275 }
276
277 static EncodedJSValue JSC_HOST_CALL callRuntimeConstructor(ExecState* exec)
278 {
279     JSObject* constructor = exec->callee();
280     ASSERT(constructor->inherits(&RuntimeObject::s_info));
281     RefPtr<Instance> instance(static_cast<RuntimeObject*>(exec->callee())->getInternalInstance());
282     instance->begin();
283     ArgList args(exec);
284     JSValue result = instance->invokeConstruct(exec, args);
285     instance->end();
286     
287     ASSERT(result);
288     return JSValue::encode(result.isObject() ? static_cast<JSObject*>(result.asCell()) : constructor);
289 }
290
291 ConstructType RuntimeObject::getConstructData(ConstructData& constructData)
292 {
293     if (!m_instance)
294         return ConstructTypeNone;
295     
296     RefPtr<Instance> instance = m_instance;
297     if (!instance->supportsConstruct())
298         return ConstructTypeNone;
299     
300     constructData.native.function = callRuntimeConstructor;
301     return ConstructTypeHost;
302 }
303
304 void RuntimeObject::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode)
305 {
306     if (!m_instance) {
307         throwInvalidAccessError(exec);
308         return;
309     }
310
311     RefPtr<Instance> instance = m_instance;
312     
313     instance->begin();
314     instance->getPropertyNames(exec, propertyNames);
315     instance->end();
316 }
317
318 JSObject* RuntimeObject::throwInvalidAccessError(ExecState* exec)
319 {
320     return throwError(exec, createReferenceError(exec, "Trying to access object from destroyed plug-in."));
321 }
322
323 }
324 }