[Streams API] Rework some readable stream internals that can be common to writable streams
https://bugs.webkit.org/show_bug.cgi?id=150133

Reviewed by Darin Adler.

Source/JavaScriptCore:

* runtime/CommonIdentifiers.h:
* runtime/JSGlobalObject.cpp:
(JSC::JSGlobalObject::init): Added RangeError also as native functions.

Source/WebCore:

There are some things in ReadableStream internals that be be used also for Writable Streams so it was necessary
to move some functions and refactor some code that can be shared by both implementations.

Queue was written with the functions declared at the implementation and keeping the improvement of having the
total size calculated instead of having to transverse the whole array.

The strategy is kept as an object and a common method is used to validate it as per spec.

Promises are reworked to keep in an internal slot inside the promise object the resolve and reject
functions. For convinience three functions were written, one to create the promise (and keep internally the
resolve and reject functions), one to resolve and another to reject. Promises can still be created with
Promise.resolve or reject as the resolve and rejectStreamsPromise functions operate under the assumption that
the internal slots might not exist.

invokeOrNoop and promiseInvokeOrNoop were moved to the common code as they will be also used by WritableStream.

Current test set suffices.

* CMakeLists.txt:
* DerivedSources.make:
* WebCore.xcodeproj/project.pbxproj:
* bindings/js/JSDOMWindowBase.cpp:
* bindings/js/WebCoreJSBuiltinInternals.h:
* bindings/js/WebCoreJSBuiltins.cpp:
* bindings/js/WebCoreJSBuiltins.h: Build infrastructure.
* Modules/streams/ReadableStream.js:
(initializeReadableStream): Reworked queue and strategy.
* Modules/streams/ReadableStreamInternals.js:
(privateInitializeReadableStreamReader):
(errorReadableStream):
(getReadableStreamDesiredSize):
(cancelReadableStream):
(closeReadableStream):
(closeReadableStreamReader):
(enqueueInReadableStream):
(readFromReadableStreamReader): Reworked queue, strategy and promises.
(invokeOrNoop): Deleted.
(promiseInvokeOrNoop): Deleted.
* Modules/streams/StreamInternals.js: Added.
(invokeOrNoop):
(promiseInvokeOrNoop): Moved from ReadableStreamInternals.js.
(validateAndNormalizeQueuingStrategy):
(createNewStreamsPromise):
(resolveStreamsPromise):
(rejectStreamsPromise):
(newQueue):
(dequeueValue):
(enqueueValueWithSize): Added according to the spec.
* bindings/js/WebCoreBuiltinNames.h: Updated private names according to the new slots.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@191335 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/bindings/js/JSDOMWindowBase.cpp b/Source/WebCore/bindings/js/JSDOMWindowBase.cpp
index 2af9799..fa2a309 100644
--- a/Source/WebCore/bindings/js/JSDOMWindowBase.cpp
+++ b/Source/WebCore/bindings/js/JSDOMWindowBase.cpp
@@ -50,6 +50,7 @@
 #if ENABLE(STREAMS_API)
 #include "JSReadableStreamPrivateConstructors.h"
 #include "ReadableStreamInternalsBuiltins.h"
+#include "StreamInternalsBuiltins.h"
 #endif
 
 using namespace JSC;
@@ -96,6 +97,12 @@
             m_privateFunctions.readableStreamInternals().m_##name##Function.get() , DontDelete | ReadOnly),
         WEBCOREREADABLESTREAMINTERNALS_FOREACH_BUILTIN_FUNCTION_NAME(DECLARE_GLOBAL_STATIC)
 #undef DECLARE_GLOBAL_STATIC
+#define DECLARE_GLOBAL_STATIC(name)\
+        GlobalPropertyInfo(\
+            static_cast<JSVMClientData*>(vm.clientData)->builtinFunctions().streamInternalsBuiltins().name##PrivateName(), \
+            m_privateFunctions.streamInternals().m_##name##Function.get() , DontDelete | ReadOnly),
+        WEBCORESTREAMINTERNALS_FOREACH_BUILTIN_FUNCTION_NAME(DECLARE_GLOBAL_STATIC)
+#undef DECLARE_GLOBAL_STATIC
 #endif
     };
 
diff --git a/Source/WebCore/bindings/js/WebCoreBuiltinNames.h b/Source/WebCore/bindings/js/WebCoreBuiltinNames.h
index be726c1..0fef220 100644
--- a/Source/WebCore/bindings/js/WebCoreBuiltinNames.h
+++ b/Source/WebCore/bindings/js/WebCoreBuiltinNames.h
@@ -34,17 +34,13 @@
 #define WEBCORE_COMMON_PRIVATE_IDENTIFIERS_EACH_PROPERTY_NAME(macro)\
     macro(closeRequested) \
     macro(closedPromise) \
-    macro(closedPromiseResolve) \
-    macro(closedPromiseReject) \
     macro(controlledReadableStream) \
     macro(controller) \
     macro(getUserMediaFromJS) \
-    macro(highWaterMark) \
     macro(ownerReadableStream) \
     macro(pulling) \
     macro(pullAgain) \
     macro(queue) \
-    macro(queueSize) \
     macro(readableStreamClosed) \
     macro(readableStreamErrored) \
     macro(readableStreamReadable) \
@@ -53,7 +49,7 @@
     macro(state) \
     macro(started) \
     macro(storedError) \
-    macro(strategySize) \
+    macro(strategy) \
     macro(underlyingSource) \
     macro(ReadableStreamReader) \
     macro(ReadableStreamController) \
