Skip to content

Commit

Permalink
Bug 1532005 - Implement a new js/public/ArrayBuffer.h header to centr…
Browse files Browse the repository at this point in the history
…alize functionality for creating and interacting with ArrayBuffers. r=sfink

Differential Revision: https://phabricator.services.mozilla.com/D21827

--HG--
extra : rebase_source : 833bbfce12d90bcb92f7cd88ec65247e48c024c0
  • Loading branch information
jswalden committed Mar 4, 2019
1 parent b15a5f4 commit 3f385ff
Show file tree
Hide file tree
Showing 44 changed files with 588 additions and 448 deletions.
5 changes: 3 additions & 2 deletions dom/base/BodyUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "nsStreamUtils.h"
#include "nsStringStream.h"

#include "js/ArrayBuffer.h" // JS::NewArrayBufferWithContents
#include "js/JSON.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/Exceptions.h"
Expand Down Expand Up @@ -386,8 +387,8 @@ void BodyUtil::ConsumeArrayBuffer(JSContext* aCx,
uint32_t aInputLength, uint8_t* aInput,
ErrorResult& aRv) {
JS::Rooted<JSObject*> arrayBuffer(aCx);
arrayBuffer = JS_NewArrayBufferWithContents(aCx, aInputLength,
reinterpret_cast<void*>(aInput));
arrayBuffer = JS::NewArrayBufferWithContents(aCx, aInputLength,
reinterpret_cast<void*>(aInput));
if (!arrayBuffer) {
JS_ClearPendingException(aCx);
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
Expand Down
7 changes: 4 additions & 3 deletions dom/base/nsContentUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "imgRequestProxy.h"
#include "jsapi.h"
#include "jsfriendapi.h"
#include "js/ArrayBuffer.h" // JS::{GetArrayBufferData,IsArrayBufferObject,NewArrayBuffer}
#include "js/JSON.h"
#include "js/Value.h"
#include "Layers.h"
Expand Down Expand Up @@ -6078,16 +6079,16 @@ nsresult nsContentUtils::CreateArrayBuffer(JSContext* aCx,
}

int32_t dataLen = aData.Length();
*aResult = JS_NewArrayBuffer(aCx, dataLen);
*aResult = JS::NewArrayBuffer(aCx, dataLen);
if (!*aResult) {
return NS_ERROR_FAILURE;
}

if (dataLen > 0) {
NS_ASSERTION(JS_IsArrayBufferObject(*aResult), "What happened?");
NS_ASSERTION(JS::IsArrayBufferObject(*aResult), "What happened?");
JS::AutoCheckCannotGC nogc;
bool isShared;
memcpy(JS_GetArrayBufferData(*aResult, &isShared, nogc),
memcpy(JS::GetArrayBufferData(*aResult, &isShared, nogc),
aData.BeginReading(), dataLen);
MOZ_ASSERT(!isShared);
}
Expand Down
15 changes: 10 additions & 5 deletions dom/bindings/TypedArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
#ifndef mozilla_dom_TypedArray_h
#define mozilla_dom_TypedArray_h

#include "jsfriendapi.h" // js::Scalar
#include "js/ArrayBuffer.h"
#include "js/SharedArrayBuffer.h"
#include "js/GCAPI.h" // JS::AutoCheckCannotGC
#include "js/RootingAPI.h" // JS::Rooted
#include "mozilla/Attributes.h"
#include "mozilla/Move.h"
#include "mozilla/dom/BindingDeclarations.h"
Expand Down Expand Up @@ -72,7 +77,7 @@ struct TypedArray_base : public SpiderMonkeyInterfaceObjectStorage,
// value if the view may not have been computed and if the value is
// known to represent a JS TypedArray.
//
// (Just use JS_IsSharedArrayBuffer() to test if any object is of
// (Just use JS::IsSharedArrayBuffer() to test if any object is of
// that type.)
//
// Code that elects to allow views that map shared memory to be used
Expand Down Expand Up @@ -257,13 +262,13 @@ typedef ArrayBufferView_base<js::UnwrapArrayBufferView,
js::GetArrayBufferViewLengthAndData,
JS_GetArrayBufferViewType>
ArrayBufferView;
typedef TypedArray<uint8_t, js::UnwrapArrayBuffer, JS_GetArrayBufferData,
js::GetArrayBufferLengthAndData, JS_NewArrayBuffer>
typedef TypedArray<uint8_t, JS::UnwrapArrayBuffer, JS::GetArrayBufferData,
JS::GetArrayBufferLengthAndData, JS::NewArrayBuffer>
ArrayBuffer;

typedef TypedArray<
uint8_t, js::UnwrapSharedArrayBuffer, JS_GetSharedArrayBufferData,
js::GetSharedArrayBufferLengthAndData, JS_NewSharedArrayBuffer>
uint8_t, JS::UnwrapSharedArrayBuffer, JS::GetSharedArrayBufferData,
JS::GetSharedArrayBufferLengthAndData, JS::NewSharedArrayBuffer>
SharedArrayBuffer;

// A class for converting an nsTArray to a TypedArray
Expand Down
3 changes: 2 additions & 1 deletion dom/file/FileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "nsIGlobalObject.h"
#include "nsITimer.h"

#include "js/ArrayBuffer.h" // JS::NewArrayBufferWithContents
#include "mozilla/Base64.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/dom/DOMException.h"
Expand Down Expand Up @@ -180,7 +181,7 @@ void FileReader::OnLoadEndArrayBuffer() {

JSContext* cx = jsapi.cx();

mResultArrayBuffer = JS_NewArrayBufferWithContents(cx, mDataLen, mFileData);
mResultArrayBuffer = JS::NewArrayBufferWithContents(cx, mDataLen, mFileData);
if (mResultArrayBuffer) {
mFileData = nullptr; // Transfer ownership
FreeDataAndDispatchSuccess();
Expand Down
6 changes: 4 additions & 2 deletions dom/file/FileReaderSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

#include "FileReaderSync.h"

#include "jsfriendapi.h"
#include "js/ArrayBuffer.h" // JS::NewArrayBufferWithContents
#include "js/RootingAPI.h" // JS::{,Mutable}Handle
#include "js/Utility.h" // js::ArrayBufferContentsArena, JS::FreePolicy, js_pod_arena_malloc
#include "mozilla/Unused.h"
#include "mozilla/Base64.h"
#include "mozilla/dom/File.h"
Expand Down Expand Up @@ -83,7 +85,7 @@ void FileReaderSync::ReadAsArrayBuffer(JSContext* aCx,
}

JSObject* arrayBuffer =
JS_NewArrayBufferWithContents(aCx, blobSize, bufferData.get());
JS::NewArrayBufferWithContents(aCx, blobSize, bufferData.get());
if (!arrayBuffer) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
Expand Down
9 changes: 5 additions & 4 deletions dom/indexedDB/Key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <algorithm>
#include <stdint.h> // for UINT32_MAX, uintptr_t
#include "IndexedDatabaseManager.h"
#include "js/ArrayBuffer.h" // JS::{IsArrayBufferObject,NewArrayBuffer{,WithContents},GetArrayBufferLengthAndData}
#include "js/Date.h"
#include "js/MemoryFunctions.h"
#include "js/Value.h"
Expand Down Expand Up @@ -296,7 +297,7 @@ nsresult Key::EncodeJSValInternal(JSContext* aCx, JS::Handle<JS::Value> aVal,
return NS_OK;
}

if (JS_IsArrayBufferObject(obj)) {
if (JS::IsArrayBufferObject(obj)) {
return EncodeBinary(obj, /* aIsViewObject */ false, aTypeOffset);
}

Expand Down Expand Up @@ -635,7 +636,7 @@ nsresult Key::EncodeBinary(JSObject* aObject, bool aIsViewObject,
js::GetArrayBufferViewLengthAndData(aObject, &bufferLength, &unused,
&bufferData);
} else {
js::GetArrayBufferLengthAndData(aObject, &bufferLength, &unused,
JS::GetArrayBufferLengthAndData(aObject, &bufferLength, &unused,
&bufferData);
}

Expand All @@ -661,7 +662,7 @@ JSObject* Key::DecodeBinary(const unsigned char*& aPos,
}

if (!size) {
return JS_NewArrayBuffer(aCx, 0);
return JS::NewArrayBuffer(aCx, 0);
}

uint8_t* out = static_cast<uint8_t*>(JS_malloc(aCx, size));
Expand Down Expand Up @@ -696,7 +697,7 @@ JSObject* Key::DecodeBinary(const unsigned char*& aPos,
MOZ_ASSERT(static_cast<size_t>(pos - out) == size,
"Should have written the whole buffer");

return JS_NewArrayBufferWithContents(aCx, size, out);
return JS::NewArrayBufferWithContents(aCx, size, out);
}

nsresult Key::BindToStatement(mozIStorageStatement* aStatement,
Expand Down
3 changes: 2 additions & 1 deletion dom/media/webaudio/AudioBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "AudioBuffer.h"
#include "mozilla/dom/AudioBufferBinding.h"
#include "jsfriendapi.h"
#include "js/ArrayBuffer.h" // JS::StealArrayBufferContents
#include "mozilla/ErrorResult.h"
#include "AudioSegment.h"
#include "AudioChannelFormat.h"
Expand Down Expand Up @@ -426,7 +427,7 @@ AudioBuffer::StealJSArrayDataIntoSharedChannels(JSContext* aJSContext) {
// RestoreJSChannelData, where they are created unshared.
MOZ_ASSERT(!isSharedMemory);
auto stolenData = arrayBuffer
? static_cast<float*>(JS_StealArrayBufferContents(
? static_cast<float*>(JS::StealArrayBufferContents(
aJSContext, arrayBuffer))
: nullptr;
if (stolenData) {
Expand Down
3 changes: 2 additions & 1 deletion dom/media/webaudio/AudioContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include "DynamicsCompressorNode.h"
#include "GainNode.h"
#include "IIRFilterNode.h"
#include "js/ArrayBuffer.h" // JS::StealArrayBufferContents
#include "MediaElementAudioSourceNode.h"
#include "MediaStreamAudioDestinationNode.h"
#include "MediaStreamAudioSourceNode.h"
Expand Down Expand Up @@ -584,7 +585,7 @@ already_AddRefed<Promise> AudioContext::DecodeAudioData(
// Detach the array buffer
size_t length = aBuffer.Length();

uint8_t* data = static_cast<uint8_t*>(JS_StealArrayBufferContents(cx, obj));
uint8_t* data = static_cast<uint8_t*>(JS::StealArrayBufferContents(cx, obj));

// Sniff the content of the media.
// Failed type sniffing will be handled by AsyncDecodeWebAudio.
Expand Down
2 changes: 1 addition & 1 deletion dom/media/webaudio/AudioNodeEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ThreadSharedFloatArrayBufferList final : public ThreadSharedObject {
}
/**
* Create with buffers suitable for transfer to
* JS_NewArrayBufferWithContents(). The buffer contents are uninitialized
* JS::NewArrayBufferWithContents(). The buffer contents are uninitialized
* and so should be set using GetDataForWrite().
*/
static already_AddRefed<ThreadSharedFloatArrayBufferList> Create(
Expand Down
2 changes: 1 addition & 1 deletion dom/media/webaudio/MediaBufferDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ void MediaDecodeTask::FinishDecode() {
mDecodeJob.mBuffer.mChannelData.SetLength(channelCount);
#if AUDIO_OUTPUT_FORMAT == AUDIO_FORMAT_FLOAT32
// This buffer has separate channel arrays that could be transferred to
// JS_NewArrayBufferWithContents(), but AudioBuffer::RestoreJSChannelData()
// JS::NewArrayBufferWithContents(), but AudioBuffer::RestoreJSChannelData()
// does not yet take advantage of this.
RefPtr<ThreadSharedFloatArrayBufferList> buffer =
ThreadSharedFloatArrayBufferList::Create(channelCount, resampledFrames,
Expand Down
10 changes: 6 additions & 4 deletions dom/network/TCPSocketChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
#include "nsITCPSocketCallback.h"
#include "TCPSocket.h"
#include "nsContentUtils.h"
#include "jsapi.h"
#include "jsfriendapi.h"
#include "js/ArrayBuffer.h" // JS::NewArrayBufferWithContents
#include "js/RootingAPI.h" // JS::MutableHandle
#include "js/Utility.h" // js::ArrayBufferContentsArena, JS::FreePolicy, js_pod_arena_malloc
#include "js/Value.h" // JS::Value

using mozilla::net::gNeckoChild;

Expand All @@ -31,9 +33,9 @@ bool DeserializeArrayBuffer(JSContext* cx,
memcpy(data.get(), aBuffer.Elements(), aBuffer.Length());

JSObject* obj =
JS_NewArrayBufferWithContents(cx, aBuffer.Length(), data.get());
JS::NewArrayBufferWithContents(cx, aBuffer.Length(), data.get());
if (!obj) return false;
// If JS_NewArrayBufferWithContents returns non-null, the ownership of
// If JS::NewArrayBufferWithContents returns non-null, the ownership of
// the data is transfered to obj, so we release the ownership here.
mozilla::Unused << data.release();

Expand Down
9 changes: 6 additions & 3 deletions dom/simpledb/SDBConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#include "SDBConnection.h"

#include "ActorsChild.h"
#include "jsfriendapi.h"
#include "jsfriendapi.h" // JS_GetObjectAsArrayBufferView
#include "js/ArrayBuffer.h" // JS::{GetObjectAsArrayBuffer,IsArrayBufferObject}
#include "js/RootingAPI.h" // JS::{Handle,Rooted}
#include "js/Value.h" // JS::Value
#include "mozilla/ipc/BackgroundChild.h"
#include "mozilla/ipc/BackgroundParent.h"
#include "mozilla/ipc/BackgroundUtils.h"
Expand All @@ -29,15 +32,15 @@ nsresult GetWriteData(JSContext* aCx, JS::Handle<JS::Value> aValue,
JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());

bool isView = false;
if (JS_IsArrayBufferObject(obj) ||
if (JS::IsArrayBufferObject(obj) ||
(isView = JS_IsArrayBufferViewObject(obj))) {
uint8_t* data;
uint32_t length;
bool unused;
if (isView) {
JS_GetObjectAsArrayBufferView(obj, &length, &unused, &data);
} else {
JS_GetObjectAsArrayBuffer(obj, &length, &data);
JS::GetObjectAsArrayBuffer(obj, &length, &data);
}

if (NS_WARN_IF(!aData.Assign(reinterpret_cast<char*>(data), length,
Expand Down
17 changes: 10 additions & 7 deletions dom/xhr/XMLHttpRequestMainThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@
#include "nsStringBuffer.h"
#include "nsIFileChannel.h"
#include "mozilla/Telemetry.h"
#include "js/JSON.h"
#include "js/ArrayBuffer.h" // JS::{Create,Release}MappedArrayBufferContents,New{,Mapped}ArrayBufferWithContents
#include "js/JSON.h" // JS_ParseJSON
#include "js/MemoryFunctions.h"
#include "jsfriendapi.h"
#include "js/RootingAPI.h" // JS::{{,Mutable}Handle,Rooted}
#include "js/Value.h" // JS::{,Undefined}Value
#include "jsapi.h" // JS_ClearPendingException
#include "GeckoProfiler.h"
#include "mozilla/dom/XMLHttpRequestBinding.h"
#include "mozilla/Attributes.h"
Expand Down Expand Up @@ -3629,7 +3632,7 @@ void ArrayBufferBuilder::reset() {
}

if (mMapPtr) {
JS_ReleaseMappedArrayBufferContents(mMapPtr, mLength);
JS::ReleaseMappedArrayBufferContents(mMapPtr, mLength);
mMapPtr = nullptr;
}

Expand Down Expand Up @@ -3705,9 +3708,9 @@ bool ArrayBufferBuilder::append(const uint8_t* aNewData, uint32_t aDataLen,

JSObject* ArrayBufferBuilder::getArrayBuffer(JSContext* aCx) {
if (mMapPtr) {
JSObject* obj = JS_NewMappedArrayBufferWithContents(aCx, mLength, mMapPtr);
JSObject* obj = JS::NewMappedArrayBufferWithContents(aCx, mLength, mMapPtr);
if (!obj) {
JS_ReleaseMappedArrayBufferContents(mMapPtr, mLength);
JS::ReleaseMappedArrayBufferContents(mMapPtr, mLength);
}
mMapPtr = nullptr;

Expand All @@ -3724,7 +3727,7 @@ JSObject* ArrayBufferBuilder::getArrayBuffer(JSContext* aCx) {
}
}

JSObject* obj = JS_NewArrayBufferWithContents(aCx, mLength, mDataPtr);
JSObject* obj = JS::NewArrayBufferWithContents(aCx, mLength, mDataPtr);
mLength = mCapacity = 0;
if (!obj) {
js_free(mDataPtr);
Expand Down Expand Up @@ -3758,7 +3761,7 @@ nsresult ArrayBufferBuilder::mapToFileInPackage(const nsCString& aFile,
if (NS_FAILED(rv)) {
return rv;
}
mMapPtr = JS_CreateMappedArrayBufferContents(
mMapPtr = JS::CreateMappedArrayBufferContents(
PR_FileDesc2NativeHandle(pr_fd), offset, size);
if (mMapPtr) {
mLength = size;
Expand Down
17 changes: 10 additions & 7 deletions dom/xhr/XMLHttpRequestWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
#include "nsIRunnable.h"
#include "nsIXPConnect.h"

#include "jsapi.h" // JS::AutoValueArray
#include "jsfriendapi.h"
#include "js/TracingAPI.h"
#include "js/ArrayBuffer.h" // JS::Is{,Detached}ArrayBufferObject
#include "js/GCPolicyAPI.h"
#include "js/RootingAPI.h" // JS::{Handle,Heap},PersistentRooted
#include "js/TracingAPI.h"
#include "js/Value.h" // JS::{Undefined,}Value
#include "mozilla/ArrayUtils.h"
#include "mozilla/dom/Exceptions.h"
#include "mozilla/dom/Event.h"
Expand Down Expand Up @@ -1034,14 +1038,14 @@ bool EventRunnable::PreDispatch(WorkerPrivate* /* unused */) {
JS::Rooted<JS::Value> transferable(cx);
JS::Rooted<JSObject*> obj(
cx, response.isObject() ? &response.toObject() : nullptr);
if (obj && JS_IsArrayBufferObject(obj)) {
if (obj && JS::IsArrayBufferObject(obj)) {
// Use cached response if the arraybuffer has been transfered.
if (mProxy->mArrayBufferResponseWasTransferred) {
MOZ_ASSERT(JS_IsDetachedArrayBufferObject(obj));
MOZ_ASSERT(JS::IsDetachedArrayBufferObject(obj));
mUseCachedArrayBufferResponse = true;
doClone = false;
} else {
MOZ_ASSERT(!JS_IsDetachedArrayBufferObject(obj));
MOZ_ASSERT(!JS::IsDetachedArrayBufferObject(obj));
JS::AutoValueArray<1> argv(cx);
argv[0].set(response);
obj = JS_NewArrayObject(cx, argv);
Expand Down Expand Up @@ -1221,8 +1225,7 @@ bool EventRunnable::WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) {

target->DispatchEvent(*event);

// After firing the event set mResponse to JSVAL_NULL for chunked response
// types.
// After firing the event set mResponse to null for chunked response types.
if (StringBeginsWith(mResponseType, NS_LITERAL_STRING("moz-chunked-"))) {
xhr->NullResponseText();
}
Expand Down Expand Up @@ -2208,7 +2211,7 @@ void XMLHttpRequestWorker::UpdateState(const StateData& aStateData,
bool aUseCachedArrayBufferResponse) {
if (aUseCachedArrayBufferResponse) {
MOZ_ASSERT(mStateData.mResponse.isObject() &&
JS_IsArrayBufferObject(&mStateData.mResponse.toObject()));
JS::IsArrayBufferObject(&mStateData.mResponse.toObject()));

JS::Rooted<JS::Value> response(mWorkerPrivate->GetJSContext(),
mStateData.mResponse);
Expand Down
Loading

0 comments on commit 3f385ff

Please sign in to comment.