@@ -202,7 +202,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
202
202
// sync version
203
203
env->PrintSyncTrace ();
204
204
ctx->DoThreadPoolWork ();
205
- if (CheckError (ctx )) {
205
+ if (ctx-> CheckError ()) {
206
206
ctx->write_result_ [0 ] = ctx->strm_ .avail_out ;
207
207
ctx->write_result_ [1 ] = ctx->strm_ .avail_in ;
208
208
ctx->write_in_progress_ = false ;
@@ -215,53 +215,43 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
215
215
ctx->ScheduleWork ();
216
216
}
217
217
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
-
228
218
// thread pool!
229
219
// This function may be called multiple times on the uv_work pool
230
220
// for a single write() call, until all of the input bytes have
231
221
// been consumed.
232
- static void Process (ZCtx* ctx ) {
222
+ void DoThreadPoolWork ( ) {
233
223
const Bytef* next_expected_header_byte = nullptr ;
234
224
235
225
// If the avail_out is left at 0, then it means that it ran out
236
226
// of room. If there was avail_out left over, then it means
237
227
// that all of the input was consumed.
238
- switch (ctx-> mode_ ) {
228
+ switch (mode_) {
239
229
case DEFLATE:
240
230
case GZIP:
241
231
case DEFLATERAW:
242
- ctx-> err_ = deflate (&ctx-> strm_ , ctx-> flush_ );
232
+ err_ = deflate (&strm_, flush_);
243
233
break ;
244
234
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 ;
247
237
}
248
238
249
- switch (ctx-> gzip_id_bytes_read_ ) {
239
+ switch (gzip_id_bytes_read_) {
250
240
case 0 :
251
241
if (next_expected_header_byte == nullptr ) {
252
242
break ;
253
243
}
254
244
255
245
if (*next_expected_header_byte == GZIP_HEADER_ID1) {
256
- ctx-> gzip_id_bytes_read_ = 1 ;
246
+ gzip_id_bytes_read_ = 1 ;
257
247
next_expected_header_byte++;
258
248
259
- if (ctx-> strm_ .avail_in == 1 ) {
249
+ if (strm_.avail_in == 1 ) {
260
250
// The only available byte was already read.
261
251
break ;
262
252
}
263
253
} else {
264
- ctx-> mode_ = INFLATE;
254
+ mode_ = INFLATE;
265
255
break ;
266
256
}
267
257
@@ -272,12 +262,12 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
272
262
}
273
263
274
264
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;
277
267
} else {
278
268
// There is no actual difference between INFLATE and INFLATERAW
279
269
// (after initialization).
280
- ctx-> mode_ = INFLATE;
270
+ mode_ = INFLATE;
281
271
}
282
272
283
273
break ;
@@ -289,39 +279,37 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
289
279
case INFLATE:
290
280
case GUNZIP:
291
281
case INFLATERAW:
292
- ctx-> err_ = inflate (&ctx-> strm_ , ctx-> flush_ );
282
+ err_ = inflate (&strm_, flush_);
293
283
294
284
// If data was encoded with dictionary (INFLATERAW will have it set in
295
285
// 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 ) {
299
289
// 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) {
304
292
// 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) {
307
295
// Both inflateSetDictionary() and inflate() return Z_DATA_ERROR.
308
296
// Make it possible for After() to tell a bad dictionary from bad
309
297
// input.
310
- ctx-> err_ = Z_NEED_DICT;
298
+ err_ = Z_NEED_DICT;
311
299
}
312
300
}
313
301
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 ) {
318
306
// Bytes remain in input buffer. Perhaps this is another compressed
319
307
// member in the same archive, or just trailing garbage.
320
308
// Trailing zero bytes are okay, though, since they are frequently
321
309
// used for padding.
322
310
323
- Reset (ctx );
324
- ctx-> err_ = inflate (&ctx-> strm_ , ctx-> flush_ );
311
+ Reset ();
312
+ err_ = inflate (&strm_, flush_);
325
313
}
326
314
break ;
327
315
default :
@@ -336,27 +324,27 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
336
324
}
337
325
338
326
339
- static bool CheckError (ZCtx* ctx ) {
327
+ bool CheckError () {
340
328
// Acceptable error states depend on the type of zlib stream.
341
- switch (ctx-> err_ ) {
329
+ switch (err_) {
342
330
case Z_OK:
343
331
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" );
346
334
return false ;
347
335
}
348
336
case Z_STREAM_END:
349
337
// normal statuses, not fatal
350
338
break ;
351
339
case Z_NEED_DICT:
352
- if (ctx-> dictionary_ == nullptr )
353
- ZCtx:: Error (ctx, " Missing dictionary" );
340
+ if (dictionary_ == nullptr )
341
+ Error (" Missing dictionary" );
354
342
else
355
- ZCtx:: Error (ctx, " Bad dictionary" );
343
+ Error (" Bad dictionary" );
356
344
return false ;
357
345
default :
358
346
// something else.
359
- ZCtx:: Error (ctx, " Zlib error" );
347
+ Error (" Zlib error" );
360
348
return false ;
361
349
}
362
350
@@ -365,59 +353,57 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
365
353
366
354
367
355
// 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 ;
371
358
372
359
if (status == UV_ECANCELED) {
373
- ctx-> Close ();
360
+ Close ();
374
361
return ;
375
362
}
376
363
377
364
CHECK_EQ (status, 0 );
378
365
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 ());
381
368
382
- if (!CheckError (ctx ))
369
+ if (!CheckError ())
383
370
return ;
384
371
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 ;
387
374
388
375
// 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 );
392
379
393
- ctx-> Unref ();
394
- if (ctx-> pending_close_ )
395
- ctx-> Close ();
380
+ Unref ();
381
+ if (pending_close_)
382
+ Close ();
396
383
}
397
384
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) {
401
387
// 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 ());
403
389
404
- if (ctx-> strm_ .msg != nullptr ) {
405
- message = ctx-> strm_ .msg ;
390
+ if (strm_.msg != nullptr ) {
391
+ message = strm_.msg ;
406
392
}
407
393
408
- HandleScope scope (env->isolate ());
394
+ HandleScope scope (env () ->isolate ());
409
395
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_)
412
398
};
413
- ctx-> MakeCallback (env->onerror_string (), arraysize (args), args);
399
+ MakeCallback (env () ->onerror_string (), arraysize (args), args);
414
400
415
401
// 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 ();
421
407
}
422
408
423
409
static void New (const FunctionCallbackInfo<Value>& args) {
@@ -510,7 +496,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
510
496
static void Reset (const FunctionCallbackInfo<Value> &args) {
511
497
ZCtx* ctx;
512
498
ASSIGN_OR_RETURN_UNWRAP (&ctx, args.Holder ());
513
- Reset (ctx );
499
+ ctx-> Reset ();
514
500
SetDictionary (ctx);
515
501
}
516
502
@@ -613,7 +599,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
613
599
}
614
600
615
601
if (ctx->err_ != Z_OK) {
616
- ZCtx:: Error (ctx, " Failed to set dictionary" );
602
+ ctx-> Error (" Failed to set dictionary" );
617
603
}
618
604
}
619
605
@@ -630,30 +616,30 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
630
616
}
631
617
632
618
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" );
634
620
}
635
621
}
636
622
637
- static void Reset (ZCtx* ctx ) {
638
- ctx-> err_ = Z_OK;
623
+ void Reset () {
624
+ err_ = Z_OK;
639
625
640
- switch (ctx-> mode_ ) {
626
+ switch (mode_) {
641
627
case DEFLATE:
642
628
case DEFLATERAW:
643
629
case GZIP:
644
- ctx-> err_ = deflateReset (&ctx-> strm_ );
630
+ err_ = deflateReset (&strm_);
645
631
break ;
646
632
case INFLATE:
647
633
case INFLATERAW:
648
634
case GUNZIP:
649
- ctx-> err_ = inflateReset (&ctx-> strm_ );
635
+ err_ = inflateReset (&strm_);
650
636
break ;
651
637
default :
652
638
break ;
653
639
}
654
640
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" );
657
643
}
658
644
}
659
645
0 commit comments