Permalink
Browse files

Add support for node 0.6 with help of TooTallNate's shim.

  • Loading branch information...
1 parent fc8be8c commit bd15b6acd86fe4cbad51407751258e026ef74aa9 @kesla committed Dec 6, 2011
Showing with 59 additions and 19 deletions.
  1. +11 −14 src/binding.cc
  2. +6 −5 src/binding.h
  3. +42 −0 src/node_async_shim.h
View
@@ -59,7 +59,7 @@ inline void Base::CallErrCallback(const v8::Handle<v8::Function>& callback,
// CompressUncompressBase
// PROTECTED
-int CompressUncompressBase::After(eio_req *req) {
+async_rtn CompressUncompressBase::After(eio_req *req) {
v8::HandleScope scope;
SnappyRequest<std::string>* snappy_req =
static_cast<SnappyRequest<std::string>*>(req->data);
@@ -72,7 +72,7 @@ int CompressUncompressBase::After(eio_req *req) {
ev_unref(EV_DEFAULT_UC);
snappy_req->callback.Dispose();
delete snappy_req;
- return 0;
+ RETURN_ASYNC_AFTER;
}
inline void
@@ -89,8 +89,7 @@ CompressUncompressBase::CallOkCallback(const v8::Handle<v8::Function>& callback,
v8::Handle<v8::Value> CompressBinding::Async(const v8::Arguments& args) {
v8::HandleScope scope;
SnappyRequest<std::string>* snappy_req = new SnappyRequest<std::string>(args);
- eio_custom(AsyncOperation, EIO_PRI_DEFAULT, After, snappy_req);
- ev_ref(EV_DEFAULT_UC);
+ BEGIN_ASYNC(snappy_req, AsyncOperation, After);
return v8::Undefined();
}
@@ -106,23 +105,22 @@ v8::Handle<v8::Value> CompressBinding::Sync(const v8::Arguments& args) {
}
// PRIVATE
-int CompressBinding::AsyncOperation(eio_req *req) {
+async_rtn CompressBinding::AsyncOperation(eio_req *req) {
SnappyRequest<std::string>* snappy_req =
static_cast<SnappyRequest<std::string>*>(req->data);
std::string dst;
std::string* input = &snappy_req->input;
snappy::Compress(input->data(), input->length(), &dst);
snappy_req->result = dst;
- return 0;
+ RETURN_ASYNC;
}
// UncompressBinding
// PUBLIC
v8::Handle<v8::Value> UncompressBinding::Async(const v8::Arguments& args) {
v8::HandleScope scope;
SnappyRequest<std::string>* snappy_req = new SnappyRequest<std::string>(args);
- eio_custom(AsyncOperation, EIO_PRI_DEFAULT, After, snappy_req);
- ev_ref(EV_DEFAULT_UC);
+ BEGIN_ASYNC(snappy_req, AsyncOperation, After);
return v8::Undefined();
}
@@ -142,7 +140,7 @@ v8::Handle<v8::Value> UncompressBinding::Sync(const v8::Arguments& args) {
}
// PRIVATE
-int UncompressBinding::AsyncOperation(eio_req *req) {
+async_rtn UncompressBinding::AsyncOperation(eio_req *req) {
SnappyRequest<std::string>* snappy_req =
static_cast<SnappyRequest<std::string>*>(req->data);
std::string dst;
@@ -152,7 +150,7 @@ int UncompressBinding::AsyncOperation(eio_req *req) {
} else {
snappy_req->err = &SnappyErrors::kInvalidInput;
}
- return 0;
+ RETURN_ASYNC;
}
// IsValidCompressedBinding
@@ -163,8 +161,7 @@ IsValidCompressedBinding::Async(const v8::Arguments& args) {
std::string dst;
v8::String::Utf8Value data(args[0]->ToString());
SnappyRequest<bool>* snappy_req = new SnappyRequest<bool>(args);
- eio_custom(AsyncOperation, EIO_PRI_DEFAULT, After, snappy_req);
- ev_ref(EV_DEFAULT_UC);
+ BEGIN_ASYNC(snappy_req, AsyncOperation, After);
return v8::Undefined();
}
@@ -181,15 +178,15 @@ IsValidCompressedBinding::Sync(const v8::Arguments& args) {
}
// PRIVATE
-int IsValidCompressedBinding::After(eio_req *req) {
+async_rtn IsValidCompressedBinding::After(eio_req *req) {
v8::HandleScope scope;
SnappyRequest<bool>* snappy_req =
static_cast<SnappyRequest<bool>*>(req->data);
CallOkCallback(snappy_req->callback, snappy_req->result);
ev_unref(EV_DEFAULT_UC);
snappy_req->callback.Dispose();
delete snappy_req;
- return 0;
+ RETURN_ASYNC_AFTER;
}
int IsValidCompressedBinding::AsyncOperation(eio_req *req) {
View
@@ -23,6 +23,7 @@
#include <node.h>
#include <v8.h>
#include <string>
+#include "./node_async_shim.h"
namespace nodesnappy {
@@ -70,7 +71,7 @@ class Base {
class CompressUncompressBase : protected Base {
protected:
/* Method run after the async operation */
- static int After(eio_req*);
+ static async_rtn After(eio_req*);
/*
* Call the specifed callback when everything has gone well.
* Use null as first argument and use the specifed string (converted to a
@@ -93,7 +94,7 @@ class CompressBinding : CompressUncompressBase {
private:
- static int AsyncOperation(eio_req*);
+ static async_rtn AsyncOperation(eio_req*);
};
/*
@@ -108,7 +109,7 @@ class UncompressBinding : CompressUncompressBase {
static v8::Handle<v8::Value> Sync(const v8::Arguments&);
private:
- static int AsyncOperation(eio_req*);
+ static async_rtn AsyncOperation(eio_req*);
};
/*
@@ -123,8 +124,8 @@ class IsValidCompressedBinding : protected Base {
static v8::Handle<v8::Value> Sync(const v8::Arguments&);
private:
- static int After(eio_req*);
- static int AsyncOperation(eio_req*);
+ static async_rtn After(eio_req*);
+ static async_rtn AsyncOperation(eio_req*);
/*
* Call the specifed callback when everything has gone well.
* Use null as first argument and use the specifed bool (converted to a
View
@@ -0,0 +1,42 @@
+/**
+ * eio_custom() vs. uv_queue_work() file.
+ * Original gist: https://gist.github.com/1368935
+ *
+ * Copyright (c) 2011-2012, Nathan Rajlich <nathan@tootallnate.net>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <node.h>
+#include <node_version.h>
+
+/* Node Thread Pool version compat */
+#if NODE_VERSION_AT_LEAST(0, 5, 6)
+ #define BEGIN_ASYNC(_data, async, after) \
+ uv_work_t *_req = new uv_work_t; \
+ _req->data = _data; \
+ uv_queue_work(uv_default_loop(), _req, async, after);
+ typedef void async_rtn;
+ #define RETURN_ASYNC
+ #define RETURN_ASYNC_AFTER delete req;
+#else
+ #define BEGIN_ASYNC(data, async, after) \
+ ev_ref(EV_DEFAULT_UC); \
+ eio_custom(async, EIO_PRI_DEFAULT, after, data);
+ typedef int async_rtn;
+ typedef eio_req uv_work_t;
+ #define RETURN_ASYNC return 0;
+ #define RETURN_ASYNC_AFTER \
+ ev_unref(EV_DEFAULT_UC); \
+ RETURN_ASYNC;
+#endif

0 comments on commit bd15b6a

Please sign in to comment.