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
221
222
223
224
225
226
227
228
|
#include "JSReadableHelper.h"
#include "JSReadableState.h"
#include "JSBufferList.h"
#include "JSBuffer.h"
#include "JSEventEmitter.h"
#include "JavaScriptCore/Lookup.h"
#include "JavaScriptCore/ObjectConstructor.h"
#include "ZigGlobalObject.h"
#include "JSDOMOperation.h"
#include "JSDOMAttribute.h"
#include "headers.h"
#include "JSDOMConvertEnumeration.h"
namespace WebCore {
using namespace JSC;
static JSC_DECLARE_HOST_FUNCTION(jsReadable_maybeReadMore_);
JSC_DEFINE_HOST_FUNCTION(jsReadable_maybeReadMore_, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame))
{
VM& vm = lexicalGlobalObject->vm();
auto throwScope = DECLARE_THROW_SCOPE(vm);
if (callFrame->argumentCount() < 2) {
throwTypeError(lexicalGlobalObject, throwScope, "Not enough arguments"_s);
return JSValue::encode(jsUndefined());
}
JSObject* stream = callFrame->uncheckedArgument(0).toObject(lexicalGlobalObject);
RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined()));
JSReadableState* state = jsDynamicCast<JSReadableState*>(callFrame->uncheckedArgument(1));
if (!state) {
throwTypeError(lexicalGlobalObject, throwScope, "Second argument not ReadableState"_s);
return JSValue::encode(jsUndefined());
}
auto read = stream->get(lexicalGlobalObject, Identifier::fromString(vm, "read"_s));
auto callData = JSC::getCallData(read);
if (callData.type == CallData::Type::None) {
throwException(lexicalGlobalObject, throwScope, createNotAFunctionError(lexicalGlobalObject, read));
return JSValue::encode(jsUndefined());
}
MarkedArgumentBuffer args;
args.append(jsNumber(0));
while (
!state->m_reading &&
!state->m_ended &&
(state->m_length < state->m_highWaterMark || (state->m_flowing > 0 && state->m_length == 0))) {
int64_t len = state->m_length;
JSC::call(lexicalGlobalObject, read, callData, JSValue(stream), args);
if (len == state->m_length)
break;
}
RELEASE_AND_RETURN(throwScope, JSValue::encode(jsUndefined()));
}
JSC_DEFINE_HOST_FUNCTION(jsReadable_maybeReadMore, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame))
{
VM& vm = lexicalGlobalObject->vm();
auto throwScope = DECLARE_THROW_SCOPE(vm);
if (callFrame->argumentCount() < 2) {
throwTypeError(lexicalGlobalObject, throwScope, "Not enough arguments"_s);
return JSValue::encode(jsUndefined());
}
JSValue streamVal = callFrame->uncheckedArgument(0);
JSValue stateVal = callFrame->uncheckedArgument(1);
// make this static?
JSFunction* maybeReadMore_ = JSC::JSFunction::create(
vm, lexicalGlobalObject, 0, "maybeReadMore_"_s, jsReadable_maybeReadMore_, ImplementationVisibility::Public);
lexicalGlobalObject->queueMicrotask(maybeReadMore_, streamVal, stateVal, JSValue{}, JSValue{});
RELEASE_AND_RETURN(throwScope, JSValue::encode(jsUndefined()));
}
void flow(JSGlobalObject* lexicalGlobalObject, JSObject* stream, JSReadableState* state)
{
VM& vm = lexicalGlobalObject->vm();
auto throwScope = DECLARE_THROW_SCOPE(vm);
auto read = stream->get(lexicalGlobalObject, Identifier::fromString(vm, "read"_s));
auto callData = JSC::getCallData(read);
if (callData.type == CallData::Type::None) {
throwException(lexicalGlobalObject, throwScope, createNotAFunctionError(lexicalGlobalObject, read));
return;
}
MarkedArgumentBuffer args;
while (state->m_flowing > 0) {
JSValue ret = JSC::call(lexicalGlobalObject, read, callData, JSValue(stream), args);
if (ret.isNull())
break;
}
}
static JSC_DECLARE_HOST_FUNCTION(jsReadable_resume_);
JSC_DEFINE_HOST_FUNCTION(jsReadable_resume_, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame))
{
VM& vm = lexicalGlobalObject->vm();
auto throwScope = DECLARE_THROW_SCOPE(vm);
if (callFrame->argumentCount() < 2) {
throwTypeError(lexicalGlobalObject, throwScope, "Not enough arguments"_s);
return JSValue::encode(jsUndefined());
}
JSObject* stream = callFrame->uncheckedArgument(0).toObject(lexicalGlobalObject);
RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined()));
JSReadableState* state = jsDynamicCast<JSReadableState*>(callFrame->uncheckedArgument(1));
if (!state) {
throwTypeError(lexicalGlobalObject, throwScope, "Second argument not ReadableState"_s);
return JSValue::encode(jsUndefined());
}
if (!state->m_reading) {
// stream.read(0)
auto read = stream->get(lexicalGlobalObject, Identifier::fromString(vm, "read"_s));
auto callData = JSC::getCallData(read);
if (callData.type == CallData::Type::None) {
throwException(lexicalGlobalObject, throwScope, createNotAFunctionError(lexicalGlobalObject, read));
return JSValue::encode(jsUndefined());
}
MarkedArgumentBuffer args;
args.append(jsNumber(0));
JSC::call(lexicalGlobalObject, read, callData, JSValue(stream), args);
}
state->m_resumeScheduled = true;
// stream.emit('resume')
auto eventType = Identifier::fromString(vm, "resume"_s);
MarkedArgumentBuffer args;
auto emitter = jsDynamicCast<JSEventEmitter*>(stream);
if (!emitter) {
throwTypeError(lexicalGlobalObject, throwScope, "stream is not EventEmitter"_s);
return JSValue::encode(jsUndefined());
}
emitter->wrapped().emitForBindings(eventType, args);
flow(lexicalGlobalObject, stream, state);
if (state->m_flowing && !state->m_reading) {
// stream.read(0)
auto read = stream->get(lexicalGlobalObject, Identifier::fromString(vm, "read"_s));
auto callData = JSC::getCallData(read);
if (callData.type == CallData::Type::None) {
throwException(lexicalGlobalObject, throwScope, createNotAFunctionError(lexicalGlobalObject, read));
return JSValue::encode(jsUndefined());
}
MarkedArgumentBuffer args;
args.append(jsNumber(0));
JSC::call(lexicalGlobalObject, read, callData, JSValue(stream), args);
}
RELEASE_AND_RETURN(throwScope, JSValue::encode(jsUndefined()));
}
JSC_DEFINE_HOST_FUNCTION(jsReadable_resume, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame))
{
VM& vm = lexicalGlobalObject->vm();
auto throwScope = DECLARE_THROW_SCOPE(vm);
if (callFrame->argumentCount() < 2) {
throwTypeError(lexicalGlobalObject, throwScope, "Not enough arguments"_s);
return JSValue::encode(jsUndefined());
}
JSValue streamVal = callFrame->uncheckedArgument(0);
JSValue stateVal = callFrame->uncheckedArgument(1);
JSReadableState* state = jsDynamicCast<JSReadableState*>(callFrame->uncheckedArgument(1));
if (!state) {
throwTypeError(lexicalGlobalObject, throwScope, "Second argument not ReadableState"_s);
return JSValue::encode(jsUndefined());
}
if (!state->m_resumeScheduled) {
state->m_resumeScheduled = true;
// make this static?
JSFunction* resume_ = JSC::JSFunction::create(
vm, lexicalGlobalObject, 0, "resume_"_s, jsReadable_resume_, ImplementationVisibility::Public);
lexicalGlobalObject->queueMicrotask(resume_, streamVal, stateVal, JSValue{}, JSValue{});
}
RELEASE_AND_RETURN(throwScope, JSValue::encode(jsUndefined()));
}
JSC_DEFINE_HOST_FUNCTION(jsReadable_emitReadable_, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame))
{
VM& vm = lexicalGlobalObject->vm();
auto throwScope = DECLARE_THROW_SCOPE(vm);
if (callFrame->argumentCount() < 2) {
throwTypeError(lexicalGlobalObject, throwScope, "Not enough arguments"_s);
return JSValue::encode(jsUndefined());
}
JSObject* stream = callFrame->uncheckedArgument(0).toObject(lexicalGlobalObject);
RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined()));
JSReadableState* state = jsDynamicCast<JSReadableState*>(callFrame->uncheckedArgument(1));
if (!state) {
throwTypeError(lexicalGlobalObject, throwScope, "Second argument not ReadableState"_s);
return JSValue::encode(jsUndefined());
}
JSValue errored = state->getDirect(vm, JSC::Identifier::fromString(vm, "errored"_s));
if (!state->m_destroyed && !errored.toBoolean(lexicalGlobalObject) && (state->m_length || state->m_ended)) {
// stream.emit('readable')
auto eventType = Identifier::fromString(vm, "readable"_s);
MarkedArgumentBuffer args;
auto emitter = jsDynamicCast<JSEventEmitter*>(stream);
if (!emitter) {
throwTypeError(lexicalGlobalObject, throwScope, "stream is not EventEmitter"_s);
return JSValue::encode(jsUndefined());
}
emitter->wrapped().emitForBindings(eventType, args);
state->m_emittedReadable = false;
}
state->m_needReadable = state->m_flowing <= 0 && !state->m_ended && state->m_length <= state->m_highWaterMark;
flow(lexicalGlobalObject, stream, state);
RELEASE_AND_RETURN(throwScope, JSValue::encode(jsUndefined()));
}
} // namespace WebCore
|