1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
/*
* Copyright (C) 2017-2021 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY CANON INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CANON INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "ReadableStream.h"
#include "Exception.h"
#include "ExceptionCode.h"
#include "JSDOMConvertSequences.h"
#include "JSReadableStreamSink.h"
#include "JSReadableStreamSource.h"
#include "WebCoreJSClientData.h"
namespace WebCore {
using namespace JSC;
static inline ExceptionOr<JSObject*> invokeConstructor(JSC::JSGlobalObject& lexicalGlobalObject, const JSC::Identifier& identifier, const Function<void(MarkedArgumentBuffer&, JSC::JSGlobalObject&, JSDOMGlobalObject&)>& buildArguments)
{
VM& vm = lexicalGlobalObject.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto& globalObject = *JSC::jsCast<JSDOMGlobalObject*>(&lexicalGlobalObject);
auto constructorValue = globalObject.get(&lexicalGlobalObject, identifier);
EXCEPTION_ASSERT(!scope.exception() || vm.hasPendingTerminationException());
RETURN_IF_EXCEPTION(scope, Exception { ExistingExceptionError });
auto constructor = JSC::asObject(constructorValue);
auto constructData = JSC::getConstructData(constructor);
ASSERT(constructData.type != CallData::Type::None);
MarkedArgumentBuffer args;
buildArguments(args, lexicalGlobalObject, globalObject);
ASSERT(!args.hasOverflowed());
JSObject* object = JSC::construct(&lexicalGlobalObject, constructor, constructData, args);
ASSERT(!!scope.exception() == !object);
EXCEPTION_ASSERT(!scope.exception() || vm.hasPendingTerminationException());
RETURN_IF_EXCEPTION(scope, Exception { ExistingExceptionError });
return object;
}
ExceptionOr<Ref<ReadableStream>> ReadableStream::create(JSC::JSGlobalObject& lexicalGlobalObject, RefPtr<ReadableStreamSource>&& source)
{
auto& builtinNames = WebCore::builtinNames(lexicalGlobalObject.vm());
auto objectOrException = invokeConstructor(lexicalGlobalObject, builtinNames.ReadableStreamPrivateName(), [&source](auto& args, auto& lexicalGlobalObject, auto& globalObject) {
args.append(source ? toJSNewlyCreated(&lexicalGlobalObject, &globalObject, source.releaseNonNull()) : JSC::jsUndefined());
});
if (objectOrException.hasException())
return objectOrException.releaseException();
return create(*JSC::jsCast<JSDOMGlobalObject*>(&lexicalGlobalObject), *jsCast<JSReadableStream*>(objectOrException.releaseReturnValue()));
}
ExceptionOr<Ref<ReadableStream>> ReadableStream::create(JSC::JSGlobalObject& lexicalGlobalObject, RefPtr<ReadableStreamSource>&& source, JSC::JSValue nativePtr)
{
auto& builtinNames = WebCore::builtinNames(lexicalGlobalObject.vm());
RELEASE_ASSERT(source != nullptr);
auto objectOrException = invokeConstructor(lexicalGlobalObject, builtinNames.ReadableStreamPrivateName(), [&source, nativePtr](auto& args, auto& lexicalGlobalObject, auto& globalObject) {
auto sourceStream = toJSNewlyCreated(&lexicalGlobalObject, &globalObject, source.releaseNonNull());
auto tag = WebCore::clientData(lexicalGlobalObject.vm())->builtinNames().bunNativePtrPrivateName();
sourceStream.getObject()->putDirect(lexicalGlobalObject.vm(), tag, nativePtr, JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::DontEnum);
args.append(sourceStream);
});
if (objectOrException.hasException())
return objectOrException.releaseException();
return create(*JSC::jsCast<JSDOMGlobalObject*>(&lexicalGlobalObject), *jsCast<JSReadableStream*>(objectOrException.releaseReturnValue()));
}
static inline std::optional<JSC::JSValue> invokeReadableStreamFunction(JSC::JSGlobalObject& lexicalGlobalObject, const JSC::Identifier& identifier, JSC::JSValue thisValue, const JSC::MarkedArgumentBuffer& arguments)
{
JSC::VM& vm = lexicalGlobalObject.vm();
JSC::JSLockHolder lock(vm);
auto function = lexicalGlobalObject.get(&lexicalGlobalObject, identifier);
ASSERT(function.isCallable());
auto scope = DECLARE_CATCH_SCOPE(vm);
auto callData = JSC::getCallData(function);
auto result = call(&lexicalGlobalObject, function, callData, thisValue, arguments);
EXCEPTION_ASSERT(!scope.exception() || vm.hasPendingTerminationException());
if (scope.exception())
return {};
return result;
}
void ReadableStream::pipeTo(ReadableStreamSink& sink)
{
auto& lexicalGlobalObject = *m_globalObject;
auto* clientData = static_cast<JSVMClientData*>(lexicalGlobalObject.vm().clientData);
auto& privateName = clientData->builtinFunctions().readableStreamInternalsBuiltins().readableStreamPipeToPrivateName();
MarkedArgumentBuffer arguments;
arguments.append(readableStream());
arguments.append(toJS(&lexicalGlobalObject, m_globalObject.get(), sink));
ASSERT(!arguments.hasOverflowed());
invokeReadableStreamFunction(lexicalGlobalObject, privateName, JSC::jsUndefined(), arguments);
}
std::optional<std::pair<Ref<ReadableStream>, Ref<ReadableStream>>> ReadableStream::tee()
{
auto& lexicalGlobalObject = *m_globalObject;
auto* clientData = static_cast<JSVMClientData*>(lexicalGlobalObject.vm().clientData);
auto& privateName = clientData->builtinFunctions().readableStreamInternalsBuiltins().readableStreamTeePrivateName();
MarkedArgumentBuffer arguments;
arguments.append(readableStream());
arguments.append(JSC::jsBoolean(true));
ASSERT(!arguments.hasOverflowed());
auto returnedValue = invokeReadableStreamFunction(lexicalGlobalObject, privateName, JSC::jsUndefined(), arguments);
if (!returnedValue)
return {};
auto results = Detail::SequenceConverter<IDLInterface<ReadableStream>>::convert(lexicalGlobalObject, *returnedValue);
ASSERT(results.size() == 2);
return std::make_pair(results[0].releaseNonNull(), results[1].releaseNonNull());
}
void ReadableStream::lock()
{
auto& builtinNames = WebCore::builtinNames(m_globalObject->vm());
invokeConstructor(*m_globalObject, builtinNames.ReadableStreamDefaultReaderPrivateName(), [this](auto& args, auto&, auto&) {
args.append(readableStream());
});
}
void ReadableStream::cancel(const Exception& exception)
{
auto& lexicalGlobalObject = *m_globalObject;
auto* clientData = static_cast<JSVMClientData*>(lexicalGlobalObject.vm().clientData);
auto& privateName = clientData->builtinFunctions().readableStreamInternalsBuiltins().readableStreamCancelPrivateName();
auto& vm = lexicalGlobalObject.vm();
JSC::JSLockHolder lock(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
auto value = createDOMException(&lexicalGlobalObject, exception.code(), exception.message());
if (UNLIKELY(scope.exception())) {
ASSERT(vm.hasPendingTerminationException());
return;
}
MarkedArgumentBuffer arguments;
arguments.append(readableStream());
arguments.append(value);
ASSERT(!arguments.hasOverflowed());
invokeReadableStreamFunction(lexicalGlobalObject, privateName, JSC::jsUndefined(), arguments);
}
static inline bool checkReadableStream(JSDOMGlobalObject& globalObject, JSReadableStream* readableStream, JSC::JSValue function)
{
auto& lexicalGlobalObject = globalObject;
ASSERT(function);
JSC::MarkedArgumentBuffer arguments;
arguments.append(readableStream);
ASSERT(!arguments.hasOverflowed());
auto& vm = lexicalGlobalObject.vm();
auto scope = DECLARE_CATCH_SCOPE(vm);
auto callData = JSC::getCallData(function);
ASSERT(callData.type != JSC::CallData::Type::None);
auto result = call(&lexicalGlobalObject, function, callData, JSC::jsUndefined(), arguments);
EXCEPTION_ASSERT(!scope.exception() || vm.hasPendingTerminationException());
return result.isTrue() || scope.exception();
}
bool ReadableStream::isLocked() const
{
return checkReadableStream(*globalObject(), readableStream(), globalObject()->builtinInternalFunctions().readableStreamInternals().m_isReadableStreamLockedFunction.get());
}
bool ReadableStream::isLocked(JSGlobalObject* globalObject, JSReadableStream* readableStream)
{
auto* dom = reinterpret_cast<JSDOMGlobalObject*>(globalObject);
return checkReadableStream(*dom, readableStream, dom->builtinInternalFunctions().readableStreamInternals().m_isReadableStreamLockedFunction.get());
}
bool ReadableStream::isDisturbed(JSGlobalObject* globalObject, JSReadableStream* readableStream)
{
auto* dom = reinterpret_cast<JSDOMGlobalObject*>(globalObject);
return checkReadableStream(*dom, readableStream, dom->builtinInternalFunctions().readableStreamInternals().m_isReadableStreamDisturbedFunction.get());
}
bool ReadableStream::isDisturbed() const
{
return checkReadableStream(*globalObject(), readableStream(), globalObject()->builtinInternalFunctions().readableStreamInternals().m_isReadableStreamDisturbedFunction.get());
}
}
|