diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-09-30 13:41:57 -0400 |
---|---|---|
committer | Daniel Bevenius <daniel.bevenius@gmail.com> | 2018-10-04 12:37:50 +0200 |
commit | 1fd99b4ac7f73f46017b459d9a8b99fcb37bcf93 (patch) | |
tree | 2389a24370d11bd5e4402e81fb9d91d3a01c1d21 | |
parent | f01adb54542f49af1dc64b36bb9aae17846f6400 (diff) | |
download | android-node-v8-1fd99b4ac7f73f46017b459d9a8b99fcb37bcf93.tar.gz android-node-v8-1fd99b4ac7f73f46017b459d9a8b99fcb37bcf93.tar.bz2 android-node-v8-1fd99b4ac7f73f46017b459d9a8b99fcb37bcf93.zip |
src: clean up zlib write code
Split the existing `Write()` method into one that takes care
of translating the arguments from JS to C++, and a non-static
method for the actual write operations, as well as some minor
stylistic drive-by fixes.
PR-URL: https://github.com/nodejs/node/pull/23183
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
-rw-r--r-- | src/node_zlib.cc | 80 |
1 files changed, 42 insertions, 38 deletions
diff --git a/src/node_zlib.cc b/src/node_zlib.cc index 93dcf0122d..5def50d3b9 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -138,24 +138,15 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { // write(flush, in, in_off, in_len, out, out_off, out_len) template <bool async> static void Write(const FunctionCallbackInfo<Value>& args) { + Environment* env = Environment::GetCurrent(args); + Local<Context> context = env->context(); CHECK_EQ(args.Length(), 7); - ZCtx* ctx; - ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); - CHECK(ctx->init_done_ && "write before init"); - CHECK(ctx->mode_ != NONE && "already finalized"); - - CHECK_EQ(false, ctx->write_in_progress_ && "write already in progress"); - CHECK_EQ(false, ctx->pending_close_ && "close is pending"); - ctx->write_in_progress_ = true; - ctx->Ref(); + uint32_t in_off, in_len, out_off, out_len, flush; + char* in; + char* out; CHECK_EQ(false, args[0]->IsUndefined() && "must provide flush value"); - - Environment* env = ctx->env(); - Local<Context> context = env->context(); - - unsigned int flush; if (!args[0]->Uint32Value(context).To(&flush)) return; if (flush != Z_NO_FLUSH && @@ -167,12 +158,6 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { CHECK(0 && "Invalid flush value"); } - AllocScope alloc_scope(ctx); - - Bytef* in; - Bytef* out; - uint32_t in_off, in_len, out_off, out_len; - if (args[1]->IsNull()) { // just a flush in = nullptr; @@ -180,43 +165,62 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { in_off = 0; } else { CHECK(Buffer::HasInstance(args[1])); - Local<Object> in_buf; - in_buf = args[1]->ToObject(context).ToLocalChecked(); + Local<Object> in_buf = args[1].As<Object>(); if (!args[2]->Uint32Value(context).To(&in_off)) return; if (!args[3]->Uint32Value(context).To(&in_len)) return; CHECK(Buffer::IsWithinBounds(in_off, in_len, Buffer::Length(in_buf))); - in = reinterpret_cast<Bytef *>(Buffer::Data(in_buf) + in_off); + in = Buffer::Data(in_buf) + in_off; } CHECK(Buffer::HasInstance(args[4])); - Local<Object> out_buf = args[4]->ToObject(context).ToLocalChecked(); + Local<Object> out_buf = args[4].As<Object>(); if (!args[5]->Uint32Value(context).To(&out_off)) return; if (!args[6]->Uint32Value(context).To(&out_len)) return; CHECK(Buffer::IsWithinBounds(out_off, out_len, Buffer::Length(out_buf))); - out = reinterpret_cast<Bytef *>(Buffer::Data(out_buf) + out_off); + out = Buffer::Data(out_buf) + out_off; + + ZCtx* ctx; + ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); + + ctx->Write<async>(flush, in, in_len, out, out_len); + } + + template <bool async> + void Write(uint32_t flush, + char* in, uint32_t in_len, + char* out, uint32_t out_len) { + AllocScope alloc_scope(this); + + CHECK(init_done_ && "write before init"); + CHECK(mode_ != NONE && "already finalized"); + + CHECK_EQ(false, write_in_progress_); + CHECK_EQ(false, pending_close_); + write_in_progress_ = true; + Ref(); - ctx->strm_.avail_in = in_len; - ctx->strm_.next_in = in; - ctx->strm_.avail_out = out_len; - ctx->strm_.next_out = out; - ctx->flush_ = flush; + strm_.avail_in = in_len; + strm_.next_in = reinterpret_cast<Bytef*>(in); + strm_.avail_out = out_len; + strm_.next_out = reinterpret_cast<Bytef*>(out); + flush_ = flush; if (!async) { // sync version - env->PrintSyncTrace(); - ctx->DoThreadPoolWork(); - if (ctx->CheckError()) { - ctx->write_result_[0] = ctx->strm_.avail_out; - ctx->write_result_[1] = ctx->strm_.avail_in; - ctx->write_in_progress_ = false; + env()->PrintSyncTrace(); + DoThreadPoolWork(); + if (CheckError()) { + write_result_[0] = strm_.avail_out; + write_result_[1] = strm_.avail_in; + write_in_progress_ = false; } - ctx->Unref(); + Unref(); return; } // async version - ctx->ScheduleWork(); + ScheduleWork(); } // thread pool! |