[Readable Streams API] Enable creation of ReadableByteStreamController
[WebKit-https.git] / Source / WebCore / bindings / js / JSReadableStreamPrivateConstructors.cpp
1 /*
2  *  Copyright (C) 2015 Canon Inc. All rights reserved.
3  *  Copyright (C) 2016 Apple Inc. All rights reserved.
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include "config.h"
21 #include "JSReadableStreamPrivateConstructors.h"
22
23 #if ENABLE(READABLE_STREAM_API)
24
25 #include "JSDOMBinding.h"
26 #include "JSDOMConstructor.h"
27 #include "JSReadableByteStreamController.h"
28 #include "JSReadableStream.h"
29 #include "JSReadableStreamDefaultController.h"
30 #include "JSReadableStreamDefaultReader.h"
31 #include "ReadableByteStreamInternalsBuiltins.h"
32 #include "ReadableStreamInternalsBuiltins.h"
33 #include <runtime/CallData.h>
34
35 using namespace JSC;
36
37 namespace WebCore {
38
39 // Public JS ReadableStreamReader and ReadableStreamDefaultController constructor callbacks.
40 EncodedJSValue JSC_HOST_CALL constructJSReadableStreamDefaultController(ExecState& exec)
41 {
42     VM& vm = exec.vm();
43     auto scope = DECLARE_THROW_SCOPE(vm);
44     return throwVMTypeError(&exec, scope, ASCIILiteral("ReadableStreamDefaultController constructor should not be called directly"));
45 }
46
47 #if ENABLE(READABLE_BYTE_STREAM_API)
48 // Public JS ReadableByteStreamController constructor callback.
49 EncodedJSValue JSC_HOST_CALL constructJSReadableByteStreamController(ExecState& exec)
50 {
51     VM& vm = exec.vm();
52     auto scope = DECLARE_THROW_SCOPE(vm);
53     return throwVMTypeError(&exec, scope, ASCIILiteral("ReadableByteStreamController constructor should not be called directly"));
54 }
55 #endif
56
57 EncodedJSValue JSC_HOST_CALL constructJSReadableStreamDefaultReader(ExecState& exec)
58 {
59     VM& vm = exec.vm();
60     auto scope = DECLARE_THROW_SCOPE(vm);
61
62     JSReadableStream* stream = jsDynamicDowncast<JSReadableStream*>(exec.argument(0));
63     if (!stream)
64         return throwArgumentTypeError(exec, scope, 0, "stream", "ReadableStreamReader", nullptr, "ReadableStream");
65
66     JSValue jsFunction = stream->get(&exec, Identifier::fromString(&exec, "getReader"));
67
68     CallData callData;
69     CallType callType = getCallData(jsFunction, callData);
70     MarkedArgumentBuffer noArguments;
71     return JSValue::encode(call(&exec, jsFunction, callType, callData, stream, noArguments));
72 }
73
74 // Private JS ReadableStreamDefaultReader and ReadableStreamDefaultController constructors.
75 using JSBuiltinReadableStreamDefaultReaderPrivateConstructor = JSBuiltinConstructor<JSReadableStreamDefaultReader>;
76 using JSBuiltinReadableStreamDefaultControllerPrivateConstructor =  JSBuiltinConstructor<JSReadableStreamDefaultController>;
77 #if ENABLE(READABLE_BYTE_STREAM_API)
78 // Private JS ReadableByteStreamController constructor.
79 using JSBuiltinReadableByteStreamControllerPrivateConstructor = JSBuiltinConstructor<JSReadableByteStreamController>;
80 #endif
81
82 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSBuiltinReadableStreamDefaultReaderPrivateConstructor);
83 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSBuiltinReadableStreamDefaultControllerPrivateConstructor);
84 #if ENABLE(READABLE_BYTE_STREAM_API)
85 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSBuiltinReadableByteStreamControllerPrivateConstructor);
86 #endif
87
88 template<> const ClassInfo JSBuiltinReadableStreamDefaultReaderPrivateConstructor::s_info = { "ReadableStreamDefaultReaderPrivateConstructor", &Base::s_info, 0, CREATE_METHOD_TABLE(JSBuiltinReadableStreamDefaultReaderPrivateConstructor) };
89 template<> const ClassInfo JSBuiltinReadableStreamDefaultControllerPrivateConstructor::s_info = { "ReadableStreamDefaultControllerPrivateConstructor", &Base::s_info, 0, CREATE_METHOD_TABLE(JSBuiltinReadableStreamDefaultControllerPrivateConstructor) };
90 #if ENABLE(READABLE_BYTE_STREAM_API)
91 template<> const ClassInfo JSBuiltinReadableByteStreamControllerPrivateConstructor::s_info = { "ReadableByteStreamControllerPrivateConstructor", &Base::s_info, 0, CREATE_METHOD_TABLE(JSBuiltinReadableByteStreamControllerPrivateConstructor) };
92 #endif
93
94 template<> FunctionExecutable* JSBuiltinReadableStreamDefaultReaderPrivateConstructor::initializeExecutable(JSC::VM& vm)
95 {
96     return readableStreamInternalsPrivateInitializeReadableStreamDefaultReaderCodeGenerator(vm);
97 }
98
99 template<> FunctionExecutable* JSBuiltinReadableStreamDefaultControllerPrivateConstructor::initializeExecutable(JSC::VM& vm)
100 {
101     return readableStreamInternalsPrivateInitializeReadableStreamDefaultControllerCodeGenerator(vm);
102 }
103
104 #if ENABLE(READABLE_BYTE_STREAM_API)
105 template<> FunctionExecutable* JSBuiltinReadableByteStreamControllerPrivateConstructor::initializeExecutable(JSC::VM& vm)
106 {
107     return readableByteStreamInternalsPrivateInitializeReadableByteStreamControllerCodeGenerator(vm);
108 }
109 #endif
110
111 JSObject* createReadableStreamDefaultReaderPrivateConstructor(VM& vm, JSDOMGlobalObject& globalObject)
112 {
113     return JSBuiltinReadableStreamDefaultReaderPrivateConstructor::create(vm, JSBuiltinReadableStreamDefaultReaderPrivateConstructor::createStructure(vm, globalObject, globalObject.objectPrototype()), globalObject);
114 }
115
116 JSObject* createReadableStreamDefaultControllerPrivateConstructor(VM& vm, JSDOMGlobalObject& globalObject)
117 {
118     return JSBuiltinReadableStreamDefaultControllerPrivateConstructor::create(vm, JSBuiltinReadableStreamDefaultControllerPrivateConstructor::createStructure(vm, globalObject, globalObject.objectPrototype()), globalObject);
119 }
120
121 #if ENABLE(READABLE_BYTE_STREAM_API)
122 JSObject* createReadableByteStreamControllerPrivateConstructor(VM& vm, JSDOMGlobalObject& globalObject)
123 {
124     return JSBuiltinReadableByteStreamControllerPrivateConstructor::create(vm, JSBuiltinReadableByteStreamControllerPrivateConstructor::createStructure(vm, globalObject, globalObject.objectPrototype()), globalObject);
125 }
126 #endif
127
128 } // namespace WebCore
129
130 #endif