2 * Copyright (C) 2003, 2008, 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "runtime_object.h"
29 #include "JSDOMBinding.h"
30 #include "runtime_method.h"
31 #include <runtime/Error.h>
33 using namespace WebCore;
38 const ClassInfo RuntimeObject::s_info = { "RuntimeObject", &JSNonFinalObject::s_info, 0, 0 };
40 RuntimeObject::RuntimeObject(ExecState*, JSGlobalObject* globalObject, Structure* structure, PassRefPtr<Instance> instance)
41 : JSNonFinalObject(globalObject->globalData(), structure)
42 , m_instance(instance)
46 void RuntimeObject::finishCreation(JSGlobalObject* globalObject)
48 Base::finishCreation(globalObject->globalData());
49 ASSERT(inherits(&s_info));
52 RuntimeObject::~RuntimeObject()
57 void RuntimeObject::invalidate()
61 m_instance->willInvalidateRuntimeObject();
65 JSValue RuntimeObject::fallbackObjectGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
67 RuntimeObject* thisObj = static_cast<RuntimeObject*>(asObject(slotBase));
68 RefPtr<Instance> instance = thisObj->m_instance;
71 return throwInvalidAccessError(exec);
75 Class *aClass = instance->getClass();
76 JSValue result = aClass->fallbackObject(exec, instance.get(), propertyName);
83 JSValue RuntimeObject::fieldGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
85 RuntimeObject* thisObj = static_cast<RuntimeObject*>(asObject(slotBase));
86 RefPtr<Instance> instance = thisObj->m_instance;
89 return throwInvalidAccessError(exec);
93 Class *aClass = instance->getClass();
94 Field* aField = aClass->fieldNamed(propertyName, instance.get());
95 JSValue result = aField->valueFromInstance(exec, instance.get());
102 JSValue RuntimeObject::methodGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
104 RuntimeObject* thisObj = static_cast<RuntimeObject*>(asObject(slotBase));
105 RefPtr<Instance> instance = thisObj->m_instance;
108 return throwInvalidAccessError(exec);
112 JSValue method = instance->getMethod(exec, propertyName);
119 bool RuntimeObject::getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot)
122 throwInvalidAccessError(exec);
126 RefPtr<Instance> instance = m_instance;
130 Class *aClass = instance->getClass();
133 // See if the instance has a field with the specified name.
134 Field *aField = aClass->fieldNamed(propertyName, instance.get());
136 slot.setCustom(this, fieldGetter);
140 // Now check if a method with specified name exists, if so return a function object for
142 MethodList methodList = aClass->methodsNamed(propertyName, instance.get());
143 if (methodList.size() > 0) {
144 slot.setCustom(this, methodGetter);
151 // Try a fallback object.
152 if (!aClass->fallbackObject(exec, instance.get(), propertyName).isUndefined()) {
153 slot.setCustom(this, fallbackObjectGetter);
161 return instance->getOwnPropertySlot(this, exec, propertyName, slot);
164 bool RuntimeObject::getOwnPropertyDescriptor(ExecState *exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
167 throwInvalidAccessError(exec);
171 RefPtr<Instance> instance = m_instance;
174 Class *aClass = instance->getClass();
177 // See if the instance has a field with the specified name.
178 Field *aField = aClass->fieldNamed(propertyName, instance.get());
181 slot.setCustom(this, fieldGetter);
183 descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete);
186 // Now check if a method with specified name exists, if so return a function object for
188 MethodList methodList = aClass->methodsNamed(propertyName, instance.get());
189 if (methodList.size() > 0) {
191 slot.setCustom(this, methodGetter);
193 descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete | ReadOnly);
198 // Try a fallback object.
199 if (!aClass->fallbackObject(exec, instance.get(), propertyName).isUndefined()) {
201 slot.setCustom(this, fallbackObjectGetter);
203 descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete | ReadOnly | DontEnum);
210 return instance->getOwnPropertyDescriptor(this, exec, propertyName, descriptor);
213 void RuntimeObject::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
216 throwInvalidAccessError(exec);
220 RefPtr<Instance> instance = m_instance;
223 // Set the value of the property.
224 Field *aField = instance->getClass()->fieldNamed(propertyName, instance.get());
226 aField->setValueToInstance(exec, instance.get(), value);
227 else if (!instance->setValueOfUndefinedField(exec, propertyName, value))
228 instance->put(this, exec, propertyName, value, slot);
233 bool RuntimeObject::deleteProperty(ExecState*, const Identifier&)
235 // Can never remove a property of a RuntimeObject.
239 JSValue RuntimeObject::defaultValue(ExecState* exec, PreferredPrimitiveType hint) const
242 return throwInvalidAccessError(exec);
244 RefPtr<Instance> instance = m_instance;
247 JSValue result = instance->defaultValue(exec, hint);
252 static EncodedJSValue JSC_HOST_CALL callRuntimeObject(ExecState* exec)
254 ASSERT(exec->callee()->inherits(&RuntimeObject::s_info));
255 RefPtr<Instance> instance(static_cast<RuntimeObject*>(exec->callee())->getInternalInstance());
257 JSValue result = instance->invokeDefaultMethod(exec);
259 return JSValue::encode(result);
262 CallType RuntimeObject::getCallData(CallData& callData)
267 RefPtr<Instance> instance = m_instance;
268 if (!instance->supportsInvokeDefaultMethod())
271 callData.native.function = callRuntimeObject;
275 static EncodedJSValue JSC_HOST_CALL callRuntimeConstructor(ExecState* exec)
277 JSObject* constructor = exec->callee();
278 ASSERT(constructor->inherits(&RuntimeObject::s_info));
279 RefPtr<Instance> instance(static_cast<RuntimeObject*>(exec->callee())->getInternalInstance());
282 JSValue result = instance->invokeConstruct(exec, args);
286 return JSValue::encode(result.isObject() ? static_cast<JSObject*>(result.asCell()) : constructor);
289 ConstructType RuntimeObject::getConstructData(ConstructData& constructData)
292 return ConstructTypeNone;
294 RefPtr<Instance> instance = m_instance;
295 if (!instance->supportsConstruct())
296 return ConstructTypeNone;
298 constructData.native.function = callRuntimeConstructor;
299 return ConstructTypeHost;
302 void RuntimeObject::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode)
305 throwInvalidAccessError(exec);
309 RefPtr<Instance> instance = m_instance;
312 instance->getPropertyNames(exec, propertyNames);
316 JSObject* RuntimeObject::throwInvalidAccessError(ExecState* exec)
318 return throwError(exec, createReferenceError(exec, "Trying to access object from destroyed plug-in."));