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 |
|
13005 |
AsyncWrap* JSStream::GetAsyncWrap() { |
34 |
|
13005 |
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 |
|
11967 |
int JSStream::ReadStart() { |
58 |
|
23934 |
HandleScope scope(env()->isolate()); |
59 |
|
11967 |
Context::Scope context_scope(env()->context()); |
60 |
|
11967 |
TryCatchScope try_catch(env()); |
61 |
|
|
Local<Value> value; |
62 |
|
11967 |
int value_int = UV_EPROTO; |
63 |
✓✗ |
35901 |
if (!MakeCallback(env()->onreadstart_string(), 0, nullptr).ToLocal(&value) || |
64 |
✗✓✗✓
|
35901 |
!value->Int32Value(env()->context()).To(&value_int)) { |
65 |
|
|
if (try_catch.HasCaught() && !try_catch.HasTerminated()) |
66 |
|
|
errors::TriggerUncaughtException(env()->isolate(), try_catch); |
67 |
|
|
} |
68 |
|
11967 |
return value_int; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
|
72 |
|
1010 |
int JSStream::ReadStop() { |
73 |
|
2020 |
HandleScope scope(env()->isolate()); |
74 |
|
1010 |
Context::Scope context_scope(env()->context()); |
75 |
|
1010 |
TryCatchScope try_catch(env()); |
76 |
|
|
Local<Value> value; |
77 |
|
1010 |
int value_int = UV_EPROTO; |
78 |
✓✓ |
3028 |
if (!MakeCallback(env()->onreadstop_string(), 0, nullptr).ToLocal(&value) || |
79 |
✗✓✓✓
|
3026 |
!value->Int32Value(env()->context()).To(&value_int)) { |
80 |
✓✓✗✓ ✗✓ |
2 |
if (try_catch.HasCaught() && !try_catch.HasTerminated()) |
81 |
|
|
errors::TriggerUncaughtException(env()->isolate(), try_catch); |
82 |
|
|
} |
83 |
|
1010 |
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 |
|
1957 |
int JSStream::DoWrite(WriteWrap* w, |
110 |
|
|
uv_buf_t* bufs, |
111 |
|
|
size_t count, |
112 |
|
|
uv_stream_t* send_handle) { |
113 |
✗✓ |
1957 |
CHECK_NULL(send_handle); |
114 |
|
|
|
115 |
|
3913 |
HandleScope scope(env()->isolate()); |
116 |
|
1957 |
Context::Scope context_scope(env()->context()); |
117 |
|
|
|
118 |
|
3913 |
MaybeStackBuffer<Local<Value>, 16> bufs_arr(count); |
119 |
✓✓ |
6389 |
for (size_t i = 0; i < count; i++) { |
120 |
|
4432 |
bufs_arr[i] = |
121 |
|
8864 |
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 |
|
3914 |
}; |
128 |
|
|
|
129 |
|
1957 |
TryCatchScope try_catch(env()); |
130 |
|
|
Local<Value> value; |
131 |
|
1957 |
int value_int = UV_EPROTO; |
132 |
|
3914 |
if (!MakeCallback(env()->onwrite_string(), |
133 |
|
1957 |
arraysize(argv), |
134 |
✓✓ |
5479 |
argv).ToLocal(&value) || |
135 |
✗✓✓✓
|
5087 |
!value->Int32Value(env()->context()).To(&value_int)) { |
136 |
✓✓✓✓ ✓✓ |
392 |
if (try_catch.HasCaught() && !try_catch.HasTerminated()) |
137 |
|
2 |
errors::TriggerUncaughtException(env()->isolate(), try_catch); |
138 |
|
|
} |
139 |
|
1956 |
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 |
✓✗ |
3096 |
void JSStream::Finish(const FunctionCallbackInfo<Value>& args) { |
155 |
✗✓ |
3096 |
CHECK(args[0]->IsObject()); |
156 |
✓✗ |
6192 |
Wrap* w = static_cast<Wrap*>(StreamReq::FromObject(args[0].As<Object>())); |
157 |
|
|
|
158 |
✗✓ |
3096 |
CHECK(args[1]->IsInt32()); |
159 |
✓✗ |
9288 |
w->Done(args[1].As<Int32>()->Value()); |
160 |
|
3096 |
} |
161 |
|
|
|
162 |
|
|
|
163 |
|
3080 |
void JSStream::ReadBuffer(const FunctionCallbackInfo<Value>& args) { |
164 |
|
|
JSStream* wrap; |
165 |
✗✓ |
3080 |
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder()); |
166 |
|
|
|
167 |
|
3080 |
ArrayBufferViewContents<char> buffer(args[0]); |
168 |
|
3080 |
const char* data = buffer.data(); |
169 |
|
3080 |
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 |
✓✓ |
6159 |
while (len != 0) { |
174 |
|
3080 |
uv_buf_t buf = wrap->EmitAlloc(len); |
175 |
|
3080 |
ssize_t avail = len; |
176 |
✗✓ |
3080 |
if (static_cast<ssize_t>(buf.len) < avail) |
177 |
|
|
avail = buf.len; |
178 |
|
|
|
179 |
|
3080 |
memcpy(buf.base, data, avail); |
180 |
|
3080 |
data += avail; |
181 |
|
3080 |
len -= static_cast<int>(avail); |
182 |
|
3080 |
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 |
|
5252 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(js_stream, node::JSStream::Initialize) |