@@ -47,26 +47,22 @@ enum node_zlib_mode {
4747 UNZIP
4848};
4949
50- template <node_zlib_mode mode> class ZCtx ;
51-
5250
5351void InitZlib (v8::Handle<v8::Object> target);
5452
5553
5654/* *
5755 * Deflate/Inflate
5856 */
59- template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
57+ class ZCtx : public ObjectWrap {
6058 public:
6159
62- ZCtx () : ObjectWrap() {
63- dictionary_ = NULL ;
64- }
60+ ZCtx (node_zlib_mode mode) : ObjectWrap(), dictionary_(NULL ), mode_(mode) {}
6561
6662 ~ZCtx () {
67- if (mode == DEFLATE || mode == GZIP || mode == DEFLATERAW ) {
63+ if (mode_ == DEFLATE || mode_ == GZIP || mode_ == DEFLATERAW ) {
6864 (void )deflateEnd (&strm_);
69- } else if (mode == INFLATE || mode == GUNZIP || mode == INFLATERAW ) {
65+ } else if (mode_ == INFLATE || mode_ == GUNZIP || mode_ == INFLATERAW ) {
7066 (void )inflateEnd (&strm_);
7167 }
7268
@@ -78,7 +74,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
7874 HandleScope scope;
7975 assert (args.Length () == 7 );
8076
81- ZCtx<mode> *ctx = ObjectWrap::Unwrap< ZCtx<mode> >(args.This ());
77+ ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This ());
8278 assert (ctx->init_done_ && " write before init" );
8379
8480 assert (!ctx->write_in_progress_ && " write already in progress" );
@@ -127,8 +123,8 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
127123
128124 uv_queue_work (uv_default_loop (),
129125 work_req,
130- ZCtx<mode> ::Process,
131- ZCtx<mode> ::After);
126+ ZCtx::Process,
127+ ZCtx::After);
132128
133129 ctx->Ref ();
134130
@@ -141,13 +137,13 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
141137 // for a single write() call, until all of the input bytes have
142138 // been consumed.
143139 static void Process (uv_work_t * work_req) {
144- ZCtx<mode> *ctx = container_of (work_req, ZCtx<mode> , work_req_);
140+ ZCtx *ctx = container_of (work_req, ZCtx, work_req_);
145141
146142 // If the avail_out is left at 0, then it means that it ran out
147143 // of room. If there was avail_out left over, then it means
148144 // that all of the input was consumed.
149145 int err;
150- switch (mode ) {
146+ switch (ctx-> mode_ ) {
151147 case DEFLATE :
152148 case GZIP :
153149 case DEFLATERAW :
@@ -186,7 +182,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
186182 // v8 land!
187183 static void After (uv_work_t * work_req) {
188184 HandleScope scope;
189- ZCtx<mode> *ctx = container_of (work_req, ZCtx<mode> , work_req_);
185+ ZCtx *ctx = container_of (work_req, ZCtx, work_req_);
190186
191187 Local<Integer> avail_out = Integer::New (ctx->strm_ .avail_out );
192188 Local<Integer> avail_in = Integer::New (ctx->strm_ .avail_in );
@@ -204,7 +200,16 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
204200
205201 static Handle<Value> New (const Arguments& args) {
206202 HandleScope scope;
207- ZCtx<mode> *ctx = new ZCtx<mode>();
203+ if (args.Length () < 1 || !args[0 ]->IsInt32 ()) {
204+ return ThrowException (Exception::TypeError (String::New (" Bad argument" )));
205+ }
206+ node_zlib_mode mode = (node_zlib_mode) args[0 ]->Int32Value ();
207+
208+ if (mode < DEFLATE || mode > UNZIP ) {
209+ return ThrowException (Exception::TypeError (String::New (" Bad argument" )));
210+ }
211+
212+ ZCtx *ctx = new ZCtx (mode);
208213 ctx->Wrap (args.This ());
209214 return args.This ();
210215 }
@@ -216,7 +221,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
216221 assert ((args.Length () == 4 || args.Length () == 5 ) &&
217222 " init(windowBits, level, memLevel, strategy, [dictionary])" );
218223
219- ZCtx<mode> *ctx = ObjectWrap::Unwrap< ZCtx<mode> >(args.This ());
224+ ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This ());
220225
221226 int windowBits = args[0 ]->Uint32Value ();
222227 assert ((windowBits >= 8 && windowBits <= 15 ) && " invalid windowBits" );
@@ -254,7 +259,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
254259 static Handle<Value> Reset (const Arguments &args) {
255260 HandleScope scope;
256261
257- ZCtx<mode> *ctx = ObjectWrap::Unwrap< ZCtx<mode> >(args.This ());
262+ ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This ());
258263
259264 Reset (ctx);
260265 SetDictionary (ctx);
@@ -274,20 +279,20 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
274279
275280 ctx->flush_ = Z_NO_FLUSH ;
276281
277- if (mode == GZIP || mode == GUNZIP ) {
282+ if (ctx-> mode_ == GZIP || ctx-> mode_ == GUNZIP ) {
278283 ctx->windowBits_ += 16 ;
279284 }
280285
281- if (mode == UNZIP ) {
286+ if (ctx-> mode_ == UNZIP ) {
282287 ctx->windowBits_ += 32 ;
283288 }
284289
285- if (mode == DEFLATERAW || mode == INFLATERAW ) {
290+ if (ctx-> mode_ == DEFLATERAW || ctx-> mode_ == INFLATERAW ) {
286291 ctx->windowBits_ *= -1 ;
287292 }
288293
289294 int err;
290- switch (mode ) {
295+ switch (ctx-> mode_ ) {
291296 case DEFLATE :
292297 case GZIP :
293298 case DEFLATERAW :
@@ -322,7 +327,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
322327
323328 int err = Z_OK ;
324329
325- switch (mode ) {
330+ switch (ctx-> mode_ ) {
326331 case DEFLATE :
327332 case DEFLATERAW :
328333 err = deflateSetDictionary (&ctx->strm_ ,
@@ -339,7 +344,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
339344 static void Reset (ZCtx* ctx) {
340345 int err = Z_OK ;
341346
342- switch (mode ) {
347+ switch (ctx-> mode_ ) {
343348 case DEFLATE :
344349 case DEFLATERAW :
345350 err = deflateReset (&ctx->strm_ );
@@ -375,30 +380,23 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
375380 bool write_in_progress_;
376381
377382 uv_work_t work_req_;
383+ node_zlib_mode mode_;
378384};
379385
380386
381- #define NODE_ZLIB_CLASS (mode, name ) \
382- { \
383- Local<FunctionTemplate> z = FunctionTemplate::New (ZCtx<mode>::New); \
384- z->InstanceTemplate ()->SetInternalFieldCount (1 ); \
385- NODE_SET_PROTOTYPE_METHOD (z, " write" , ZCtx<mode>::Write); \
386- NODE_SET_PROTOTYPE_METHOD (z, " init" , ZCtx<mode>::Init); \
387- NODE_SET_PROTOTYPE_METHOD (z, " reset" , ZCtx<mode>::Reset); \
388- z->SetClassName (String::NewSymbol (name)); \
389- target->Set (String::NewSymbol (name), z->GetFunction ()); \
390- }
391-
392387void InitZlib (Handle<Object> target) {
393388 HandleScope scope;
394389
395- NODE_ZLIB_CLASS (INFLATE , " Inflate" )
396- NODE_ZLIB_CLASS (DEFLATE , " Deflate" )
397- NODE_ZLIB_CLASS (INFLATERAW , " InflateRaw" )
398- NODE_ZLIB_CLASS (DEFLATERAW , " DeflateRaw" )
399- NODE_ZLIB_CLASS (GZIP , " Gzip" )
400- NODE_ZLIB_CLASS (GUNZIP , " Gunzip" )
401- NODE_ZLIB_CLASS (UNZIP , " Unzip" )
390+ Local<FunctionTemplate> z = FunctionTemplate::New (ZCtx::New);
391+
392+ z->InstanceTemplate ()->SetInternalFieldCount (1 );
393+
394+ NODE_SET_PROTOTYPE_METHOD (z, " write" , ZCtx::Write);
395+ NODE_SET_PROTOTYPE_METHOD (z, " init" , ZCtx::Init);
396+ NODE_SET_PROTOTYPE_METHOD (z, " reset" , ZCtx::Reset);
397+
398+ z->SetClassName (String::NewSymbol (" Zlib" ));
399+ target->Set (String::NewSymbol (" Zlib" ), z->GetFunction ());
402400
403401 callback_sym = NODE_PSYMBOL (" callback" );
404402
@@ -428,6 +426,14 @@ void InitZlib(Handle<Object> target) {
428426 NODE_DEFINE_CONSTANT (target, Z_DEFAULT_STRATEGY );
429427 NODE_DEFINE_CONSTANT (target, ZLIB_VERNUM );
430428
429+ NODE_DEFINE_CONSTANT (target, DEFLATE );
430+ NODE_DEFINE_CONSTANT (target, INFLATE );
431+ NODE_DEFINE_CONSTANT (target, GZIP );
432+ NODE_DEFINE_CONSTANT (target, GUNZIP );
433+ NODE_DEFINE_CONSTANT (target, DEFLATERAW );
434+ NODE_DEFINE_CONSTANT (target, INFLATERAW );
435+ NODE_DEFINE_CONSTANT (target, UNZIP );
436+
431437 target->Set (String::NewSymbol (" ZLIB_VERSION" ), String::New (ZLIB_VERSION ));
432438}
433439
0 commit comments