Skip to content

Commit 8f5fb6f

Browse files
addaleaxtargos
authored andcommitted
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: #23183 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]>
1 parent e93c94c commit 8f5fb6f

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

src/node_zlib.cc

+42-38
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,15 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
138138
// write(flush, in, in_off, in_len, out, out_off, out_len)
139139
template <bool async>
140140
static void Write(const FunctionCallbackInfo<Value>& args) {
141+
Environment* env = Environment::GetCurrent(args);
142+
Local<Context> context = env->context();
141143
CHECK_EQ(args.Length(), 7);
142144

143-
ZCtx* ctx;
144-
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
145-
CHECK(ctx->init_done_ && "write before init");
146-
CHECK(ctx->mode_ != NONE && "already finalized");
147-
148-
CHECK_EQ(false, ctx->write_in_progress_ && "write already in progress");
149-
CHECK_EQ(false, ctx->pending_close_ && "close is pending");
150-
ctx->write_in_progress_ = true;
151-
ctx->Ref();
145+
uint32_t in_off, in_len, out_off, out_len, flush;
146+
char* in;
147+
char* out;
152148

153149
CHECK_EQ(false, args[0]->IsUndefined() && "must provide flush value");
154-
155-
Environment* env = ctx->env();
156-
Local<Context> context = env->context();
157-
158-
unsigned int flush;
159150
if (!args[0]->Uint32Value(context).To(&flush)) return;
160151

161152
if (flush != Z_NO_FLUSH &&
@@ -167,56 +158,69 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
167158
CHECK(0 && "Invalid flush value");
168159
}
169160

170-
AllocScope alloc_scope(ctx);
171-
172-
Bytef* in;
173-
Bytef* out;
174-
uint32_t in_off, in_len, out_off, out_len;
175-
176161
if (args[1]->IsNull()) {
177162
// just a flush
178163
in = nullptr;
179164
in_len = 0;
180165
in_off = 0;
181166
} else {
182167
CHECK(Buffer::HasInstance(args[1]));
183-
Local<Object> in_buf;
184-
in_buf = args[1]->ToObject(context).ToLocalChecked();
168+
Local<Object> in_buf = args[1].As<Object>();
185169
if (!args[2]->Uint32Value(context).To(&in_off)) return;
186170
if (!args[3]->Uint32Value(context).To(&in_len)) return;
187171

188172
CHECK(Buffer::IsWithinBounds(in_off, in_len, Buffer::Length(in_buf)));
189-
in = reinterpret_cast<Bytef *>(Buffer::Data(in_buf) + in_off);
173+
in = Buffer::Data(in_buf) + in_off;
190174
}
191175

192176
CHECK(Buffer::HasInstance(args[4]));
193-
Local<Object> out_buf = args[4]->ToObject(context).ToLocalChecked();
177+
Local<Object> out_buf = args[4].As<Object>();
194178
if (!args[5]->Uint32Value(context).To(&out_off)) return;
195179
if (!args[6]->Uint32Value(context).To(&out_len)) return;
196180
CHECK(Buffer::IsWithinBounds(out_off, out_len, Buffer::Length(out_buf)));
197-
out = reinterpret_cast<Bytef *>(Buffer::Data(out_buf) + out_off);
181+
out = Buffer::Data(out_buf) + out_off;
182+
183+
ZCtx* ctx;
184+
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
185+
186+
ctx->Write<async>(flush, in, in_len, out, out_len);
187+
}
188+
189+
template <bool async>
190+
void Write(uint32_t flush,
191+
char* in, uint32_t in_len,
192+
char* out, uint32_t out_len) {
193+
AllocScope alloc_scope(this);
194+
195+
CHECK(init_done_ && "write before init");
196+
CHECK(mode_ != NONE && "already finalized");
197+
198+
CHECK_EQ(false, write_in_progress_);
199+
CHECK_EQ(false, pending_close_);
200+
write_in_progress_ = true;
201+
Ref();
198202

199-
ctx->strm_.avail_in = in_len;
200-
ctx->strm_.next_in = in;
201-
ctx->strm_.avail_out = out_len;
202-
ctx->strm_.next_out = out;
203-
ctx->flush_ = flush;
203+
strm_.avail_in = in_len;
204+
strm_.next_in = reinterpret_cast<Bytef*>(in);
205+
strm_.avail_out = out_len;
206+
strm_.next_out = reinterpret_cast<Bytef*>(out);
207+
flush_ = flush;
204208

205209
if (!async) {
206210
// sync version
207-
env->PrintSyncTrace();
208-
ctx->DoThreadPoolWork();
209-
if (ctx->CheckError()) {
210-
ctx->write_result_[0] = ctx->strm_.avail_out;
211-
ctx->write_result_[1] = ctx->strm_.avail_in;
212-
ctx->write_in_progress_ = false;
211+
env()->PrintSyncTrace();
212+
DoThreadPoolWork();
213+
if (CheckError()) {
214+
write_result_[0] = strm_.avail_out;
215+
write_result_[1] = strm_.avail_in;
216+
write_in_progress_ = false;
213217
}
214-
ctx->Unref();
218+
Unref();
215219
return;
216220
}
217221

218222
// async version
219-
ctx->ScheduleWork();
223+
ScheduleWork();
220224
}
221225

222226
// thread pool!

0 commit comments

Comments
 (0)