ca97e9611d0322b652dcce4f6ef98e48a2d7336d
[WebKit-https.git] / Source / WebCore / bindings / js / JSReadableStreamPrivateConstructors.cpp
1 /*
2  *  Copyright (C) 2015 Canon Inc. All rights reserved.
3  *  Copyright (C) 2016-2017 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(STREAMS_API)
24
25 #include "JSDOMBuiltinConstructor.h"
26 #include "JSReadableByteStreamController.h"
27 #include "JSReadableStream.h"
28 #include "JSReadableStreamBYOBReader.h"
29 #include "JSReadableStreamBYOBRequest.h"
30 #include "JSReadableStreamDefaultController.h"
31 #include "JSReadableStreamDefaultReader.h"
32 #include "ReadableByteStreamInternalsBuiltins.h"
33 #include "ReadableStreamInternalsBuiltins.h"
34 #include "WebCoreJSClientData.h"
35 #include <JavaScriptCore/JSCInlines.h>
36
37 namespace WebCore {
38 using namespace JSC;
39
40 enum class ReaderType {
41     Byob,
42     Default,
43 };
44
45 template <ReaderType type>
46 EncodedJSValue JSC_HOST_CALL constructJSReadableStreamReaderGeneric(ExecState&);
47
48 // Public JS ReadableStreamDefaultController constructor callback.
49 EncodedJSValue JSC_HOST_CALL constructJSReadableStreamDefaultController(ExecState& exec)
50 {
51     VM& vm = exec.vm();
52     auto scope = DECLARE_THROW_SCOPE(vm);
53     return throwVMTypeError(&exec, scope, ASCIILiteral("ReadableStreamDefaultController constructor should not be called directly"));
54 }
55
56 // Public JS ReadableStreamDefaultReader and ReadableStreamBYOBReader callbacks.
57 template<ReaderType type>
58 EncodedJSValue JSC_HOST_CALL constructJSReadableStreamReaderGeneric(ExecState& exec)
59 {
60     VM& vm = exec.vm();
61     auto& clientData = *static_cast<JSVMClientData*>(vm.clientData);
62     auto& globalObject = *JSC::jsCast<JSDOMGlobalObject*>(exec.lexicalGlobalObject());
63
64     JSC::JSObject* constructor;
65     if (type == ReaderType::Byob)
66         constructor = JSC::asObject(globalObject.get(&exec, clientData.builtinNames().ReadableStreamBYOBReaderPrivateName()));
67     else
68         constructor = JSC::asObject(globalObject.get(&exec, clientData.builtinNames().ReadableStreamDefaultReaderPrivateName()));
69
70     ConstructData constructData;
71     ConstructType constructType = constructor->methodTable(vm)->getConstructData(constructor, constructData);
72     ASSERT(constructType != ConstructType::None);
73
74     MarkedArgumentBuffer args;
75     args.append(exec.argument(0));
76     ASSERT(!args.hasOverflowed());
77     return JSValue::encode(JSC::construct(&exec, constructor, constructType, constructData, args));
78 }
79
80 EncodedJSValue JSC_HOST_CALL constructJSReadableStreamDefaultReader(ExecState& exec)
81 {
82     return constructJSReadableStreamReaderGeneric<ReaderType::Default>(exec);
83 }
84
85 EncodedJSValue JSC_HOST_CALL constructJSReadableStreamBYOBReader(ExecState& exec)
86 {
87     return constructJSReadableStreamReaderGeneric<ReaderType::Byob>(exec);
88 }
89
90 // Public JS ReadableByteStreamController and ReadableStreamBYOBRequest constructor callbacks.
91 EncodedJSValue JSC_HOST_CALL constructJSReadableByteStreamController(ExecState& exec)
92 {
93     VM& vm = exec.vm();
94     auto scope = DECLARE_THROW_SCOPE(vm);
95     return throwVMTypeError(&exec, scope, ASCIILiteral("ReadableByteStreamController constructor should not be called directly"));
96 }
97
98 EncodedJSValue JSC_HOST_CALL constructJSReadableStreamBYOBRequest(ExecState& exec)
99 {
100     VM& vm = exec.vm();
101     auto scope = DECLARE_THROW_SCOPE(vm);
102     return throwVMTypeError(&exec, scope, ASCIILiteral("ReadableStreamBYOBRequest constructor should not be called directly"));
103 }
104
105 // Private JS ReadableStreamDefaultReader and ReadableStreamDefaultController constructors.
106 using JSBuiltinReadableStreamDefaultReaderPrivateConstructor = JSDOMBuiltinConstructor<JSReadableStreamDefaultReader>;
107 using JSBuiltinReadableStreamDefaultControllerPrivateConstructor = JSDOMBuiltinConstructor<JSReadableStreamDefaultController>;
108 // Private JS ReadableByteStreamController, ReadableStreamBYOBReader and ReadableStreamBYOBRequest constructors.
109 using JSBuiltinReadableByteStreamControllerPrivateConstructor = JSDOMBuiltinConstructor<JSReadableByteStreamController>;
110 using JSBuiltinReadableStreamBYOBReaderPrivateConstructor = JSDOMBuiltinConstructor<JSReadableStreamBYOBReader>;
111 using JSBuiltinReadableStreamBYOBRequestPrivateConstructor = JSDOMBuiltinConstructor<JSReadableStreamBYOBRequest>;
112
113 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSBuiltinReadableStreamDefaultReaderPrivateConstructor);
114 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSBuiltinReadableStreamDefaultControllerPrivateConstructor);
115 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSBuiltinReadableByteStreamControllerPrivateConstructor);
116 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSBuiltinReadableStreamBYOBReaderPrivateConstructor);
117 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSBuiltinReadableStreamBYOBRequestPrivateConstructor);
118
119 template<> const ClassInfo JSBuiltinReadableStreamDefaultReaderPrivateConstructor::s_info = { "ReadableStreamDefaultReaderPrivateConstructor", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSBuiltinReadableStreamDefaultReaderPrivateConstructor) };
120 template<> const ClassInfo JSBuiltinReadableStreamDefaultControllerPrivateConstructor::s_info = { "ReadableStreamDefaultControllerPrivateConstructor", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSBuiltinReadableStreamDefaultControllerPrivateConstructor) };
121 template<> const ClassInfo JSBuiltinReadableByteStreamControllerPrivateConstructor::s_info = { "ReadableByteStreamControllerPrivateConstructor", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSBuiltinReadableByteStreamControllerPrivateConstructor) };
122 template<> const ClassInfo JSBuiltinReadableStreamBYOBReaderPrivateConstructor::s_info = { "ReadableStreamBYOBReaderPrivateConstructor", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSBuiltinReadableStreamBYOBReaderPrivateConstructor) };
123 template<> const ClassInfo JSBuiltinReadableStreamBYOBRequestPrivateConstructor::s_info = { "ReadableStreamBYOBRequestPrivateConstructor", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSBuiltinReadableStreamBYOBRequestPrivateConstructor) };
124
125 template<> FunctionExecutable* JSBuiltinReadableStreamDefaultReaderPrivateConstructor::initializeExecutable(JSC::VM& vm)
126 {
127     return readableStreamInternalsPrivateInitializeReadableStreamDefaultReaderCodeGenerator(vm);
128 }
129
130 template<> FunctionExecutable* JSBuiltinReadableStreamDefaultControllerPrivateConstructor::initializeExecutable(JSC::VM& vm)
131 {
132     return readableStreamInternalsPrivateInitializeReadableStreamDefaultControllerCodeGenerator(vm);
133 }
134
135 template<> FunctionExecutable* JSBuiltinReadableByteStreamControllerPrivateConstructor::initializeExecutable(JSC::VM& vm)
136 {
137     return readableByteStreamInternalsPrivateInitializeReadableByteStreamControllerCodeGenerator(vm);
138 }
139
140 template<> FunctionExecutable* JSBuiltinReadableStreamBYOBReaderPrivateConstructor::initializeExecutable(JSC::VM& vm)
141 {
142     return readableByteStreamInternalsPrivateInitializeReadableStreamBYOBReaderCodeGenerator(vm);
143 }
144
145 template<> FunctionExecutable* JSBuiltinReadableStreamBYOBRequestPrivateConstructor::initializeExecutable(JSC::VM& vm)
146 {
147     return readableByteStreamInternalsPrivateInitializeReadableStreamBYOBRequestCodeGenerator(vm);
148 }
149
150 JSObject* createReadableStreamDefaultReaderPrivateConstructor(VM& vm, JSDOMGlobalObject& globalObject)
151 {
152     return JSBuiltinReadableStreamDefaultReaderPrivateConstructor::create(vm, JSBuiltinReadableStreamDefaultReaderPrivateConstructor::createStructure(vm, globalObject, globalObject.objectPrototype()), globalObject);
153 }
154
155 JSObject* createReadableStreamDefaultControllerPrivateConstructor(VM& vm, JSDOMGlobalObject& globalObject)
156 {
157     return JSBuiltinReadableStreamDefaultControllerPrivateConstructor::create(vm, JSBuiltinReadableStreamDefaultControllerPrivateConstructor::createStructure(vm, globalObject, globalObject.objectPrototype()), globalObject);
158 }
159
160 JSObject* createReadableByteStreamControllerPrivateConstructor(VM& vm, JSDOMGlobalObject& globalObject)
161 {
162     return JSBuiltinReadableByteStreamControllerPrivateConstructor::create(vm, JSBuiltinReadableByteStreamControllerPrivateConstructor::createStructure(vm, globalObject, globalObject.objectPrototype()), globalObject);
163 }
164
165 JSObject* createReadableStreamBYOBReaderPrivateConstructor(VM& vm, JSDOMGlobalObject& globalObject)
166 {
167     return JSBuiltinReadableStreamBYOBReaderPrivateConstructor::create(vm, JSBuiltinReadableStreamBYOBReaderPrivateConstructor::createStructure(vm, globalObject, globalObject.objectPrototype()), globalObject);
168 }
169
170 JSObject* createReadableStreamBYOBRequestPrivateConstructor(VM& vm, JSDOMGlobalObject& globalObject)
171 {
172     return JSBuiltinReadableStreamBYOBRequestPrivateConstructor::create(vm, JSBuiltinReadableStreamBYOBRequestPrivateConstructor::createStructure(vm, globalObject, globalObject.objectPrototype()), globalObject);
173 }
174
175 } // namespace WebCore
176
177 #endif