Skip to content

Commit d5db576

Browse files
addaleaxMylesBorins
authored andcommitted
zlib: reduce number of static internal methods
There really isn’t any good reason for these to be static methods, it just adds one layer of indirection (when reading the code, not in a way that affects behaviour). Addresses a `TODO` comment introduced in c072057. PR-URL: #20674 Refs: c072057 Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 7d28f5b commit d5db576

File tree

1 file changed

+74
-88
lines changed

1 file changed

+74
-88
lines changed

src/node_zlib.cc

+74-88
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
202202
// sync version
203203
env->PrintSyncTrace();
204204
ctx->DoThreadPoolWork();
205-
if (CheckError(ctx)) {
205+
if (ctx->CheckError()) {
206206
ctx->write_result_[0] = ctx->strm_.avail_out;
207207
ctx->write_result_[1] = ctx->strm_.avail_in;
208208
ctx->write_in_progress_ = false;
@@ -215,53 +215,43 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
215215
ctx->ScheduleWork();
216216
}
217217

218-
// TODO(addaleax): Make these methods non-static. It's a significant bunch
219-
// of churn that's better left for a separate PR.
220-
void DoThreadPoolWork() override {
221-
Process(this);
222-
}
223-
224-
void AfterThreadPoolWork(int status) override {
225-
After(this, status);
226-
}
227-
228218
// thread pool!
229219
// This function may be called multiple times on the uv_work pool
230220
// for a single write() call, until all of the input bytes have
231221
// been consumed.
232-
static void Process(ZCtx* ctx) {
222+
void DoThreadPoolWork() {
233223
const Bytef* next_expected_header_byte = nullptr;
234224

235225
// If the avail_out is left at 0, then it means that it ran out
236226
// of room. If there was avail_out left over, then it means
237227
// that all of the input was consumed.
238-
switch (ctx->mode_) {
228+
switch (mode_) {
239229
case DEFLATE:
240230
case GZIP:
241231
case DEFLATERAW:
242-
ctx->err_ = deflate(&ctx->strm_, ctx->flush_);
232+
err_ = deflate(&strm_, flush_);
243233
break;
244234
case UNZIP:
245-
if (ctx->strm_.avail_in > 0) {
246-
next_expected_header_byte = ctx->strm_.next_in;
235+
if (strm_.avail_in > 0) {
236+
next_expected_header_byte = strm_.next_in;
247237
}
248238

249-
switch (ctx->gzip_id_bytes_read_) {
239+
switch (gzip_id_bytes_read_) {
250240
case 0:
251241
if (next_expected_header_byte == nullptr) {
252242
break;
253243
}
254244

255245
if (*next_expected_header_byte == GZIP_HEADER_ID1) {
256-
ctx->gzip_id_bytes_read_ = 1;
246+
gzip_id_bytes_read_ = 1;
257247
next_expected_header_byte++;
258248

259-
if (ctx->strm_.avail_in == 1) {
249+
if (strm_.avail_in == 1) {
260250
// The only available byte was already read.
261251
break;
262252
}
263253
} else {
264-
ctx->mode_ = INFLATE;
254+
mode_ = INFLATE;
265255
break;
266256
}
267257

@@ -272,12 +262,12 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
272262
}
273263

274264
if (*next_expected_header_byte == GZIP_HEADER_ID2) {
275-
ctx->gzip_id_bytes_read_ = 2;
276-
ctx->mode_ = GUNZIP;
265+
gzip_id_bytes_read_ = 2;
266+
mode_ = GUNZIP;
277267
} else {
278268
// There is no actual difference between INFLATE and INFLATERAW
279269
// (after initialization).
280-
ctx->mode_ = INFLATE;
270+
mode_ = INFLATE;
281271
}
282272

283273
break;
@@ -289,39 +279,37 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
289279
case INFLATE:
290280
case GUNZIP:
291281
case INFLATERAW:
292-
ctx->err_ = inflate(&ctx->strm_, ctx->flush_);
282+
err_ = inflate(&strm_, flush_);
293283

294284
// If data was encoded with dictionary (INFLATERAW will have it set in
295285
// SetDictionary, don't repeat that here)
296-
if (ctx->mode_ != INFLATERAW &&
297-
ctx->err_ == Z_NEED_DICT &&
298-
ctx->dictionary_ != nullptr) {
286+
if (mode_ != INFLATERAW &&
287+
err_ == Z_NEED_DICT &&
288+
dictionary_ != nullptr) {
299289
// Load it
300-
ctx->err_ = inflateSetDictionary(&ctx->strm_,
301-
ctx->dictionary_,
302-
ctx->dictionary_len_);
303-
if (ctx->err_ == Z_OK) {
290+
err_ = inflateSetDictionary(&strm_, dictionary_, dictionary_len_);
291+
if (err_ == Z_OK) {
304292
// And try to decode again
305-
ctx->err_ = inflate(&ctx->strm_, ctx->flush_);
306-
} else if (ctx->err_ == Z_DATA_ERROR) {
293+
err_ = inflate(&strm_, flush_);
294+
} else if (err_ == Z_DATA_ERROR) {
307295
// Both inflateSetDictionary() and inflate() return Z_DATA_ERROR.
308296
// Make it possible for After() to tell a bad dictionary from bad
309297
// input.
310-
ctx->err_ = Z_NEED_DICT;
298+
err_ = Z_NEED_DICT;
311299
}
312300
}
313301

314-
while (ctx->strm_.avail_in > 0 &&
315-
ctx->mode_ == GUNZIP &&
316-
ctx->err_ == Z_STREAM_END &&
317-
ctx->strm_.next_in[0] != 0x00) {
302+
while (strm_.avail_in > 0 &&
303+
mode_ == GUNZIP &&
304+
err_ == Z_STREAM_END &&
305+
strm_.next_in[0] != 0x00) {
318306
// Bytes remain in input buffer. Perhaps this is another compressed
319307
// member in the same archive, or just trailing garbage.
320308
// Trailing zero bytes are okay, though, since they are frequently
321309
// used for padding.
322310

323-
Reset(ctx);
324-
ctx->err_ = inflate(&ctx->strm_, ctx->flush_);
311+
Reset();
312+
err_ = inflate(&strm_, flush_);
325313
}
326314
break;
327315
default:
@@ -336,27 +324,27 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
336324
}
337325

338326

339-
static bool CheckError(ZCtx* ctx) {
327+
bool CheckError() {
340328
// Acceptable error states depend on the type of zlib stream.
341-
switch (ctx->err_) {
329+
switch (err_) {
342330
case Z_OK:
343331
case Z_BUF_ERROR:
344-
if (ctx->strm_.avail_out != 0 && ctx->flush_ == Z_FINISH) {
345-
ZCtx::Error(ctx, "unexpected end of file");
332+
if (strm_.avail_out != 0 && flush_ == Z_FINISH) {
333+
Error("unexpected end of file");
346334
return false;
347335
}
348336
case Z_STREAM_END:
349337
// normal statuses, not fatal
350338
break;
351339
case Z_NEED_DICT:
352-
if (ctx->dictionary_ == nullptr)
353-
ZCtx::Error(ctx, "Missing dictionary");
340+
if (dictionary_ == nullptr)
341+
Error("Missing dictionary");
354342
else
355-
ZCtx::Error(ctx, "Bad dictionary");
343+
Error("Bad dictionary");
356344
return false;
357345
default:
358346
// something else.
359-
ZCtx::Error(ctx, "Zlib error");
347+
Error("Zlib error");
360348
return false;
361349
}
362350

@@ -365,59 +353,57 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
365353

366354

367355
// v8 land!
368-
static void After(ZCtx* ctx, int status) {
369-
Environment* env = ctx->env();
370-
ctx->write_in_progress_ = false;
356+
void AfterThreadPoolWork(int status) {
357+
write_in_progress_ = false;
371358

372359
if (status == UV_ECANCELED) {
373-
ctx->Close();
360+
Close();
374361
return;
375362
}
376363

377364
CHECK_EQ(status, 0);
378365

379-
HandleScope handle_scope(env->isolate());
380-
Context::Scope context_scope(env->context());
366+
HandleScope handle_scope(env()->isolate());
367+
Context::Scope context_scope(env()->context());
381368

382-
if (!CheckError(ctx))
369+
if (!CheckError())
383370
return;
384371

385-
ctx->write_result_[0] = ctx->strm_.avail_out;
386-
ctx->write_result_[1] = ctx->strm_.avail_in;
372+
write_result_[0] = strm_.avail_out;
373+
write_result_[1] = strm_.avail_in;
387374

388375
// call the write() cb
389-
Local<Function> cb = PersistentToLocal(env->isolate(),
390-
ctx->write_js_callback_);
391-
ctx->MakeCallback(cb, 0, nullptr);
376+
Local<Function> cb = PersistentToLocal(env()->isolate(),
377+
write_js_callback_);
378+
MakeCallback(cb, 0, nullptr);
392379

393-
ctx->Unref();
394-
if (ctx->pending_close_)
395-
ctx->Close();
380+
Unref();
381+
if (pending_close_)
382+
Close();
396383
}
397384

398-
static void Error(ZCtx* ctx, const char* message) {
399-
Environment* env = ctx->env();
400-
385+
// TODO(addaleax): Switch to modern error system (node_errors.h).
386+
void Error(const char* message) {
401387
// If you hit this assertion, you forgot to enter the v8::Context first.
402-
CHECK_EQ(env->context(), env->isolate()->GetCurrentContext());
388+
CHECK_EQ(env()->context(), env()->isolate()->GetCurrentContext());
403389

404-
if (ctx->strm_.msg != nullptr) {
405-
message = ctx->strm_.msg;
390+
if (strm_.msg != nullptr) {
391+
message = strm_.msg;
406392
}
407393

408-
HandleScope scope(env->isolate());
394+
HandleScope scope(env()->isolate());
409395
Local<Value> args[2] = {
410-
OneByteString(env->isolate(), message),
411-
Number::New(env->isolate(), ctx->err_)
396+
OneByteString(env()->isolate(), message),
397+
Number::New(env()->isolate(), err_)
412398
};
413-
ctx->MakeCallback(env->onerror_string(), arraysize(args), args);
399+
MakeCallback(env()->onerror_string(), arraysize(args), args);
414400

415401
// no hope of rescue.
416-
if (ctx->write_in_progress_)
417-
ctx->Unref();
418-
ctx->write_in_progress_ = false;
419-
if (ctx->pending_close_)
420-
ctx->Close();
402+
if (write_in_progress_)
403+
Unref();
404+
write_in_progress_ = false;
405+
if (pending_close_)
406+
Close();
421407
}
422408

423409
static void New(const FunctionCallbackInfo<Value>& args) {
@@ -510,7 +496,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
510496
static void Reset(const FunctionCallbackInfo<Value> &args) {
511497
ZCtx* ctx;
512498
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
513-
Reset(ctx);
499+
ctx->Reset();
514500
SetDictionary(ctx);
515501
}
516502

@@ -613,7 +599,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
613599
}
614600

615601
if (ctx->err_ != Z_OK) {
616-
ZCtx::Error(ctx, "Failed to set dictionary");
602+
ctx->Error("Failed to set dictionary");
617603
}
618604
}
619605

@@ -630,30 +616,30 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
630616
}
631617

632618
if (ctx->err_ != Z_OK && ctx->err_ != Z_BUF_ERROR) {
633-
ZCtx::Error(ctx, "Failed to set parameters");
619+
ctx->Error("Failed to set parameters");
634620
}
635621
}
636622

637-
static void Reset(ZCtx* ctx) {
638-
ctx->err_ = Z_OK;
623+
void Reset() {
624+
err_ = Z_OK;
639625

640-
switch (ctx->mode_) {
626+
switch (mode_) {
641627
case DEFLATE:
642628
case DEFLATERAW:
643629
case GZIP:
644-
ctx->err_ = deflateReset(&ctx->strm_);
630+
err_ = deflateReset(&strm_);
645631
break;
646632
case INFLATE:
647633
case INFLATERAW:
648634
case GUNZIP:
649-
ctx->err_ = inflateReset(&ctx->strm_);
635+
err_ = inflateReset(&strm_);
650636
break;
651637
default:
652638
break;
653639
}
654640

655-
if (ctx->err_ != Z_OK) {
656-
ZCtx::Error(ctx, "Failed to reset stream");
641+
if (err_ != Z_OK) {
642+
Error("Failed to reset stream");
657643
}
658644
}
659645

0 commit comments

Comments
 (0)