#include "config.h"
#include "runtime_array.h"
#include "JSGlobalObject.h"
#include "array_object.h"
using namespace KJS;
const ClassInfo RuntimeArray::info = { "RuntimeArray", &ArrayInstance::info, 0 };
RuntimeArray::RuntimeArray(ExecState *exec, Bindings::Array *a)
: JSObject(exec->lexicalGlobalObject()->arrayPrototype())
, _array(a)
{
}
JSValue *RuntimeArray::lengthGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot)
{
RuntimeArray *thisObj = static_cast<RuntimeArray *>(slot.slotBase());
return jsNumber(thisObj->getLength());
}
JSValue *RuntimeArray::indexGetter(ExecState* exec, JSObject*, const Identifier&, const PropertySlot& slot)
{
RuntimeArray *thisObj = static_cast<RuntimeArray *>(slot.slotBase());
return thisObj->getConcreteArray()->valueAt(exec, slot.index());
}
bool RuntimeArray::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
{
if (propertyName == exec->propertyNames().length) {
slot.setCustom(this, lengthGetter);
return true;
}
bool ok;
unsigned index = propertyName.toArrayIndex(&ok);
if (ok) {
if (index < getLength()) {
slot.setCustomIndex(this, index, indexGetter);
return true;
}
}
return JSObject::getOwnPropertySlot(exec, propertyName, slot);
}
bool RuntimeArray::getOwnPropertySlot(ExecState *exec, unsigned index, PropertySlot& slot)
{
if (index < getLength()) {
slot.setCustomIndex(this, index, indexGetter);
return true;
}
return JSObject::getOwnPropertySlot(exec, index, slot);
}
void RuntimeArray::put(ExecState* exec, const Identifier& propertyName, JSValue* value, int attr)
{
if (propertyName == exec->propertyNames().length) {
throwError(exec, RangeError);
return;
}
bool ok;
unsigned index = propertyName.toArrayIndex(&ok);
if (ok) {
getConcreteArray()->setValueAt(exec, index, value);
return;
}
JSObject::put(exec, propertyName, value, attr);
}
void RuntimeArray::put(ExecState* exec, unsigned index, JSValue* value, int)
{
if (index >= getLength()) {
throwError(exec, RangeError);
return;
}
getConcreteArray()->setValueAt(exec, index, value);
}
bool RuntimeArray::deleteProperty(ExecState*, const Identifier&)
{
return false;
}
bool RuntimeArray::deleteProperty(ExecState*, unsigned)
{
return false;
}