1 | /* |
2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY CANON INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CANON INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "ReadableStream.h" |
28 | |
29 | #include "JSDOMConvertSequences.h" |
30 | #include "JSReadableStreamSink.h" |
31 | #include "JSReadableStreamSource.h" |
32 | #include "WebCoreJSClientData.h" |
33 | |
34 | |
35 | namespace WebCore { |
36 | using namespace JSC; |
37 | |
38 | Ref<ReadableStream> ReadableStream::create(JSC::ExecState& execState, RefPtr<ReadableStreamSource>&& source) |
39 | { |
40 | VM& vm = execState.vm(); |
41 | auto scope = DECLARE_CATCH_SCOPE(vm); |
42 | |
43 | auto& clientData = *static_cast<JSVMClientData*>(vm.clientData); |
44 | auto& globalObject = *JSC::jsCast<JSDOMGlobalObject*>(execState.lexicalGlobalObject()); |
45 | |
46 | auto* constructor = JSC::asObject(globalObject.get(&execState, clientData.builtinNames().ReadableStreamPrivateName())); |
47 | |
48 | ConstructData constructData; |
49 | ConstructType constructType = constructor->methodTable(vm)->getConstructData(constructor, constructData); |
50 | ASSERT(constructType != ConstructType::None); |
51 | |
52 | MarkedArgumentBuffer args; |
53 | args.append(source ? toJSNewlyCreated(&execState, &globalObject, source.releaseNonNull()) : JSC::jsUndefined()); |
54 | ASSERT(!args.hasOverflowed()); |
55 | |
56 | auto newReadableStream = jsDynamicCast<JSReadableStream*>(vm, JSC::construct(&execState, constructor, constructType, constructData, args)); |
57 | scope.assertNoException(); |
58 | |
59 | return create(globalObject, *newReadableStream); |
60 | } |
61 | |
62 | namespace ReadableStreamInternal { |
63 | static inline JSC::JSValue callFunction(JSC::ExecState& state, JSC::JSValue jsFunction, JSC::JSValue thisValue, const JSC::ArgList& arguments) |
64 | { |
65 | VM& vm = state.vm(); |
66 | auto scope = DECLARE_CATCH_SCOPE(vm); |
67 | JSC::CallData callData; |
68 | auto callType = JSC::getCallData(vm, jsFunction, callData); |
69 | ASSERT(callType != JSC::CallType::None); |
70 | auto result = call(&state, jsFunction, callType, callData, thisValue, arguments); |
71 | scope.assertNoException(); |
72 | return result; |
73 | } |
74 | } |
75 | |
76 | void ReadableStream::pipeTo(ReadableStreamSink& sink) |
77 | { |
78 | auto& state = *m_globalObject->globalExec(); |
79 | JSVMClientData* clientData = static_cast<JSVMClientData*>(state.vm().clientData); |
80 | const Identifier& privateName = clientData->builtinFunctions().readableStreamInternalsBuiltins().readableStreamPipeToPrivateName(); |
81 | |
82 | auto readableStreamPipeTo = m_globalObject->get(&state, privateName); |
83 | ASSERT(readableStreamPipeTo.isFunction(state.vm())); |
84 | |
85 | MarkedArgumentBuffer arguments; |
86 | arguments.append(readableStream()); |
87 | arguments.append(toJS(&state, m_globalObject.get(), sink)); |
88 | ASSERT(!arguments.hasOverflowed()); |
89 | ReadableStreamInternal::callFunction(state, readableStreamPipeTo, JSC::jsUndefined(), arguments); |
90 | } |
91 | |
92 | std::pair<Ref<ReadableStream>, Ref<ReadableStream>> ReadableStream::tee() |
93 | { |
94 | auto& state = *m_globalObject->globalExec(); |
95 | JSVMClientData* clientData = static_cast<JSVMClientData*>(state.vm().clientData); |
96 | const Identifier& privateName = clientData->builtinFunctions().readableStreamInternalsBuiltins().readableStreamTeePrivateName(); |
97 | |
98 | auto readableStreamTee = m_globalObject->get(&state, privateName); |
99 | ASSERT(readableStreamTee.isFunction(state.vm())); |
100 | |
101 | MarkedArgumentBuffer arguments; |
102 | arguments.append(readableStream()); |
103 | arguments.append(JSC::jsBoolean(true)); |
104 | ASSERT(!arguments.hasOverflowed()); |
105 | auto returnedValue = ReadableStreamInternal::callFunction(state, readableStreamTee, JSC::jsUndefined(), arguments); |
106 | |
107 | auto results = Detail::SequenceConverter<IDLInterface<ReadableStream>>::convert(state, returnedValue); |
108 | |
109 | ASSERT(results.size() == 2); |
110 | return std::make_pair(results[0].releaseNonNull(), results[1].releaseNonNull()); |
111 | } |
112 | |
113 | void ReadableStream::lock() |
114 | { |
115 | auto& state = *m_globalObject->globalExec(); |
116 | VM& vm = state.vm(); |
117 | auto scope = DECLARE_CATCH_SCOPE(vm); |
118 | |
119 | auto& clientData = *static_cast<JSVMClientData*>(vm.clientData); |
120 | |
121 | auto* constructor = JSC::asObject(m_globalObject->get(&state, clientData.builtinNames().ReadableStreamDefaultReaderPrivateName())); |
122 | |
123 | ConstructData constructData; |
124 | ConstructType constructType = constructor->methodTable(vm)->getConstructData(constructor, constructData); |
125 | ASSERT(constructType != ConstructType::None); |
126 | |
127 | MarkedArgumentBuffer args; |
128 | args.append(readableStream()); |
129 | ASSERT(!args.hasOverflowed()); |
130 | |
131 | JSC::construct(&state, constructor, constructType, constructData, args); |
132 | scope.assertNoException(); |
133 | } |
134 | |
135 | static inline bool checkReadableStream(JSDOMGlobalObject& globalObject, JSReadableStream* readableStream, JSC::JSValue function) |
136 | { |
137 | auto& state = *globalObject.globalExec(); |
138 | |
139 | ASSERT(function); |
140 | JSC::MarkedArgumentBuffer arguments; |
141 | arguments.append(readableStream); |
142 | ASSERT(!arguments.hasOverflowed()); |
143 | return ReadableStreamInternal::callFunction(state, function, JSC::jsUndefined(), arguments).isTrue(); |
144 | } |
145 | |
146 | bool ReadableStream::isLocked() const |
147 | { |
148 | return checkReadableStream(*globalObject(), readableStream(), globalObject()->builtinInternalFunctions().readableStreamInternals().m_isReadableStreamLockedFunction.get()); |
149 | } |
150 | |
151 | bool ReadableStream::isDisturbed() const |
152 | { |
153 | return checkReadableStream(*globalObject(), readableStream(), globalObject()->builtinInternalFunctions().readableStreamInternals().m_isReadableStreamDisturbedFunction.get()); |
154 | } |
155 | |
156 | bool ReadableStream::isDisturbed(ExecState& state, JSValue value) |
157 | { |
158 | auto& vm = state.vm(); |
159 | auto& globalObject = *jsDynamicCast<JSDOMGlobalObject*>(vm, state.lexicalGlobalObject()); |
160 | auto* readableStream = jsDynamicCast<JSReadableStream*>(vm, value); |
161 | |
162 | return checkReadableStream(globalObject, readableStream, globalObject.builtinInternalFunctions().readableStreamInternals().m_isReadableStreamDisturbedFunction.get()); |
163 | } |
164 | |
165 | } |
166 | |