Skip to content

Commit 08b83ee

Browse files
committed
src: refactor setting JS properties on WriteWrap
`async` and `bytes` are only interesting when the write is coming from JS, and unnecessary otherwise. Also, make all of the stream `Write*()` bindings use the same code for setting these, and upgrade to the non-deprecated versions. PR-URL: #18963 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 8d595bb commit 08b83ee

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/stream_base-inl.h

-2
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,6 @@ inline StreamWriteResult StreamBase::Write(
220220
ClearError();
221221
}
222222

223-
req_wrap_obj->Set(env->async(), v8::Boolean::New(env->isolate(), async));
224-
225223
return StreamWriteResult { async, err, req_wrap };
226224
}
227225

src/stream_base.cc

+18-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace node {
1515

1616
using v8::Array;
17+
using v8::Boolean;
1718
using v8::Context;
1819
using v8::FunctionCallbackInfo;
1920
using v8::HandleScope;
@@ -56,6 +57,20 @@ int StreamBase::Shutdown(const FunctionCallbackInfo<Value>& args) {
5657
return Shutdown(req_wrap_obj);
5758
}
5859

60+
inline void SetWriteResultPropertiesOnWrapObject(
61+
Environment* env,
62+
Local<Object> req_wrap_obj,
63+
const StreamWriteResult& res,
64+
size_t bytes) {
65+
req_wrap_obj->Set(
66+
env->context(),
67+
env->bytes_string(),
68+
Number::New(env->isolate(), bytes)).FromJust();
69+
req_wrap_obj->Set(
70+
env->context(),
71+
env->async(),
72+
Boolean::New(env->isolate(), res.async)).FromJust();
73+
}
5974

6075
int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
6176
Environment* env = Environment::GetCurrent(args);
@@ -150,7 +165,7 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
150165
}
151166

152167
StreamWriteResult res = Write(*bufs, count, nullptr, req_wrap_obj);
153-
req_wrap_obj->Set(env->bytes_string(), Number::New(env->isolate(), bytes));
168+
SetWriteResultPropertiesOnWrapObject(env, req_wrap_obj, res, bytes);
154169
if (res.wrap != nullptr && storage) {
155170
res.wrap->SetAllocatedStorage(storage.release(), storage_size);
156171
}
@@ -178,9 +193,7 @@ int StreamBase::WriteBuffer(const FunctionCallbackInfo<Value>& args) {
178193

179194
if (res.async)
180195
req_wrap_obj->Set(env->context(), env->buffer_string(), args[1]).FromJust();
181-
req_wrap_obj->Set(env->context(), env->bytes_string(),
182-
Integer::NewFromUnsigned(env->isolate(), buf.len))
183-
.FromJust();
196+
SetWriteResultPropertiesOnWrapObject(env, req_wrap_obj, res, buf.len);
184197

185198
return res.err;
186199
}
@@ -286,10 +299,7 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
286299

287300
StreamWriteResult res = Write(&buf, 1, send_handle, req_wrap_obj);
288301

289-
req_wrap_obj->Set(env->context(), env->bytes_string(),
290-
Integer::NewFromUnsigned(env->isolate(), data_size))
291-
.FromJust();
292-
302+
SetWriteResultPropertiesOnWrapObject(env, req_wrap_obj, res, data_size);
293303
if (res.wrap != nullptr) {
294304
res.wrap->SetAllocatedStorage(data.release(), data_size);
295305
}

0 commit comments

Comments
 (0)