[Pkg-javascript-commits] [node-leveldown] 374/492: upgrade to nan at 0.2.0
Andrew Kelley
andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:14:19 UTC 2014
This is an automated email from the git hooks/post-receive script.
andrewrk-guest pushed a commit to annotated tag rocksdb-0.10.1
in repository node-leveldown.
commit 7e38c363971cf398ecfa7c549b7a7bd920c7efe9
Author: Rod Vagg <rod at vagg.org>
Date: Mon Aug 5 09:53:44 2013 +1000
upgrade to nan at 0.2.0
---
src/iterator.cc | 4 +-
src/nan.h | 482 +++++++++++++++++++++++++++++++++++++++++++++++++-------
2 files changed, 431 insertions(+), 55 deletions(-)
diff --git a/src/iterator.cc b/src/iterator.cc
index d6b4553..545432f 100644
--- a/src/iterator.cc
+++ b/src/iterator.cc
@@ -43,7 +43,7 @@ Iterator::Iterator (
v8::Local<v8::Object> obj = v8::Object::New();
if (!startHandle.IsEmpty())
obj->Set(NanSymbol("start"), startHandle);
- NanAssignPersistent(v8::Object, persistentHandle, obj)
+ NanAssignPersistent(v8::Object, persistentHandle, obj);
options = new leveldb::ReadOptions();
options->fill_cache = fillCache;
@@ -197,7 +197,7 @@ NAN_METHOD(Iterator::End) {
void Iterator::Init () {
v8::Local<v8::FunctionTemplate> tpl =
v8::FunctionTemplate::New(Iterator::New);
- NanAssignPersistent(v8::FunctionTemplate, iterator_constructor, tpl)
+ NanAssignPersistent(v8::FunctionTemplate, iterator_constructor, tpl);
tpl->SetClassName(NanSymbol("Iterator"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(tpl, "next", Iterator::Next);
diff --git a/src/nan.h b/src/nan.h
index ead7d65..5ea2380 100644
--- a/src/nan.h
+++ b/src/nan.h
@@ -1,20 +1,45 @@
/**********************************************************************************
* NAN - Native Abstractions for Node.js
*
- * Copyright (c) 2013 Rod Vagg
+ * Copyright (c) 2013 NAN contributors:
+ * - Rod Vagg <https://github.com/rvagg>
+ * - Benjamin Byholm <https://github.com/kkoopa>
+ * - Trevor Norris <https://github.com/trevnorris>
+ *
* MIT +no-false-attribs License <https://github.com/rvagg/nan/blob/master/LICENSE>
*
- * Version 0.1.0 (current Node unstable: 0.11.4)
+ * Version 0.2.0 (current Node unstable: 0.11.4)
+ *
+ * ChangeLog:
+ * * 0.2.0 Aug 5 2013
+ * - Added NAN_PROPERTY_GETTER, NAN_PROPERTY_SETTER, NAN_PROPERTY_ENUMERATOR,
+ * NAN_PROPERTY_DELETER, NAN_PROPERTY_QUERY
+ * - Extracted _NAN_METHOD_ARGS, _NAN_GETTER_ARGS, _NAN_SETTER_ARGS,
+ * _NAN_PROPERTY_GETTER_ARGS, _NAN_PROPERTY_SETTER_ARGS,
+ * _NAN_PROPERTY_ENUMERATOR_ARGS, _NAN_PROPERTY_DELETER_ARGS,
+ * _NAN_PROPERTY_QUERY_ARGS
+ * - Added NanGetInternalFieldPointer, NanSetInternalFieldPointer
+ * - Added NAN_WEAK_CALLBACK, NAN_WEAK_CALLBACK_OBJECT,
+ * NAN_WEAK_CALLBACK_DATA, NanMakeWeak
+ * - Renamed THROW_ERROR to _NAN_THROW_ERROR
+ * - Added NanNewBufferHandle(char*, size_t, node::smalloc::FreeCallback, void*)
+ * - Added NanBufferUse(char*, uint32_t)
+ * - Added NanNewContextHandle(v8::ExtensionConfiguration*,
+ * v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::Value>)
+ * - Fixed broken NanCallback#GetFunction()
+ * - Added optional encoding and size arguments to NanFromV8String()
+ * - Added NanGetPointerSafe() and NanSetPointerSafe()
+ * - Added initial test suite (to be expanded)
+ * - Allow NanUInt32OptionValue to convert any Number object
*
- * Changelog:
* * 0.1.0 Jul 21 2013
- * - Added `NAN_GETTER`, `NAN_SETTER`
- * - Added `NanThrowError` with single Local<Value> argument
- * - Added `NanNewBufferHandle` with single uint32_t argument
- * - Added `NanHasInstance(Persistent<FunctionTemplate>&, Handle<Value>)`
- * - Added `Local<Function> NanCallback#GetFunction()`
- * - Added `NanCallback#Call(int, Local<Value>[])`
- * - Deprecated `NanCallback#Run(int, Local<Value>[])` in favour of Call
+ * - Added `NAN_GETTER`, `NAN_SETTER`
+ * - Added `NanThrowError` with single Local<Value> argument
+ * - Added `NanNewBufferHandle` with single uint32_t argument
+ * - Added `NanHasInstance(Persistent<FunctionTemplate>&, Handle<Value>)`
+ * - Added `Local<Function> NanCallback#GetFunction()`
+ * - Added `NanCallback#Call(int, Local<Value>[])`
+ * - Deprecated `NanCallback#Run(int, Local<Value>[])` in favour of Call
*
* See https://github.com/rvagg/nan for the latest update to this file
**********************************************************************************/
@@ -24,21 +49,29 @@
#include <node.h>
#include <node_buffer.h>
+#include <string.h>
// some generic helpers
-#define NanSymbol(value) v8::String::NewSymbol(value)
+template<class T> static inline bool NanSetPointerSafe(T *var, T val) {
+ if (var) {
+ *var = val;
+ return true;
+ } else {
+ return false;
+ }
+}
-static inline char* NanFromV8String(v8::Local<v8::Value> from) {
- size_t sz_;
- char* to;
- v8::Local<v8::String> toStr = from->ToString();
- sz_ = toStr->Utf8Length();
- to = new char[sz_ + 1];
- toStr->WriteUtf8(to, -1, NULL, v8::String::NO_OPTIONS);
- return to;
+template<class T> static inline T NanGetPointerSafe(T *var, T fallback = reinterpret_cast<T>(0)) {
+ if (var) {
+ return *var;
+ } else {
+ return fallback;
+ }
}
+#define NanSymbol(value) v8::String::NewSymbol(value)
+
static inline bool NanBooleanOptionValue(
v8::Local<v8::Object> optionsObj
, v8::Handle<v8::String> opt, bool def) {
@@ -67,7 +100,7 @@ static inline uint32_t NanUInt32OptionValue(
return !optionsObj.IsEmpty()
&& optionsObj->Has(opt)
- && optionsObj->Get(opt)->IsUint32()
+ && optionsObj->Get(opt)->IsNumber()
? optionsObj->Get(opt)->Uint32Value()
: def;
}
@@ -77,32 +110,71 @@ static inline uint32_t NanUInt32OptionValue(
static v8::Isolate* nan_isolate = v8::Isolate::GetCurrent();
-# define NAN_METHOD(name) \
- void name(const v8::FunctionCallbackInfo<v8::Value>& args)
+# define _NAN_METHOD_ARGS const v8::FunctionCallbackInfo<v8::Value>& args
+# define NAN_METHOD(name) void name(_NAN_METHOD_ARGS)
+# define _NAN_GETTER_ARGS const v8::PropertyCallbackInfo<v8::Value>& args
# define NAN_GETTER(name) \
- void name( \
- v8::Local<v8::String> property \
- , const v8::PropertyCallbackInfo<v8::Value>& args)
+ void name(v8::Local<v8::String> property, _NAN_GETTER_ARGS)
+# define _NAN_SETTER_ARGS const v8::PropertyCallbackInfo<void>& args
# define NAN_SETTER(name) \
void name( \
v8::Local<v8::String> property \
, v8::Local<v8::Value> value \
- , const v8::PropertyCallbackInfo<void>& args)
+ , _NAN_SETTER_ARGS)
+# define _NAN_PROPERTY_GETTER_ARGS \
+ const v8::PropertyCallbackInfo<v8::Value>& args
+# define NAN_PROPERTY_GETTER(name) \
+ void name(v8::Local<v8::String> property \
+ , _NAN_PROPERTY_GETTER_ARGS)
+# define _NAN_PROPERTY_SETTER_ARGS \
+ const v8::PropertyCallbackInfo<v8::Value>& args
+# define NAN_PROPERTY_SETTER(name) \
+ void name(v8::Local<v8::String> property \
+ , v8::Local<v8::Value> value \
+ , _NAN_PROPERTY_SETTER_ARGS)
+# define _NAN_PROPERTY_ENUMERATOR_ARGS \
+ const v8::PropertyCallbackInfo<v8::Array>& args
+# define NAN_PROPERTY_ENUMERATOR(name) \
+ void name(_NAN_PROPERTY_ENUMERATOR_ARGS)
+# define _NAN_PROPERTY_DELETER_ARGS \
+ const v8::PropertyCallbackInfo<v8::Boolean>& args
+# define NAN_PROPERTY_DELETER(name) \
+ void name( \
+ v8::Local<v8::String> property \
+ , _NAN_PROPERTY_DELETER_ARGS)
+# define _NAN_PROPERTY_QUERY_ARGS \
+ const v8::PropertyCallbackInfo<v8::Integer>& args
+# define NAN_PROPERTY_QUERY(name) \
+ void name(v8::Local<v8::String> property, _NAN_PROPERTY_QUERY_ARGS)
+# define NanGetInternalFieldPointer(object, index) \
+ object->GetAlignedPointerFromInternalField(index)
+# define NanSetInternalFieldPointer(object, index, value) \
+ object->SetAlignedPointerInInternalField(index, value)
+
+# define NAN_WEAK_CALLBACK(type, name) \
+ void name( \
+ v8::Isolate* isolate, \
+ v8::Persistent<v8::Object>* object, \
+ type data)
+# define NAN_WEAK_CALLBACK_OBJECT (*object)
+# define NAN_WEAK_CALLBACK_DATA(type) ((type) data)
# define NanScope() v8::HandleScope scope(nan_isolate)
-# define NanReturnValue(value) return args.GetReturnValue().Set(value);
-# define NanReturnUndefined() return;
-# define NanAssignPersistent(type, handle, obj) handle.Reset(nan_isolate, obj);
+# define NanReturnValue(value) return args.GetReturnValue().Set(value)
+# define NanReturnUndefined() return
+# define NanAssignPersistent(type, handle, obj) handle.Reset(nan_isolate, obj)
# define NanObjectWrapHandle(obj) obj->handle()
+# define NanMakeWeak(handle, parameter, callback) \
+ handle.MakeWeak(nan_isolate, parameter, callback)
-# define THROW_ERROR(fun, errmsg) \
+# define _NAN_THROW_ERROR(fun, errmsg) \
do { \
NanScope(); \
v8::ThrowException(fun(v8::String::New(errmsg))); \
} while (0);
inline static void NanThrowError(const char* errmsg) {
- THROW_ERROR(v8::Exception::Error, errmsg);
+ _NAN_THROW_ERROR(v8::Exception::Error, errmsg);
}
inline static void NanThrowError(v8::Local<v8::Value> error) {
@@ -111,18 +183,26 @@ static v8::Isolate* nan_isolate = v8::Isolate::GetCurrent();
}
inline static void NanThrowTypeError(const char* errmsg) {
- THROW_ERROR(v8::Exception::TypeError, errmsg);
+ _NAN_THROW_ERROR(v8::Exception::TypeError, errmsg);
}
inline static void NanThrowRangeError(const char* errmsg) {
- THROW_ERROR(v8::Exception::RangeError, errmsg);
+ _NAN_THROW_ERROR(v8::Exception::RangeError, errmsg);
}
- static inline void NanDispose(v8::Persistent<v8::Object> &handle) {
+ template<class T> static inline void NanDispose(v8::Persistent<T> &handle) {
handle.Dispose(nan_isolate);
}
static inline v8::Local<v8::Object> NanNewBufferHandle (
+ char *data,
+ size_t length,
+ node::smalloc::FreeCallback callback,
+ void *hint) {
+ return node::Buffer::New(data, length, callback, hint);
+ }
+
+ static inline v8::Local<v8::Object> NanNewBufferHandle (
char *data, uint32_t size) {
return node::Buffer::New(data, size);
}
@@ -131,6 +211,10 @@ static v8::Isolate* nan_isolate = v8::Isolate::GetCurrent();
return node::Buffer::New(size);
}
+ static inline v8::Local<v8::Object> NanBufferUse(char* data, uint32_t size) {
+ return node::Buffer::Use(data, size);
+ }
+
template <class TypeName>
inline v8::Local<TypeName> NanPersistentToLocal(
const v8::Persistent<TypeName>& persistent) {
@@ -148,36 +232,78 @@ static v8::Isolate* nan_isolate = v8::Isolate::GetCurrent();
return NanPersistentToLocal(function_template)->HasInstance(value);
}
+ static inline v8::Local<v8::Context> NanNewContextHandle(
+ v8::ExtensionConfiguration* extensions = NULL,
+ v8::Handle<v8::ObjectTemplate> g_template = v8::Handle<v8::ObjectTemplate>(),
+ v8::Handle<v8::Value> g_object = v8::Handle<v8::Value>()) {
+ return v8::Local<v8::Context>::New(nan_isolate, v8::Context::New(
+ nan_isolate, extensions, g_template, g_object));
+ }
+
#else
// Node 0.8 and 0.10
-# define NAN_METHOD(name) \
- v8::Handle<v8::Value> name(const v8::Arguments& args)
+# define _NAN_METHOD_ARGS const v8::Arguments& args
+# define NAN_METHOD(name) v8::Handle<v8::Value> name(_NAN_METHOD_ARGS)
+# define _NAN_GETTER_ARGS const v8::AccessorInfo &args
# define NAN_GETTER(name) \
- v8::Handle<v8::Value> name( \
- v8::Local<v8::String> property \
- , const v8::AccessorInfo &args)
+ v8::Handle<v8::Value> name(v8::Local<v8::String> property, _NAN_GETTER_ARGS)
+# define _NAN_SETTER_ARGS const v8::AccessorInfo &args
# define NAN_SETTER(name) \
void name( \
- v8::Local<v8::String> property \
- , v8::Local<v8::Value> value \
- , const v8::AccessorInfo &args)
+ v8::Local<v8::String> property \
+ , v8::Local<v8::Value> value \
+ , _NAN_SETTER_ARGS)
+# define _NAN_PROPERTY_GETTER_ARGS const v8::AccessorInfo& args
+# define NAN_PROPERTY_GETTER(name) \
+ v8::Handle<v8::Value> name(v8::Local<v8::String> property \
+ , _NAN_PROPERTY_GETTER_ARGS)
+# define _NAN_PROPERTY_SETTER_ARGS const v8::AccessorInfo& args
+# define NAN_PROPERTY_SETTER(name) \
+ v8::Handle<v8::Value> name(v8::Local<v8::String> property \
+ , v8::Local<v8::Value> value \
+ , _NAN_PROPERTY_SETTER_ARGS)
+# define _NAN_PROPERTY_ENUMERATOR_ARGS const v8::AccessorInfo& args
+# define NAN_PROPERTY_ENUMERATOR(name) \
+ v8::Handle<v8::Array> name(_NAN_PROPERTY_ENUMERATOR_ARGS)
+# define _NAN_PROPERTY_DELETER_ARGS const v8::AccessorInfo& args
+# define NAN_PROPERTY_DELETER(name) \
+ v8::Handle<v8::Boolean> name( \
+ v8::Local<v8::String> property \
+ , _NAN_PROPERTY_DELETER_ARGS)
+# define _NAN_PROPERTY_QUERY_ARGS const v8::AccessorInfo& args
+# define NAN_PROPERTY_QUERY(name) \
+ v8::Handle<v8::Integer> name( \
+ v8::Local<v8::String> property \
+ , _NAN_PROPERTY_QUERY_ARGS)
+
+# define NanGetInternalFieldPointer(object, index) \
+ object->GetPointerFromInternalField(index)
+# define NanSetInternalFieldPointer(object, index, value) \
+ object->SetPointerInInternalField(index, value)
+# define NAN_WEAK_CALLBACK(type, name) void name( \
+ v8::Persistent<v8::Value> object, \
+ void *data)
+# define NAN_WEAK_CALLBACK_OBJECT object
+# define NAN_WEAK_CALLBACK_DATA(type) ((type) data)
# define NanScope() v8::HandleScope scope
-# define NanReturnValue(value) return scope.Close(value);
-# define NanReturnUndefined() return v8::Undefined();
+# define NanReturnValue(value) return scope.Close(value)
+# define NanReturnUndefined() return v8::Undefined()
# define NanAssignPersistent(type, handle, obj) \
- handle = v8::Persistent<type>::New(obj);
+ handle = v8::Persistent<type>::New(obj)
# define NanObjectWrapHandle(obj) obj->handle_
+# define NanMakeWeak(handle, parameters, callback) \
+ handle.MakeWeak(parameters, callback)
-# define THROW_ERROR(fun, errmsg) \
+# define _NAN_THROW_ERROR(fun, errmsg) \
do { \
NanScope(); \
return v8::ThrowException(fun(v8::String::New(errmsg))); \
} while (0);
inline static v8::Handle<v8::Value> NanThrowError(const char* errmsg) {
- THROW_ERROR(v8::Exception::Error, errmsg);
+ _NAN_THROW_ERROR(v8::Exception::Error, errmsg);
}
inline static v8::Handle<v8::Value> NanThrowError(
@@ -187,22 +313,44 @@ static v8::Isolate* nan_isolate = v8::Isolate::GetCurrent();
}
inline static v8::Handle<v8::Value> NanThrowTypeError(const char* errmsg) {
- THROW_ERROR(v8::Exception::TypeError, errmsg);
+ _NAN_THROW_ERROR(v8::Exception::TypeError, errmsg);
}
inline static v8::Handle<v8::Value> NanThrowRangeError(const char* errmsg) {
- THROW_ERROR(v8::Exception::RangeError, errmsg);
+ _NAN_THROW_ERROR(v8::Exception::RangeError, errmsg);
}
- static inline void NanDispose(v8::Persistent<v8::Object> &handle) {
+ template<class T> static inline void NanDispose(v8::Persistent<T> &handle) {
handle.Dispose();
}
static inline v8::Local<v8::Object> NanNewBufferHandle (
+ char *data,
+ size_t length,
+ node::Buffer::free_callback callback,
+ void *hint) {
+ return v8::Local<v8::Object>::New(
+ node::Buffer::New(data, length, callback, hint)->handle_);
+ }
+
+ static inline v8::Local<v8::Object> NanNewBufferHandle (
char *data, uint32_t size) {
return v8::Local<v8::Object>::New(node::Buffer::New(data, size)->handle_);
}
+ static inline v8::Local<v8::Object> NanNewBufferHandle (uint32_t size) {
+ return v8::Local<v8::Object>::New(node::Buffer::New(size)->handle_);
+ }
+
+ static inline void FreeData(char *data, void *hint) {
+ delete[] data;
+ }
+
+ static inline v8::Local<v8::Object> NanBufferUse(char* data, uint32_t size) {
+ return v8::Local<v8::Object>::New(
+ node::Buffer::New(data, size, FreeData, NULL)->handle_);
+ }
+
template <class TypeName>
inline v8::Local<TypeName> NanPersistentToLocal(
const v8::Persistent<TypeName>& persistent) {
@@ -220,6 +368,19 @@ static v8::Isolate* nan_isolate = v8::Isolate::GetCurrent();
return function_template->HasInstance(value);
}
+ static inline v8::Local<v8::Context> NanNewContextHandle(
+ v8::ExtensionConfiguration* extensions = NULL
+ , v8::Handle<v8::ObjectTemplate> g_template =
+ v8::Handle<v8::ObjectTemplate>()
+ , v8::Handle<v8::Value> g_object = v8::Handle<v8::Value>()
+ ) {
+ v8::Persistent<v8::Context> ctx =
+ v8::Context::New(extensions, g_template, g_object);
+ v8::Local<v8::Context> lctx = v8::Local<v8::Context>::New(ctx);
+ ctx.Dispose();
+ return lctx;
+ }
+
#endif // node version
class NanCallback {
@@ -236,9 +397,9 @@ class NanCallback {
handle.Dispose();
}
- v8::Local<v8::Function> GetFunction () {
- NanScope();
- return NanPersistentToLocal(handle).As<v8::Function>();
+ inline v8::Local<v8::Function> GetFunction () {
+ return NanPersistentToLocal(handle)->Get(NanSymbol("callback"))
+ .As<v8::Function>();
}
// deprecated
@@ -341,4 +502,219 @@ inline void NanAsyncQueueWorker (NanAsyncWorker* worker) {
);
}
+//// Base 64 ////
+
+#define _nan_base64_encoded_size(size) ((size + 2 - ((size + 2) % 3)) / 3 * 4)
+
+
+// Doesn't check for padding at the end. Can be 1-2 bytes over.
+static inline size_t _nan_base64_decoded_size_fast(size_t size) {
+ size_t remainder = size % 4;
+
+ size = (size / 4) * 3;
+ if (remainder) {
+ if (size == 0 && remainder == 1) {
+ // special case: 1-byte input cannot be decoded
+ size = 0;
+ } else {
+ // non-padded input, add 1 or 2 extra bytes
+ size += 1 + (remainder == 3);
+ }
+ }
+
+ return size;
+}
+
+template <typename TypeName>
+static size_t _nan_base64_decoded_size(const TypeName* src, size_t size) {
+ if (size == 0)
+ return 0;
+
+ if (src[size - 1] == '=')
+ size--;
+ if (size > 0 && src[size - 1] == '=')
+ size--;
+
+ return _nan_base64_decoded_size_fast(size);
+}
+
+
+// supports regular and URL-safe base64
+static const int _nan_unbase64_table[] =
+ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -1, -1, -2, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63,
+ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
+ -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,
+ -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
+ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+ };
+
+#define _nan_unbase64(x) _nan_unbase64_table[(uint8_t)(x)]
+
+
+template <typename TypeName>
+static size_t _nan_base64_decode(char* buf,
+ size_t len,
+ const TypeName* src,
+ const size_t srcLen) {
+ char a, b, c, d;
+ char* dst = buf;
+ char* dstEnd = buf + len;
+ const TypeName* srcEnd = src + srcLen;
+
+ while (src < srcEnd && dst < dstEnd) {
+ int remaining = srcEnd - src;
+
+ while (_nan_unbase64(*src) < 0 && src < srcEnd) src++, remaining--;
+ if (remaining == 0 || *src == '=') break;
+ a = _nan_unbase64(*src++);
+
+ while (_nan_unbase64(*src) < 0 && src < srcEnd) src++, remaining--;
+ if (remaining <= 1 || *src == '=') break;
+ b = _nan_unbase64(*src++);
+
+ *dst++ = (a << 2) | ((b & 0x30) >> 4);
+ if (dst == dstEnd) break;
+
+ while (_nan_unbase64(*src) < 0 && src < srcEnd) src++, remaining--;
+ if (remaining <= 2 || *src == '=') break;
+ c = _nan_unbase64(*src++);
+
+ *dst++ = ((b & 0x0F) << 4) | ((c & 0x3C) >> 2);
+ if (dst == dstEnd) break;
+
+ while (_nan_unbase64(*src) < 0 && src < srcEnd) src++, remaining--;
+ if (remaining <= 3 || *src == '=') break;
+ d = _nan_unbase64(*src++);
+
+ *dst++ = ((c & 0x03) << 6) | (d & 0x3F);
+ }
+
+ return dst - buf;
+}
+
+//// HEX ////
+
+template <typename TypeName>
+unsigned _nan_hex2bin(TypeName c) {
+ if (c >= '0' && c <= '9') return c - '0';
+ if (c >= 'A' && c <= 'F') return 10 + (c - 'A');
+ if (c >= 'a' && c <= 'f') return 10 + (c - 'a');
+ return static_cast<unsigned>(-1);
+}
+
+
+template <typename TypeName>
+static size_t _nan_hex_decode(char* buf,
+ size_t len,
+ const TypeName* src,
+ const size_t srcLen) {
+ size_t i;
+ for (i = 0; i < len && i * 2 + 1 < srcLen; ++i) {
+ unsigned a = _nan_hex2bin(src[i * 2 + 0]);
+ unsigned b = _nan_hex2bin(src[i * 2 + 1]);
+ if (!~a || !~b) return i;
+ buf[i] = a * 16 + b;
+ }
+
+ return i;
+}
+
+static bool _NanGetExternalParts(v8::Handle<v8::Value> val, const char** data, size_t* len) {
+ if (node::Buffer::HasInstance(val)) {
+ *data = node::Buffer::Data(val);
+ *len = node::Buffer::Length(val);
+ return true;
+
+ }
+
+ assert(val->IsString());
+ v8::Local<v8::String> str = v8::Local<v8::String>::New(val.As<v8::String>());
+
+ if (str->IsExternalAscii()) {
+ const v8::String::ExternalAsciiStringResource* ext;
+ ext = str->GetExternalAsciiStringResource();
+ *data = ext->data();
+ *len = ext->length();
+ return true;
+
+ } else if (str->IsExternal()) {
+ const v8::String::ExternalStringResource* ext;
+ ext = str->GetExternalStringResource();
+ *data = reinterpret_cast<const char*>(ext->data());
+ *len = ext->length();
+ return true;
+ }
+
+ return false;
+}
+
+static inline char* NanFromV8String(v8::Local<v8::Value> from, enum node::encoding encoding = node::UTF8, size_t *datalen = NULL) {
+ NanScope();
+
+ size_t sz_;
+ char *data = NULL;
+ size_t len;
+ bool is_extern = _NanGetExternalParts(from, const_cast<const char**>(&data), &len);
+
+ if (is_extern) {
+ NanSetPointerSafe(datalen, len);
+ return data;
+ }
+
+ assert(from->IsString());
+ v8::Local<v8::String> toStr = from.As<v8::String>();
+
+ int flags = v8::String::NO_NULL_TERMINATION | v8::String::HINT_MANY_WRITES_EXPECTED;
+
+ char *to;
+ v8::String::AsciiValue value(toStr);
+ switch(encoding) {
+ case node::ASCII:
+ case node::BINARY:
+ case node::BUFFER:
+ sz_ = toStr->Length();
+ to = new char[sz_];
+ #if (NODE_MODULE_VERSION > 0x000B)
+ NanSetPointerSafe<size_t>(datalen, toStr->WriteOneByte(reinterpret_cast<uint8_t *>(to), 0, sz_, flags));
+ #else
+ assert(0 && "not implemented");
+ #endif
+ return to;
+ case node::UTF8:
+ sz_ = toStr->Utf8Length();
+ to = new char[sz_];
+ NanSetPointerSafe<size_t>(datalen, toStr->WriteUtf8(to));
+ return to;
+ case node::BASE64:
+ sz_ = _nan_base64_decoded_size(*value, sz_);
+ to = new char[sz_];
+ NanSetPointerSafe<size_t>(datalen, _nan_base64_decode(to, sz_, *value, value.length()));
+ return to;
+ case node::UCS2:
+ sz_ = toStr->Length();
+ to = new char[sz_ * 2];
+ NanSetPointerSafe<size_t>(datalen, toStr->Write(reinterpret_cast<uint16_t *>(to), 0, sz_, flags));
+ return to;
+ case node::HEX:
+ sz_ = toStr->Length();
+ assert(!(sz_ & 1) && "bad hex data");
+ to = new char[sz_ / 2];
+ NanSetPointerSafe<size_t>(datalen, _nan_hex_decode(to, sz_ / 2, *value, value.length()));
+ return to;
+ default:
+ assert(0 && "unknown encoding");
+ }
+}
+
#endif
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-leveldown.git
More information about the Pkg-javascript-commits
mailing list