Skip to content

Commit

Permalink
src: refactor setting JS properties on WriteWrap
Browse files Browse the repository at this point in the history
`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: nodejs#18963
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
addaleax authored and MayaLekova committed May 8, 2018
1 parent 33b4a05 commit dad419e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
2 changes: 0 additions & 2 deletions src/stream_base-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ inline StreamWriteResult StreamBase::Write(
ClearError();
}

req_wrap_obj->Set(env->async(), v8::Boolean::New(env->isolate(), async));

return StreamWriteResult { async, err, req_wrap };
}

Expand Down
26 changes: 18 additions & 8 deletions src/stream_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace node {

using v8::Array;
using v8::Boolean;
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::HandleScope;
Expand Down Expand Up @@ -56,6 +57,20 @@ int StreamBase::Shutdown(const FunctionCallbackInfo<Value>& args) {
return Shutdown(req_wrap_obj);
}

inline void SetWriteResultPropertiesOnWrapObject(
Environment* env,
Local<Object> req_wrap_obj,
const StreamWriteResult& res,
size_t bytes) {
req_wrap_obj->Set(
env->context(),
env->bytes_string(),
Number::New(env->isolate(), bytes)).FromJust();
req_wrap_obj->Set(
env->context(),
env->async(),
Boolean::New(env->isolate(), res.async)).FromJust();
}

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

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

if (res.async)
req_wrap_obj->Set(env->context(), env->buffer_string(), args[1]).FromJust();
req_wrap_obj->Set(env->context(), env->bytes_string(),
Integer::NewFromUnsigned(env->isolate(), buf.len))
.FromJust();
SetWriteResultPropertiesOnWrapObject(env, req_wrap_obj, res, buf.len);

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

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

req_wrap_obj->Set(env->context(), env->bytes_string(),
Integer::NewFromUnsigned(env->isolate(), data_size))
.FromJust();

SetWriteResultPropertiesOnWrapObject(env, req_wrap_obj, res, data_size);
if (res.wrap != nullptr) {
res.wrap->SetAllocatedStorage(data.release(), data_size);
}
Expand Down

0 comments on commit dad419e

Please sign in to comment.