JSGenericTypedArrayViewInlines.h [plain text]
#ifndef JSGenericTypedArrayViewInlines_h
#define JSGenericTypedArrayViewInlines_h
#include "ArrayBufferView.h"
#include "DeferGC.h"
#include "Error.h"
#include "ExceptionHelpers.h"
#include "JSGenericTypedArrayView.h"
#include "Reject.h"
#include "TypedArrays.h"
namespace JSC {
template<typename Adaptor>
JSGenericTypedArrayView<Adaptor>::JSGenericTypedArrayView(
VM& vm, ConstructionContext& context)
: Base(vm, context)
{
}
template<typename Adaptor>
JSGenericTypedArrayView<Adaptor>* JSGenericTypedArrayView<Adaptor>::create(
ExecState* exec, Structure* structure, unsigned length)
{
ConstructionContext context(exec->vm(), structure, length, sizeof(typename Adaptor::Type));
if (!context) {
exec->vm().throwException(exec, createOutOfMemoryError(exec));
return 0;
}
JSGenericTypedArrayView* result =
new (NotNull, allocateCell<JSGenericTypedArrayView>(exec->vm().heap))
JSGenericTypedArrayView(exec->vm(), context);
result->finishCreation(exec->vm());
return result;
}
template<typename Adaptor>
JSGenericTypedArrayView<Adaptor>* JSGenericTypedArrayView<Adaptor>::createUninitialized(
ExecState* exec, Structure* structure, unsigned length)
{
ConstructionContext context(
exec->vm(), structure, length, sizeof(typename Adaptor::Type),
ConstructionContext::DontInitialize);
if (!context) {
exec->vm().throwException(exec, createOutOfMemoryError(exec));
return 0;
}
JSGenericTypedArrayView* result =
new (NotNull, allocateCell<JSGenericTypedArrayView>(exec->vm().heap))
JSGenericTypedArrayView(exec->vm(), context);
result->finishCreation(exec->vm());
return result;
}
template<typename Adaptor>
JSGenericTypedArrayView<Adaptor>* JSGenericTypedArrayView<Adaptor>::create(
ExecState* exec, Structure* structure, PassRefPtr<ArrayBuffer> passedBuffer,
unsigned byteOffset, unsigned length)
{
RefPtr<ArrayBuffer> buffer = passedBuffer;
size_t size = sizeof(typename Adaptor::Type);
if (!ArrayBufferView::verifySubRangeLength(buffer, byteOffset, length, size)) {
exec->vm().throwException(exec, createRangeError(exec, "Length out of range of buffer"));
return nullptr;
}
if (!ArrayBufferView::verifyByteOffsetAlignment(byteOffset, size)) {
exec->vm().throwException(exec, createRangeError(exec, "Byte offset is not aligned"));
return nullptr;
}
ConstructionContext context(exec->vm(), structure, buffer, byteOffset, length);
ASSERT(context);
JSGenericTypedArrayView* result =
new (NotNull, allocateCell<JSGenericTypedArrayView>(exec->vm().heap))
JSGenericTypedArrayView(exec->vm(), context);
result->finishCreation(exec->vm());
return result;
}
template<typename Adaptor>
JSGenericTypedArrayView<Adaptor>* JSGenericTypedArrayView<Adaptor>::create(
VM& vm, Structure* structure, PassRefPtr<typename Adaptor::ViewType> impl)
{
RefPtr<ArrayBuffer> buffer = impl->buffer();
ConstructionContext context(vm, structure, buffer, impl->byteOffset(), impl->length());
ASSERT(context);
JSGenericTypedArrayView* result =
new (NotNull, allocateCell<JSGenericTypedArrayView>(vm.heap))
JSGenericTypedArrayView(vm, context);
result->finishCreation(vm);
return result;
}
template<typename Adaptor>
JSGenericTypedArrayView<Adaptor>* JSGenericTypedArrayView<Adaptor>::create(
Structure* structure, JSGlobalObject* globalObject,
PassRefPtr<typename Adaptor::ViewType> impl)
{
return create(globalObject->vm(), structure, impl);
}
template<typename Adaptor>
bool JSGenericTypedArrayView<Adaptor>::validateRange(
ExecState* exec, unsigned offset, unsigned length)
{
if (canAccessRangeQuickly(offset, length))
return true;
exec->vm().throwException(exec, createRangeError(exec, "Range consisting of offset and length are out of bounds"));
return false;
}
template<typename Adaptor>
template<typename OtherAdaptor>
bool JSGenericTypedArrayView<Adaptor>::setWithSpecificType(
ExecState* exec, JSGenericTypedArrayView<OtherAdaptor>* other,
unsigned offset, unsigned length)
{
length = std::min(length, other->length());
if (!validateRange(exec, offset, length))
return false;
if (other->length() != length) {
exec->vm().throwException(exec, createRangeError(exec, "Length of incoming array changed unexpectedly."));
return false;
}
ASSERT(static_cast<JSCell*>(this) != static_cast<JSCell*>(other));
unsigned otherElementSize = sizeof(typename OtherAdaptor::Type);
if (!hasArrayBuffer() || !other->hasArrayBuffer()
|| existingBuffer() != other->existingBuffer()
|| (elementSize == otherElementSize && vector() > other->vector())) {
for (unsigned i = length; i--;) {
setIndexQuicklyToNativeValue(
offset + i, OtherAdaptor::template convertTo<Adaptor>(
other->getIndexQuicklyAsNativeValue(i)));
}
return true;
}
if (elementSize == otherElementSize) {
for (unsigned i = 0; i < length; ++i) {
setIndexQuicklyToNativeValue(
offset + i, OtherAdaptor::template convertTo<Adaptor>(
other->getIndexQuicklyAsNativeValue(i)));
}
return true;
}
Vector<typename Adaptor::Type, 32> transferBuffer(length);
for (unsigned i = length; i--;) {
transferBuffer[i] = OtherAdaptor::template convertTo<Adaptor>(
other->getIndexQuicklyAsNativeValue(i));
}
for (unsigned i = length; i--;)
setIndexQuicklyToNativeValue(offset + i, transferBuffer[i]);
return true;
}
template<typename Adaptor>
bool JSGenericTypedArrayView<Adaptor>::set(
ExecState* exec, JSObject* object, unsigned offset, unsigned length)
{
const ClassInfo* ci = object->classInfo();
if (ci->typedArrayStorageType == Adaptor::typeValue) {
JSGenericTypedArrayView* other = jsCast<JSGenericTypedArrayView*>(object);
length = std::min(length, other->length());
if (!validateRange(exec, offset, length))
return false;
memmove(typedVector() + offset, other->typedVector(), other->byteLength());
return true;
}
switch (ci->typedArrayStorageType) {
case TypeInt8:
return setWithSpecificType<Int8Adaptor>(
exec, jsCast<JSInt8Array*>(object), offset, length);
case TypeInt16:
return setWithSpecificType<Int16Adaptor>(
exec, jsCast<JSInt16Array*>(object), offset, length);
case TypeInt32:
return setWithSpecificType<Int32Adaptor>(
exec, jsCast<JSInt32Array*>(object), offset, length);
case TypeUint8:
return setWithSpecificType<Uint8Adaptor>(
exec, jsCast<JSUint8Array*>(object), offset, length);
case TypeUint8Clamped:
return setWithSpecificType<Uint8ClampedAdaptor>(
exec, jsCast<JSUint8ClampedArray*>(object), offset, length);
case TypeUint16:
return setWithSpecificType<Uint16Adaptor>(
exec, jsCast<JSUint16Array*>(object), offset, length);
case TypeUint32:
return setWithSpecificType<Uint32Adaptor>(
exec, jsCast<JSUint32Array*>(object), offset, length);
case TypeFloat32:
return setWithSpecificType<Float32Adaptor>(
exec, jsCast<JSFloat32Array*>(object), offset, length);
case TypeFloat64:
return setWithSpecificType<Float64Adaptor>(
exec, jsCast<JSFloat64Array*>(object), offset, length);
case NotTypedArray:
case TypeDataView: {
if (!validateRange(exec, offset, length))
return false;
for (unsigned i = 0; i < length; ++i) {
JSValue value = object->get(exec, i);
if (!setIndex(exec, offset + i, value))
return false;
}
return true;
} }
RELEASE_ASSERT_NOT_REACHED();
return false;
}
template<typename Adaptor>
ArrayBuffer* JSGenericTypedArrayView<Adaptor>::existingBuffer()
{
return existingBufferInButterfly();
}
template<typename Adaptor>
bool JSGenericTypedArrayView<Adaptor>::getOwnPropertySlot(
JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
if (propertyName == exec->propertyNames().length) {
slot.setValue(thisObject, DontDelete | ReadOnly, jsNumber(thisObject->length()));
return true;
}
if (propertyName == exec->propertyNames().byteLength) {
slot.setValue(thisObject, DontDelete | ReadOnly, jsNumber(thisObject->byteLength()));
return true;
}
Optional<uint32_t> index = parseIndex(propertyName);
if (index && thisObject->canGetIndexQuickly(index.value())) {
slot.setValue(thisObject, DontDelete | ReadOnly, thisObject->getIndexQuickly(index.value()));
return true;
}
return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
}
template<typename Adaptor>
void JSGenericTypedArrayView<Adaptor>::put(
JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value,
PutPropertySlot& slot)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(cell);
if (propertyName == exec->propertyNames().length) {
return;
}
if (Optional<uint32_t> index = parseIndex(propertyName)) {
putByIndex(thisObject, exec, index.value(), value, slot.isStrictMode());
return;
}
Base::put(thisObject, exec, propertyName, value, slot);
}
template<typename Adaptor>
bool JSGenericTypedArrayView<Adaptor>::defineOwnProperty(
JSObject* object, ExecState* exec, PropertyName propertyName,
const PropertyDescriptor& descriptor, bool shouldThrow)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
if (propertyName == exec->propertyNames().length || parseIndex(propertyName))
return reject(exec, shouldThrow, "Attempting to write to a read-only typed array property.");
return Base::defineOwnProperty(thisObject, exec, propertyName, descriptor, shouldThrow);
}
template<typename Adaptor>
bool JSGenericTypedArrayView<Adaptor>::deleteProperty(
JSCell* cell, ExecState* exec, PropertyName propertyName)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(cell);
if (propertyName == exec->propertyNames().length || parseIndex(propertyName))
return false;
return Base::deleteProperty(thisObject, exec, propertyName);
}
template<typename Adaptor>
bool JSGenericTypedArrayView<Adaptor>::getOwnPropertySlotByIndex(
JSObject* object, ExecState* exec, unsigned propertyName, PropertySlot& slot)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
if (propertyName > MAX_ARRAY_INDEX) {
return thisObject->methodTable()->getOwnPropertySlot(
thisObject, exec, Identifier::from(exec, propertyName), slot);
}
if (!thisObject->canGetIndexQuickly(propertyName))
return false;
slot.setValue(thisObject, None, thisObject->getIndexQuickly(propertyName));
return true;
}
template<typename Adaptor>
void JSGenericTypedArrayView<Adaptor>::putByIndex(
JSCell* cell, ExecState* exec, unsigned propertyName, JSValue value, bool shouldThrow)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(cell);
if (propertyName > MAX_ARRAY_INDEX) {
PutPropertySlot slot(JSValue(thisObject), shouldThrow);
thisObject->methodTable()->put(
thisObject, exec, Identifier::from(exec, propertyName), value, slot);
return;
}
thisObject->setIndex(exec, propertyName, value);
}
template<typename Adaptor>
bool JSGenericTypedArrayView<Adaptor>::deletePropertyByIndex(
JSCell* cell, ExecState* exec, unsigned propertyName)
{
if (propertyName > MAX_ARRAY_INDEX) {
return cell->methodTable()->deleteProperty(
cell, exec, Identifier::from(exec, propertyName));
}
return false;
}
template<typename Adaptor>
void JSGenericTypedArrayView<Adaptor>::getOwnNonIndexPropertyNames(
JSObject* object, ExecState* exec, PropertyNameArray& array, EnumerationMode mode)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
if (mode.includeDontEnumProperties())
array.add(exec->propertyNames().length);
Base::getOwnNonIndexPropertyNames(thisObject, exec, array, mode);
}
template<typename Adaptor>
void JSGenericTypedArrayView<Adaptor>::getOwnPropertyNames(
JSObject* object, ExecState* exec, PropertyNameArray& array, EnumerationMode mode)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
for (unsigned i = 0; i < thisObject->m_length; ++i)
array.add(Identifier::from(exec, i));
return Base::getOwnPropertyNames(object, exec, array, mode);
}
template<typename Adaptor>
void JSGenericTypedArrayView<Adaptor>::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(cell);
switch (thisObject->m_mode) {
case FastTypedArray: {
if (thisObject->m_vector)
visitor.copyLater(thisObject, TypedArrayVectorCopyToken, thisObject->m_vector, thisObject->byteSize());
break;
}
case OversizeTypedArray: {
visitor.reportExtraMemoryVisited(thisObject, thisObject->byteSize());
break;
}
case WastefulTypedArray:
break;
case DataViewMode:
RELEASE_ASSERT_NOT_REACHED();
break;
}
Base::visitChildren(thisObject, visitor);
}
template<typename Adaptor>
void JSGenericTypedArrayView<Adaptor>::copyBackingStore(
JSCell* cell, CopyVisitor& visitor, CopyToken token)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(cell);
if (token == TypedArrayVectorCopyToken
&& visitor.checkIfShouldCopy(thisObject->m_vector)) {
ASSERT(thisObject->m_vector);
void* oldVector = thisObject->m_vector;
void* newVector = visitor.allocateNewSpace(thisObject->byteSize());
memcpy(newVector, oldVector, thisObject->byteSize());
thisObject->m_vector = newVector;
visitor.didCopy(oldVector, thisObject->byteSize());
}
Base::copyBackingStore(thisObject, visitor, token);
}
template<typename Adaptor>
ArrayBuffer* JSGenericTypedArrayView<Adaptor>::slowDownAndWasteMemory(JSArrayBufferView* object)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
Heap* heap = Heap::heap(thisObject);
DeferGCForAWhile deferGC(*heap);
ASSERT(!thisObject->hasIndexingHeader());
size_t size = thisObject->byteSize();
if (thisObject->m_mode == FastTypedArray
&& !thisObject->butterfly() && size >= sizeof(IndexingHeader)) {
ASSERT(thisObject->m_vector);
thisObject->m_butterfly.setWithoutWriteBarrier(
static_cast<IndexingHeader*>(thisObject->m_vector)->butterfly());
} else {
VM& vm = *heap->vm();
thisObject->m_butterfly.set(vm, thisObject, Butterfly::createOrGrowArrayRight(
thisObject->butterfly(), vm, thisObject, thisObject->structure(),
thisObject->structure()->outOfLineCapacity(), false, 0, 0));
}
RefPtr<ArrayBuffer> buffer;
switch (thisObject->m_mode) {
case FastTypedArray:
buffer = ArrayBuffer::create(thisObject->m_vector, thisObject->byteLength());
break;
case OversizeTypedArray:
buffer = ArrayBuffer::createAdopted(thisObject->m_vector, thisObject->byteLength());
break;
default:
RELEASE_ASSERT_NOT_REACHED();
break;
}
thisObject->butterfly()->indexingHeader()->setArrayBuffer(buffer.get());
thisObject->m_vector = buffer->data();
thisObject->m_mode = WastefulTypedArray;
heap->addReference(thisObject, buffer.get());
return buffer.get();
}
template<typename Adaptor>
PassRefPtr<ArrayBufferView>
JSGenericTypedArrayView<Adaptor>::getTypedArrayImpl(JSArrayBufferView* object)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
return thisObject->typedImpl();
}
}
#endif // JSGenericTypedArrayViewInlines_h