From dad419e0f338646f2638234fc72197d3a8f16ed8 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 23 Feb 2018 15:21:48 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/18963 Reviewed-By: Anatoli Papirovski Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Joyee Cheung --- src/stream_base-inl.h | 2 -- src/stream_base.cc | 26 ++++++++++++++++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/stream_base-inl.h b/src/stream_base-inl.h index c864feb9270b52..81adf7a866b927 100644 --- a/src/stream_base-inl.h +++ b/src/stream_base-inl.h @@ -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 }; } diff --git a/src/stream_base.cc b/src/stream_base.cc index 9ad9fd5bcb4a46..1d1d324841537f 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -14,6 +14,7 @@ namespace node { using v8::Array; +using v8::Boolean; using v8::Context; using v8::FunctionCallbackInfo; using v8::HandleScope; @@ -56,6 +57,20 @@ int StreamBase::Shutdown(const FunctionCallbackInfo& args) { return Shutdown(req_wrap_obj); } +inline void SetWriteResultPropertiesOnWrapObject( + Environment* env, + Local 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& args) { Environment* env = Environment::GetCurrent(args); @@ -150,7 +165,7 @@ int StreamBase::Writev(const FunctionCallbackInfo& 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); } @@ -178,9 +193,7 @@ int StreamBase::WriteBuffer(const FunctionCallbackInfo& 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; } @@ -286,10 +299,7 @@ int StreamBase::WriteString(const FunctionCallbackInfo& 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); }