Bug 1812315 - Part 2: Implement ReadableStream::CreateNative and WritableStream::ErrorNative for WebTransport r=evilpie

Differential Revision: https://phabricator.services.mozilla.com/D167825
This commit is contained in:
Kagami Sascha Rosylight 2023-01-31 17:31:09 +00:00
Родитель d377991e6f
Коммит ad248024a6
5 изменённых файлов: 75 добавлений и 21 удалений

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

@ -1003,6 +1003,45 @@ already_AddRefed<ReadableStream> CreateReadableByteStream(
return stream.forget();
}
// https://streams.spec.whatwg.org/#readablestream-set-up
// (except this instead creates a new ReadableStream rather than accepting an
// existing instance)
already_AddRefed<ReadableStream> ReadableStream::CreateNative(
JSContext* aCx, nsIGlobalObject* aGlobal,
UnderlyingSourceAlgorithmsWrapper& aAlgorithms,
mozilla::Maybe<double> aHighWaterMark, QueuingStrategySize* aSizeAlgorithm,
ErrorResult& aRv) {
// an optional number highWaterMark (default 1)
double highWaterMark = aHighWaterMark.valueOr(1);
// and if given, highWaterMark must be a non-negative, non-NaN number.
MOZ_ASSERT(IsNonNegativeNumber(highWaterMark));
// Step 1: Let startAlgorithm be an algorithm that returns undefined.
// Step 2: Let pullAlgorithmWrapper be an algorithm that runs these steps:
// Step 3: Let cancelAlgorithmWrapper be an algorithm that runs these steps:
// (Done by UnderlyingSourceAlgorithmsWrapper)
// Step 4: If sizeAlgorithm was not given, then set it to an algorithm that
// returns 1. (Callers will treat nullptr as such, see
// ReadableStream::Constructor for details)
// Step 5: Perform ! InitializeReadableStream(stream).
auto stream = MakeRefPtr<ReadableStream>(aGlobal);
// Step 6: Let controller be a new ReadableStreamDefaultController.
auto controller = MakeRefPtr<ReadableStreamDefaultController>(aGlobal);
// Step 7: Perform ! SetUpReadableStreamDefaultController(stream, controller,
// startAlgorithm, pullAlgorithmWrapper, cancelAlgorithmWrapper,
// highWaterMark, sizeAlgorithm).
SetUpReadableStreamDefaultController(aCx, stream, controller, &aAlgorithms,
highWaterMark, aSizeAlgorithm, aRv);
if (aRv.Failed()) {
return nullptr;
}
return stream.forget();
}
// https://streams.spec.whatwg.org/#readablestream-set-up-with-byte-reading-support
// (except this instead creates a new ReadableStream rather than accepting an
// existing instance)

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

@ -103,7 +103,14 @@ class ReadableStream : public nsISupports, public nsWrapperCache {
JS::MutableHandle<JSObject*> aReturnObject);
// Public functions to implement other specs
// https://streams.spec.whatwg.org/#other-specs-rs-create
// https://streams.spec.whatwg.org/#other-specs-rs
// https://streams.spec.whatwg.org/#readablestream-set-up
MOZ_CAN_RUN_SCRIPT static already_AddRefed<ReadableStream> CreateNative(
JSContext* aCx, nsIGlobalObject* aGlobal,
UnderlyingSourceAlgorithmsWrapper& aAlgorithms,
mozilla::Maybe<double> aHighWaterMark,
QueuingStrategySize* aSizeAlgorithm, ErrorResult& aRv);
// https://streams.spec.whatwg.org/#readablestream-set-up-with-byte-reading-support
MOZ_CAN_RUN_SCRIPT static already_AddRefed<ReadableStream> CreateByteNative(

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

@ -788,4 +788,15 @@ void WritableStream::SetUpNative(JSContext* aCx,
highWaterMark, aSizeAlgorithm, aRv);
}
// https://streams.spec.whatwg.org/#writablestream-error
// To error a WritableStream stream given a JavaScript value e, perform !
// WritableStreamDefaultControllerErrorIfNeeded(stream.[[controller]], e).
void WritableStream::ErrorNative(JSContext* aCx, JS::Handle<JS::Value> aError,
ErrorResult& aRv) {
// MOZ_KnownLive here instead of MOZ_KNOWN_LIVE at the field, because
// mController is set outside of the constructor
WritableStreamDefaultControllerErrorIfNeeded(aCx, MOZ_KnownLive(mController),
aError, aRv);
}
} // namespace mozilla::dom

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

