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-11 08:24:04 +00:00
parent 882dd2b836
commit 29a54c8a17
20 changed files with 48 additions and 89 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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