initial import
[vuplus_webkit] / 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 }
45
46 void RuntimeObject::finishCreation(JSGlobalObject* globalObject)
47 {
48     Base::finishCreation(globalObject->globalData());
49     ASSERT(inherits(&s_info));
50 }
51
52 RuntimeObject::~RuntimeObject()
53 {
54     ASSERT(!m_instance);
55 }
56
57 void RuntimeObject::invalidate()
58 {
59     ASSERT(m_instance);
60     if (m_instance)
61         m_instance->willInvalidateRuntimeObject();
62     m_instance = 0;
63 }
64
65 JSValue RuntimeObject::fallbackObjectGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
66 {
67     RuntimeObject* thisObj = static_cast<RuntimeObject*>(asObject(slotBase));
68     RefPtr<Instance> instance = thisObj->m_instance;
69
70     if (!instance)
71         return throwInvalidAccessError(exec);
72     
73     instance->begin();
74
75     Class *aClass = instance->getClass();
76     JSValue result = aClass->fallbackObject(exec, instance.get(), propertyName);
77
78     instance->end();
79             
80     return result;
81 }
82
83 JSValue RuntimeObject::fieldGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
84 {    
85     RuntimeObject* thisObj = static_cast<RuntimeObject*>(asObject(slotBase));
86     RefPtr<Instance> instance = thisObj->m_instance;
87
88     if (!instance)
89         return throwInvalidAccessError(exec);
90     
91     instance->begin();
92
93     Class *aClass = instance->getClass();
94     Field* aField = aClass->fieldNamed(propertyName, instance.get());
95     JSValue result = aField->valueFromInstance(exec, instance.get());
96     
97     instance->end();
98             
99     return result;
100 }
101
102 JSValue RuntimeObject::methodGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
103 {
104     RuntimeObject* thisObj = static_cast<RuntimeObject*>(asObject(slotBase));
105     RefPtr<Instance> instance = thisObj->m_instance;
106
107     if (!instance)
108         return throwInvalidAccessError(exec);
109     
110     instance->begin();
111
112     JSValue method = instance->getMethod(exec, propertyName);
113
114     instance->end();
115             
116     return method;
117 }
118
119 bool RuntimeObject::getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot)
120 {
121     if (!m_instance) {
122         throwInvalidAccessError(exec);
123         return false;
124     }
125     
126     RefPtr<Instance> instance = m_instance;
127
128     instance->begin();
129     
130     Class *aClass = instance->getClass();
131     
132     if (aClass) {
133         // See if the instance has a field with the specified name.
134         Field *aField = aClass->fieldNamed(propertyName, instance.get());
135         if (aField) {
136             slot.setCustom(this, fieldGetter);
137             instance->end();
138             return true;
139         } else {
140             // Now check if a method with specified name exists, if so return a function object for
141             // that method.
142             MethodList methodList = aClass->methodsNamed(propertyName, instance.get());
143             if (methodList.size() > 0) {
144                 slot.setCustom(this, methodGetter);
145                 
146                 instance->end();
147                 return true;
148             }
149         }
150
151         // Try a fallback object.
152         if (!aClass->fallbackObject(exec, instance.get(), propertyName).isUndefined()) {
153             slot.setCustom(this, fallbackObjectGetter);
154             instance->end();
155             return true;
156         }
157     }
158         
159     instance->end();
160     
161     return instance->getOwnPropertySlot(this, exec, propertyName, slot);
162 }
163
164 bool RuntimeObject::getOwnPropertyDescriptor(ExecState *exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
165 {
166     if (!m_instance) {
167         throwInvalidAccessError(exec);
168         return false;
169     }
170     
171     RefPtr<Instance> instance = m_instance;
172     instance->begin();
173     
174     Class *aClass = instance->getClass();
175     
176     if (aClass) {
177         // See if the instance has a field with the specified name.
178         Field *aField = aClass->fieldNamed(propertyName, instance.get());
179         if (aField) {
180             PropertySlot slot;
181             slot.setCustom(this, fieldGetter);
182             instance->end();
183             descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete);
184             return true;
185         } else {
186             // Now check if a method with specified name exists, if so return a function object for
187             // that method.
188             MethodList methodList = aClass->methodsNamed(propertyName, instance.get());
189             if (methodList.size() > 0) {
190                 PropertySlot slot;
191                 slot.setCustom(this, methodGetter);
192                 instance->end();
193                 descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete | ReadOnly);
194                 return true;
195             }
196         }
197         
198         // Try a fallback object.
199         if (!aClass->fallbackObject(exec, instance.get(), propertyName).isUndefined()) {
200             PropertySlot slot;
201             slot.setCustom(this, fallbackObjectGetter);
202             instance->end();
203             descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete | ReadOnly | DontEnum);
204             return true;
205         }
206     }
207     
208     instance->end();
209     
210     return instance->getOwnPropertyDescriptor(this, exec, propertyName, descriptor);
211 }
212
213 void RuntimeObject::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
214 {
215     if (!m_instance) {
216         throwInvalidAccessError(exec);
217         return;
218     }
219     
220     RefPtr<Instance> instance = m_instance;
221     instance->begin();
222
223     // Set the value of the property.
224     Field *aField = instance->getClass()->fieldNamed(propertyName, instance.get());
225     if (aField)
226         aField->setValueToInstance(exec, instance.get(), value);
227     else if (!instance->setValueOfUndefinedField(exec, propertyName, value))
228         instance->put(this, exec, propertyName, value, slot);
229
230     instance->end();
231 }
232
233 bool RuntimeObject::deleteProperty(ExecState*, const Identifier&)
234 {
235     // Can never remove a property of a RuntimeObject.
236     return false;
237 }
238
239 JSValue RuntimeObject::defaultValue(ExecState* exec, PreferredPrimitiveType hint) const
240 {
241     if (!m_instance)
242         return throwInvalidAccessError(exec);
243     
244     RefPtr<Instance> instance = m_instance;
245
246     instance->begin();
247     JSValue result = instance->defaultValue(exec, hint);
248     instance->end();
249     return result;
250 }
251
252 static EncodedJSValue JSC_HOST_CALL callRuntimeObject(ExecState* exec)
253 {
254     ASSERT(exec->callee()->inherits(&RuntimeObject::s_info));
255     RefPtr<Instance> instance(static_cast<RuntimeObject*>(exec->callee())->getInternalInstance());
256     instance->begin();
257     JSValue result = instance->invokeDefaultMethod(exec);
258     instance->end();
259     return JSValue::encode(result);
260 }
261
262 CallType RuntimeObject::getCallData(CallData& callData)
263 {
264     if (!m_instance)
265         return CallTypeNone;
266     
267     RefPtr<Instance> instance = m_instance;
268     if (!instance->supportsInvokeDefaultMethod())
269         return CallTypeNone;
270     
271     callData.native.function = callRuntimeObject;
272     return CallTypeHost;
273 }
274
275 static EncodedJSValue JSC_HOST_CALL callRuntimeConstructor(ExecState* exec)
276 {
277     JSObject* constructor = exec->callee();
278     ASSERT(constructor->inherits(&RuntimeObject::s_info));
279     RefPtr<Instance> instance(static_cast<RuntimeObject*>(exec->callee())->getInternalInstance());
280     instance->begin();
281     ArgList args(exec);
282     JSValue result = instance->invokeConstruct(exec, args);
283     instance->end();
284     
285     ASSERT(result);
286     return JSValue::encode(result.isObject() ? static_cast<JSObject*>(result.asCell()) : constructor);
287 }
288
289 ConstructType RuntimeObject::getConstructData(ConstructData& constructData)
290 {
291     if (!m_instance)
292         return ConstructTypeNone;
293     
294     RefPtr<Instance> instance = m_instance;
295     if (!instance->supportsConstruct())
296         return ConstructTypeNone;
297     
298     constructData.native.function = callRuntimeConstructor;
299     return ConstructTypeHost;
300 }
301
302 void RuntimeObject::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode)
303 {
304     if (!m_instance) {
305         throwInvalidAccessError(exec);
306         return;
307     }
308
309     RefPtr<Instance> instance = m_instance;
310     
311     instance->begin();
312     instance->getPropertyNames(exec, propertyNames);
313     instance->end();
314 }
315
316 JSObject* RuntimeObject::throwInvalidAccessError(ExecState* exec)
317 {
318     return throwError(exec, createReferenceError(exec, "Trying to access object from destroyed plug-in."));
319 }
320
321 }
322 }