Skip to content

Commit

Permalink
Bug 1582348 - Implement |WritableStreamAbort|. r=arai
Browse files Browse the repository at this point in the history
Differential Revision: https://phabricator.services.mozilla.com/D49550

--HG--
extra : moz-landing-system : lando
  • Loading branch information
jswalden committed Oct 30, 2019
1 parent 37a13c4 commit df10b07
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
9 changes: 9 additions & 0 deletions js/src/builtin/streams/WritableStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,15 @@ class WritableStream : public NativeObject {
return flags() & PendingAbortRequestWasAlreadyErroring;
}

void setPendingAbortRequest(JSObject* promise, const JS::Value& reason,
bool wasAlreadyErroring) {
MOZ_ASSERT(!hasPendingAbortRequest());
MOZ_ASSERT(!(flags() & PendingAbortRequestWasAlreadyErroring));
setFixedSlot(Slot_PendingAbortRequestPromise, JS::ObjectValue(*promise));
setFixedSlot(Slot_PendingAbortRequestReason, reason);
setFlag(PendingAbortRequestWasAlreadyErroring, wasAlreadyErroring);
}

void clearPendingAbortRequest() {
MOZ_ASSERT(stateIsInitialized());
MOZ_ASSERT(hasPendingAbortRequest());
Expand Down
81 changes: 80 additions & 1 deletion js/src/builtin/streams/WritableStreamOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "builtin/streams/WritableStreamWriterOperations.h" // js::WritableStreamDefaultWriterEnsureReadyPromiseRejected
#include "js/Promise.h" // JS::{Reject,Resolve}Promise
#include "js/RootingAPI.h" // JS::Handle, JS::Rooted
#include "js/Value.h" // JS::Value, JS::ObjecValue
#include "js/Value.h" // JS::Value, JS::ObjecValue, JS::UndefinedHandleValue
#include "vm/Compartment.h" // JS::Compartment
#include "vm/JSContext.h" // JSContext

Expand All @@ -40,6 +40,7 @@ using JS::ObjectValue;
using JS::RejectPromise;
using JS::ResolvePromise;
using JS::Rooted;
using JS::UndefinedHandleValue;
using JS::Value;

/*** 4.3. General writable stream abstract operations. **********************/
Expand Down Expand Up @@ -105,6 +106,84 @@ void WritableStream::clearInFlightWriteRequest(JSContext* cx) {
MOZ_ASSERT(inFlightWriteRequest().isUndefined());
}

/**
* Streams spec, 4.3.6.
* WritableStreamAbort ( stream, reason )
*
* Note: The object (a promise) returned by this function is in the current
* compartment and does not require special wrapping to be put to use.
*/
JSObject* js::WritableStreamAbort(JSContext* cx,
Handle<WritableStream*> unwrappedStream,
Handle<Value> reason) {
cx->check(reason);

// Step 1: Let state be stream.[[state]].
// Step 2: If state is "closed" or "errored", return a promise resolved with
// undefined.
if (unwrappedStream->closed() || unwrappedStream->errored()) {
return PromiseObject::unforgeableResolve(cx, UndefinedHandleValue);
}

// Step 3: If stream.[[pendingAbortRequest]] is not undefined, return
// stream.[[pendingAbortRequest]].[[promise]].
if (unwrappedStream->hasPendingAbortRequest()) {
Rooted<JSObject*> pendingPromise(
cx, unwrappedStream->pendingAbortRequestPromise());
if (!cx->compartment()->wrap(cx, &pendingPromise)) {
return nullptr;
}
return pendingPromise;
}

// Step 4: Assert: state is "writable" or "erroring".
MOZ_ASSERT(unwrappedStream->writable() ^ unwrappedStream->erroring());

// Step 7: Let promise be a new promise (reordered).
Rooted<PromiseObject*> promise(cx, PromiseObject::createSkippingExecutor(cx));
if (!promise) {
return nullptr;
}

// Step 5: Let wasAlreadyErroring be false.
// Step 6: If state is "erroring",
// Step 6.a: Set wasAlreadyErroring to true.
// Step 6.b: Set reason to undefined.
bool wasAlreadyErroring = unwrappedStream->erroring();
Handle<Value> pendingReason =
wasAlreadyErroring ? UndefinedHandleValue : reason;

// Step 8: Set stream.[[pendingAbortRequest]] to
// Record {[[promise]]: promise, [[reason]]: reason,
// [[wasAlreadyErroring]]: wasAlreadyErroring}.
{
AutoRealm ar(cx, unwrappedStream);

Rooted<JSObject*> wrappedPromise(cx, promise);
Rooted<Value> wrappedPendingReason(cx, pendingReason);

JS::Compartment* comp = cx->compartment();
if (!comp->wrap(cx, &wrappedPromise) ||
!comp->wrap(cx, &wrappedPendingReason)) {
return nullptr;
}

unwrappedStream->setPendingAbortRequest(
wrappedPromise, wrappedPendingReason, wasAlreadyErroring);
}

// Step 9: If wasAlreadyErroring is false, perform
// ! WritableStreamStartErroring(stream, reason).
if (!wasAlreadyErroring) {
if (!WritableStreamStartErroring(cx, unwrappedStream, pendingReason)) {
return nullptr;
}
}

// Step 10: Return promise.
return promise;
}

/*** 4.4. Writable stream abstract operations used by controllers ***********/

/**
Expand Down
4 changes: 4 additions & 0 deletions js/src/builtin/streams/WritableStreamOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ namespace js {
class PromiseObject;
class WritableStream;

extern JSObject* WritableStreamAbort(
JSContext* cx, JS::Handle<WritableStream*> unwrappedStream,
JS::Handle<JS::Value> reason);

extern MOZ_MUST_USE PromiseObject* WritableStreamAddWriteRequest(
JSContext* cx, JS::Handle<WritableStream*> unwrappedStream);

Expand Down

0 comments on commit df10b07

Please sign in to comment.