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", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(RuntimeObject) };
40
RuntimeObject::RuntimeObject(ExecState*, JSGlobalObject* globalObject, Structure* structure, PassRefPtr<Instance> instance)
41
: JSDestructibleObject(globalObject->globalData(), structure)
42
, m_instance(instance)
46
void RuntimeObject::finishCreation(JSGlobalObject* globalObject)
48
Base::finishCreation(globalObject->globalData());
49
ASSERT(inherits(&s_info));
52
void RuntimeObject::destroy(JSCell* cell)
54
static_cast<RuntimeObject*>(cell)->RuntimeObject::~RuntimeObject();
57
void RuntimeObject::invalidate()
61
m_instance->willInvalidateRuntimeObject();
65
JSValue RuntimeObject::fallbackObjectGetter(ExecState* exec, JSValue slotBase, PropertyName 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, PropertyName 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, PropertyName 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(JSCell* cell, ExecState *exec, PropertyName propertyName, PropertySlot& slot)
121
RuntimeObject* thisObject = jsCast<RuntimeObject*>(cell);
122
if (!thisObject->m_instance) {
123
throwInvalidAccessError(exec);
127
RefPtr<Instance> instance = thisObject->m_instance;
131
Class *aClass = instance->getClass();
134
// See if the instance has a field with the specified name.
135
Field *aField = aClass->fieldNamed(propertyName, instance.get());
137
slot.setCustom(thisObject, thisObject->fieldGetter);
141
// Now check if a method with specified name exists, if so return a function object for
143
if (aClass->methodNamed(propertyName, instance.get())) {
144
slot.setCustom(thisObject, thisObject->methodGetter);
151
// Try a fallback object.
152
if (!aClass->fallbackObject(exec, instance.get(), propertyName).isUndefined()) {
153
slot.setCustom(thisObject, thisObject->fallbackObjectGetter);
161
return instance->getOwnPropertySlot(thisObject, exec, propertyName, slot);
164
bool RuntimeObject::getOwnPropertyDescriptor(JSObject* object, ExecState *exec, PropertyName propertyName, PropertyDescriptor& descriptor)
166
RuntimeObject* thisObject = jsCast<RuntimeObject*>(object);
167
if (!thisObject->m_instance) {
168
throwInvalidAccessError(exec);
172
RefPtr<Instance> instance = thisObject->m_instance;
175
Class *aClass = instance->getClass();
178
// See if the instance has a field with the specified name.
179
Field *aField = aClass->fieldNamed(propertyName, instance.get());
182
slot.setCustom(thisObject, fieldGetter);
184
descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete);
187
// Now check if a method with specified name exists, if so return a function object for
189
if (aClass->methodNamed(propertyName, instance.get())) {
191
slot.setCustom(thisObject, 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(thisObject, fallbackObjectGetter);
203
descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete | ReadOnly | DontEnum);
210
return instance->getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
213
void RuntimeObject::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
215
RuntimeObject* thisObject = jsCast<RuntimeObject*>(cell);
216
if (!thisObject->m_instance) {
217
throwInvalidAccessError(exec);
221
RefPtr<Instance> instance = thisObject->m_instance;
224
// Set the value of the property.
225
Field *aField = instance->getClass()->fieldNamed(propertyName, instance.get());
227
aField->setValueToInstance(exec, instance.get(), value);
228
else if (!instance->setValueOfUndefinedField(exec, propertyName, value))
229
instance->put(thisObject, exec, propertyName, value, slot);
234
bool RuntimeObject::deleteProperty(JSCell*, ExecState*, PropertyName)
236
// Can never remove a property of a RuntimeObject.
240
JSValue RuntimeObject::defaultValue(const JSObject* object, ExecState* exec, PreferredPrimitiveType hint)
242
const RuntimeObject* thisObject = jsCast<const RuntimeObject*>(object);
243
if (!thisObject->m_instance)
244
return throwInvalidAccessError(exec);
246
RefPtr<Instance> instance = thisObject->m_instance;
249
JSValue result = instance->defaultValue(exec, hint);
254
static EncodedJSValue JSC_HOST_CALL callRuntimeObject(ExecState* exec)
256
ASSERT(exec->callee()->inherits(&RuntimeObject::s_info));
257
RefPtr<Instance> instance(static_cast<RuntimeObject*>(exec->callee())->getInternalInstance());
259
JSValue result = instance->invokeDefaultMethod(exec);
261
return JSValue::encode(result);
264
CallType RuntimeObject::getCallData(JSCell* cell, CallData& callData)
266
RuntimeObject* thisObject = jsCast<RuntimeObject*>(cell);
267
if (!thisObject->m_instance)
270
RefPtr<Instance> instance = thisObject->m_instance;
271
if (!instance->supportsInvokeDefaultMethod())
274
callData.native.function = callRuntimeObject;
278
static EncodedJSValue JSC_HOST_CALL callRuntimeConstructor(ExecState* exec)
280
JSObject* constructor = exec->callee();
281
ASSERT(constructor->inherits(&RuntimeObject::s_info));
282
RefPtr<Instance> instance(static_cast<RuntimeObject*>(exec->callee())->getInternalInstance());
285
JSValue result = instance->invokeConstruct(exec, args);
289
return JSValue::encode(result.isObject() ? jsCast<JSObject*>(result.asCell()) : constructor);
292
ConstructType RuntimeObject::getConstructData(JSCell* cell, ConstructData& constructData)
294
RuntimeObject* thisObject = jsCast<RuntimeObject*>(cell);
295
if (!thisObject->m_instance)
296
return ConstructTypeNone;
298
RefPtr<Instance> instance = thisObject->m_instance;
299
if (!instance->supportsConstruct())
300
return ConstructTypeNone;
302
constructData.native.function = callRuntimeConstructor;
303
return ConstructTypeHost;
306
void RuntimeObject::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode)
308
RuntimeObject* thisObject = jsCast<RuntimeObject*>(object);
309
if (!thisObject->m_instance) {
310
throwInvalidAccessError(exec);
314
RefPtr<Instance> instance = thisObject->m_instance;
317
instance->getPropertyNames(exec, propertyNames);
321
JSObject* RuntimeObject::throwInvalidAccessError(ExecState* exec)
323
return throwError(exec, createReferenceError(exec, "Trying to access object from destroyed plug-in."));