GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: js_stream.cc Lines: 96 111 86.5 %
Date: 2021-09-23 04:12:37 Branches: 37 80 46.2 %

Line Branch Exec Source
1
#include "js_stream.h"
2
3
#include "async_wrap.h"
4
#include "env-inl.h"
5
#include "node_errors.h"
6
#include "stream_base-inl.h"
7
#include "util-inl.h"
8
#include "v8.h"
9
10
namespace node {
11
12
using errors::TryCatchScope;
13
14
using v8::Array;
15
using v8::Context;
16
using v8::FunctionCallbackInfo;
17
using v8::FunctionTemplate;
18
using v8::HandleScope;
19
using v8::Int32;
20
using v8::Local;
21
using v8::Object;
22
using v8::Value;
23
24
25
10582
JSStream::JSStream(Environment* env, Local<Object> obj)
26
    : AsyncWrap(env, obj, AsyncWrap::PROVIDER_JSSTREAM),
27
10582
      StreamBase(env) {
28
10582
  MakeWeak();
29
10582
  StreamBase::AttachToObject(obj);
30
10582
}
31
32
33
11238
AsyncWrap* JSStream::GetAsyncWrap() {
34
11238
  return static_cast<AsyncWrap*>(this);
35
}
36
37
38
10620
bool JSStream::IsAlive() {
39
10620
  return true;
40
}
41
42
43
bool JSStream::IsClosing() {
44
  HandleScope scope(env()->isolate());
45
  Context::Scope context_scope(env()->context());
46
  TryCatchScope try_catch(env());
47
  Local<Value> value;
48
  if (!MakeCallback(env()->isclosing_string(), 0, nullptr).ToLocal(&value)) {
49
    if (try_catch.HasCaught() && !try_catch.HasTerminated())
50
      errors::TriggerUncaughtException(env()->isolate(), try_catch);
51
    return true;
52
  }
53
  return value->IsTrue();
54
}
55
56
57
10608
int JSStream::ReadStart() {
58
21216
  HandleScope scope(env()->isolate());
59
10608
  Context::Scope context_scope(env()->context());
60
10608
  TryCatchScope try_catch(env());
61
  Local<Value> value;
62
10608
  int value_int = UV_EPROTO;
63
31824
  if (!MakeCallback(env()->onreadstart_string(), 0, nullptr).ToLocal(&value) ||
64

31824
      !value->Int32Value(env()->context()).To(&value_int)) {
65
    if (try_catch.HasCaught() && !try_catch.HasTerminated())
66
      errors::TriggerUncaughtException(env()->isolate(), try_catch);
67
  }
68
10608
  return value_int;
69
}
70
71
72
62
int JSStream::ReadStop() {
73
124
  HandleScope scope(env()->isolate());
74
62
  Context::Scope context_scope(env()->context());
75
62
  TryCatchScope try_catch(env());
76
  Local<Value> value;
77
62
  int value_int = UV_EPROTO;
78
185
  if (!MakeCallback(env()->onreadstop_string(), 0, nullptr).ToLocal(&value) ||
79

184
      !value->Int32Value(env()->context()).To(&value_int)) {
80

1
    if (try_catch.HasCaught() && !try_catch.HasTerminated())
81
      errors::TriggerUncaughtException(env()->isolate(), try_catch);
82
  }
83
62
  return value_int;
84
}
85
86
87
13
int JSStream::DoShutdown(ShutdownWrap* req_wrap) {
88
26
  HandleScope scope(env()->isolate());
89
13
  Context::Scope context_scope(env()->context());
90
91
  Local<Value> argv[] = {
92
    req_wrap->object()
93
13
  };
94
95
13
  TryCatchScope try_catch(env());
96
  Local<Value> value;
97
13
  int value_int = UV_EPROTO;
98
26
  if (!MakeCallback(env()->onshutdown_string(),
99
13
                    arraysize(argv),
100
39
                    argv).ToLocal(&value) ||
101

39
      !value->Int32Value(env()->context()).To(&value_int)) {
102
    if (try_catch.HasCaught() && !try_catch.HasTerminated())
103
      errors::TriggerUncaughtException(env()->isolate(), try_catch);
104
  }
105
13
  return value_int;
106
}
107
108
109
607
int JSStream::DoWrite(WriteWrap* w,
110
                      uv_buf_t* bufs,
111
                      size_t count,
112
                      uv_stream_t* send_handle) {
113
607
  CHECK_NULL(send_handle);
114
115
1213
  HandleScope scope(env()->isolate());
116
607
  Context::Scope context_scope(env()->context());
117
118
1213
  MaybeStackBuffer<Local<Value>, 16> bufs_arr(count);
119
1270
  for (size_t i = 0; i < count; i++) {
120
663
    bufs_arr[i] =
121
1326
        Buffer::Copy(env(), bufs[i].base, bufs[i].len).ToLocalChecked();
122
  }
123
124
  Local<Value> argv[] = {
125
    w->object(),
126
    Array::New(env()->isolate(), bufs_arr.out(), count)
127
1214
  };
128
129
607
  TryCatchScope try_catch(env());
130
  Local<Value> value;
131
607
  int value_int = UV_EPROTO;
132
1214
  if (!MakeCallback(env()->onwrite_string(),
133
607
                    arraysize(argv),
134
1818
                    argv).ToLocal(&value) ||
135

1815
      !value->Int32Value(env()->context()).To(&value_int)) {
136

3
    if (try_catch.HasCaught() && !try_catch.HasTerminated())
137
2
      errors::TriggerUncaughtException(env()->isolate(), try_catch);
138
  }
139
606
  return value_int;
140
}
141
142
143
10582
void JSStream::New(const FunctionCallbackInfo<Value>& args) {
144
  // This constructor should not be exposed to public javascript.
145
  // Therefore we assert that we are not trying to call this as a
146
  // normal function.
147
10582
  CHECK(args.IsConstructCall());
148
10582
  Environment* env = Environment::GetCurrent(args);
149
10582
  new JSStream(env, args.This());
150
10582
}
151
152
153
template <class Wrap>
154
1218
void JSStream::Finish(const FunctionCallbackInfo<Value>& args) {
155
1218
  CHECK(args[0]->IsObject());
156
2436
  Wrap* w = static_cast<Wrap*>(StreamReq::FromObject(args[0].As<Object>()));
157
158
1218
  CHECK(args[1]->IsInt32());
159
3654
  w->Done(args[1].As<Int32>()->Value());
160
1218
}
161
162
163
640
void JSStream::ReadBuffer(const FunctionCallbackInfo<Value>& args) {
164
  JSStream* wrap;
165
640
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
166
167
640
  ArrayBufferViewContents<char> buffer(args[0]);
168
640
  const char* data = buffer.data();
169
640
  int len = buffer.length();
170
171
  // Repeatedly ask the stream's owner for memory, copy the data that we
172
  // just read from JS into those buffers and emit them as reads.
173
1279
  while (len != 0) {
174
640
    uv_buf_t buf = wrap->EmitAlloc(len);
175
640
    ssize_t avail = len;
176
640
    if (static_cast<ssize_t>(buf.len) < avail)
177
      avail = buf.len;
178
179
640
    memcpy(buf.base, data, avail);
180
640
    data += avail;
181
640
    len -= static_cast<int>(avail);
182
640
    wrap->EmitRead(avail, buf);
183
  }
184
}
185
186
187
11
void JSStream::EmitEOF(const FunctionCallbackInfo<Value>& args) {
188
  JSStream* wrap;
189
11
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
190
191
11
  wrap->EmitRead(UV_EOF);
192
}
193
194
195
547
void JSStream::Initialize(Local<Object> target,
196
                          Local<Value> unused,
197
                          Local<Context> context,
198
                          void* priv) {
199
547
  Environment* env = Environment::GetCurrent(context);
200
201
547
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
202
1094
  t->InstanceTemplate()
203
547
    ->SetInternalFieldCount(StreamBase::kInternalFieldCount);
204
547
  t->Inherit(AsyncWrap::GetConstructorTemplate(env));
205
206
547
  env->SetProtoMethod(t, "finishWrite", Finish<WriteWrap>);
207
547
  env->SetProtoMethod(t, "finishShutdown", Finish<ShutdownWrap>);
208
547
  env->SetProtoMethod(t, "readBuffer", ReadBuffer);
209
547
  env->SetProtoMethod(t, "emitEOF", EmitEOF);
210
211
547
  StreamBase::AddMethods(env, t);
212
547
  env->SetConstructorFunction(target, "JSStream", t);
213
547
}
214
215
}  // namespace node
216
217
4925
NODE_MODULE_CONTEXT_AWARE_INTERNAL(js_stream, node::JSStream::Initialize)