Skip to content
Browse files

Cleanup & refactoring.

  • Loading branch information...
1 parent 2781e68 commit 9f56b3328808e29a7e4de395ac29c17d9639d220 @kesla committed Apr 20, 2011
Showing with 67 additions and 72 deletions.
  1. +26 −58 binding.cc
  2. +41 −14 binding.h
View
84 binding.cc
@@ -26,54 +26,36 @@
#include <string.h> // memcpy
namespace nodesnappy {
-// Request used in the async versions, used to store the data
-template<class T> struct SnappyRequest {
- std::string input;
- T result;
- v8::Persistent<v8::Function> callback;
-};
// CompressUncompressBinding
// PROTECTED
int CompressUncompressBase::After(eio_req *req) {
v8::HandleScope scope;
SnappyRequest<std::string>* snappy_request =
static_cast<SnappyRequest<std::string>*>(req->data);
- v8::Local<v8::Value> argv[2];
- argv[0] = v8::Local<v8::Value>::New(v8::Null());
- argv[1] = CreateBuffer(snappy_request->result);
- snappy_request->callback->Call(v8::Context::GetCurrent()->Global(), 2, argv);
+ CallCallback(snappy_request->callback, snappy_request->result);
ev_unref(EV_DEFAULT_UC);
snappy_request->callback.Dispose();
delete snappy_request;
return 0;
}
-// Create a node-buffer, as described in
-// http://sambro.is-super-awesome.com/2011/03/03/creating-a-proper-buffer-in-a-node-c-addon/
-inline v8::Local<v8::Object>
-CompressUncompressBase::CreateBuffer(const std::string& str) {
- size_t length = str.length();
- node::Buffer *slowBuffer = node::Buffer::New(length);
- memcpy(node::Buffer::Data(slowBuffer), str.data(), length);
- v8::Local<v8::Object> obj = v8::Context::GetCurrent()->Global();
- v8::Local<v8::Function> bufferConstructor =
- v8::Local<v8::Function>::Cast(obj->Get(v8::String::New("Buffer")));
- v8::Handle<v8::Value> constructorArgs[3] =
- { slowBuffer->handle_, v8::Integer::New(length), v8::Integer::New(0) };
- return bufferConstructor->NewInstance(3, constructorArgs);
+inline void
+CompressUncompressBase::CallCallback(const v8::Handle<v8::Function>& callback,
+ const std::string& str) {
+ v8::Handle<v8::Object> buffer =
+ node::Buffer::New(v8::String::New(str.data(), str.length()));
+ v8::Handle<v8::Value> argv[2] = {v8::Local<v8::Value>::New(v8::Null()),
+ buffer};
+ callback->Call(v8::Context::GetCurrent()->Global(), 2, argv);
}
// CompressBinding
// PUBLIC
v8::Handle<v8::Value> CompressBinding::Async(const v8::Arguments& args) {
v8::HandleScope scope;
- std::string dst;
- SnappyRequest<std::string>* snappy_request = new SnappyRequest<std::string>();
- v8::String::Utf8Value data(args[0]->ToString());
- v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(args[1]);
- snappy_request->callback = v8::Persistent<v8::Function>::New(callback);
- snappy_request->input = std::string(*data, data.length());
+ SnappyRequest<std::string>* snappy_request =
+ new SnappyRequest<std::string>(args);
eio_custom(AsyncOperation, EIO_PRI_DEFAULT, After, snappy_request);
ev_ref(EV_DEFAULT_UC);
return v8::Undefined();
@@ -84,11 +66,7 @@ v8::Handle<v8::Value> CompressBinding::Sync(const v8::Arguments& args) {
v8::String::Utf8Value data(args[0]->ToString());
std::string dst;
snappy::Compress(*data, data.length(), &dst);
- v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(args[1]);
- v8::Local<v8::Value> argv[2];
- argv[0] = v8::Local<v8::Value>::New(v8::Null());
- argv[1] = CreateBuffer(dst);
- callback->Call(v8::Context::GetCurrent()->Global(), 2, argv);
+ CallCallback(v8::Local<v8::Function>::Cast(args[1]), dst);
return scope.Close(v8::Undefined());
}
@@ -107,12 +85,8 @@ int CompressBinding::AsyncOperation(eio_req *req) {
// PUBLIC
v8::Handle<v8::Value> UncompressBinding::Async(const v8::Arguments& args) {
v8::HandleScope scope;
- std::string dst;
- v8::String::Utf8Value data(args[0]->ToString());
- SnappyRequest<std::string>* snappy_request = new SnappyRequest<std::string>();
- v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(args[1]);
- snappy_request->callback = v8::Persistent<v8::Function>::New(callback);
- snappy_request->input = std::string(*data, data.length());
+ SnappyRequest<std::string>* snappy_request =
+ new SnappyRequest<std::string>(args);
eio_custom(AsyncOperation, EIO_PRI_DEFAULT, After, snappy_request);
ev_ref(EV_DEFAULT_UC);
return v8::Undefined();
@@ -123,11 +97,7 @@ v8::Handle<v8::Value> UncompressBinding::Sync(const v8::Arguments& args) {
std::string dst;
v8::String::Utf8Value data(args[0]->ToString());
snappy::Uncompress(*data, data.length(), &dst);
- v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(args[1]);
- v8::Local<v8::Value> argv[2];
- argv[0] = v8::Local<v8::Value>::New(v8::Null());
- argv[1] = CreateBuffer(dst);
- callback->Call(v8::Context::GetCurrent()->Global(), 2, argv);
+ CallCallback(v8::Local<v8::Function>::Cast(args[1]), dst);
return scope.Close(v8::Undefined());
}
@@ -149,10 +119,7 @@ IsValidCompressedBinding::Async(const v8::Arguments& args) {
v8::HandleScope scope;
std::string dst;
v8::String::Utf8Value data(args[0]->ToString());
- SnappyRequest<bool>* snappy_request = new SnappyRequest<bool>();
- v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(args[1]);
- snappy_request->callback = v8::Persistent<v8::Function>::New(callback);
- snappy_request->input = std::string(*data, data.length());
+ SnappyRequest<bool>* snappy_request = new SnappyRequest<bool>(args);
eio_custom(AsyncOperation, EIO_PRI_DEFAULT, After, snappy_request);
ev_ref(EV_DEFAULT_UC);
return v8::Undefined();
@@ -164,11 +131,7 @@ IsValidCompressedBinding::Sync(const v8::Arguments& args) {
std::string dst;
v8::String::Utf8Value data(args[0]->ToString());
bool valid = snappy::IsValidCompressedBuffer(*data, data.length());
- v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(args[1]);
- v8::Local<v8::Value> argv[2];
- argv[0] = v8::Local<v8::Value>::New(v8::Null());
- argv[1] = v8::Local<v8::Value>::New(v8::Boolean::New(valid));
- callback->Call(v8::Context::GetCurrent()->Global(), 2, argv);
+ CallCallback(v8::Local<v8::Function>::Cast(args[1]), valid);
return scope.Close(v8::Undefined());
}
@@ -177,10 +140,7 @@ int IsValidCompressedBinding::After(eio_req *req) {
v8::HandleScope scope;
SnappyRequest<bool>* snappy_request =
static_cast<SnappyRequest<bool>*>(req->data);
- v8::Local<v8::Value> argv[2];
- argv[0] = v8::Local<v8::Value>::New(v8::Null());
- argv[1] = v8::Local<v8::Value>::New(v8::Boolean::New(snappy_request->result));
- snappy_request->callback->Call(v8::Context::GetCurrent()->Global(), 2, argv);
+ CallCallback(snappy_request->callback, snappy_request->result);
ev_unref(EV_DEFAULT_UC);
snappy_request->callback.Dispose();
delete snappy_request;
@@ -195,6 +155,14 @@ int IsValidCompressedBinding::AsyncOperation(eio_req *req) {
return 0;
}
+inline void
+IsValidCompressedBinding::CallCallback(const v8::Handle<v8::Function>& callback,
+ const bool data) {
+ v8::Handle<v8::Value> argv[2] = {v8::Local<v8::Value>::New(v8::Null()),
+ v8::Local<v8::Value>::New(v8::Boolean::New(data))};
+ callback->Call(v8::Context::GetCurrent()->Global(), 2, argv);
+}
+
extern "C" void
init(v8::Handle<v8::Object> target) {
v8::HandleScope scope;
View
55 binding.h
@@ -26,51 +26,78 @@
namespace nodesnappy {
-// Base class for both compress and uncompress including shared methods
+/*
+ * struct used in the async versions, used to store data.
+ */
+template<class T> struct SnappyRequest {
+ std::string input;
+ T result;
+ v8::Persistent<v8::Function> callback;
+ inline SnappyRequest(const v8::Arguments& args) {
+ v8::String::Utf8Value data(args[0]->ToString());
+ input = std::string(*data, data.length());
+ v8::Local<v8::Function> local = v8::Local<v8::Function>::Cast(args[1]);
+ callback = v8::Persistent<v8::Function>::New(local);
+ }
+};
+
+/*
+ * Base class for both compress and uncompress including shared methods
+ */
class CompressUncompressBase {
protected:
static int After(eio_req *req);
- static v8::Local<v8::Object> CreateBuffer(const std::string&);
+ /* Call the specifed callback.
+ * Use null as first argument and use the specifed string (converted to a
+ * Buffer) as second argument.*/
+ static void CallCallback(const v8::Handle<v8::Function>&, const std::string&);
};
-// Bindings to the snappy::Compress-method. Includes both asynchronous
-// and synchronous bindings
+/*
+ * Bindings to the snappy::Compress-method. Includes both asynchronous
+ * and synchronous bindings
+ */
class CompressBinding : CompressUncompressBase {
public:
- // Asynchronous binding
+ /* Asynchronous binding */
static v8::Handle<v8::Value> Async(const v8::Arguments& args);
- // Synchronous binding
+ /* Synchronous binding */
static v8::Handle<v8::Value> Sync(const v8::Arguments& args);
private:
static int AsyncOperation(eio_req *req);
};
-// Bindings to the snappy::Uncompress-method. Includes both asynchronous
-// and synchronous bindings
+/*
+ * Bindings to the snappy::Uncompress-method. Includes both asynchronous
+ * and synchronous bindings
+ */
class UncompressBinding : CompressUncompressBase {
public:
- // Asynchronous binding
+ /* Asynchronous binding */
static v8::Handle<v8::Value> Async(const v8::Arguments& args);
- // Synchronous binding
+ /* Synchronous binding */
static v8::Handle<v8::Value> Sync(const v8::Arguments& args);
private:
static int AsyncOperation(eio_req *req);
};
-// Bindings to the snappy::IsValidCompressedBuffer-method. Includes both
-// asynchronous and synchronous bindings
+/*
+ * Bindings to the snappy::IsValidCompressedBuffer-method. Includes both
+ * asynchronous and synchronous bindings
+ */
class IsValidCompressedBinding {
public:
- // Asynchronous binding
+ /* Asynchronous binding */
static v8::Handle<v8::Value> Async(const v8::Arguments& args);
- // Synchronous binding
+ /* Synchronous binding */
static v8::Handle<v8::Value> Sync(const v8::Arguments& args);
private:
static int After(eio_req *req);
static int AsyncOperation(eio_req *req);
+ static void CallCallback(const v8::Handle<v8::Function>&, const bool);
};
} // namespace nodesnappy

0 comments on commit 9f56b33

Please sign in to comment.
Something went wrong with that request. Please try again.