diff --git a/Source/WebCore/bindings/js/WebCoreJSBuiltinInternals.h b/Source/WebCore/bindings/js/WebCoreJSBuiltinInternals.h
index 96344f9..e29f1ef 100644
--- a/Source/WebCore/bindings/js/WebCoreJSBuiltinInternals.h
+++ b/Source/WebCore/bindings/js/WebCoreJSBuiltinInternals.h
@@ -29,6 +29,7 @@
 
 #if ENABLE(STREAMS_API)
 #include "ReadableStreamInternalsBuiltinsWrapper.h"
+#include "StreamInternalsBuiltinsWrapper.h"
 #endif
 
 namespace WebCore {
@@ -39,15 +40,18 @@
         : vm(v)
 #if ENABLE(STREAMS_API)
         , m_readableStreamInternalsFunctions(vm)
+        , m_streamInternalsFunctions(vm)
 #endif
     { }
 
 #if ENABLE(STREAMS_API)
     ReadableStreamInternalsBuiltinFunctions readableStreamInternals() { return m_readableStreamInternalsFunctions; }
+    StreamInternalsBuiltinFunctions streamInternals() { return m_streamInternalsFunctions; }
 #endif
     void visit(JSC::SlotVisitor& visitor) {
 #if ENABLE(STREAMS_API)
         m_readableStreamInternalsFunctions.visit(visitor);
+        m_streamInternalsFunctions.visit(visitor);
 #else
         UNUSED_PARAM(visitor);
 #endif
@@ -55,6 +59,7 @@
     void init(JSC::JSGlobalObject& globalObject) {
 #if ENABLE(STREAMS_API)
         m_readableStreamInternalsFunctions.init(globalObject);
+        m_streamInternalsFunctions.init(globalObject);
 #else
         UNUSED_PARAM(globalObject);
 #endif
@@ -63,7 +68,8 @@
 private:
     JSC::VM& vm;
 #if ENABLE(STREAMS_API)
-     ReadableStreamInternalsBuiltinFunctions m_readableStreamInternalsFunctions;
+    ReadableStreamInternalsBuiltinFunctions m_readableStreamInternalsFunctions;
+    StreamInternalsBuiltinFunctions m_streamInternalsFunctions;
 #endif
 
 };
diff --git a/Source/WebCore/bindings/js/WebCoreJSBuiltins.cpp b/Source/WebCore/bindings/js/WebCoreJSBuiltins.cpp
index 41fe6e4..3460db3 100644
--- a/Source/WebCore/bindings/js/WebCoreJSBuiltins.cpp
+++ b/Source/WebCore/bindings/js/WebCoreJSBuiltins.cpp
@@ -37,5 +37,6 @@
 #include "ReadableStreamControllerBuiltins.cpp"
 #include "ReadableStreamInternalsBuiltins.cpp"
 #include "ReadableStreamReaderBuiltins.cpp"
+#include "StreamInternalsBuiltins.cpp"
 #include "WritableStreamBuiltins.cpp"
 #endif
diff --git a/Source/WebCore/bindings/js/WebCoreJSBuiltins.h b/Source/WebCore/bindings/js/WebCoreJSBuiltins.h
index 4d141ff..b4055c9 100644
--- a/Source/WebCore/bindings/js/WebCoreJSBuiltins.h
+++ b/Source/WebCore/bindings/js/WebCoreJSBuiltins.h
@@ -38,6 +38,7 @@
 #include "ReadableStreamControllerBuiltinsWrapper.h"
 #include "ReadableStreamInternalsBuiltinsWrapper.h"
 #include "ReadableStreamReaderBuiltinsWrapper.h"
+#include "StreamInternalsBuiltinsWrapper.h"
 #include "WritableStreamBuiltinsWrapper.h"
 #endif
 
@@ -56,6 +57,7 @@
         , m_readableStreamControllerBuiltins(&vm)
         , m_readableStreamInternalsBuiltins(&vm)
         , m_readableStreamReaderBuiltins(&vm)
+        , m_streamInternalsBuiltins(&vm)
         , m_writableStreamBuiltins(&vm)
 #endif
 #if ENABLE(MEDIA_STREAM)
@@ -64,6 +66,7 @@
     {
 #if ENABLE(STREAMS_API)
         m_readableStreamInternalsBuiltins.exportNames();
+        m_streamInternalsBuiltins.exportNames();
 #endif
     }
 #if ENABLE(STREAMS_API)
@@ -73,6 +76,7 @@
     ReadableStreamControllerBuiltinsWrapper& readableStreamControllerBuiltins() { return m_readableStreamControllerBuiltins; }
     ReadableStreamInternalsBuiltinsWrapper& readableStreamInternalsBuiltins() { return m_readableStreamInternalsBuiltins; }
     ReadableStreamReaderBuiltinsWrapper& readableStreamReaderBuiltins() { return m_readableStreamReaderBuiltins; }
+    StreamInternalsBuiltinsWrapper& streamInternalsBuiltins() { return m_streamInternalsBuiltins; }
     WritableStreamBuiltinsWrapper& writableStreamBuiltins() { return m_writableStreamBuiltins; }
 #endif
 #if ENABLE(MEDIA_STREAM)
@@ -88,6 +92,7 @@
     ReadableStreamControllerBuiltinsWrapper m_readableStreamControllerBuiltins;
     ReadableStreamInternalsBuiltinsWrapper m_readableStreamInternalsBuiltins;
     ReadableStreamReaderBuiltinsWrapper m_readableStreamReaderBuiltins;
+    StreamInternalsBuiltinsWrapper m_streamInternalsBuiltins;
     WritableStreamBuiltinsWrapper m_writableStreamBuiltins;
 #endif
 #if ENABLE(MEDIA_STREAM)