JSGenericTypedArrayViewInlines.h [plain text]
#pragma once
#include "ArrayBufferView.h"
#include "DeferGC.h"
#include "Error.h"
#include "ExceptionHelpers.h"
#include "JSArrayBuffer.h"
#include "JSGenericTypedArrayView.h"
#include "TypeError.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)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ConstructionContext context(vm, structure, length, sizeof(typename Adaptor::Type));
if (!context) {
throwOutOfMemoryError(exec, scope);
return nullptr;
}
JSGenericTypedArrayView* result =
new (NotNull, allocateCell<JSGenericTypedArrayView>(vm.heap))
JSGenericTypedArrayView(vm, context);
result->finishCreation(vm);
return result;
}
template<typename Adaptor>
JSGenericTypedArrayView<Adaptor>* JSGenericTypedArrayView<Adaptor>::createWithFastVector(
ExecState* exec, Structure* structure, unsigned length, void* vector)
{
VM& vm = exec->vm();
ConstructionContext context(structure, length, vector);
RELEASE_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>::createUninitialized(
ExecState* exec, Structure* structure, unsigned length)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ConstructionContext context(
vm, structure, length, sizeof(typename Adaptor::Type),
ConstructionContext::DontInitialize);
if (!context) {
throwOutOfMemoryError(exec, scope);
return nullptr;
}
JSGenericTypedArrayView* result =
new (NotNull, allocateCell<JSGenericTypedArrayView>(vm.heap))
JSGenericTypedArrayView(vm, context);
result->finishCreation(vm);
return result;
}
template<typename Adaptor>
JSGenericTypedArrayView<Adaptor>* JSGenericTypedArrayView<Adaptor>::create(
ExecState* exec, Structure* structure, RefPtr<ArrayBuffer>&& buffer,
unsigned byteOffset, unsigned length)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
size_t size = sizeof(typename Adaptor::Type);
ASSERT(buffer);
if (!ArrayBufferView::verifySubRangeLength(*buffer, byteOffset, length, size)) {
throwException(exec, scope, createRangeError(exec, "Length out of range of buffer"));
return nullptr;
}
if (!ArrayBufferView::verifyByteOffsetAlignment(byteOffset, size)) {
throwException(exec, scope, createRangeError(exec, "Byte offset is not aligned"));
return nullptr;
}
ConstructionContext context(vm, structure, WTFMove(buffer), byteOffset, 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(
VM& vm, Structure* structure, RefPtr<typename Adaptor::ViewType>&& impl)
{
ConstructionContext context(vm, structure, impl->possiblySharedBuffer(), 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,
RefPtr<typename Adaptor::ViewType>&& impl)
{
return create(globalObject->vm(), structure, WTFMove(impl));
}
template<typename Adaptor>
bool JSGenericTypedArrayView<Adaptor>::validateRange(
ExecState* exec, unsigned offset, unsigned length)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (canAccessRangeQuickly(offset, length))
return true;
throwException(exec, scope, 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, unsigned offset, JSGenericTypedArrayView<OtherAdaptor>* other,
unsigned otherOffset, unsigned length, CopyType type)
{
length = std::min(length, other->length());
RELEASE_ASSERT(other->canAccessRangeQuickly(otherOffset, length));
if (!validateRange(exec, offset, length))
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())
|| type == CopyType::LeftToRight) {
for (unsigned i = 0; i < length; ++i) {
setIndexQuicklyToNativeValue(
offset + i, OtherAdaptor::template convertTo<Adaptor>(
other->getIndexQuicklyAsNativeValue(i + otherOffset)));
}
return true;
}
if (elementSize == otherElementSize) {
for (unsigned i = length; i--;) {
setIndexQuicklyToNativeValue(
offset + i, OtherAdaptor::template convertTo<Adaptor>(
other->getIndexQuicklyAsNativeValue(i + otherOffset)));
}
return true;
}
Vector<typename Adaptor::Type, 32> transferBuffer(length);
for (unsigned i = length; i--;) {
transferBuffer[i] = OtherAdaptor::template convertTo<Adaptor>(
other->getIndexQuicklyAsNativeValue(i + otherOffset));
}
for (unsigned i = length; i--;)
setIndexQuicklyToNativeValue(offset + i, transferBuffer[i]);
return true;
}
template<typename Adaptor>
bool JSGenericTypedArrayView<Adaptor>::set(
ExecState* exec, unsigned offset, JSObject* object, unsigned objectOffset, unsigned length, CopyType type)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
const ClassInfo* ci = object->classInfo(vm);
if (ci->typedArrayStorageType == Adaptor::typeValue) {
JSGenericTypedArrayView* other = jsCast<JSGenericTypedArrayView*>(object);
length = std::min(length, other->length());
RELEASE_ASSERT(other->canAccessRangeQuickly(objectOffset, length));
bool success = validateRange(exec, offset, length);
EXCEPTION_ASSERT(!scope.exception() == success);
if (!success)
return false;
memmove(typedVector() + offset, other->typedVector() + objectOffset, length * elementSize);
return true;
}
switch (ci->typedArrayStorageType) {
case TypeInt8:
scope.release();
return setWithSpecificType<Int8Adaptor>(
exec, offset, jsCast<JSInt8Array*>(object), objectOffset, length, type);
case TypeInt16:
scope.release();
return setWithSpecificType<Int16Adaptor>(
exec, offset, jsCast<JSInt16Array*>(object), objectOffset, length, type);
case TypeInt32:
scope.release();
return setWithSpecificType<Int32Adaptor>(
exec, offset, jsCast<JSInt32Array*>(object), objectOffset, length, type);
case TypeUint8:
scope.release();
return setWithSpecificType<Uint8Adaptor>(
exec, offset, jsCast<JSUint8Array*>(object), objectOffset, length, type);
case TypeUint8Clamped:
scope.release();
return setWithSpecificType<Uint8ClampedAdaptor>(
exec, offset, jsCast<JSUint8ClampedArray*>(object), objectOffset, length, type);
case TypeUint16:
scope.release();
return setWithSpecificType<Uint16Adaptor>(
exec, offset, jsCast<JSUint16Array*>(object), objectOffset, length, type);
case TypeUint32:
scope.release();
return setWithSpecificType<Uint32Adaptor>(
exec, offset, jsCast<JSUint32Array*>(object), objectOffset, length, type);
case TypeFloat32:
scope.release();
return setWithSpecificType<Float32Adaptor>(
exec, offset, jsCast<JSFloat32Array*>(object), objectOffset, length, type);
case TypeFloat64:
scope.release();
return setWithSpecificType<Float64Adaptor>(
exec, offset, jsCast<JSFloat64Array*>(object), objectOffset, length, type);
case NotTypedArray:
case TypeDataView: {
bool success = validateRange(exec, offset, length);
EXCEPTION_ASSERT(!scope.exception() == success);
if (!success)
return false;
for (unsigned i = 0; i < length; ++i) {
JSValue value = object->get(exec, i + objectOffset);
RETURN_IF_EXCEPTION(scope, false);
bool success = setIndex(exec, offset + i, value);
EXCEPTION_ASSERT(!scope.exception() || !success);
if (!success)
return false;
}
return true;
} }
RELEASE_ASSERT_NOT_REACHED();
return false;
}
template<typename Adaptor>
RefPtr<typename Adaptor::ViewType> JSGenericTypedArrayView<Adaptor>::possiblySharedTypedImpl()
{
return Adaptor::ViewType::create(possiblySharedBuffer(), byteOffset(), length());
}
template<typename Adaptor>
RefPtr<typename Adaptor::ViewType> JSGenericTypedArrayView<Adaptor>::unsharedTypedImpl()
{
return Adaptor::ViewType::create(unsharedBuffer(), byteOffset(), length());
}
template<typename Adaptor>
ArrayBuffer* JSGenericTypedArrayView<Adaptor>::existingBuffer()
{
return existingBufferInButterfly();
}
template<typename Adaptor>
EncodedJSValue JSGenericTypedArrayView<Adaptor>::throwNeuteredTypedArrayTypeError(ExecState* exec, EncodedJSValue object, PropertyName)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT_UNUSED(object, jsCast<JSGenericTypedArrayView*>(JSValue::decode(object))->isNeutered());
return throwVMTypeError(exec, scope, typedArrayBufferHasBeenDetachedErrorMessage);
}
template<typename Adaptor>
bool JSGenericTypedArrayView<Adaptor>::getOwnPropertySlot(
JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
if (std::optional<uint32_t> index = parseIndex(propertyName)) {
if (thisObject->isNeutered()) {
slot.setCustom(thisObject, static_cast<unsigned>(PropertyAttribute::None), throwNeuteredTypedArrayTypeError);
return true;
}
if (thisObject->canGetIndexQuickly(index.value())) {
slot.setValue(thisObject, PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly, thisObject->getIndexQuickly(index.value()));
return true;
}
slot.setValue(thisObject, PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly, jsUndefined());
return false;
}
return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
}
template<typename Adaptor>
bool JSGenericTypedArrayView<Adaptor>::put(
JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value,
PutPropertySlot& slot)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(cell);
if (std::optional<uint32_t> index = parseIndex(propertyName))
return 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)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
if (std::optional<uint32_t> index = parseIndex(propertyName)) {
auto throwTypeErrorIfNeeded = [&] (const char* errorMessage) -> bool {
if (shouldThrow)
throwTypeError(exec, scope, makeString(errorMessage, String::number(*index)));
return false;
};
if (descriptor.isAccessorDescriptor())
return throwTypeErrorIfNeeded("Attempting to store accessor property on a typed array at index: ");
if (descriptor.configurable())
return throwTypeErrorIfNeeded("Attempting to configure non-configurable property on a typed array at index: ");
if (!descriptor.enumerable() || !descriptor.writable())
return throwTypeErrorIfNeeded("Attempting to store non-enumerable or non-writable property on a typed array at index: ");
if (descriptor.value()) {
PutPropertySlot unused(JSValue(thisObject), shouldThrow);
scope.release();
return thisObject->put(thisObject, exec, propertyName, descriptor.value(), unused);
}
return true;
}
scope.release();
return Base::defineOwnProperty(thisObject, exec, propertyName, descriptor, shouldThrow);
}
template<typename Adaptor>
bool JSGenericTypedArrayView<Adaptor>::deleteProperty(
JSCell* cell, ExecState* exec, PropertyName propertyName)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(cell);
if (thisObject->isNeutered())
return typeError(exec, scope, true, typedArrayBufferHasBeenDetachedErrorMessage);
if (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 (thisObject->isNeutered()) {
slot.setCustom(thisObject, static_cast<unsigned>(PropertyAttribute::None), throwNeuteredTypedArrayTypeError);
return true;
}
if (propertyName > MAX_ARRAY_INDEX) {
return thisObject->methodTable(exec->vm())->getOwnPropertySlot(
thisObject, exec, Identifier::from(exec, propertyName), slot);
}
if (!thisObject->canGetIndexQuickly(propertyName))
return false;
slot.setValue(thisObject, static_cast<unsigned>(PropertyAttribute::DontDelete), thisObject->getIndexQuickly(propertyName));
return true;
}
template<typename Adaptor>
bool 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);
return thisObject->methodTable(exec->vm())->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)
{
return cell->methodTable(exec->vm())->deleteProperty(cell, exec, Identifier::from(exec, propertyName));
}
template<typename Adaptor>
void JSGenericTypedArrayView<Adaptor>::getOwnPropertyNames(
JSObject* object, ExecState* exec, PropertyNameArray& array, EnumerationMode mode)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
if (array.includeStringProperties()) {
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>
size_t JSGenericTypedArrayView<Adaptor>::estimatedSize(JSCell* cell, VM& vm)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(cell);
if (thisObject->m_mode == OversizeTypedArray)
return Base::estimatedSize(thisObject, vm) + thisObject->byteSize();
if (thisObject->m_mode == FastTypedArray && thisObject->m_vector)
return Base::estimatedSize(thisObject, vm) + thisObject->byteSize();
return Base::estimatedSize(thisObject, vm);
}
template<typename Adaptor>
void JSGenericTypedArrayView<Adaptor>::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(cell);
Base::visitChildren(thisObject, visitor);
TypedArrayMode mode;
void* vector;
size_t byteSize;
{
auto locker = holdLock(thisObject->cellLock());
mode = thisObject->m_mode;
vector = thisObject->m_vector.getMayBeNull();
byteSize = thisObject->byteSize();
}
switch (mode) {
case FastTypedArray: {
if (vector)
visitor.markAuxiliary(vector);
break;
}
case OversizeTypedArray: {
visitor.reportExtraMemoryVisited(byteSize);
break;
}
case WastefulTypedArray:
break;
case DataViewMode:
RELEASE_ASSERT_NOT_REACHED();
break;
}
}
}