9fa200e042c66aaca908bf090a9c1ea84345e940
[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 "JSReadableStream.h"
28 #include "JSReadableStreamDefaultController.h"
29 #include "JSReadableStreamDefaultReader.h"
30 #include "ReadableStreamInternalsBuiltins.h"
31 #include <runtime/CallData.h>
32
33 using namespace JSC;
34
35 namespace WebCore {
36
37 // Public JS ReadableStreamReder and ReadableStreamDefaultController constructor callbacks.
38 EncodedJSValue JSC_HOST_CALL constructJSReadableStreamDefaultController(ExecState& exec)
39 {
40     VM& vm = exec.vm();
41     auto scope = DECLARE_THROW_SCOPE(vm);
42     return throwVMTypeError(&exec, scope, ASCIILiteral("ReadableStreamDefaultController constructor should not be called directly"));
43 }
44
45 EncodedJSValue JSC_HOST_CALL constructJSReadableStreamDefaultReader(ExecState& exec)
46 {
47     VM& vm = exec.vm();
48     auto scope = DECLARE_THROW_SCOPE(vm);
49     
50     JSReadableStream* stream = jsDynamicDowncast<JSReadableStream*>(exec.argument(0));
51     if (!stream)
52         return throwArgumentTypeError(exec, scope, 0, "stream", "ReadableStreamReader", nullptr, "ReadableStream");
53
54     JSValue jsFunction = stream->get(&exec, Identifier::fromString(&exec, "getReader"));
55
56     CallData callData;
57     CallType callType = getCallData(jsFunction, callData);
58     MarkedArgumentBuffer noArguments;
59     return JSValue::encode(call(&exec, jsFunction, callType, callData, stream, noArguments));
60 }
61
62 // Private JS ReadableStreamDefaultReader and ReadableStreamDefaultController constructors.
63 typedef JSBuiltinConstructor<JSReadableStreamDefaultReader> JSBuiltinReadableStreamDefaultReaderPrivateConstructor;
64 typedef JSBuiltinConstructor<JSReadableStreamDefaultController> JSBuiltinReadableStreamDefaultControllerPrivateConstructor;
65
66 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSBuiltinReadableStreamDefaultReaderPrivateConstructor);
67 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSBuiltinReadableStreamDefaultControllerPrivateConstructor);
68
69 template<> const ClassInfo JSBuiltinReadableStreamDefaultReaderPrivateConstructor::s_info = { "ReadableStreamDefaultReaderPrivateConstructor", &Base::s_info, 0, CREATE_METHOD_TABLE(JSBuiltinReadableStreamDefaultReaderPrivateConstructor) };
70 template<> const ClassInfo JSBuiltinReadableStreamDefaultControllerPrivateConstructor::s_info = { "ReadableStreamDefaultControllerPrivateConstructor", &Base::s_info, 0, CREATE_METHOD_TABLE(JSBuiltinReadableStreamDefaultControllerPrivateConstructor) };
71
72 template<> FunctionExecutable* JSBuiltinReadableStreamDefaultReaderPrivateConstructor::initializeExecutable(JSC::VM& vm)
73 {
74     return readableStreamInternalsPrivateInitializeReadableStreamDefaultReaderCodeGenerator(vm);
75 }
76
77 template<> FunctionExecutable* JSBuiltinReadableStreamDefaultControllerPrivateConstructor::initializeExecutable(JSC::VM& vm)
78 {
79     return readableStreamInternalsPrivateInitializeReadableStreamDefaultControllerCodeGenerator(vm);
80 }
81
82 JSObject* createReadableStreamDefaultReaderPrivateConstructor(VM& vm, JSDOMGlobalObject& globalObject)
83 {
84     return JSBuiltinReadableStreamDefaultReaderPrivateConstructor::create(vm, JSBuiltinReadableStreamDefaultReaderPrivateConstructor::createStructure(vm, globalObject, globalObject.objectPrototype()), globalObject);
85 }
86
87 JSObject* createReadableStreamDefaultControllerPrivateConstructor(VM& vm, JSDOMGlobalObject& globalObject)
88 {
89     return JSBuiltinReadableStreamDefaultControllerPrivateConstructor::create(vm, JSBuiltinReadableStreamDefaultControllerPrivateConstructor::createStructure(vm, globalObject, globalObject.objectPrototype()), globalObject);
90 }
91
92 } // namespace WebCore
93
94 #endif