From cfc4a08120ccfbf09abe0034eaf743bee72d2323 Mon Sep 17 00:00:00 2001 From: King Koopa Date: Mon, 19 Jan 2015 14:50:24 +0200 Subject: [PATCH] Add support for node::Encode/DecodeBytes/DecodeWrite. Fixes #250 --- README.md | 18 ++++++++++++++++ nan.h | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e8d22a13..d0a959ee 100644 --- a/README.md +++ b/README.md @@ -293,6 +293,9 @@ NAN_METHOD(CalculateAsync) { * NanSetPrototypeTemplate * NanSetInstanceTemplate * NanMakeCallback + * NanEncode + * NanDecodeBytes + * NanDecodeWrite * NanCompileScript * NanRunScript * NanAdjustExternalMemory @@ -1066,6 +1069,21 @@ Use to add instance properties on function templates. Use instead of `node::MakeCallback` to call javascript functions. This (or `NanCallback`) is the only proper way of calling functions. You must _*never, ever*_ directly use `Function::Call`, it will lead to run-time failures. + +### NanEncode(const void*, size_t[, enum Nan::Encoding]) + +Replaces `node::Encode`. + + +### NanDecodeBytes(v8::Handle<v8::Value>[, enum Nan::Encoding]) + +Replaces `node::DecodeBytes`. + + +### NanDecodeWrite(char *, size_t, v8::Handle<v8::Value>[, enum Nan::Encoding]) + +Replaces `node::DecodeWrite`. + ### NanCompileScript(Handle s [, const ScriptOrigin& origin]) diff --git a/nan.h b/nan.h index 6c11b47a..3d08be87 100644 --- a/nan.h +++ b/nan.h @@ -155,7 +155,7 @@ NAN_INLINE v8::Local _NanEnsureLocal(v8::Local val) { return val; } -#if NODE_MODULE_VERSION >= 42 // io.js 1.0 +#if NODE_MODULE_VERSION >= 42 // io.js 1.0 NAN_INLINE void NanSetCounterFunction(v8::CounterLookupCallback cb) { v8::Isolate::GetCurrent()->SetCounterFunction(cb); @@ -1816,6 +1816,67 @@ namespace Nan { enum Encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER}; } +NAN_INLINE v8::Local NanEncode( + const void *buf, size_t len, enum Nan::Encoding encoding = Nan::BINARY) { +#if (NODE_MODULE_VERSION > 0x000B) + return node::Encode( + v8::Isolate::GetCurrent() + , buf, len + , static_cast(encoding)); +#else +# if (NODE_MODULE_VERSION < 0x000B) + if (encoding == Nan::BUFFER) { + assert(len <= node::Buffer::kMaxLength); + return node::Buffer::New(buf, len)->handle_; + } +# endif + return node::Encode(buf, len, static_cast(encoding)); +#endif +} + +NAN_INLINE ssize_t NanDecodeBytes( + v8::Handle val, enum Nan::Encoding encoding = Nan::BINARY) { +#if (NODE_MODULE_VERSION > 0x000B) + return node::DecodeBytes( + v8::Isolate::GetCurrent() + , val + , static_cast(encoding)); +#else +# if (NODE_MODULE_VERSION < 0x000B) + if (encoding == Nan::BUFFER) { + return node::DecodeBytes(val, node::BINARY); + } +# endif + return node::DecodeBytes(val, static_cast(encoding)); +#endif +} + +NAN_INLINE ssize_t NanDecodeWrite( + char *buf + , size_t len + , v8::Handle val + , enum Nan::Encoding encoding = Nan::BINARY) { +#if (NODE_MODULE_VERSION > 0x000B) + return node::DecodeWrite( + v8::Isolate::GetCurrent() + , buf + , len + , val + , static_cast(encoding)); +#else +# if (NODE_MODULE_VERSION < 0x000B) + if (encoding == Nan::BUFFER) { + return node::DecodeWrite(buf, len, val, node::BINARY); + } +# endif + return node::DecodeWrite( + buf + , len + , val + , static_cast(encoding)); +#endif +} + /* NAN_DEPRECATED */ NAN_INLINE void* _NanRawString( v8::Handle from , enum Nan::Encoding encoding