summaryrefslogtreecommitdiffstats
path: root/js/src/builtin/DataViewObject.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'js/src/builtin/DataViewObject.cpp')
-rw-r--r--js/src/builtin/DataViewObject.cpp141
1 files changed, 9 insertions, 132 deletions
diff --git a/js/src/builtin/DataViewObject.cpp b/js/src/builtin/DataViewObject.cpp
index 1f7be86a70..425fdce51d 100644
--- a/js/src/builtin/DataViewObject.cpp
+++ b/js/src/builtin/DataViewObject.cpp
@@ -65,150 +65,27 @@ DataViewObject* DataViewObject::create(
}
ResizableDataViewObject* ResizableDataViewObject::create(
- JSContext* cx, size_t byteOffset, size_t byteLength, bool autoLength,
+ JSContext* cx, size_t byteOffset, size_t byteLength, AutoLength autoLength,
Handle<ArrayBufferObjectMaybeShared*> arrayBuffer, HandleObject proto) {
MOZ_ASSERT(arrayBuffer->isResizable());
MOZ_ASSERT(!arrayBuffer->isDetached());
- MOZ_ASSERT(!autoLength || byteLength == 0,
+ MOZ_ASSERT(autoLength == AutoLength::No || byteLength == 0,
"byte length is zero for 'auto' length views");
auto* obj = NewObjectWithClassProto<ResizableDataViewObject>(cx, proto);
- if (!obj || !obj->init(cx, arrayBuffer, byteOffset, byteLength,
- /* bytesPerElement = */ 1)) {
+ if (!obj || !obj->initResizable(cx, arrayBuffer, byteOffset, byteLength,
+ /* bytesPerElement = */ 1, autoLength)) {
return nullptr;
}
- obj->setFixedSlot(AUTO_LENGTH_SLOT, BooleanValue(autoLength));
-
return obj;
}
-/**
- * GetViewByteLength ( viewRecord )
- *
- * GetViewByteLength can be rewritten into the following spec steps when
- * inlining the calls to MakeDataViewWithBufferWitnessRecord and
- * IsViewOutOfBounds.
- *
- * 1. Let buffer be view.[[ViewedArrayBuffer]].
- * 2. If IsDetachedBuffer(buffer) is true, then
- * a. Return out-of-bounds.
- * 3. If IsFixedLengthArrayBuffer(buffer) is true,
- * a. Return view.[[ByteLength]].
- * 4. Let bufferByteLength be ArrayBufferByteLength(buffer, order).
- * 5. Let byteOffsetStart be view.[[ByteOffset]].
- * 6. If byteOffsetStart > bufferByteLength, then
- * a. Return out-of-bounds.
- * 7. If view.[[ByteLength]] is auto, then
- * a. Return bufferByteLength - byteOffsetStart.
- * 8. Let viewByteLength be view.[[ByteLength]].
- * 9. Let byteOffsetEnd be byteOffsetStart + viewByteLength.
- * 10. If byteOffsetEnd > bufferByteLength, then
- * a. Return out-of-bounds.
- * 11. Return viewByteLength.
- *
- * The additional call to IsFixedLengthArrayBuffer is an optimization to skip
- * unnecessary validation which doesn't apply for fixed length data-views.
- *
- * https://tc39.es/ecma262/#sec-getviewbytelength
- * https://tc39.es/ecma262/#sec-makedataviewwithbufferwitnessrecord
- * https://tc39.es/ecma262/#sec-isviewoutofbounds
- */
-mozilla::Maybe<size_t> DataViewObject::byteLength() {
- if (MOZ_UNLIKELY(hasDetachedBuffer())) {
- return mozilla::Nothing{};
- }
-
- if (MOZ_LIKELY(is<FixedLengthDataViewObject>())) {
- size_t viewByteLength = rawByteLength();
- return mozilla::Some(viewByteLength);
- }
-
- auto* buffer = bufferEither();
- MOZ_ASSERT(buffer->isResizable());
-
- size_t bufferByteLength = buffer->byteLength();
- size_t byteOffsetStart = ArrayBufferViewObject::byteOffset();
- if (byteOffsetStart > bufferByteLength) {
- return mozilla::Nothing{};
- }
-
- if (as<ResizableDataViewObject>().isAutoLength()) {
- return mozilla::Some(bufferByteLength - byteOffsetStart);
- }
-
- size_t viewByteLength = rawByteLength();
- size_t byteOffsetEnd = byteOffsetStart + viewByteLength;
- if (byteOffsetEnd > bufferByteLength) {
- return mozilla::Nothing{};
- }
- return mozilla::Some(viewByteLength);
-}
-
-/**
- * IsViewOutOfBounds ( viewRecord )
- *
- * IsViewOutOfBounds can be rewritten into the following spec steps when
- * inlining the call to MakeDataViewWithBufferWitnessRecord.
- *
- * 1. Let buffer be obj.[[ViewedArrayBuffer]].
- * 2. If IsDetachedBuffer(buffer) is true, then
- * a. Return true.
- * 3. If IsFixedLengthArrayBuffer(buffer) is true, then
- * a. Return false.
- * 4. Let byteLength be ArrayBufferByteLength(buffer, order).
- * 5. Let byteOffsetStart be view.[[ByteOffset]].
- * 6. If byteOffsetStart > bufferByteLength, then
- * a. Return true.
- * 7. If view.[[ByteLength]] is auto, then
- * a. Return false.
- * 8. Let byteOffsetEnd be byteOffsetStart + view.[[ByteLength]].
- * 9. If byteOffsetEnd > bufferByteLength, then
- * a. Return true.
- * 10. Return false.
- *
- * The additional call to IsFixedLengthArrayBuffer is an optimization to skip
- * unnecessary validation which doesn't apply for fixed length data-views.
- *
- * https://tc39.es/ecma262/#sec-makedataviewwithbufferwitnessrecord
- * https://tc39.es/ecma262/#sec-isviewoutofbounds
- */
-mozilla::Maybe<size_t> DataViewObject::byteOffset() {
- if (MOZ_UNLIKELY(hasDetachedBuffer())) {
- return mozilla::Nothing{};
- }
-
- size_t byteOffsetStart = ArrayBufferViewObject::byteOffset();
-
- if (MOZ_LIKELY(is<FixedLengthDataViewObject>())) {
- return mozilla::Some(byteOffsetStart);
- }
-
- auto* buffer = bufferEither();
- MOZ_ASSERT(buffer->isResizable());
-
- size_t bufferByteLength = buffer->byteLength();
- if (byteOffsetStart > bufferByteLength) {
- return mozilla::Nothing{};
- }
-
- if (as<ResizableDataViewObject>().isAutoLength()) {
- return mozilla::Some(byteOffsetStart);
- }
-
- size_t viewByteLength = rawByteLength();
- size_t byteOffsetEnd = byteOffsetStart + viewByteLength;
- if (byteOffsetEnd > bufferByteLength) {
- return mozilla::Nothing{};
- }
- return mozilla::Some(byteOffsetStart);
-}
-
// ES2017 draft rev 931261ecef9b047b14daacf82884134da48dfe0f
// 24.3.2.1 DataView (extracted part of the main algorithm)
bool DataViewObject::getAndCheckConstructorArgs(
JSContext* cx, HandleObject bufobj, const CallArgs& args,
- size_t* byteOffsetPtr, size_t* byteLengthPtr, bool* autoLengthPtr) {
+ size_t* byteOffsetPtr, size_t* byteLengthPtr, AutoLength* autoLengthPtr) {
// Step 3.
if (!bufobj->is<ArrayBufferObjectMaybeShared>()) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
@@ -243,10 +120,10 @@ bool DataViewObject::getAndCheckConstructorArgs(
MOZ_ASSERT(offset <= ArrayBufferObject::ByteLengthLimit);
uint64_t viewByteLength = 0;
- bool autoLength = false;
+ auto autoLength = AutoLength::No;
if (!args.hasDefined(2)) {
if (buffer->isResizable()) {
- autoLength = true;
+ autoLength = AutoLength::Yes;
} else {
// Step 8.a
viewByteLength = bufferByteLength - offset;
@@ -305,7 +182,7 @@ bool DataViewObject::constructSameCompartment(JSContext* cx,
size_t byteOffset = 0;
size_t byteLength = 0;
- bool autoLength = false;
+ auto autoLength = AutoLength::No;
if (!getAndCheckConstructorArgs(cx, bufobj, args, &byteOffset, &byteLength,
&autoLength)) {
return false;
@@ -365,7 +242,7 @@ bool DataViewObject::constructWrapped(JSContext* cx, HandleObject bufobj,
// NB: This entails the IsArrayBuffer check
size_t byteOffset = 0;
size_t byteLength = 0;
- bool autoLength = false;
+ auto autoLength = AutoLength::No;
if (!getAndCheckConstructorArgs(cx, unwrapped, args, &byteOffset, &byteLength,
&autoLength)) {
return false;