runtime_object.cpp [plain text]
#include "config.h"
#include "runtime_object.h"
#include "error_object.h"
#include "operations.h"
#include "runtime_method.h"
#include "runtime_root.h"
using namespace KJS;
using namespace Bindings;
const ClassInfo RuntimeObjectImp::info = { "RuntimeObject", 0, 0 };
RuntimeObjectImp::RuntimeObjectImp(Bindings::Instance *i)
: instance(i)
{
instance->rootObject()->addRuntimeObject(this);
}
RuntimeObjectImp::~RuntimeObjectImp()
{
if (instance)
instance->rootObject()->removeRuntimeObject(this);
}
void RuntimeObjectImp::invalidate()
{
ASSERT(instance);
instance = 0;
}
JSValue *RuntimeObjectImp::fallbackObjectGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot)
{
RuntimeObjectImp *thisObj = static_cast<RuntimeObjectImp *>(slot.slotBase());
RefPtr<Bindings::Instance> instance = thisObj->instance;
if (!instance)
return throwInvalidAccessError(exec);
instance->begin();
Class *aClass = instance->getClass();
JSValue* result = aClass->fallbackObject(exec, instance.get(), propertyName);
instance->end();
return result;
}
JSValue *RuntimeObjectImp::fieldGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot)
{
RuntimeObjectImp *thisObj = static_cast<RuntimeObjectImp *>(slot.slotBase());
RefPtr<Bindings::Instance> instance = thisObj->instance;
if (!instance)
return throwInvalidAccessError(exec);
instance->begin();
Class *aClass = instance->getClass();
Field* aField = aClass->fieldNamed(propertyName, instance.get());
JSValue *result = instance->getValueOfField(exec, aField);
instance->end();
return result;
}
JSValue *RuntimeObjectImp::methodGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot)
{
RuntimeObjectImp *thisObj = static_cast<RuntimeObjectImp *>(slot.slotBase());
RefPtr<Bindings::Instance> instance = thisObj->instance;
if (!instance)
return throwInvalidAccessError(exec);
instance->begin();
Class *aClass = instance->getClass();
MethodList methodList = aClass->methodsNamed(propertyName, instance.get());
JSValue *result = new RuntimeMethod(exec, propertyName, methodList);
instance->end();
return result;
}
bool RuntimeObjectImp::getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot)
{
if (!instance) {
throwInvalidAccessError(exec);
return false;
}
instance->begin();
Class *aClass = instance->getClass();
if (aClass) {
Field *aField = aClass->fieldNamed(propertyName, instance.get());
if (aField) {
slot.setCustom(this, fieldGetter);
instance->end();
return true;
} else {
MethodList methodList = aClass->methodsNamed(propertyName, instance.get());
if (methodList.size() > 0) {
slot.setCustom(this, methodGetter);
instance->end();
return true;
}
}
if (!aClass->fallbackObject(exec, instance.get(), propertyName)->isUndefined()) {
slot.setCustom(this, fallbackObjectGetter);
instance->end();
return true;
}
}
instance->end();
return false;
}
void RuntimeObjectImp::put(ExecState* exec, const Identifier& propertyName, JSValue* value, int)
{
if (!instance) {
throwInvalidAccessError(exec);
return;
}
RefPtr<Bindings::Instance> protector(instance);
instance->begin();
Field *aField = instance->getClass()->fieldNamed(propertyName, instance.get());
if (aField)
instance->setValueOfField(exec, aField, value);
else if (instance->supportsSetValueOfUndefinedField())
instance->setValueOfUndefinedField(exec, propertyName, value);
instance->end();
}
bool RuntimeObjectImp::canPut(ExecState* exec, const Identifier& propertyName) const
{
if (!instance) {
throwInvalidAccessError(exec);
return false;
}
instance->begin();
Field *aField = instance->getClass()->fieldNamed(propertyName, instance.get());
instance->end();
return !!aField;
}
bool RuntimeObjectImp::deleteProperty(ExecState*, const Identifier&)
{
return false;
}
JSValue *RuntimeObjectImp::defaultValue(ExecState* exec, JSType hint) const
{
if (!instance)
return throwInvalidAccessError(exec);
JSValue *result;
RefPtr<Bindings::Instance> protector(instance);
instance->begin();
result = instance->defaultValue(hint);
instance->end();
return result;
}
bool RuntimeObjectImp::implementsCall() const
{
if (!instance)
return false;
return instance->implementsCall();
}
JSValue *RuntimeObjectImp::callAsFunction(ExecState* exec, JSObject*, const List& args)
{
if (!instance)
return throwInvalidAccessError(exec);
RefPtr<Bindings::Instance> protector(instance);
instance->begin();
JSValue *aValue = instance->invokeDefaultMethod(exec, args);
instance->end();
return aValue;
}
void RuntimeObjectImp::getPropertyNames(ExecState* exec, PropertyNameArray& propertyNames)
{
if (!instance) {
throwInvalidAccessError(exec);
return;
}
instance->begin();
instance->getPropertyNames(exec, propertyNames);
instance->end();
}
JSObject* RuntimeObjectImp::throwInvalidAccessError(ExecState* exec)
{
return throwError(exec, ReferenceError, "Trying to access object from destroyed plug-in.");
}