runtime_method.cpp [plain text]
#include "config.h"
#include "runtime_method.h"
#include "ExecState.h"
#include "JSGlobalObject.h"
#include "runtime_object.h"
#include "function_object.h"
using namespace KJS::Bindings;
using namespace KJS;
RuntimeMethod::RuntimeMethod(ExecState *exec, const Identifier &ident, Bindings::MethodList &m)
: InternalFunctionImp(exec->lexicalGlobalObject()->functionPrototype(), ident)
, _methodList(new MethodList(m))
{
}
JSValue *RuntimeMethod::lengthGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot)
{
RuntimeMethod *thisObj = static_cast<RuntimeMethod *>(slot.slotBase());
return jsNumber(thisObj->_methodList->at(0)->numParameters());
}
bool RuntimeMethod::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
{
if (propertyName == exec->propertyNames().length) {
slot.setCustom(this, lengthGetter);
return true;
}
return InternalFunctionImp::getOwnPropertySlot(exec, propertyName, slot);
}
JSValue *RuntimeMethod::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args)
{
if (_methodList->isEmpty())
return jsUndefined();
RuntimeObjectImp *imp = 0;
if (thisObj->classInfo() == &KJS::RuntimeObjectImp::info) {
imp = static_cast<RuntimeObjectImp*>(thisObj);
} else {
JSValue* value = thisObj->get(exec, "__apple_runtime_object");
if (value->isObject(&KJS::RuntimeObjectImp::info))
imp = static_cast<RuntimeObjectImp*>(value);
}
if (!imp)
return throwError(exec, TypeError);
RefPtr<Instance> instance = imp->getInternalInstance();
if (!instance)
return RuntimeObjectImp::throwInvalidAccessError(exec);
instance->begin();
JSValue *aValue = instance->invokeMethod(exec, *_methodList, args);
instance->end();
return aValue;
}