@ -163,7 +163,15 @@ class WritableStream : public nsISupports, public nsWrapperCache {
Maybe<double> aHighWaterMark, QueuingStrategySize* aSizeAlgorithm,
ErrorResult& aRv);
// The following definitions must only be used on WritableStream instances
// initialized via the above set up algorithm:
public:
// https://streams.spec.whatwg.org/#writablestream-error
MOZ_CAN_RUN_SCRIPT void ErrorNative(JSContext* aCx,
JS::Handle<JS::Value> aError,
ErrorResult& aRv);
// IDL layer functions
nsIGlobalObject* GetParentObject() const { return mGlobal; }

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

@ -225,8 +225,8 @@ void WebTransport::Init(const GlobalObject& aGlobal, const nsAString& aURL,
new WebTransportIncomingStreamsAlgorithms(mIncomingBidirectionalPromise,
false, this);
mIncomingBidirectionalStreams = CreateReadableStream(
cx, global, algorithm, Some(0.0), nullptr, aError); // XXX
mIncomingBidirectionalStreams = ReadableStream::CreateNative(
cx, global, *algorithm, Some(0.0), nullptr, aError); // XXX
if (aError.Failed()) {
return;
}
@ -245,8 +245,8 @@ void WebTransport::Init(const GlobalObject& aGlobal, const nsAString& aURL,
algorithm = new WebTransportIncomingStreamsAlgorithms(
mIncomingUnidirectionalPromise, true, this);
mIncomingUnidirectionalStreams =
CreateReadableStream(cx, global, algorithm, Some(0.0), nullptr, aError);
mIncomingUnidirectionalStreams = ReadableStream::CreateNative(
cx, global, *algorithm, Some(0.0), nullptr, aError);
if (aError.Failed()) {
return;
}
@ -494,18 +494,13 @@ void WebTransport::Cleanup(WebTransportError* aError,
}
for (const auto& stream : sendStreams) {
RefPtr<WritableStreamDefaultController> controller = stream->Controller();
WritableStreamDefaultControllerErrorIfNeeded(cx, controller, errorValue,
IgnoreErrors());
// This MOZ_KnownLive is redundant, see bug 1620312
MOZ_KnownLive(stream)->ErrorNative(cx, errorValue, IgnoreErrors());
}
// Step 11: For each receiveStream in receiveStreams, error receiveStream with
// error.
for (const auto& stream : receiveStreams) {
RefPtr<ReadableStreamDefaultController> controller =
stream->Controller()->AsDefault();
// XXX replace with ErrorNative() when bug 1809895 lands
ReadableStreamDefaultControllerError(cx, controller, errorValue,
IgnoreErrors());
stream->ErrorNative(cx, errorValue, IgnoreErrors());
}
// Step 12:
if (aCloseInfo) {
@ -527,15 +522,9 @@ void WebTransport::Cleanup(WebTransportError* aError,
// 13.2: Reject ready with error
mReady->MaybeReject(errorValue);
// 13.3: Error incomingBidirectionalStreams with error
RefPtr<ReadableStreamDefaultController> controller =
mIncomingBidirectionalStreams->Controller()->AsDefault();
// XXX replace with ErrorNative() when bug 1809895 lands
ReadableStreamDefaultControllerError(cx, controller, errorValue,
IgnoreErrors());
mIncomingBidirectionalStreams->ErrorNative(cx, errorValue, IgnoreErrors());
// 13.4: Error incomingUnidirectionalStreams with error
controller = mIncomingUnidirectionalStreams->Controller()->AsDefault();
ReadableStreamDefaultControllerError(cx, controller, errorValue,
IgnoreErrors());
mIncomingUnidirectionalStreams->ErrorNative(cx, errorValue, IgnoreErrors());
}
// abort any pending pulls from Incoming*Streams (not in spec)
mIncomingUnidirectionalPromise->MaybeResolveWithUndefined();