summaryrefslogtreecommitdiff
path: root/src/node_zlib.cc
diff options
context:
space:
mode:
authorNikolai Vavilov <vvnicholas@gmail.com>2014-01-28 19:35:51 +0200
committerFedor Indutny <fedor.indutny@gmail.com>2014-02-01 03:45:45 +0400
commit9b37b83a2079447662e11c8ee8456be298606a2a (patch)
treee9d59fbd76db4ec6a2cccf5013f7453e809049a2 /src/node_zlib.cc
parent49c2372e684ee91c2791488aaa8caaecfcf64e11 (diff)
downloadandroid-node-v8-9b37b83a2079447662e11c8ee8456be298606a2a.tar.gz
android-node-v8-9b37b83a2079447662e11c8ee8456be298606a2a.tar.bz2
android-node-v8-9b37b83a2079447662e11c8ee8456be298606a2a.zip
zlib: add sync versions for convenience methods
Diffstat (limited to 'src/node_zlib.cc')
-rw-r--r--src/node_zlib.cc75
1 files changed, 55 insertions, 20 deletions
diff --git a/src/node_zlib.cc b/src/node_zlib.cc
index 16372bd811..8a2125fa3f 100644
--- a/src/node_zlib.cc
+++ b/src/node_zlib.cc
@@ -39,6 +39,7 @@
namespace node {
+using v8::Array;
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
@@ -125,6 +126,7 @@ class ZCtx : public AsyncWrap {
// write(flush, in, in_off, in_len, out, out_off, out_len)
+ template <bool async>
static void Write(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);
assert(args.Length() == 7);
@@ -190,6 +192,15 @@ class ZCtx : public AsyncWrap {
// set this so that later on, I can easily tell how much was written.
ctx->chunk_size_ = out_len;
+ if (!async) {
+ // sync version
+ Process(work_req);
+ if (CheckError(ctx))
+ AfterSync(ctx, args);
+ return;
+ }
+
+ // async version
uv_queue_work(ctx->env()->event_loop(),
work_req,
ZCtx::Process,
@@ -199,6 +210,21 @@ class ZCtx : public AsyncWrap {
}
+ static void AfterSync(ZCtx* ctx, const FunctionCallbackInfo<Value>& args) {
+ Local<Integer> avail_out = Integer::New(ctx->strm_.avail_out, node_isolate);
+ Local<Integer> avail_in = Integer::New(ctx->strm_.avail_in, node_isolate);
+
+ ctx->write_in_progress_ = false;
+
+ Local<Array> result = Array::New(2);
+ result->Set(0, avail_in);
+ result->Set(1, avail_out);
+ args.GetReturnValue().Set(result);
+
+ ctx->Unref();
+ }
+
+
// thread pool!
// This function may be called multiple times on the uv_work pool
// for a single write() call, until all of the input bytes have
@@ -249,6 +275,31 @@ class ZCtx : public AsyncWrap {
// or shift the queue and call Process.
}
+
+ static bool CheckError(ZCtx* ctx) {
+ // Acceptable error states depend on the type of zlib stream.
+ switch (ctx->err_) {
+ case Z_OK:
+ case Z_STREAM_END:
+ case Z_BUF_ERROR:
+ // normal statuses, not fatal
+ break;
+ case Z_NEED_DICT:
+ if (ctx->dictionary_ == NULL)
+ ZCtx::Error(ctx, "Missing dictionary");
+ else
+ ZCtx::Error(ctx, "Bad dictionary");
+ return false;
+ default:
+ // something else.
+ ZCtx::Error(ctx, "Zlib error");
+ return false;
+ }
+
+ return true;
+ }
+
+
// v8 land!
static void After(uv_work_t* work_req, int status) {
assert(status == 0);
@@ -259,25 +310,8 @@ class ZCtx : public AsyncWrap {
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());
- // Acceptable error states depend on the type of zlib stream.
- switch (ctx->err_) {
- case Z_OK:
- case Z_STREAM_END:
- case Z_BUF_ERROR:
- // normal statuses, not fatal
- break;
- case Z_NEED_DICT:
- if (ctx->dictionary_ == NULL) {
- ZCtx::Error(ctx, "Missing dictionary");
- } else {
- ZCtx::Error(ctx, "Bad dictionary");
- }
- return;
- default:
- // something else.
- ZCtx::Error(ctx, "Zlib error");
- return;
- }
+ if (!CheckError(ctx))
+ return;
Local<Integer> avail_out = Integer::New(ctx->strm_.avail_out, node_isolate);
Local<Integer> avail_in = Integer::New(ctx->strm_.avail_in, node_isolate);
@@ -556,7 +590,8 @@ void InitZlib(Handle<Object> target,
z->InstanceTemplate()->SetInternalFieldCount(1);
- NODE_SET_PROTOTYPE_METHOD(z, "write", ZCtx::Write);
+ NODE_SET_PROTOTYPE_METHOD(z, "write", ZCtx::Write<true>);
+ NODE_SET_PROTOTYPE_METHOD(z, "writeSync", ZCtx::Write<false>);
NODE_SET_PROTOTYPE_METHOD(z, "init", ZCtx::Init);
NODE_SET_PROTOTYPE_METHOD(z, "close", ZCtx::Close);
NODE_SET_PROTOTYPE_METHOD(z, "params", ZCtx::Params);