JSDataViewPrototype.cpp [plain text]
#include "config.h"
#include "JSDataViewPrototype.h"
#include "JSArrayBuffer.h"
#include "JSCInlines.h"
#include "JSDataView.h"
#include "ToNativeFromValue.h"
#include "TypedArrayAdaptors.h"
#include <wtf/FlipBytes.h>
namespace JSC {
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncGetInt8);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncGetInt16);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncGetInt32);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncGetUint8);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncGetUint16);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncGetUint32);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncGetFloat32);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncGetFloat64);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncSetInt8);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncSetInt16);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncSetInt32);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncSetUint8);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncSetUint16);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncSetUint32);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncSetFloat32);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoFuncSetFloat64);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoGetterBuffer);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoGetterByteLength);
static JSC_DECLARE_HOST_FUNCTION(dataViewProtoGetterByteOffset);
}
#include "JSDataViewPrototype.lut.h"
namespace JSC {
const ClassInfo JSDataViewPrototype::s_info = {
"DataView", &Base::s_info, &dataViewTable, nullptr,
CREATE_METHOD_TABLE(JSDataViewPrototype)
};
JSDataViewPrototype::JSDataViewPrototype(VM& vm, Structure* structure)
: Base(vm, structure)
{
}
JSDataViewPrototype* JSDataViewPrototype::create(VM& vm, Structure* structure)
{
JSDataViewPrototype* prototype =
new (NotNull, allocateCell<JSDataViewPrototype>(vm.heap))
JSDataViewPrototype(vm, structure);
prototype->finishCreation(vm);
return prototype;
}
void JSDataViewPrototype::finishCreation(JSC::VM& vm)
{
Base::finishCreation(vm);
ASSERT(inherits(vm, info()));
JSC_TO_STRING_TAG_WITHOUT_TRANSITION();
}
Structure* JSDataViewPrototype::createStructure(
VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
return Structure::create(
vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
}
template<typename Adaptor>
EncodedJSValue getData(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSDataView* dataView = jsDynamicCast<JSDataView*>(vm, callFrame->thisValue());
if (!dataView)
return throwVMTypeError(globalObject, scope, "Receiver of DataView method must be a DataView"_s);
unsigned byteOffset = callFrame->argument(0).toIndex(globalObject, "byteOffset");
RETURN_IF_EXCEPTION(scope, encodedJSValue());
bool littleEndian = false;
unsigned elementSize = sizeof(typename Adaptor::Type);
if (elementSize > 1 && callFrame->argumentCount() >= 2) {
littleEndian = callFrame->uncheckedArgument(1).toBoolean(globalObject);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
}
if (dataView->isDetached())
return throwVMTypeError(globalObject, scope, "Underlying ArrayBuffer has been detached from the view"_s);
unsigned byteLength = dataView->length();
if (elementSize > byteLength || byteOffset > byteLength - elementSize)
return throwVMRangeError(globalObject, scope, "Out of bounds access"_s);
const unsigned dataSize = sizeof(typename Adaptor::Type);
union {
typename Adaptor::Type value;
uint8_t rawBytes[dataSize];
} u = { };
uint8_t* dataPtr = static_cast<uint8_t*>(dataView->vector()) + byteOffset;
if (needToFlipBytesIfLittleEndian(littleEndian)) {
for (unsigned i = dataSize; i--;)
u.rawBytes[i] = *dataPtr++;
} else {
for (unsigned i = 0; i < dataSize; i++)
u.rawBytes[i] = *dataPtr++;
}
return JSValue::encode(Adaptor::toJSValue(u.value));
}
template<typename Adaptor>
EncodedJSValue setData(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSDataView* dataView = jsDynamicCast<JSDataView*>(vm, callFrame->thisValue());
if (!dataView)
return throwVMTypeError(globalObject, scope, "Receiver of DataView method must be a DataView"_s);
unsigned byteOffset = callFrame->argument(0).toIndex(globalObject, "byteOffset");
RETURN_IF_EXCEPTION(scope, encodedJSValue());
const unsigned dataSize = sizeof(typename Adaptor::Type);
union {
typename Adaptor::Type value;
uint8_t rawBytes[dataSize];
} u;
u.value = toNativeFromValue<Adaptor>(globalObject, callFrame->argument(1));
RETURN_IF_EXCEPTION(scope, encodedJSValue());
bool littleEndian = false;
unsigned elementSize = sizeof(typename Adaptor::Type);
if (elementSize > 1 && callFrame->argumentCount() >= 3) {
littleEndian = callFrame->uncheckedArgument(2).toBoolean(globalObject);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
}
if (dataView->isDetached())
return throwVMTypeError(globalObject, scope, "Underlying ArrayBuffer has been detached from the view"_s);
unsigned byteLength = dataView->length();
if (elementSize > byteLength || byteOffset > byteLength - elementSize)
return throwVMRangeError(globalObject, scope, "Out of bounds access"_s);
uint8_t* dataPtr = static_cast<uint8_t*>(dataView->vector()) + byteOffset;
if (needToFlipBytesIfLittleEndian(littleEndian)) {
for (unsigned i = dataSize; i--;)
*dataPtr++ = u.rawBytes[i];
} else {
for (unsigned i = 0; i < dataSize; i++)
*dataPtr++ = u.rawBytes[i];
}
return JSValue::encode(jsUndefined());
}
IGNORE_CLANG_WARNINGS_BEGIN("missing-prototypes")
JSC_DEFINE_HOST_FUNCTION(dataViewProtoGetterBuffer, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSDataView* view = jsDynamicCast<JSDataView*>(vm, callFrame->thisValue());
if (!view)
return throwVMTypeError(globalObject, scope, "DataView.prototype.buffer expects |this| to be a DataView object");
RELEASE_AND_RETURN(scope, JSValue::encode(view->possiblySharedJSBuffer(globalObject)));
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoGetterByteLength, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSDataView* view = jsDynamicCast<JSDataView*>(vm, callFrame->thisValue());
if (!view)
return throwVMTypeError(globalObject, scope, "DataView.prototype.byteLength expects |this| to be a DataView object");
if (view->isDetached())
return throwVMTypeError(globalObject, scope, "Underlying ArrayBuffer has been detached from the view"_s);
return JSValue::encode(jsNumber(view->length()));
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoGetterByteOffset, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSDataView* view = jsDynamicCast<JSDataView*>(vm, callFrame->thisValue());
if (!view)
return throwVMTypeError(globalObject, scope, "DataView.prototype.byteOffset expects |this| to be a DataView object");
if (view->isDetached())
return throwVMTypeError(globalObject, scope, "Underlying ArrayBuffer has been detached from the view"_s);
return JSValue::encode(jsNumber(view->byteOffset()));
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncGetInt8, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return getData<Int8Adaptor>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncGetInt16, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return getData<Int16Adaptor>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncGetInt32, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return getData<Int32Adaptor>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncGetUint8, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return getData<Uint8Adaptor>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncGetUint16, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return getData<Uint16Adaptor>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncGetUint32, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return getData<Uint32Adaptor>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncGetFloat32, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return getData<Float32Adaptor>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncGetFloat64, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return getData<Float64Adaptor>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncSetInt8, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return setData<Int8Adaptor>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncSetInt16, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return setData<Int16Adaptor>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncSetInt32, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return setData<Int32Adaptor>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncSetUint8, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return setData<Uint8Adaptor>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncSetUint16, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return setData<Uint16Adaptor>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncSetUint32, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return setData<Uint32Adaptor>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncSetFloat32, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return setData<Float32Adaptor>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(dataViewProtoFuncSetFloat64, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return setData<Float64Adaptor>(globalObject, callFrame);
}
IGNORE_CLANG_WARNINGS_END
}