diff --git a/js/src/vm/ArrayBufferObject.cpp b/js/src/vm/ArrayBufferObject.cpp index 40c08a0694c5..34d4c1933aa0 100644 --- a/js/src/vm/ArrayBufferObject.cpp +++ b/js/src/vm/ArrayBufferObject.cpp @@ -344,9 +344,9 @@ ArrayBufferObject::neuter(JSContext *cx, Handle buffer, void } if (buffer->isMappedArrayBuffer()) - buffer->changeContents(cx, nullptr); + buffer->setNewOwnedData(cx->runtime()->defaultFreeOp(), nullptr); else if (newData != buffer->dataPointer()) - buffer->changeContents(cx, newData); + buffer->setNewOwnedData(cx->runtime()->defaultFreeOp(), newData); buffer->setByteLength(0); buffer->setViewList(nullptr); @@ -371,12 +371,27 @@ ArrayBufferObject::neuter(JSContext *cx, Handle buffer, void } void -ArrayBufferObject::changeContents(JSContext *cx, void *newData) +ArrayBufferObject::setNewOwnedData(FreeOp* fop, void *newData) { JS_ASSERT(!isAsmJSArrayBuffer()); JS_ASSERT(!isSharedArrayBuffer()); JS_ASSERT_IF(isMappedArrayBuffer(), !newData); + if (ownsData()) { + JS_ASSERT(newData != dataPointer()); + releaseData(fop); + } + + setDataPointer(static_cast(newData), OwnsData); +} + +void +ArrayBufferObject::changeContents(JSContext *cx, void *newData) +{ + // Change buffer contents. + uint8_t* oldDataPointer = dataPointer(); + setNewOwnedData(cx->runtime()->defaultFreeOp(), newData); + // Update all views. ArrayBufferViewObject *viewListHead = viewList(); for (ArrayBufferViewObject *view = viewListHead; view; view = view->nextView()) { @@ -386,18 +401,14 @@ ArrayBufferObject::changeContents(JSContext *cx, void *newData) uint8_t *viewDataPointer = view->dataPointer(); if (viewDataPointer) { JS_ASSERT(newData); - viewDataPointer += static_cast(newData) - dataPointer(); + ptrdiff_t offset = viewDataPointer - oldDataPointer; + viewDataPointer = static_cast(newData) + offset; view->setPrivate(viewDataPointer); } // Notify compiled jit code that the base pointer has moved. MarkObjectStateChange(cx, view); } - - if (ownsData()) - releaseData(cx->runtime()->defaultFreeOp()); - - setDataPointer(static_cast(newData), OwnsData); } #if defined(JS_CPU_X64) diff --git a/js/src/vm/ArrayBufferObject.h b/js/src/vm/ArrayBufferObject.h index 940be5a955c4..6ef48c5f0e1b 100644 --- a/js/src/vm/ArrayBufferObject.h +++ b/js/src/vm/ArrayBufferObject.h @@ -124,6 +124,7 @@ class ArrayBufferObject : public JSObject void addView(ArrayBufferViewObject *view); + void setNewOwnedData(FreeOp* fop, void *newData); void changeContents(JSContext *cx, void *newData); /*