GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: js_stream.cc Lines: 96 111 86.5 %
Date: 2022-06-06 04:15:48 Branches: 38 80 47.5 %

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
11002
JSStream::JSStream(Environment* env, Local<Object> obj)
26
    : AsyncWrap(env, obj, AsyncWrap::PROVIDER_JSSTREAM),
27
11002
      StreamBase(env) {
28
11002
  MakeWeak();
29
11002
  StreamBase::AttachToObject(obj);
30
11002
}
31
32
33
12923
AsyncWrap* JSStream::GetAsyncWrap() {
34
12923
  return static_cast<AsyncWrap*>(this);
35
}
36
37
38
11027
bool JSStream::IsAlive() {
39
11027
  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
11879
int JSStream::ReadStart() {
58
23758
  HandleScope scope(env()->isolate());
59
11879
  Context::Scope context_scope(env()->context());
60
11879
  TryCatchScope try_catch(env());
61
  Local<Value> value;
62
11879
  int value_int = UV_EPROTO;
63
35637
  if (!MakeCallback(env()->onreadstart_string(), 0, nullptr).ToLocal(&value) ||
64

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

2716
      !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
906
  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
1875
int JSStream::DoWrite(WriteWrap* w,
110
                      uv_buf_t* bufs,
111
                      size_t count,
112
                      uv_stream_t* send_handle) {
113
1875
  CHECK_NULL(send_handle);
114
115
3749
  HandleScope scope(env()->isolate());
116
1875
  Context::Scope context_scope(env()->context());
117
118
3749
  MaybeStackBuffer<Local<Value>, 16> bufs_arr(count);
119
6009
  for (size_t i = 0; i < count; i++) {
120
4134
    bufs_arr[i] =
121
8268
        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
3750
  };
128
129
1875
  TryCatchScope try_catch(env());
130
  Local<Value> value;
131
1875
  int value_int = UV_EPROTO;
132
3750
  if (!MakeCallback(env()->onwrite_string(),
133
1875
                    arraysize(argv),
134
5211
                    argv).ToLocal(&value) ||
135

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

414
    if (try_catch.HasCaught() && !try_catch.HasTerminated())
137
2
      errors::TriggerUncaughtException(env()->isolate(), try_catch);
138
  }
139
1874
  return value_int;
140
}
141
142
143
11002
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
11002
  CHECK(args.IsConstructCall());
148
11002
  Environment* env = Environment::GetCurrent(args);
149
11002
  new JSStream(env, args.This());
150
11002
}
151
152
153
template <class Wrap>
154
2920
void JSStream::Finish(const FunctionCallbackInfo<Value>& args) {
155
2920
  CHECK(args[0]->IsObject());
156
5840
  Wrap* w = static_cast<Wrap*>(StreamReq::FromObject(args[0].As<Object>()));
157
158
2920
  CHECK(args[1]->IsInt32());
159
8760
  w->Done(args[1].As<Int32>()->Value());
160
2920
}
161
162
163
2970
void JSStream::ReadBuffer(const FunctionCallbackInfo<Value>& args) {
164
  JSStream* wrap;
165
2970
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
166
167
2970
  ArrayBufferViewContents<char> buffer(args[0]);
168
2970
  const char* data = buffer.data();
169
2970
  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
5939
  while (len != 0) {
174
2970
    uv_buf_t buf = wrap->EmitAlloc(len);
175
2970
    ssize_t avail = len;
176
2970
    if (static_cast<ssize_t>(buf.len) < avail)
177
      avail = buf.len;
178
179
2970
    memcpy(buf.base, data, avail);
180
2970
    data += avail;
181
2970
    len -= static_cast<int>(avail);
182
2970
    wrap->EmitRead(avail, buf);
183
  }
184
}
185
186
187
12
void JSStream::EmitEOF(const FunctionCallbackInfo<Value>& args) {
188
  JSStream* wrap;
189
12
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
190
191
12
  wrap->EmitRead(UV_EOF);
192
}
193
194
195
766
void JSStream::Initialize(Local<Object> target,
196
                          Local<Value> unused,
197
                          Local<Context> context,
198
                          void* priv) {
199
766
  Environment* env = Environment::GetCurrent(context);
200
201
766
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
202
1532
  t->InstanceTemplate()
203
766
    ->SetInternalFieldCount(StreamBase::kInternalFieldCount);
204
766
  t->Inherit(AsyncWrap::GetConstructorTemplate(env));
205
206
766
  env->SetProtoMethod(t, "finishWrite", Finish<WriteWrap>);
207
766
  env->SetProtoMethod(t, "finishShutdown", Finish<ShutdownWrap>);
208
766
  env->SetProtoMethod(t, "readBuffer", ReadBuffer);
209
766
  env->SetProtoMethod(t, "emitEOF", EmitEOF);
210
211
766
  StreamBase::AddMethods(env, t);
212
766
  env->SetConstructorFunction(target, "JSStream", t);
213
766
}
214
215
}  // namespace node
216
217
5274
NODE_MODULE_CONTEXT_AWARE_INTERNAL(js_stream, node::JSStream::Initialize)