Bug 1849037 - Pass objects that support conversion to a Span directly to TypedArray::Create as a Span. r=necko-reviewers,extension-reviewers,media-playback-reviewers,profiler-reviewers,farre,padenot,jesup

Differential Revision: https://phabricator.services.mozilla.com/D191416
This commit is contained in:
Peter Van der Beken 2023-11-08 11:29:04 +00:00
Родитель a1050a3bd8
Коммит 7cb765ad2c
20 изменённых файлов: 48 добавлений и 89 удалений

Просмотреть файл

@ -161,9 +161,8 @@ void ChromeUtils::Base64URLDecode(GlobalObject& aGlobal,
return;
}
JS::Rooted<JSObject*> buffer(
aGlobal.Context(),
ArrayBuffer::Create(aGlobal.Context(), data.Length(), data.Elements()));
JS::Rooted<JSObject*> buffer(aGlobal.Context(),
ArrayBuffer::Create(aGlobal.Context(), data));
if (NS_WARN_IF(!buffer)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;

Просмотреть файл

@ -799,14 +799,14 @@ using ArrayBuffer = TypedArray<JS::ArrayBuffer>;
// So this is best used to pass from things that understand nsTArray to
// things that understand TypedArray, as with ToJSValue.
template <typename TypedArrayType>
class TypedArrayCreator {
class MOZ_STACK_CLASS TypedArrayCreator {
typedef nsTArray<typename TypedArrayType::element_type> ArrayType;
public:
explicit TypedArrayCreator(const ArrayType& aArray) : mArray(aArray) {}
JSObject* Create(JSContext* aCx) const {
return TypedArrayType::Create(aCx, mArray.Length(), mArray.Elements());
return TypedArrayType::Create(aCx, mArray);
}
private:

Просмотреть файл

@ -1935,6 +1935,16 @@ bool ClientWebGLContext::IsEnabled(GLenum cap) const {
return ret;
}
template <typename T, typename S>
static JS::Value Create(JSContext* cx, nsWrapperCache* creator, const S& src,
ErrorResult& rv) {
const auto obj = T::Create(cx, creator, src);
if (!obj) {
rv = NS_ERROR_OUT_OF_MEMORY;
}
return JS::ObjectOrNullValue(obj);
}
void ClientWebGLContext::GetInternalformatParameter(
JSContext* cx, GLenum target, GLenum internalformat, GLenum pname,
JS::MutableHandle<JS::Value> retval, ErrorResult& rv) {
@ -1961,13 +1971,8 @@ void ClientWebGLContext::GetInternalformatParameter(
if (!maybe) {
return;
}
// zero-length array indicates out-of-memory
JSObject* obj =
dom::Int32Array::Create(cx, this, maybe->size(), maybe->data());
if (!obj) {
rv = NS_ERROR_OUT_OF_MEMORY;
}
retval.setObjectOrNull(obj);
retval.set(Create<dom::Int32Array>(cx, this, *maybe, rv));
}
static JS::Value StringValue(JSContext* cx, const std::string& str,
@ -1991,23 +1996,6 @@ bool ToJSValueOrNull(JSContext* const cx, const RefPtr<T>& ptr,
return dom::ToJSValue(cx, ptr, retval);
}
template <typename T, typename U, typename S>
static JS::Value CreateAs(JSContext* cx, nsWrapperCache* creator, const S& src,
ErrorResult& rv) {
const auto obj =
T::Create(cx, creator, src.size(), reinterpret_cast<U>(src.data()));
if (!obj) {
rv = NS_ERROR_OUT_OF_MEMORY;
}
return JS::ObjectOrNullValue(obj);
}
template <typename T, typename S>
static JS::Value Create(JSContext* cx, nsWrapperCache* creator, const S& src,
ErrorResult& rv) {
return CreateAs<T, decltype(&src[0]), S>(cx, creator, src, rv);
}
Maybe<double> ClientWebGLContext::GetNumber(const GLenum pname) {
MOZ_ASSERT(!IsContextLost());
@ -2178,9 +2166,9 @@ void ClientWebGLContext::GetParameter(JSContext* cx, GLenum pname,
// 2 ints
case LOCAL_GL_MAX_VIEWPORT_DIMS: {
const auto dims =
std::array<uint32_t, 2>{limits.maxViewportDim, limits.maxViewportDim};
retval.set(CreateAs<dom::Int32Array, const int32_t*>(cx, this, dims, rv));
auto maxViewportDim = BitwiseCast<int32_t>(limits.maxViewportDim);
const auto dims = std::array<int32_t, 2>{maxViewportDim, maxViewportDim};
retval.set(Create<dom::Int32Array>(cx, this, dims, rv));
return;
}
@ -6112,13 +6100,7 @@ void ClientWebGLContext::GetActiveUniformBlockParameter(
case LOCAL_GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES: {
const auto& indices = block.activeUniformIndices;
JS::Rooted<JSObject*> obj(
cx,
dom::Uint32Array::Create(cx, this, indices.size(), indices.data()));
if (!obj) {
rv = NS_ERROR_OUT_OF_MEMORY;
}
return JS::ObjectOrNullValue(obj);
return Create<dom::Uint32Array>(cx, this, indices, rv);
}
case LOCAL_GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:

Просмотреть файл

@ -123,11 +123,11 @@ bool CryptoBuffer::ToSECItem(PLArenaPool* aArena, SECItem* aItem) const {
}
JSObject* CryptoBuffer::ToUint8Array(JSContext* aCx) const {
return Uint8Array::Create(aCx, Length(), Elements());
return Uint8Array::Create(aCx, *this);
}
JSObject* CryptoBuffer::ToArrayBuffer(JSContext* aCx) const {
return ArrayBuffer::Create(aCx, Length(), Elements());
return ArrayBuffer::Create(aCx, *this);
}
// "BigInt" comes from the WebCrypto spec

Просмотреть файл

@ -33,7 +33,7 @@ GamepadTouch::~GamepadTouch() { mozilla::DropJSObjects(this); }
void GamepadTouch::GetPosition(JSContext* aCx,
JS::MutableHandle<JSObject*> aRetval,
ErrorResult& aRv) {
mPosition = Float32Array::Create(aCx, this, 2, mTouchState.position);
mPosition = Float32Array::Create(aCx, this, mTouchState.position);
if (!mPosition) {
aRv.NoteJSContextException(aCx);
return;

Просмотреть файл

@ -94,8 +94,7 @@ void MediaEncryptedEvent::GetInitData(JSContext* cx,
JS::MutableHandle<JSObject*> aData,
ErrorResult& aRv) {
if (mRawInitData.Length()) {
mInitData = ArrayBuffer::Create(cx, this, mRawInitData.Length(),
mRawInitData.Elements());
mInitData = ArrayBuffer::Create(cx, this, mRawInitData);
if (!mInitData) {
aRv.NoteJSContextException(cx);
return;

Просмотреть файл

@ -89,8 +89,7 @@ void MediaKeyMessageEvent::GetMessage(JSContext* cx,
JS::MutableHandle<JSObject*> aMessage,
ErrorResult& aRv) {
if (!mMessage) {
mMessage = ArrayBuffer::Create(cx, this, mRawMessage.Length(),
mRawMessage.Elements());
mMessage = ArrayBuffer::Create(cx, this, mRawMessage);
if (!mMessage) {
aRv.NoteJSContextException(cx);
return;

Просмотреть файл

@ -372,8 +372,7 @@ void WaveShaperNode::GetCurve(JSContext* aCx,
}
MOZ_ASSERT(mCurve.Length() >= 2);
aRetval.set(
Float32Array::Create(aCx, this, mCurve.Length(), mCurve.Elements()));
aRetval.set(Float32Array::Create(aCx, this, mCurve));
}
void WaveShaperNode::SetOversample(OverSampleType aType) {

Просмотреть файл

@ -83,8 +83,7 @@ void MIDIMessageEvent::GetData(JSContext* cx,
JS::MutableHandle<JSObject*> aData,
ErrorResult& aRv) {
if (!mData) {
mData =
Uint8Array::Create(cx, this, mRawData.Length(), mRawData.Elements());
mData = Uint8Array::Create(cx, this, mRawData);
if (!mData) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;

Просмотреть файл

@ -578,8 +578,7 @@ nsresult UDPSocket::DispatchReceivedData(const nsACString& aRemoteAddress,
JSContext* cx = jsapi.cx();
// Copy packet data to ArrayBuffer
JS::Rooted<JSObject*> arrayBuf(
cx, ArrayBuffer::Create(cx, aData.Length(), aData.Elements()));
JS::Rooted<JSObject*> arrayBuf(cx, ArrayBuffer::Create(cx, aData));
if (NS_WARN_IF(!arrayBuf)) {
return NS_ERROR_FAILURE;

Просмотреть файл

@ -26,8 +26,7 @@ void PushUtil::CopyArrayToArrayBuffer(JSContext* aCx,
aValue.set(nullptr);
return;
}
JS::Rooted<JSObject*> buffer(
aCx, ArrayBuffer::Create(aCx, aArray.Length(), aArray.Elements()));
JS::Rooted<JSObject*> buffer(aCx, ArrayBuffer::Create(aCx, aArray));
if (NS_WARN_IF(!buffer)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;

Просмотреть файл

@ -670,8 +670,7 @@ class PushEventOp final : public ExtendableEventOp {
if (args.data().type() != OptionalPushData::Tvoid_t) {
auto& bytes = args.data().get_ArrayOfuint8_t();
JSObject* data =
Uint8Array::Create(aCx, bytes.Length(), bytes.Elements());
JSObject* data = Uint8Array::Create(aCx, bytes);
if (!data) {
result = ErrorResult(NS_ERROR_FAILURE);

Просмотреть файл

@ -146,8 +146,7 @@ void VREyeParameters::GetOffset(JSContext* aCx,
ErrorResult& aRv) {
if (!mOffset) {
// Lazily create the Float32Array
mOffset =
dom::Float32Array::Create(aCx, this, 3, mEyeTranslation.components);
mOffset = dom::Float32Array::Create(aCx, this, mEyeTranslation.components);
if (!mOffset) {
aRv.NoteJSContextException(aCx);
return;
@ -201,7 +200,7 @@ void VRStageParameters::GetSittingToStandingTransform(
if (!mSittingToStandingTransformArray) {
// Lazily create the Float32Array
mSittingToStandingTransformArray = dom::Float32Array::Create(
aCx, this, 16, mSittingToStandingTransform.components);
aCx, this, mSittingToStandingTransform.components);
if (!mSittingToStandingTransformArray) {
aRv.NoteJSContextException(aCx);
return;

Просмотреть файл

@ -58,8 +58,7 @@ JSObject* AuthenticatorAssertionResponse::WrapObject(
void AuthenticatorAssertionResponse::GetAuthenticatorData(
JSContext* aCx, JS::MutableHandle<JSObject*> aValue, ErrorResult& aRv) {
if (!mAuthenticatorDataCachedObj) {
mAuthenticatorDataCachedObj = ArrayBuffer::Create(
aCx, mAuthenticatorData.Length(), mAuthenticatorData.Elements());
mAuthenticatorDataCachedObj = ArrayBuffer::Create(aCx, mAuthenticatorData);
if (!mAuthenticatorDataCachedObj) {
aRv.NoteJSContextException(aCx);
return;
@ -76,8 +75,7 @@ void AuthenticatorAssertionResponse::SetAuthenticatorData(
void AuthenticatorAssertionResponse::GetSignature(
JSContext* aCx, JS::MutableHandle<JSObject*> aValue, ErrorResult& aRv) {
if (!mSignatureCachedObj) {
mSignatureCachedObj =
ArrayBuffer::Create(aCx, mSignature.Length(), mSignature.Elements());
mSignatureCachedObj = ArrayBuffer::Create(aCx, mSignature);
if (!mSignatureCachedObj) {
aRv.NoteJSContextException(aCx);
return;
@ -100,8 +98,7 @@ void AuthenticatorAssertionResponse::GetUserHandle(
aValue.set(nullptr);
} else {
if (!mUserHandleCachedObj) {
mUserHandleCachedObj = ArrayBuffer::Create(aCx, mUserHandle.Length(),
mUserHandle.Elements());
mUserHandleCachedObj = ArrayBuffer::Create(aCx, mUserHandle);
if (!mUserHandleCachedObj) {
aRv.NoteJSContextException(aCx);
return;

Просмотреть файл

@ -54,8 +54,7 @@ JSObject* AuthenticatorAttestationResponse::WrapObject(
void AuthenticatorAttestationResponse::GetAttestationObject(
JSContext* aCx, JS::MutableHandle<JSObject*> aValue, ErrorResult& aRv) {
if (!mAttestationObjectCachedObj) {
mAttestationObjectCachedObj = ArrayBuffer::Create(
aCx, mAttestationObject.Length(), mAttestationObject.Elements());
mAttestationObjectCachedObj = ArrayBuffer::Create(aCx, mAttestationObject);
if (!mAttestationObjectCachedObj) {
aRv.NoteJSContextException(aCx);
return;
@ -106,8 +105,7 @@ void AuthenticatorAttestationResponse::GetAuthenticatorData(
return;
}
JS::Heap<JSObject*> buffer(ArrayBuffer::Create(
aCx, authenticatorData.Length(), authenticatorData.Elements()));
JS::Heap<JSObject*> buffer(ArrayBuffer::Create(aCx, authenticatorData));
if (!buffer) {
aRv.NoteJSContextException(aCx);
return;
@ -145,8 +143,7 @@ void AuthenticatorAttestationResponse::GetPublicKey(
return;
}
JS::Heap<JSObject*> buffer(
ArrayBuffer::Create(aCx, publicKey.Length(), publicKey.Elements()));
JS::Heap<JSObject*> buffer(ArrayBuffer::Create(aCx, publicKey));
if (!buffer) {
aRv.NoteJSContextException(aCx);
return;

Просмотреть файл

@ -65,8 +65,7 @@ void PublicKeyCredential::GetRawId(JSContext* aCx,
JS::MutableHandle<JSObject*> aValue,
ErrorResult& aRv) {
if (!mRawIdCachedObj) {
mRawIdCachedObj =
ArrayBuffer::Create(aCx, mRawId.Length(), mRawId.Elements());
mRawIdCachedObj = ArrayBuffer::Create(aCx, mRawId);
if (!mRawIdCachedObj) {
aRv.NoteJSContextException(aCx);
return;

Просмотреть файл

@ -224,8 +224,7 @@ void IncomingDatagramStreamAlgorithms::ReturnDatagram(JSContext* aCx,
UniquePtr<DatagramEntry> entry = mDatagrams->mIncomingDatagramsQueue.Pop();
// Pull Step 6: Let chunk be a new Uint8Array object representing bytes.
JSObject* outView = Uint8Array::Create(aCx, entry->mBuffer.Length(),
entry->mBuffer.Elements());
JSObject* outView = Uint8Array::Create(aCx, entry->mBuffer);
if (!outView) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;

Просмотреть файл

@ -223,8 +223,7 @@ nsUDPMessage::GetOutputStream(nsIOutputStream** aOutputStream) {
NS_IMETHODIMP
nsUDPMessage::GetRawData(JSContext* cx, JS::MutableHandle<JS::Value> aRawData) {
if (!mJsobj) {
mJsobj =
dom::Uint8Array::Create(cx, nullptr, mData.Length(), mData.Elements());
mJsobj = dom::Uint8Array::Create(cx, nullptr, mData);
HoldJSObjects(this);
}
aRawData.setObject(*mJsobj);

Просмотреть файл

@ -201,7 +201,7 @@ void StreamFilter::FireDataEvent(const nsTArray<uint8_t>& aData) {
init.mBubbles = false;
init.mCancelable = false;
auto buffer = ArrayBuffer::Create(cx, aData.Length(), aData.Elements());
auto buffer = ArrayBuffer::Create(cx, aData);
if (!buffer) {
// TODO: There is no way to recover from this. This chunk of data is lost.
FireErrorEvent(u"Out of memory"_ns);

Просмотреть файл

@ -478,9 +478,8 @@ nsProfiler::GetProfileDataAsArrayBuffer(double aSinceTime, JSContext* aCx,
}
JSContext* cx = jsapi.cx();
JSObject* typedArray = dom::ArrayBuffer::Create(
cx, aResult.mProfile.Length(),
reinterpret_cast<const uint8_t*>(aResult.mProfile.Data()));
JSObject* typedArray =
dom::ArrayBuffer::Create(cx, aResult.mProfile);
if (typedArray) {
JS::Rooted<JS::Value> val(cx, JS::ObjectValue(*typedArray));
promise->MaybeResolve(val);
@ -582,8 +581,7 @@ nsProfiler::GetProfileDataAsGzippedArrayBuffer(double aSinceTime,
JSContext* cx = jsapi.cx();
// Get the profile typedArray.
JSObject* typedArray = dom::ArrayBuffer::Create(
cx, outBuff.Length(), outBuff.Elements());
JSObject* typedArray = dom::ArrayBuffer::Create(cx, outBuff);
if (!typedArray) {
promise->MaybeReject(NS_ERROR_OUT_OF_MEMORY);
return;
@ -706,14 +704,11 @@ nsProfiler::GetSymbolTable(const nsACString& aDebugPath,
JSContext* cx = jsapi.cx();
JS::Rooted<JSObject*> addrsArray(
cx, dom::Uint32Array::Create(cx, aSymbolTable.mAddrs.Length(),
aSymbolTable.mAddrs.Elements()));
cx, dom::Uint32Array::Create(cx, aSymbolTable.mAddrs));
JS::Rooted<JSObject*> indexArray(
cx, dom::Uint32Array::Create(cx, aSymbolTable.mIndex.Length(),
aSymbolTable.mIndex.Elements()));
cx, dom::Uint32Array::Create(cx, aSymbolTable.mIndex));
JS::Rooted<JSObject*> bufferArray(
cx, dom::Uint8Array::Create(cx, aSymbolTable.mBuffer.Length(),
aSymbolTable.mBuffer.Elements()));
cx, dom::Uint8Array::Create(cx, aSymbolTable.mBuffer));
if (addrsArray && indexArray && bufferArray) {
JS::Rooted<JSObject*> tuple(cx, JS::NewArrayObject(cx, 3));