[Streams API] Calling controller.error() should trigger storing an undefined error
[WebKit-https.git] / Source / WebCore / bindings / js / JSReadableStreamControllerCustom.cpp
index 846ac70..12d49cb 100644 (file)
@@ -33,8 +33,7 @@
 #if ENABLE(STREAMS_API)
 
 #include "JSDOMBinding.h"
-#include "NotImplemented.h"
-#include "ReadableStreamJSSource.h"
+#include "ReadableJSStream.h"
 #include <runtime/Error.h>
 
 using namespace JSC;
@@ -43,36 +42,39 @@ namespace WebCore {
 
 JSValue JSReadableStreamController::close(ExecState* exec)
 {
-    ReadableJSStream* stream = impl().stream();
-    if (!stream)
-        return exec->vm().throwException(exec, createTypeError(exec, ASCIILiteral("Controller has no readablestream")));
+    ReadableJSStream& stream = impl().stream();
     // FIXME: Handle the case of draining.
-    if (stream->internalState() != ReadableStream::State::Readable)
+    if (!stream.isReadable())
         return exec->vm().throwException(exec, createTypeError(exec, ASCIILiteral("Calling close on a stream which is not readable")));
-    stream->changeStateToClosed();
+    stream.changeStateToClosed();
     return jsUndefined();
 }
 
 JSValue JSReadableStreamController::enqueue(ExecState* exec)
 {
-    ReadableJSStream* stream = impl().stream();
-    if (!stream)
-        return exec->vm().throwException(exec, createTypeError(exec, ASCIILiteral("Controller has no readablestream")));
-    notImplemented();
-    return jsBoolean(false);
+    ReadableJSStream& stream = impl().stream();
+    if (stream.isErrored())
+        return exec->vm().throwException(exec, stream.error());
+    if (stream.isCloseRequested())
+        return exec->vm().throwException(exec, createTypeError(exec, ASCIILiteral("Calling enqueue on a stream which is closing")));
+    stream.enqueue(*exec);
+    return jsUndefined();
 }
 
-JSValue JSReadableStreamController::error(ExecState* exec)
+JSValue JSReadableStreamController::error(ExecState* state)
 {
-    ReadableJSStream* stream = impl().stream();
-    if (!stream)
-        return exec->vm().throwException(exec, createTypeError(exec, ASCIILiteral("Controller has no readablestream")));
-    if (stream->internalState() != ReadableStream::State::Readable)
-        return exec->vm().throwException(exec, createTypeError(exec, ASCIILiteral("Calling error on a stream which is not readable")));
-    notImplemented();
+    ReadableJSStream& stream = impl().stream();
+    if (!stream.isReadable())
+        return state->vm().throwException(state, createTypeError(state, ASCIILiteral("Calling error on a stream which is not readable")));
+    stream.storeError(*state, state->argument(0));
     return jsUndefined();
 }
 
+EncodedJSValue JSC_HOST_CALL constructJSReadableStreamController(ExecState* exec)
+{
+    return throwVMError(exec, createTypeError(exec, ASCIILiteral("ReadableStreamController constructor should not be called directly")));
+}
+
 } // namespace WebCore
 
 #endif