@@ -138,24 +138,15 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { |
138 | 138 | // write(flush, in, in_off, in_len, out, out_off, out_len) |
139 | 139 | template <bool async> |
140 | 140 | static void Write(const FunctionCallbackInfo<Value>& args) { |
| 141 | + Environment* env = Environment::GetCurrent(args); |
| 142 | + Local<Context> context = env->context(); |
141 | 143 | CHECK_EQ(args.Length(), 7); |
142 | 144 |
|
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; |
152 | 148 |
|
153 | 149 | 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; |
159 | 150 | if (!args[0]->Uint32Value(context).To(&flush)) return; |
160 | 151 |
|
161 | 152 | if (flush != Z_NO_FLUSH && |
@@ -167,56 +158,69 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { |
167 | 158 | CHECK(0 && "Invalid flush value"); |
168 | 159 | } |
169 | 160 |
|
170 | | - AllocScope alloc_scope(ctx); |
171 | | - |
172 | | - Bytef* in; |
173 | | - Bytef* out; |
174 | | - uint32_t in_off, in_len, out_off, out_len; |
175 | | - |
176 | 161 | if (args[1]->IsNull()) { |
177 | 162 | // just a flush |
178 | 163 | in = nullptr; |
179 | 164 | in_len = 0; |
180 | 165 | in_off = 0; |
181 | 166 | } else { |
182 | 167 | 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>(); |
185 | 169 | if (!args[2]->Uint32Value(context).To(&in_off)) return; |
186 | 170 | if (!args[3]->Uint32Value(context).To(&in_len)) return; |
187 | 171 |
|
188 | 172 | 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; |
190 | 174 | } |
191 | 175 |
|
192 | 176 | CHECK(Buffer::HasInstance(args[4])); |
193 | | - Local<Object> out_buf = args[4]->ToObject(context).ToLocalChecked(); |
| 177 | + Local<Object> out_buf = args[4].As<Object>(); |
194 | 178 | if (!args[5]->Uint32Value(context).To(&out_off)) return; |
195 | 179 | if (!args[6]->Uint32Value(context).To(&out_len)) return; |
196 | 180 | 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(); |
198 | 202 |
|
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; |
204 | 208 |
|
205 | 209 | if (!async) { |
206 | 210 | // 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; |
213 | 217 | } |
214 | | - ctx->Unref(); |
| 218 | + Unref(); |
215 | 219 | return; |
216 | 220 | } |
217 | 221 |
|
218 | 222 | // async version |
219 | | - ctx->ScheduleWork(); |
| 223 | + ScheduleWork(); |
220 | 224 | } |
221 | 225 |
|
222 | 226 | // thread pool! |
|