GenericTypedArrayView.h [plain text]
#pragma once
#include "ArrayBuffer.h"
#include "ArrayBufferView.h"
namespace JSC {
template<typename Adaptor>
class GenericTypedArrayView final : public ArrayBufferView {
protected:
GenericTypedArrayView(RefPtr<ArrayBuffer>&&, unsigned byteOffset, unsigned length);
public:
static RefPtr<GenericTypedArrayView> create(unsigned length);
static RefPtr<GenericTypedArrayView> create(const typename Adaptor::Type* array, unsigned length);
static RefPtr<GenericTypedArrayView> create(RefPtr<ArrayBuffer>&&, unsigned byteOffset, unsigned length);
static RefPtr<GenericTypedArrayView> createUninitialized(unsigned length);
typename Adaptor::Type* data() const { return static_cast<typename Adaptor::Type*>(baseAddress()); }
bool set(GenericTypedArrayView<Adaptor>* array, unsigned offset)
{
return setImpl(array, offset * sizeof(typename Adaptor::Type));
}
bool setRange(const typename Adaptor::Type* data, size_t count, unsigned offset)
{
return setRangeImpl(
reinterpret_cast<const char*>(data),
count * sizeof(typename Adaptor::Type),
offset * sizeof(typename Adaptor::Type),
internalByteLength());
}
bool zeroRange(unsigned offset, size_t count)
{
return zeroRangeImpl(offset * sizeof(typename Adaptor::Type), count * sizeof(typename Adaptor::Type));
}
void zeroFill() { zeroRange(0, length()); }
unsigned length() const
{
if (isNeutered())
return 0;
return m_length;
}
unsigned byteLength() const override
{
return internalByteLength();
}
typename Adaptor::Type item(unsigned index) const
{
ASSERT_WITH_SECURITY_IMPLICATION(index < this->length());
return data()[index];
}
void set(unsigned index, double value) const
{
ASSERT_WITH_SECURITY_IMPLICATION(index < this->length());
data()[index] = Adaptor::toNativeFromDouble(value);
}
void setNative(unsigned index, typename Adaptor::Type value) const
{
ASSERT_WITH_SECURITY_IMPLICATION(index < this->length());
data()[index] = value;
}
bool getRange(typename Adaptor::Type* data, size_t count, unsigned offset)
{
return getRangeImpl(
reinterpret_cast<char*>(data),
count * sizeof(typename Adaptor::Type),
offset * sizeof(typename Adaptor::Type),
internalByteLength());
}
bool checkInboundData(unsigned offset, size_t count) const
{
unsigned length = this->length();
return (offset <= length
&& offset + count <= length
&& offset + count >= offset);
}
RefPtr<GenericTypedArrayView> subarray(int start) const;
RefPtr<GenericTypedArrayView> subarray(int start, int end) const;
TypedArrayType getType() const override
{
return Adaptor::typeValue;
}
JSArrayBufferView* wrap(ExecState*, JSGlobalObject*) override;
private:
unsigned internalByteLength() const
{
return length() * sizeof(typename Adaptor::Type);
}
unsigned m_length;
};
}