diff --git a/src/buffer_compat.cc b/src/buffer_compat.cc new file mode 100644 index 0000000..d791e7f --- /dev/null +++ b/src/buffer_compat.cc @@ -0,0 +1,59 @@ + +#include +#include +#include +#include +#include "buffer_compat.h" + + +#if NODE_MINOR_VERSION < 3 + + +char *BufferData(node::Buffer *b) { + return b->data(); +} + + +size_t BufferLength(node::Buffer *b) { + return b->length(); +} + + +char *BufferData(v8::Local buf_obj) { + v8::HandleScope scope; + node::Buffer *buf = node::ObjectWrap::Unwrap(buf_obj); + return buf->data(); +} + + +size_t BufferLength(v8::Local buf_obj) { + v8::HandleScope scope; + node::Buffer *buf = node::ObjectWrap::Unwrap(buf_obj); + return buf->length(); +} + +#else // NODE_VERSION + + +char *BufferData(node::Buffer *b) { + return node::Buffer::Data(b->handle_); +} + + +size_t BufferLength(node::Buffer *b) { + return node::Buffer::Length(b->handle_); +} + + +char *BufferData(v8::Local buf_obj) { + v8::HandleScope scope; + return node::Buffer::Data(buf_obj); +} + + +size_t BufferLength(v8::Local buf_obj) { + v8::HandleScope scope; + return node::Buffer::Length(buf_obj); +} + +#endif // NODE_VERSION diff --git a/src/buffer_compat.h b/src/buffer_compat.h new file mode 100644 index 0000000..d5fab1f --- /dev/null +++ b/src/buffer_compat.h @@ -0,0 +1,16 @@ + +#ifndef buffer_compat_h +#define buffer_compat_h + +#include +#include +#include + +char *BufferData(node::Buffer *b); +size_t BufferLength(node::Buffer *b); + +char *BufferData(v8::Local buf_obj); +size_t BufferLength(v8::Local buf_obj); + + +#endif // buffer_compat_h