Skip to content

Commit

Permalink
Add support for node::Encode/DecodeBytes/DecodeWrite. Fixes #250
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoopa committed Jan 19, 2015
1 parent ed4a8fb commit 6caf664
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ NAN_METHOD(CalculateAsync) {
* <a href="#api_nan_set_prototype_template"><b><code>NanSetPrototypeTemplate</code></b></a>
* <a href="#api_nan_set_instance_template"><b><code>NanSetInstanceTemplate</code></b></a>
* <a href="#api_nan_make_callback"><b><code>NanMakeCallback</code></b></a>
* <a href="#api_nan_encode"><b><code>NanEncode</code></b></a>
* <a href="#api_nan_decode_bytes"><b><code>NanDecodeBytes</code></b></a>
* <a href="#api_nan_decode_write"><b><code>NanDecodeWrite</code></b></a>
* <a href="#api_nan_compile_script"><b><code>NanCompileScript</code></b></a>
* <a href="#api_nan_run_script"><b><code>NanRunScript</code></b></a>
* <a href="#api_nan_adjust_external_memory"><b><code>NanAdjustExternalMemory</code></b></a>
Expand Down Expand Up @@ -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.
<a name="api_nan_encode"></a>
### NanEncode(const void*, size_t[, enum Nan::Encoding])
Replaces `node::Encode`.
<a name="api_nan_decode_bytes"></a>
### NanDecodeBytes(v8::Handle&lt;v8::Value&gt;[, enum Nan::Encoding])
Replaces `node::DecodeBytes`.
<a name="api_nan_decode_write"></a>
### NanDecodeWrite(char *, size_t, v8::Handle&lt;v8::Value&gt;[, enum Nan::Encoding])
Replaces `node::DecodeWrite`.
<a name="api_nan_compile_script"></a>
### NanCompileScript(Handle<String> s [, const ScriptOrigin&amp; origin])
Expand Down
63 changes: 62 additions & 1 deletion nan.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ NAN_INLINE v8::Local<T> _NanEnsureLocal(v8::Local<T> 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);
Expand Down Expand Up @@ -1816,6 +1816,67 @@ namespace Nan {
enum Encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER};
}

NAN_INLINE v8::Local<v8::Value> 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<node::encoding>(encoding));
#else
# if (NODE_MODULE_VERSION < 0x000B)
if (encoding == Nan::BUFFER) {
assert(len <= Buffer::kMaxLength);
return Buffer::New(buf, len)->handle_;
}
# endif
return node::Encode(buf, len, static_cast<node::encoding>(encoding));
#endif
}

NAN_INLINE ssize_t NanDecodeBytes(
v8::Handle<v8::Value> val, enum Nan::Encoding encoding = Nan::BINARY) {
#if (NODE_MODULE_VERSION > 0x000B)
return node::DecodeBytes(
v8::Isolate::GetCurrent()
, val
, static_cast<node::encoding>(encoding));
#else
# if (NODE_MODULE_VERSION < 0x000B)
if (encoding == Nan::BUFFER) {
return node::DecodeBytes(val, node::BINARY);
}
# endif
return node::DecodeBytes(val, static_cast<node::encoding>(encoding));
#endif
}

NAN_INLINE ssize_t NanDecodeWrite(
char *buf
, size_t len
, v8::Handle<v8::Value> val
, enum Nan::Encoding encoding = Nan::BINARY) {
#if (NODE_MODULE_VERSION > 0x000B)
return node::DecodeWrite(
v8::Isolate::GetCurrent()
, buf
, len
, val
, static_cast<node::encoding>(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<node::encoding>(encoding));
#endif
}

/* NAN_DEPRECATED */ NAN_INLINE void* _NanRawString(
v8::Handle<v8::Value> from
, enum Nan::Encoding encoding
Expand Down

0 comments on commit 6caf664

Please sign in to comment.