#ifndef JSByteArray_h
#define JSByteArray_h
#include "JSObject.h"
#include <wtf/ByteArray.h>
namespace JSC {
class JSByteArray : public JSNonFinalObject {
friend class JSGlobalData;
public:
typedef JSNonFinalObject Base;
bool canAccessIndex(unsigned i) { return i < m_storage->length(); }
JSValue getIndex(ExecState*, unsigned i)
{
ASSERT(canAccessIndex(i));
return jsNumber(m_storage->data()[i]);
}
void setIndex(unsigned i, int value)
{
ASSERT(canAccessIndex(i));
if (value & ~0xFF) {
if (value < 0)
value = 0;
else
value = 255;
}
m_storage->data()[i] = static_cast<unsigned char>(value);
}
void setIndex(unsigned i, double value)
{
ASSERT(canAccessIndex(i));
if (!(value > 0)) value = 0;
else if (value > 255)
value = 255;
m_storage->data()[i] = static_cast<unsigned char>(value + 0.5);
}
void setIndex(ExecState* exec, unsigned i, JSValue value)
{
double byteValue = value.toNumber(exec);
if (exec->hadException())
return;
if (canAccessIndex(i))
setIndex(i, byteValue);
}
JSByteArray(ExecState*, Structure*, WTF::ByteArray* storage);
static Structure* createStructure(JSGlobalData&, JSValue prototype, const JSC::ClassInfo* = &s_defaultInfo);
virtual bool getOwnPropertySlot(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertySlot&);
virtual bool getOwnPropertySlot(JSC::ExecState*, unsigned propertyName, JSC::PropertySlot&);
virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
virtual void put(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSValue, JSC::PutPropertySlot&);
virtual void put(JSC::ExecState*, unsigned propertyName, JSC::JSValue);
virtual void getOwnPropertyNames(JSC::ExecState*, JSC::PropertyNameArray&, EnumerationMode mode = ExcludeDontEnumProperties);
static const ClassInfo s_defaultInfo;
size_t length() const { return m_storage->length(); }
WTF::ByteArray* storage() const { return m_storage.get(); }
#if !ASSERT_DISABLED
virtual ~JSByteArray();
#endif
protected:
static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesGetPropertyNames | JSObject::StructureFlags;
private:
JSByteArray(VPtrStealingHackType)
: JSNonFinalObject(VPtrStealingHack)
{
}
RefPtr<WTF::ByteArray> m_storage;
};
JSByteArray* asByteArray(JSValue value);
inline JSByteArray* asByteArray(JSValue value)
{
return static_cast<JSByteArray*>(value.asCell());
}
inline bool isJSByteArray(JSGlobalData* globalData, JSValue v) { return v.isCell() && v.asCell()->vptr() == globalData->jsByteArrayVPtr; }
}
#endif // JSByteArray_h