From df3a406f7542693b674278912f451e3bae363b5e Mon Sep 17 00:00:00 2001 From: Benjamin Byholm Date: Fri, 8 May 2015 02:18:56 +0300 Subject: [PATCH 01/84] Remove deprecated functions --- nan.h | 481 ---------------------------------------------------------- 1 file changed, 481 deletions(-) diff --git a/nan.h b/nan.h index c059429a..8cc1e335 100644 --- a/nan.h +++ b/nan.h @@ -219,91 +219,6 @@ inline void nauv_key_set(nauv_key_t* key, void* value) { #endif #endif -// some generic helpers - -namespace Nan { namespace imp { -template NAN_INLINE bool NanSetPointerSafe( - T *var - , T val -) { - if (var) { - *var = val; - return true; - } else { - return false; - } -} - -template NAN_INLINE T NanGetPointerSafe( - T *var - , T fallback = reinterpret_cast(0) -) { - if (var) { - return *var; - } else { - return fallback; - } -} - -NAN_INLINE bool NanBooleanOptionValue( - v8::Local optionsObj - , v8::Handle opt, bool def -) { - if (def) { - return optionsObj.IsEmpty() - || !optionsObj->Has(opt) - || optionsObj->Get(opt)->BooleanValue(); - } else { - return !optionsObj.IsEmpty() - && optionsObj->Has(opt) - && optionsObj->Get(opt)->BooleanValue(); - } -} - -} // end of namespace imp -} // end of namespace Nan - -template NAN_DEPRECATED NAN_INLINE bool NanSetPointerSafe( - T *var - , T val -) { - return Nan::imp::NanSetPointerSafe(var, val); -} - -template NAN_DEPRECATED NAN_INLINE T NanGetPointerSafe( - T *var - , T fallback = reinterpret_cast(0) -) { - return Nan::imp::NanGetPointerSafe(var, fallback); -} - - -NAN_DEPRECATED NAN_INLINE bool NanBooleanOptionValue( - v8::Local optionsObj - , v8::Handle opt, bool def -) { - return Nan::imp::NanBooleanOptionValue(optionsObj, opt, def); -} - -NAN_DEPRECATED NAN_INLINE bool NanBooleanOptionValue( - v8::Local optionsObj - , v8::Handle opt -) { - return Nan::imp::NanBooleanOptionValue(optionsObj, opt, false); -} - -NAN_DEPRECATED NAN_INLINE uint32_t NanUInt32OptionValue( - v8::Local optionsObj - , v8::Handle opt - , uint32_t def -) { - return !optionsObj.IsEmpty() - && optionsObj->Has(opt) - && optionsObj->Get(opt)->IsNumber() - ? optionsObj->Get(opt)->Uint32Value() - : def; -} - template v8::Local NanNew(v8::Handle); @@ -583,11 +498,6 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) v8::Isolate::GetCurrent()->GetHeapStatistics(heap_statistics); } - NAN_DEPRECATED NAN_INLINE v8::Local NanSymbol( - const char* data, int length = -1) { - return NanNew(data, length); - } - template NAN_INLINE void NanAssignPersistent( v8::Persistent& handle @@ -647,9 +557,6 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) return info_; } - NAN_DEPRECATED NAN_INLINE void Dispose() const { - } - private: _NanWeakCallbackInfo* info_; }; @@ -786,18 +693,6 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) return NanNew(function_template)->HasInstance(value); } - NAN_DEPRECATED NAN_INLINE v8::Local NanNewContextHandle( - v8::ExtensionConfiguration* extensions = NULL - , v8::Handle tmpl = v8::Handle() - , v8::Handle obj = v8::Handle() - ) { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - return v8::Local::New( - isolate - , v8::Context::New(isolate, extensions, tmpl, obj) - ); - } - NAN_INLINE v8::Local NanCompileScript( v8::Local s , const v8::ScriptOrigin& origin @@ -889,10 +784,6 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) size = toStr->WriteOneByte(reinterpret_cast(buf)); } - NAN_DEPRECATED NAN_INLINE int Size() const { - return size; - } - NAN_INLINE int length() const { return size; } @@ -921,10 +812,6 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) toStr->WriteUtf8(buf); } - NAN_DEPRECATED NAN_INLINE int Size() const { - return size; - } - NAN_INLINE int length() const { return size; } @@ -952,10 +839,6 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) toStr->Write(buf); } - NAN_DEPRECATED NAN_INLINE int Size() const { - return size; - } - NAN_INLINE int length() const { return size; } @@ -1029,11 +912,6 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) # define _NAN_INDEX_QUERY_ARGS _NAN_INDEX_QUERY_ARGS_TYPE args # define _NAN_INDEX_QUERY_RETURN_TYPE v8::Handle - NAN_DEPRECATED NAN_INLINE v8::Local NanSymbol( - const char* data, int length = -1) { - return v8::String::NewSymbol(data, length); - } - # define NanScope() v8::HandleScope scope # define NanEscapableScope() v8::HandleScope scope # define NanEscapeScope(val) scope.Close(val) @@ -1188,9 +1066,6 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) return info_; } - NAN_DEPRECATED NAN_INLINE void Dispose() const { - } - private: _NanWeakCallbackInfo* info_; }; @@ -1347,17 +1222,6 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) return function_template->HasInstance(value); } - NAN_DEPRECATED NAN_INLINE v8::Local NanNewContextHandle( - v8::ExtensionConfiguration* extensions = NULL - , v8::Handle tmpl = v8::Handle() - , v8::Handle obj = v8::Handle() - ) { - v8::Persistent ctx = v8::Context::New(extensions, tmpl, obj); - v8::Local lctx = NanNew(ctx); - ctx.Dispose(); - return lctx; - } - NAN_INLINE v8::Local NanCompileScript( v8::Local s , const v8::ScriptOrigin& origin @@ -1454,10 +1318,6 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) size = toStr->WriteAscii(buf); } - NAN_DEPRECATED NAN_INLINE int Size() const { - return size; - } - NAN_INLINE int length() const { return size; } @@ -1486,10 +1346,6 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) toStr->WriteUtf8(buf); } - NAN_DEPRECATED NAN_INLINE int Size() const { - return size; - } - NAN_INLINE int length() const { return size; } @@ -1517,10 +1373,6 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) toStr->Write(buf); } - NAN_DEPRECATED NAN_INLINE int Size() const { - return size; - } - NAN_INLINE int length() const { return size; } @@ -1952,134 +1804,6 @@ NAN_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. -NAN_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 -NAN_INLINE size_t _nan_base64_decoded_size( - const T* 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 static size_t _nan_base64_decode( - char* buf - , size_t len - , const T* src - , const size_t srcLen -) { - char* dst = buf; - char* dstEnd = buf + len; - const T* srcEnd = src + srcLen; - - while (src < srcEnd && dst < dstEnd) { - ptrdiff_t remaining = srcEnd - src; - char a, b, c, d; - - 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 unsigned _nan_hex2bin(T 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(-1); -} - -template static size_t _nan_hex_decode( - char* buf - , size_t len - , const T* 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; -} - namespace Nan { namespace imp { inline @@ -2105,39 +1829,6 @@ IsExternal(v8::Local str) { } // end of namespace imp } // end of namespace Nan -static bool _NanGetExternalParts( - v8::Handle val - , const char** data - , size_t* len -) { - if (node::Buffer::HasInstance(val)) { - *data = node::Buffer::Data(val.As()); - *len = node::Buffer::Length(val.As()); - return true; - } - - assert(val->IsString()); - v8::Local str = NanNew(val.As()); - - if (Nan::imp::IsExternal(str)) { - const NanExternalOneByteStringResource* ext; - ext = Nan::imp::GetExternalResource(str); - *data = ext->data(); - *len = ext->length(); - return true; - } - - if (str->IsExternal()) { - const v8::String::ExternalStringResource* ext; - ext = str->GetExternalStringResource(); - *data = reinterpret_cast(ext->data()); - *len = ext->length(); - return true; - } - - return false; -} - namespace Nan { enum Encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER}; } @@ -2221,178 +1912,6 @@ NAN_INLINE ssize_t NanDecodeWrite( #endif } -/* NAN_DEPRECATED */ NAN_INLINE void* _NanRawString( - v8::Handle from - , enum Nan::Encoding encoding - , size_t *datalen - , void *buf - , size_t buflen - , int flags -) { - NanScope(); - - size_t sz_; - size_t term_len = !(flags & v8::String::NO_NULL_TERMINATION); - char *data = NULL; - size_t len; - bool is_extern = _NanGetExternalParts( - from - , const_cast(&data) - , &len); - - if (is_extern && !term_len) { - Nan::imp::NanSetPointerSafe(datalen, len); - return data; - } - - v8::Local toStr = from->ToString(); - - char *to = static_cast(buf); - - switch (encoding) { - case Nan::ASCII: -#if NODE_MODULE_VERSION < NODE_0_12_MODULE_VERSION - sz_ = toStr->Length(); - if (to == NULL) { - to = new char[sz_ + term_len]; - } else { - assert(buflen >= sz_ + term_len && "too small buffer"); - } - Nan::imp::NanSetPointerSafe( - datalen - , toStr->WriteAscii(to, 0, static_cast(sz_ + term_len), flags)); - return to; -#endif - case Nan::BINARY: - case Nan::BUFFER: - sz_ = toStr->Length(); - if (to == NULL) { - to = new char[sz_ + term_len]; - } else { - assert(buflen >= sz_ + term_len && "too small buffer"); - } -#if NODE_MODULE_VERSION < NODE_0_12_MODULE_VERSION - { - uint16_t* twobytebuf = new uint16_t[sz_ + term_len]; - - size_t somelen = toStr->Write(twobytebuf, 0, - static_cast(sz_ + term_len), flags); - - for (size_t i = 0; i < sz_ + term_len && i < somelen + term_len; i++) { - unsigned char *b = reinterpret_cast(&twobytebuf[i]); - to[i] = *b; - } - - Nan::imp::NanSetPointerSafe(datalen, somelen); - - delete[] twobytebuf; - return to; - } -#else - Nan::imp::NanSetPointerSafe( - datalen, - toStr->WriteOneByte( - reinterpret_cast(to) - , 0 - , static_cast(sz_ + term_len) - , flags)); - return to; -#endif - case Nan::UTF8: - sz_ = toStr->Utf8Length(); - if (to == NULL) { - to = new char[sz_ + term_len]; - } else { - assert(buflen >= sz_ + term_len && "too small buffer"); - } - Nan::imp::NanSetPointerSafe( - datalen - , toStr->WriteUtf8(to, static_cast(sz_ + term_len) - , NULL, flags) - - term_len); - return to; - case Nan::BASE64: - { - v8::String::Value value(toStr); - sz_ = _nan_base64_decoded_size(*value, value.length()); - if (to == NULL) { - to = new char[sz_ + term_len]; - } else { - assert(buflen >= sz_ + term_len); - } - Nan::imp::NanSetPointerSafe( - datalen - , _nan_base64_decode(to, sz_, *value, value.length())); - if (term_len) { - to[sz_] = '\0'; - } - return to; - } - case Nan::UCS2: - { - sz_ = toStr->Length(); - if (to == NULL) { - to = new char[(sz_ + term_len) * 2]; - } else { - assert(buflen >= (sz_ + term_len) * 2 && "too small buffer"); - } - - int bc = 2 * toStr->Write( - reinterpret_cast(to) - , 0 - , static_cast(sz_ + term_len) - , flags); - Nan::imp::NanSetPointerSafe(datalen, bc); - return to; - } - case Nan::HEX: - { - v8::String::Value value(toStr); - sz_ = value.length(); - assert(!(sz_ & 1) && "bad hex data"); - if (to == NULL) { - to = new char[sz_ / 2 + term_len]; - } else { - assert(buflen >= sz_ / 2 + term_len && "too small buffer"); - } - Nan::imp::NanSetPointerSafe( - datalen - , _nan_hex_decode(to, sz_ / 2, *value, value.length())); - } - if (term_len) { - to[sz_ / 2] = '\0'; - } - return to; - default: - assert(0 && "unknown encoding"); - } - return to; -} - -NAN_DEPRECATED NAN_INLINE void* NanRawString( - v8::Handle from - , enum Nan::Encoding encoding - , size_t *datalen - , void *buf - , size_t buflen - , int flags -) { - return _NanRawString(from, encoding, datalen, buf, buflen, flags); -} - - -NAN_DEPRECATED NAN_INLINE char* NanCString( - v8::Handle from - , size_t *datalen - , char *buf = NULL - , size_t buflen = 0 - , int flags = v8::String::NO_OPTIONS -) { - return static_cast( - _NanRawString(from, Nan::UTF8, datalen, buf, buflen, flags) - ); -} - NAN_INLINE void NanSetPrototypeTemplate( v8::Local templ , const char *name From 058fae8bad524e9c53c340e7a70d0a92188ec11c Mon Sep 17 00:00:00 2001 From: Benjamin Byholm Date: Fri, 8 May 2015 22:11:31 +0300 Subject: [PATCH 02/84] Remove remnants of 0.6 compatibility --- nan.h | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/nan.h b/nan.h index 8cc1e335..a438a138 100644 --- a/nan.h +++ b/nan.h @@ -1244,16 +1244,7 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) , v8::Handle func , int argc , v8::Handle* argv) { -# if NODE_VERSION_AT_LEAST(0, 8, 0) return NanNew(node::MakeCallback(target, func, argc, argv)); -# else - v8::TryCatch try_catch; - v8::Local result = func->Call(target, argc, argv); - if (try_catch.HasCaught()) { - node::FatalException(try_catch); - } - return result; -# endif } NAN_INLINE v8::Local NanMakeCallback( @@ -1261,12 +1252,7 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) , v8::Handle symbol , int argc , v8::Handle* argv) { -# if NODE_VERSION_AT_LEAST(0, 8, 0) return NanNew(node::MakeCallback(target, symbol, argc, argv)); -# else - v8::Local callback = target->Get(symbol).As(); - return NanMakeCallback(target, callback, argc, argv); -# endif } NAN_INLINE v8::Local NanMakeCallback( @@ -1274,11 +1260,7 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) , const char* method , int argc , v8::Handle* argv) { -# if NODE_VERSION_AT_LEAST(0, 8, 0) return NanNew(node::MakeCallback(target, method, argc, argv)); -# else - return NanMakeCallback(target, NanNew(method), argc, argv); -# endif } NAN_INLINE void NanFatalException(const v8::TryCatch& try_catch) { @@ -1553,7 +1535,6 @@ class NanCallback { , v8::Handle argv[]) const { NanEscapableScope(); -#if NODE_VERSION_AT_LEAST(0, 8, 0) v8::Local callback = handle-> Get(kCallbackIndex).As(); return NanEscapeScope(NanNew(node::MakeCallback( @@ -1561,13 +1542,7 @@ class NanCallback { , callback , argc , argv - ))); -#else - v8::Local callback = handle-> - Get(kCallbackIndex).As(); - return NanEscapeScope(NanMakeCallback( - target, callback, argc, argv)); -#endif + )); } #endif }; From 4cf44af6d90321c5ea007790531960ce0076d2c5 Mon Sep 17 00:00:00 2001 From: Benjamin Byholm Date: Fri, 8 May 2015 22:15:44 +0300 Subject: [PATCH 03/84] Remove 0.11 compatibility --- nan.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/nan.h b/nan.h index a438a138..2f00aceb 100644 --- a/nan.h +++ b/nan.h @@ -267,9 +267,8 @@ namespace Nan { namespace imp { } // end of namespace imp } // end of namespace Nan -/* io.js 1.0 */ -#if NODE_MODULE_VERSION >= IOJS_1_0_MODULE_VERSION \ - || NODE_VERSION_AT_LEAST(0, 11, 15) +/* node 0.12 */ +#if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION NAN_INLINE void NanSetCounterFunction(v8::CounterLookupCallback cb) { v8::Isolate::GetCurrent()->SetCounterFunction(cb); @@ -1808,7 +1807,7 @@ namespace Nan { enum Encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER}; } -#if !NODE_VERSION_AT_LEAST(0, 10, 0) +#if NODE_MODULE_VERSION < NODE_0_10_MODULE_VERSION # include "nan_string_bytes.h" // NOLINT(build/include) #endif @@ -1836,7 +1835,7 @@ NAN_INLINE v8::Local NanEncode( , buf, len , static_cast(encoding)); #else -# if NODE_VERSION_AT_LEAST(0, 10, 0) +# if NODE_MODULE_VERSION >= NODE_0_10_MODULE_VERSION return node::Encode(buf, len, static_cast(encoding)); # else return Nan::imp::Encode(reinterpret_cast(buf), len, encoding); From ba9986d92f70225ac02bd1cebbdd5b7d6841eab8 Mon Sep 17 00:00:00 2001 From: David Siegel Date: Sat, 8 Nov 2014 21:14:28 +0100 Subject: [PATCH 04/84] WeakPersistent redesign --- nan.h | 272 ++++++++++++++++++---------------------------- test/cpp/weak.cpp | 8 +- 2 files changed, 110 insertions(+), 170 deletions(-) diff --git a/nan.h b/nan.h index 2f00aceb..3131ec51 100644 --- a/nan.h +++ b/nan.h @@ -511,86 +511,6 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) handle.Reset(v8::Isolate::GetCurrent(), obj); } - template - class _NanWeakCallbackData; - - template - struct _NanWeakCallbackInfo { - typedef void (*Callback)(const _NanWeakCallbackData& data); - NAN_INLINE _NanWeakCallbackInfo(v8::Handle handle, P* param, Callback cb) - : parameter(param), callback(cb) { - NanAssignPersistent(persistent, handle); - } - - NAN_INLINE ~_NanWeakCallbackInfo() { - persistent.Reset(); - } - - P* const parameter; - Callback const callback; - v8::Persistent persistent; - - private: - NAN_DISALLOW_ASSIGN_COPY_MOVE(_NanWeakCallbackInfo) - }; - - template - class _NanWeakCallbackData { - public: - NAN_INLINE _NanWeakCallbackData(_NanWeakCallbackInfo *info) - : info_(info) { } - - NAN_INLINE v8::Local GetValue() const { - return NanNew(info_->persistent); - } - - NAN_INLINE P* GetParameter() const { return info_->parameter; } - - NAN_INLINE bool IsNearDeath() const { - return info_->persistent.IsNearDeath(); - } - - NAN_INLINE void Revive() const; - - NAN_INLINE _NanWeakCallbackInfo* GetCallbackInfo() const { - return info_; - } - - private: - _NanWeakCallbackInfo* info_; - }; - - template - static void _NanWeakCallbackDispatcher( - const v8::WeakCallbackData > &data) { - _NanWeakCallbackInfo *info = data.GetParameter(); - _NanWeakCallbackData wcbd(info); - info->callback(wcbd); - if (wcbd.IsNearDeath()) { - delete wcbd.GetCallbackInfo(); - } - } - - template - NAN_INLINE void _NanWeakCallbackData::Revive() const { - info_->persistent.SetWeak(info_, &_NanWeakCallbackDispatcher); - } - - template - NAN_INLINE _NanWeakCallbackInfo* NanMakeWeakPersistent( - v8::Handle handle - , P* parameter - , typename _NanWeakCallbackInfo::Callback callback) { - _NanWeakCallbackInfo *cbinfo = - new _NanWeakCallbackInfo(handle, parameter, callback); - cbinfo->persistent.SetWeak(cbinfo, &_NanWeakCallbackDispatcher); - return cbinfo; - } - -# define NAN_WEAK_CALLBACK(name) \ - template \ - static void name(const _NanWeakCallbackData &data) - # define X(NAME) \ NAN_INLINE v8::Local Nan ## NAME(const char *errmsg) { \ NanEscapableScope(); \ @@ -1019,93 +939,6 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) handle = v8::Persistent::New(obj); } - template - class _NanWeakCallbackData; - - template - struct _NanWeakCallbackInfo { - typedef void (*Callback)(const _NanWeakCallbackData &data); - NAN_INLINE _NanWeakCallbackInfo(v8::Handle handle, P* param, Callback cb) - : parameter(param) - , callback(cb) - , persistent(v8::Persistent::New(handle)) { } - - NAN_INLINE ~_NanWeakCallbackInfo() { - persistent.Dispose(); - persistent.Clear(); - } - - P* const parameter; - Callback const callback; - v8::Persistent persistent; - - private: - NAN_DISALLOW_ASSIGN_COPY_MOVE(_NanWeakCallbackInfo) - }; - - template - class _NanWeakCallbackData { - public: - NAN_INLINE _NanWeakCallbackData(_NanWeakCallbackInfo *info) - : info_(info) { } - - NAN_INLINE v8::Local GetValue() const { - return NanNew(info_->persistent); - } - - NAN_INLINE P* GetParameter() const { return info_->parameter; } - - NAN_INLINE bool IsNearDeath() const { - return info_->persistent.IsNearDeath(); - } - - NAN_INLINE void Revive() const; - - NAN_INLINE _NanWeakCallbackInfo* GetCallbackInfo() const { - return info_; - } - - private: - _NanWeakCallbackInfo* info_; - }; - - template - static void _NanWeakPersistentDispatcher( - v8::Persistent object, void *data) { - (void) object; // unused - _NanWeakCallbackInfo* info = - static_cast<_NanWeakCallbackInfo*>(data); - _NanWeakCallbackData wcbd(info); - info->callback(wcbd); - if (wcbd.IsNearDeath()) { - delete wcbd.GetCallbackInfo(); - } - } - - template - NAN_INLINE void _NanWeakCallbackData::Revive() const { - info_->persistent.MakeWeak( - info_ - , &_NanWeakPersistentDispatcher); - } - - template - NAN_INLINE _NanWeakCallbackInfo* NanMakeWeakPersistent( - v8::Handle handle - , P* parameter - , typename _NanWeakCallbackInfo::Callback callback) { - _NanWeakCallbackInfo *cbinfo = - new _NanWeakCallbackInfo(handle, parameter, callback); - cbinfo->persistent.MakeWeak( - cbinfo - , &_NanWeakPersistentDispatcher); - return cbinfo; - } - -# define NAN_WEAK_CALLBACK(name) \ - template \ - static void name(const _NanWeakCallbackData &data) - # define X(NAME) \ NAN_INLINE v8::Local Nan ## NAME(const char *errmsg) { \ NanEscapableScope(); \ @@ -1920,6 +1753,111 @@ NAN_INLINE void NanSetInstanceTemplate( NanSetTemplate(templ->InstanceTemplate(), name, value, attributes); } +//=== Weak Persistent Handling ================================================= + +#if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION +# define NAN_WEAK_CALLBACK_DATA_TYPE_ \ + v8::WeakCallbackData > const& +# define NAN_WEAK_CALLBACK_SIG_ NAN_WEAK_CALLBACK_DATA_TYPE_ +#else +# define NAN_WEAK_CALLBACK_DATA_TYPE_ void * +# define NAN_WEAK_CALLBACK_SIG_ \ + v8::Persistent, NAN_WEAK_CALLBACK_DATA_TYPE_ +#endif + +template +class NanWeakCallbackData { + public: // constructors + typedef void (*Callback)( + NanWeakCallbackData & data // NOLINT(runtime/references) + ); + NanWeakCallbackData(v8::Handle handle, P* param, Callback cb) + : parameter(param), callback(cb) { + NanAssignPersistent(persistent, handle); + Revive(); + } + inline ~NanWeakCallbackData(); + + public: // member functions + v8::Local GetValue() const { return NanNew(persistent); } + v8::Persistent &GetPersistent() const { return persistent; } + P* GetParameter() const { return parameter; } + bool IsNearDeath() const { return persistent.IsNearDeath(); } + inline void Revive(); + + private: // constructors + NAN_DISALLOW_ASSIGN_COPY_MOVE(NanWeakCallbackData) + + private: // static member functions + static + void + invoke(NAN_WEAK_CALLBACK_SIG_ data) { + NanWeakCallbackData * wcbd = unwrap(data); + wcbd->callback(*wcbd); + if (wcbd->IsNearDeath()) { + delete wcbd; + } + } + + static inline + NanWeakCallbackData * + unwrap(NAN_WEAK_CALLBACK_DATA_TYPE_ data); + + private: // data members + P* const parameter; + Callback const callback; + v8::Persistent persistent; +}; + +#undef NAN_WEAK_CALLBACK_DATA_TYPE_ +#undef NAN_WEAK_CALLBACK_SIG_ + +#if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION + +template +NanWeakCallbackData::~NanWeakCallbackData() { persistent.Reset(); } + +template +void +NanWeakCallbackData::Revive() { persistent.SetWeak(this, &invoke); } + +template +NanWeakCallbackData * +NanWeakCallbackData::unwrap( + v8::WeakCallbackData const& data) { + return data.GetParameter(); +} + +#else + +template +NanWeakCallbackData::~NanWeakCallbackData() { + persistent.Dispose(); + persistent.Clear(); +} + +template +void +NanWeakCallbackData::Revive() { persistent.MakeWeak(this, &invoke); } + +template +NanWeakCallbackData * +NanWeakCallbackData::unwrap(void * data) { + return static_cast(data); +} + +#endif + +template +inline +NanWeakCallbackData* +NanMakeWeakPersistent( + v8::Handle handle + , P* parameter + , typename NanWeakCallbackData::Callback callback) { + return new NanWeakCallbackData(handle, parameter, callback); +} + //=== Export ================================================================== inline diff --git a/test/cpp/weak.cpp b/test/cpp/weak.cpp index b156d8ec..69f47e96 100644 --- a/test/cpp/weak.cpp +++ b/test/cpp/weak.cpp @@ -8,7 +8,8 @@ #include -NAN_WEAK_CALLBACK(weakCallback) { +void weakCallback( +NanWeakCallbackData & data) { // NOLINT(runtime/references) int *parameter = data.GetParameter(); NanMakeCallback(NanGetCurrentContext()->Global(), data.GetValue(), 0, NULL); if ((*parameter)++ == 0) { @@ -19,10 +20,11 @@ NAN_WEAK_CALLBACK(weakCallback) { } v8::Handle wrap(v8::Local func) { + NanEscapableScope(); v8::Local lstring = NanNew("result"); int *parameter = new int(0); - NanMakeWeakPersistent(func, parameter, &weakCallback); - return lstring; + NanMakeWeakPersistent(func, parameter, weakCallback); + return NanEscapeScope(lstring); } NAN_METHOD(Hustle) { From b5932b42fdb6edc064b9f2e097de56a10c17e07f Mon Sep 17 00:00:00 2001 From: Benjamin Byholm Date: Fri, 1 May 2015 17:19:11 +0300 Subject: [PATCH 05/84] Renamed identifiers with leading underscores --- nan.h | 230 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 101 insertions(+), 129 deletions(-) diff --git a/nan.h b/nan.h index 3131ec51..91f3ea77 100644 --- a/nan.h +++ b/nan.h @@ -324,71 +324,53 @@ namespace Nan { namespace imp { } #endif -#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION) -// Node 0.11+ (0.11.12 and below won't compile with these) - -# define _NAN_METHOD_ARGS_TYPE const v8::FunctionCallbackInfo& -# define _NAN_METHOD_ARGS _NAN_METHOD_ARGS_TYPE args -# define _NAN_METHOD_RETURN_TYPE void - -# define _NAN_GETTER_ARGS_TYPE const v8::PropertyCallbackInfo& -# define _NAN_GETTER_ARGS _NAN_GETTER_ARGS_TYPE args -# define _NAN_GETTER_RETURN_TYPE void - -# define _NAN_SETTER_ARGS_TYPE const v8::PropertyCallbackInfo& -# define _NAN_SETTER_ARGS _NAN_SETTER_ARGS_TYPE args -# define _NAN_SETTER_RETURN_TYPE void - -# define _NAN_PROPERTY_GETTER_ARGS_TYPE \ - const v8::PropertyCallbackInfo& -# define _NAN_PROPERTY_GETTER_ARGS _NAN_PROPERTY_GETTER_ARGS_TYPE args -# define _NAN_PROPERTY_GETTER_RETURN_TYPE void - -# define _NAN_PROPERTY_SETTER_ARGS_TYPE \ - const v8::PropertyCallbackInfo& -# define _NAN_PROPERTY_SETTER_ARGS _NAN_PROPERTY_SETTER_ARGS_TYPE args -# define _NAN_PROPERTY_SETTER_RETURN_TYPE void - -# define _NAN_PROPERTY_ENUMERATOR_ARGS_TYPE \ - const v8::PropertyCallbackInfo& -# define _NAN_PROPERTY_ENUMERATOR_ARGS _NAN_PROPERTY_ENUMERATOR_ARGS_TYPE args -# define _NAN_PROPERTY_ENUMERATOR_RETURN_TYPE void - -# define _NAN_PROPERTY_DELETER_ARGS_TYPE \ - const v8::PropertyCallbackInfo& -# define _NAN_PROPERTY_DELETER_ARGS \ - _NAN_PROPERTY_DELETER_ARGS_TYPE args -# define _NAN_PROPERTY_DELETER_RETURN_TYPE void - -# define _NAN_PROPERTY_QUERY_ARGS_TYPE \ - const v8::PropertyCallbackInfo& -# define _NAN_PROPERTY_QUERY_ARGS _NAN_PROPERTY_QUERY_ARGS_TYPE args -# define _NAN_PROPERTY_QUERY_RETURN_TYPE void - -# define _NAN_INDEX_GETTER_ARGS_TYPE \ - const v8::PropertyCallbackInfo& -# define _NAN_INDEX_GETTER_ARGS _NAN_INDEX_GETTER_ARGS_TYPE args -# define _NAN_INDEX_GETTER_RETURN_TYPE void - -# define _NAN_INDEX_SETTER_ARGS_TYPE \ - const v8::PropertyCallbackInfo& -# define _NAN_INDEX_SETTER_ARGS _NAN_INDEX_SETTER_ARGS_TYPE args -# define _NAN_INDEX_SETTER_RETURN_TYPE void - -# define _NAN_INDEX_ENUMERATOR_ARGS_TYPE \ - const v8::PropertyCallbackInfo& -# define _NAN_INDEX_ENUMERATOR_ARGS _NAN_INDEX_ENUMERATOR_ARGS_TYPE args -# define _NAN_INDEX_ENUMERATOR_RETURN_TYPE void - -# define _NAN_INDEX_DELETER_ARGS_TYPE \ - const v8::PropertyCallbackInfo& -# define _NAN_INDEX_DELETER_ARGS _NAN_INDEX_DELETER_ARGS_TYPE args -# define _NAN_INDEX_DELETER_RETURN_TYPE void - -# define _NAN_INDEX_QUERY_ARGS_TYPE \ - const v8::PropertyCallbackInfo& -# define _NAN_INDEX_QUERY_ARGS _NAN_INDEX_QUERY_ARGS_TYPE args -# define _NAN_INDEX_QUERY_RETURN_TYPE void +#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION) // Node 0.12 + typedef const v8::FunctionCallbackInfo& NAN_METHOD_ARGS_TYPE; + typedef void NAN_METHOD_RETURN_TYPE; + + typedef const v8::PropertyCallbackInfo& NAN_GETTER_ARGS_TYPE; + typedef void NAN_GETTER_RETURN_TYPE; + + typedef const v8::PropertyCallbackInfo& NAN_SETTER_ARGS_TYPE; + typedef void NAN_SETTER_RETURN_TYPE; + + typedef const v8::PropertyCallbackInfo& + NAN_PROPERTY_GETTER_ARGS_TYPE; + typedef void NAN_PROPERTY_GETTER_RETURN_TYPE; + + typedef const v8::PropertyCallbackInfo& + NAN_PROPERTY_SETTER_ARGS_TYPE; + typedef void NAN_PROPERTY_SETTER_RETURN_TYPE; + + typedef const v8::PropertyCallbackInfo& + NAN_PROPERTY_ENUMERATOR_ARGS_TYPE; + typedef void NAN_PROPERTY_ENUMERATOR_RETURN_TYPE; + + typedef const v8::PropertyCallbackInfo& + NAN_PROPERTY_DELETER_ARGS_TYPE; + typedef void NAN_PROPERTY_DELETER_RETURN_TYPE; + + typedef const v8::PropertyCallbackInfo& + NAN_PROPERTY_QUERY_ARGS_TYPE; + typedef void NAN_PROPERTY_QUERY_RETURN_TYPE; + + typedef const v8::PropertyCallbackInfo& NAN_INDEX_GETTER_ARGS_TYPE; + typedef void NAN_INDEX_GETTER_RETURN_TYPE; + + typedef const v8::PropertyCallbackInfo& NAN_INDEX_SETTER_ARGS_TYPE; + typedef void NAN_INDEX_SETTER_RETURN_TYPE; + + typedef const v8::PropertyCallbackInfo& + NAN_INDEX_ENUMERATOR_ARGS_TYPE; + typedef void NAN_INDEX_ENUMERATOR_RETURN_TYPE; + + typedef const v8::PropertyCallbackInfo& + NAN_INDEX_DELETER_ARGS_TYPE; + typedef void NAN_INDEX_DELETER_RETURN_TYPE; + + typedef const v8::PropertyCallbackInfo& + NAN_INDEX_QUERY_ARGS_TYPE; + typedef void NAN_INDEX_QUERY_RETURN_TYPE; # define NanScope() v8::HandleScope scope(v8::Isolate::GetCurrent()) # define NanEscapableScope() \ @@ -398,7 +380,7 @@ namespace Nan { namespace imp { # define NanLocker() v8::Locker locker(v8::Isolate::GetCurrent()) # define NanUnlocker() v8::Unlocker unlocker(v8::Isolate::GetCurrent()) # define NanReturnValue(value) \ -return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) + return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) # define NanReturnUndefined() return # define NanReturnHolder() NanReturnValue(args.Holder()) # define NanReturnThis() NanReturnValue(args.This()) @@ -776,60 +758,45 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) int size; }; -#else -// Node 0.8 and 0.10 - -# define _NAN_METHOD_ARGS_TYPE const v8::Arguments& -# define _NAN_METHOD_ARGS _NAN_METHOD_ARGS_TYPE args -# define _NAN_METHOD_RETURN_TYPE v8::Handle +#else // Node 0.8 and 0.10 + typedef const v8::Arguments& NAN_METHOD_ARGS_TYPE; + typedef v8::Handle NAN_METHOD_RETURN_TYPE; -# define _NAN_GETTER_ARGS_TYPE const v8::AccessorInfo & -# define _NAN_GETTER_ARGS _NAN_GETTER_ARGS_TYPE args -# define _NAN_GETTER_RETURN_TYPE v8::Handle + typedef const v8::AccessorInfo & NAN_GETTER_ARGS_TYPE; + typedef v8::Handle NAN_GETTER_RETURN_TYPE; -# define _NAN_SETTER_ARGS_TYPE const v8::AccessorInfo & -# define _NAN_SETTER_ARGS _NAN_SETTER_ARGS_TYPE args -# define _NAN_SETTER_RETURN_TYPE void + typedef const v8::AccessorInfo & NAN_SETTER_ARGS_TYPE; + typedef void NAN_SETTER_RETURN_TYPE; -# define _NAN_PROPERTY_GETTER_ARGS_TYPE const v8::AccessorInfo& -# define _NAN_PROPERTY_GETTER_ARGS _NAN_PROPERTY_GETTER_ARGS_TYPE args -# define _NAN_PROPERTY_GETTER_RETURN_TYPE v8::Handle + typedef const v8::AccessorInfo& NAN_PROPERTY_GETTER_ARGS_TYPE; + typedef v8::Handle NAN_PROPERTY_GETTER_RETURN_TYPE; -# define _NAN_PROPERTY_SETTER_ARGS_TYPE const v8::AccessorInfo& -# define _NAN_PROPERTY_SETTER_ARGS _NAN_PROPERTY_SETTER_ARGS_TYPE args -# define _NAN_PROPERTY_SETTER_RETURN_TYPE v8::Handle + typedef const v8::AccessorInfo& NAN_PROPERTY_SETTER_ARGS_TYPE; + typedef v8::Handle NAN_PROPERTY_SETTER_RETURN_TYPE; -# define _NAN_PROPERTY_ENUMERATOR_ARGS_TYPE const v8::AccessorInfo& -# define _NAN_PROPERTY_ENUMERATOR_ARGS _NAN_PROPERTY_ENUMERATOR_ARGS_TYPE args -# define _NAN_PROPERTY_ENUMERATOR_RETURN_TYPE v8::Handle + typedef const v8::AccessorInfo& NAN_PROPERTY_ENUMERATOR_ARGS_TYPE; + typedef v8::Handle NAN_PROPERTY_ENUMERATOR_RETURN_TYPE; -# define _NAN_PROPERTY_DELETER_ARGS_TYPE const v8::AccessorInfo& -# define _NAN_PROPERTY_DELETER_ARGS _NAN_PROPERTY_DELETER_ARGS_TYPE args -# define _NAN_PROPERTY_DELETER_RETURN_TYPE v8::Handle + typedef const v8::AccessorInfo& NAN_PROPERTY_DELETER_ARGS_TYPE; + typedef v8::Handle NAN_PROPERTY_DELETER_RETURN_TYPE; -# define _NAN_PROPERTY_QUERY_ARGS_TYPE const v8::AccessorInfo& -# define _NAN_PROPERTY_QUERY_ARGS _NAN_PROPERTY_QUERY_ARGS_TYPE args -# define _NAN_PROPERTY_QUERY_RETURN_TYPE v8::Handle + typedef const v8::AccessorInfo& NAN_PROPERTY_QUERY_ARGS_TYPE; + typedef v8::Handle NAN_PROPERTY_QUERY_RETURN_TYPE; -# define _NAN_INDEX_GETTER_ARGS_TYPE const v8::AccessorInfo& -# define _NAN_INDEX_GETTER_ARGS _NAN_INDEX_GETTER_ARGS_TYPE args -# define _NAN_INDEX_GETTER_RETURN_TYPE v8::Handle + typedef const v8::AccessorInfo& NAN_INDEX_GETTER_ARGS_TYPE; + typedef v8::Handle NAN_INDEX_GETTER_RETURN_TYPE; -# define _NAN_INDEX_SETTER_ARGS_TYPE const v8::AccessorInfo& -# define _NAN_INDEX_SETTER_ARGS _NAN_INDEX_SETTER_ARGS_TYPE args -# define _NAN_INDEX_SETTER_RETURN_TYPE v8::Handle + typedef const v8::AccessorInfo& NAN_INDEX_SETTER_ARGS_TYPE; + typedef v8::Handle NAN_INDEX_SETTER_RETURN_TYPE; -# define _NAN_INDEX_ENUMERATOR_ARGS_TYPE const v8::AccessorInfo& -# define _NAN_INDEX_ENUMERATOR_ARGS _NAN_INDEX_ENUMERATOR_ARGS_TYPE args -# define _NAN_INDEX_ENUMERATOR_RETURN_TYPE v8::Handle + typedef const v8::AccessorInfo& NAN_INDEX_ENUMERATOR_ARGS_TYPE; + typedef v8::Handle NAN_INDEX_ENUMERATOR_RETURN_TYPE; -# define _NAN_INDEX_DELETER_ARGS_TYPE const v8::AccessorInfo& -# define _NAN_INDEX_DELETER_ARGS _NAN_INDEX_DELETER_ARGS_TYPE args -# define _NAN_INDEX_DELETER_RETURN_TYPE v8::Handle + typedef const v8::AccessorInfo& NAN_INDEX_DELETER_ARGS_TYPE; + typedef v8::Handle NAN_INDEX_DELETER_RETURN_TYPE; -# define _NAN_INDEX_QUERY_ARGS_TYPE const v8::AccessorInfo& -# define _NAN_INDEX_QUERY_ARGS _NAN_INDEX_QUERY_ARGS_TYPE args -# define _NAN_INDEX_QUERY_RETURN_TYPE v8::Handle + typedef const v8::AccessorInfo& NAN_INDEX_QUERY_ARGS_TYPE; + typedef v8::Handle NAN_INDEX_QUERY_RETURN_TYPE; # define NanScope() v8::HandleScope scope # define NanEscapableScope() v8::HandleScope scope @@ -1209,50 +1176,55 @@ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) typedef void (*NanFreeCallback)(char *data, void *hint); -#define NAN_METHOD(name) _NAN_METHOD_RETURN_TYPE name(_NAN_METHOD_ARGS) +#define NAN_METHOD(name) NAN_METHOD_RETURN_TYPE name(NAN_METHOD_ARGS_TYPE args) #define NAN_GETTER(name) \ - _NAN_GETTER_RETURN_TYPE name( \ + NAN_GETTER_RETURN_TYPE name( \ v8::Local property \ - , _NAN_GETTER_ARGS) + , NAN_GETTER_ARGS_TYPE args) #define NAN_SETTER(name) \ - _NAN_SETTER_RETURN_TYPE name( \ + NAN_SETTER_RETURN_TYPE name( \ v8::Local property \ , v8::Local value \ - , _NAN_SETTER_ARGS) + , NAN_SETTER_ARGS_TYPE args) #define NAN_PROPERTY_GETTER(name) \ - _NAN_PROPERTY_GETTER_RETURN_TYPE name( \ + NAN_PROPERTY_GETTER_RETURN_TYPE name( \ v8::Local property \ - , _NAN_PROPERTY_GETTER_ARGS) + , NAN_PROPERTY_GETTER_ARGS_TYPE args) #define NAN_PROPERTY_SETTER(name) \ - _NAN_PROPERTY_SETTER_RETURN_TYPE name( \ + NAN_PROPERTY_SETTER_RETURN_TYPE name( \ v8::Local property \ , v8::Local value \ - , _NAN_PROPERTY_SETTER_ARGS) + , NAN_PROPERTY_SETTER_ARGS_TYPE args) #define NAN_PROPERTY_ENUMERATOR(name) \ - _NAN_PROPERTY_ENUMERATOR_RETURN_TYPE name(_NAN_PROPERTY_ENUMERATOR_ARGS) + NAN_PROPERTY_ENUMERATOR_RETURN_TYPE name( \ + NAN_PROPERTY_ENUMERATOR_ARGS_TYPE args) #define NAN_PROPERTY_DELETER(name) \ - _NAN_PROPERTY_DELETER_RETURN_TYPE name( \ + NAN_PROPERTY_DELETER_RETURN_TYPE name( \ v8::Local property \ - , _NAN_PROPERTY_DELETER_ARGS) + , NAN_PROPERTY_DELETER_ARGS_TYPE args) #define NAN_PROPERTY_QUERY(name) \ - _NAN_PROPERTY_QUERY_RETURN_TYPE name( \ + NAN_PROPERTY_QUERY_RETURN_TYPE name( \ v8::Local property \ - , _NAN_PROPERTY_QUERY_ARGS) + , NAN_PROPERTY_QUERY_ARGS_TYPE args) # define NAN_INDEX_GETTER(name) \ - _NAN_INDEX_GETTER_RETURN_TYPE name(uint32_t index, _NAN_INDEX_GETTER_ARGS) + NAN_INDEX_GETTER_RETURN_TYPE name( \ + uint32_t index \ + , NAN_INDEX_GETTER_ARGS_TYPE args) #define NAN_INDEX_SETTER(name) \ - _NAN_INDEX_SETTER_RETURN_TYPE name( \ + NAN_INDEX_SETTER_RETURN_TYPE name( \ uint32_t index \ , v8::Local value \ - , _NAN_INDEX_SETTER_ARGS) + , NAN_INDEX_SETTER_ARGS_TYPE args) #define NAN_INDEX_ENUMERATOR(name) \ - _NAN_INDEX_ENUMERATOR_RETURN_TYPE name(_NAN_INDEX_ENUMERATOR_ARGS) + NAN_INDEX_ENUMERATOR_RETURN_TYPE name(NAN_INDEX_ENUMERATOR_ARGS_TYPE args) #define NAN_INDEX_DELETER(name) \ - _NAN_INDEX_DELETER_RETURN_TYPE name( \ + NAN_INDEX_DELETER_RETURN_TYPE name( \ uint32_t index \ - , _NAN_INDEX_DELETER_ARGS) + , NAN_INDEX_DELETER_ARGS_TYPE args) #define NAN_INDEX_QUERY(name) \ - _NAN_INDEX_QUERY_RETURN_TYPE name(uint32_t index, _NAN_INDEX_QUERY_ARGS) + NAN_INDEX_QUERY_RETURN_TYPE name( \ + uint32_t index \ + , NAN_INDEX_QUERY_ARGS_TYPE args) class NanCallback { public: From f77b7e7eafce44503dce381c30fe53c329892aee Mon Sep 17 00:00:00 2001 From: Benjamin Byholm Date: Wed, 11 Feb 2015 00:43:32 +0200 Subject: [PATCH 06/84] Disconnect NanScope and NanReturnValue --- README.md | 18 +----------------- examples/async_pi_estimate/async.cc | 2 -- examples/async_pi_estimate/sync.cc | 2 -- nan.h | 2 +- test/cpp/asyncprogressworker.cpp | 1 - test/cpp/asyncworker.cpp | 1 - test/cpp/asyncworkererror.cpp | 1 - test/cpp/bufferworkerpersistent.cpp | 5 ----- test/cpp/gc.cpp | 2 -- test/cpp/isolatedata.cpp | 2 -- test/cpp/makecallback.cpp | 20 ++++++++------------ test/cpp/morenews.cpp | 8 -------- test/cpp/multifile2.cpp | 1 - test/cpp/nancallback.cpp | 13 ------------- test/cpp/nannew.cpp | 27 --------------------------- test/cpp/news.cpp | 27 --------------------------- test/cpp/objectwraphandle.cpp | 4 ---- test/cpp/persistent.cpp | 14 -------------- test/cpp/returnemptystring.cpp | 1 - test/cpp/returnnull.cpp | 1 - test/cpp/returnundefined.cpp | 1 - test/cpp/returnvalue.cpp | 4 ---- test/cpp/settemplate.cpp | 4 ---- test/cpp/settergetter.cpp | 14 ++------------ test/cpp/strings.cpp | 8 -------- test/cpp/threadlocal.cpp | 2 +- test/cpp/weak.cpp | 1 - 27 files changed, 13 insertions(+), 173 deletions(-) diff --git a/README.md b/README.md index 8e155dcc..6cb0acc3 100644 --- a/README.md +++ b/README.md @@ -178,8 +178,6 @@ using v8::Number; // Simple synchronous access to the `Estimate()` function NAN_METHOD(CalculateSync) { - NanScope(); - // expect a number as the first argument int points = args[0]->Uint32Value(); double est = Estimate(points); @@ -245,8 +243,6 @@ class PiWorker : public NanAsyncWorker { // Asynchronous access to the `Estimate()` function NAN_METHOD(CalculateAsync) { - NanScope(); - int points = args[0]->Uint32Value(); NanCallback *callback = new NanCallback(args[1].As()); @@ -461,7 +457,6 @@ NAN_GC_CALLBACK(gcPrologueCallback) { } NAN_METHOD(Hook) { - NanScope(); NanAssignPersistent(callback, args[0].As()); NanAddGCPrologueCallback(gcPrologueCallback); NanReturnValue(args.Holder()); @@ -635,12 +630,10 @@ NAN_METHOD(Foo::Baz) { ### NanScope() -The introduction of `isolate` references for many V8 calls in Node 0.11 makes `NanScope()` necessary, use it in place of `HandleScope scope` when you do not wish to return handles (`Handle` or `Local`) to the surrounding scope (or in functions directly exposed to V8, as they do not return values in the normal sense): +The introduction of `isolate` references for many V8 calls in Node 0.11 makes `NanScope()` necessary, use it in place of `HandleScope scope` when you do not wish to return handles (`Handle` or `Local`) to the surrounding scope (or in functions directly exposed to V8, as they do not return values in the normal sense). V8-exposed methods (NAN_METHOD, etc.) have implicit handle scopes and do not need extra scopes: ```c++ NAN_METHOD(Foo::Bar) { - NanScope(); - NanReturnValue(NanNew("FooBar!")); } ``` @@ -815,7 +808,6 @@ Convert a `String` to zero-terminated, sort-of Ascii-encoded `char *`. The under ```c++ NAN_METHOD(foo) { - NanScope(); NanReturnValue(NanNew(*NanAsciiString(arg[0]))); } ``` @@ -826,7 +818,6 @@ the buffer `str` points to has been freed when `baz` was destroyed: static char *str; NAN_METHOD(bar) { - NanScope(); NanAsciiString baz(arg[0]); str = *baz; @@ -843,7 +834,6 @@ printf(str); // use-after-free error static NanAsciiString *str; NAN_METHOD(bar) { - NanScope(); str = new NanAsciiString(arg[0]); NanReturnUndefined(); } @@ -861,7 +851,6 @@ Convert a `String` to zero-terminated, Utf8-encoded `char *`. The underlying buf ```c++ NAN_METHOD(foo) { - NanScope(); NanReturnValue(NanNew(*NanUtf8String(arg[0]))); } ``` @@ -872,7 +861,6 @@ the buffer `str` points to has been freed when `baz` was destroyed: static char *str; NAN_METHOD(bar) { - NanScope(); NanUtf8String baz(arg[0]); str = *baz; @@ -889,7 +877,6 @@ printf(str); // use-after-free error static NanUtf8String *str; NAN_METHOD(bar) { - NanScope(); str = new NanUtf8String(arg[0]); NanReturnUndefined(); } @@ -908,7 +895,6 @@ Convert a `String` to zero-terminated, Ucs2-encoded `uint16_t *`. The underlying ```c++ NAN_METHOD(foo) { - NanScope(); NanReturnValue(NanNew(*NanUcs2String(arg[0]))); } ``` @@ -919,7 +905,6 @@ the buffer `str` points to has been freed when `baz` was destroyed: static char *str; NAN_METHOD(bar) { - NanScope(); NanUcs2String baz(arg[0]); str = *baz; @@ -936,7 +921,6 @@ printf(str); // use-after-free error static NanUcs2String *str; NAN_METHOD(bar) { - NanScope(); str = new NanUcs2String(arg[0]); NanReturnUndefined(); } diff --git a/examples/async_pi_estimate/async.cc b/examples/async_pi_estimate/async.cc index d6b2982c..8ce559c8 100644 --- a/examples/async_pi_estimate/async.cc +++ b/examples/async_pi_estimate/async.cc @@ -51,8 +51,6 @@ class PiWorker : public NanAsyncWorker { // Asynchronous access to the `Estimate()` function NAN_METHOD(CalculateAsync) { - NanScope(); - int points = args[0]->Uint32Value(); NanCallback *callback = new NanCallback(args[1].As()); diff --git a/examples/async_pi_estimate/sync.cc b/examples/async_pi_estimate/sync.cc index d46380a2..b2e7ba6c 100644 --- a/examples/async_pi_estimate/sync.cc +++ b/examples/async_pi_estimate/sync.cc @@ -14,8 +14,6 @@ using v8::Number; // Simple synchronous access to the `Estimate()` function NAN_METHOD(CalculateSync) { - NanScope(); - // expect a number as the first argument int points = args[0]->Uint32Value(); double est = Estimate(points); diff --git a/nan.h b/nan.h index 91f3ea77..b9928928 100644 --- a/nan.h +++ b/nan.h @@ -804,7 +804,7 @@ namespace Nan { namespace imp { # define NanLocker() v8::Locker locker # define NanUnlocker() v8::Unlocker unlocker # define NanReturnValue(value) \ - return scope.Close(Nan::imp::NanEnsureHandleOrPersistent(value)) + return Nan::imp::NanEnsureHandleOrPersistent(value) # define NanReturnHolder() NanReturnValue(args.Holder()) # define NanReturnThis() NanReturnValue(args.This()) # define NanReturnUndefined() return v8::Undefined() diff --git a/test/cpp/asyncprogressworker.cpp b/test/cpp/asyncprogressworker.cpp index 92afd389..68c4a5aa 100644 --- a/test/cpp/asyncprogressworker.cpp +++ b/test/cpp/asyncprogressworker.cpp @@ -46,7 +46,6 @@ class ProgressWorker : public NanAsyncProgressWorker { }; NAN_METHOD(DoProgress) { - NanScope(); NanCallback *progress = new NanCallback(args[2].As()); NanCallback *callback = new NanCallback(args[3].As()); NanAsyncQueueWorker(new ProgressWorker( diff --git a/test/cpp/asyncworker.cpp b/test/cpp/asyncworker.cpp index 39ee0156..f11b489a 100644 --- a/test/cpp/asyncworker.cpp +++ b/test/cpp/asyncworker.cpp @@ -27,7 +27,6 @@ class SleepWorker : public NanAsyncWorker { }; NAN_METHOD(DoSleep) { - NanScope(); NanCallback *callback = new NanCallback(args[1].As()); NanAsyncQueueWorker(new SleepWorker(callback, args[0]->Uint32Value())); NanReturnUndefined(); diff --git a/test/cpp/asyncworkererror.cpp b/test/cpp/asyncworkererror.cpp index 089f6442..6b26a65b 100644 --- a/test/cpp/asyncworkererror.cpp +++ b/test/cpp/asyncworkererror.cpp @@ -19,7 +19,6 @@ class ErrorWorker : public NanAsyncWorker { }; NAN_METHOD(Work) { - NanScope(); NanCallback *callback = new NanCallback(args[0].As()); NanAsyncQueueWorker(new ErrorWorker(callback)); NanReturnUndefined(); diff --git a/test/cpp/bufferworkerpersistent.cpp b/test/cpp/bufferworkerpersistent.cpp index 188e0713..88be47a4 100644 --- a/test/cpp/bufferworkerpersistent.cpp +++ b/test/cpp/bufferworkerpersistent.cpp @@ -20,10 +20,6 @@ class BufferWorker : public NanAsyncWorker { , v8::Local &bufferHandle ) : NanAsyncWorker(callback), milliseconds(milliseconds) { - - NanScope(); - - /* test them all */ SaveToPersistent("buffer", bufferHandle); SaveToPersistent(NanNew("puffer"), bufferHandle); SaveToPersistent(0u, bufferHandle); @@ -53,7 +49,6 @@ class BufferWorker : public NanAsyncWorker { }; NAN_METHOD(DoSleep) { - NanScope(); v8::Local bufferHandle = args[1].As(); NanCallback *callback = new NanCallback(args[2].As()); assert(!callback->IsEmpty() && "Callback shoud not be empty"); diff --git a/test/cpp/gc.cpp b/test/cpp/gc.cpp index a5a62450..c046f5a6 100644 --- a/test/cpp/gc.cpp +++ b/test/cpp/gc.cpp @@ -20,14 +20,12 @@ NAN_GC_CALLBACK(gcEpilogueCallback) { } NAN_METHOD(Hook) { - NanScope(); NanAddGCPrologueCallback(gcPrologueCallback); NanAddGCEpilogueCallback(gcEpilogueCallback); NanReturnUndefined(); } NAN_METHOD(Check) { - NanScope(); NanReturnValue(NanNew(prologue_called && epilogue_called)); } diff --git a/test/cpp/isolatedata.cpp b/test/cpp/isolatedata.cpp index 32808053..a97a8ee5 100644 --- a/test/cpp/isolatedata.cpp +++ b/test/cpp/isolatedata.cpp @@ -13,8 +13,6 @@ struct Dummy { }; NAN_METHOD(SetAndGet) { - NanScope(); - Dummy *d0 = new Dummy; Dummy *d1 = NULL; diff --git a/test/cpp/makecallback.cpp b/test/cpp/makecallback.cpp index 6abe7710..b54c1608 100644 --- a/test/cpp/makecallback.cpp +++ b/test/cpp/makecallback.cpp @@ -30,8 +30,6 @@ MyObject::~MyObject() { } void MyObject::Init(v8::Handle exports) { - NanScope(); - // Prepare constructor template v8::Local tpl = NanNew(New); tpl->SetClassName(NanNew("MyObject")); @@ -44,22 +42,20 @@ void MyObject::Init(v8::Handle exports) { } NAN_METHOD(MyObject::New) { - NanScope(); - if (args.IsConstructCall()) { - MyObject* obj = new MyObject(); - obj->Wrap(args.This()); - NanReturnValue(args.This()); - } else { - v8::Local cons = NanNew(constructor); - NanReturnValue(cons->NewInstance()); + MyObject* obj = new MyObject(); + obj->Wrap(args.This()); + NanReturnValue(args.This()); + } + else { + v8::Local cons = NanNew(constructor); + NanReturnValue(cons->NewInstance()); } } NAN_METHOD(MyObject::CallEmit) { - NanScope(); v8::Handle argv[1] = { - NanNew("event"), // event name + NanNew("event"), // event name }; NanMakeCallback(args.This(), "emit", 1, argv); diff --git a/test/cpp/morenews.cpp b/test/cpp/morenews.cpp index 3290a3c6..86983da6 100644 --- a/test/cpp/morenews.cpp +++ b/test/cpp/morenews.cpp @@ -9,34 +9,28 @@ #include NAN_METHOD(NewNumber) { - NanScope(); NanReturnValue(NanNew(0.5)); } NAN_METHOD(NewNegativeInteger) { - NanScope(); NanReturnValue(NanNew(-1)); } NAN_METHOD(NewPositiveInteger) { - NanScope(); NanReturnValue(NanNew(1)); } NAN_METHOD(NewUtf8String) { - NanScope(); const char s[] = "strïng"; NanReturnValue(NanNew(s)); } NAN_METHOD(NewLatin1String) { - NanScope(); const uint8_t s[] = "str\xefng"; NanReturnValue(NanNew(s)); } NAN_METHOD(NewUcs2String) { - NanScope(); uint16_t s[] = {'s', 't', 'r', 0xef, 'n', 'g', '\0'}; NanReturnValue(NanNew(s)); } @@ -60,13 +54,11 @@ class ExtAsciiString : public NanExternalOneByteStringResource { }; NAN_METHOD(NewExternalStringResource) { - NanScope(); v8::Local ext = NanNew(new ExtString()); NanReturnValue(ext); } NAN_METHOD(NewExternalAsciiStringResource) { - NanScope(); v8::Local ext = NanNew(new ExtAsciiString()); NanReturnValue(ext); } diff --git a/test/cpp/multifile2.cpp b/test/cpp/multifile2.cpp index a5ec0271..7f0c0c51 100644 --- a/test/cpp/multifile2.cpp +++ b/test/cpp/multifile2.cpp @@ -9,7 +9,6 @@ #include NAN_METHOD(ReturnString) { - NanScope(); v8::Local s = NanNew(*NanUtf8String(args[0])); NanReturnValue(s); } diff --git a/test/cpp/nancallback.cpp b/test/cpp/nancallback.cpp index b99d2cbb..4b418149 100644 --- a/test/cpp/nancallback.cpp +++ b/test/cpp/nancallback.cpp @@ -8,33 +8,24 @@ #include - NAN_METHOD(GlobalContext) { - NanScope(); - NanCallback(args[0].As()).Call(0, NULL); NanReturnUndefined(); } NAN_METHOD(SpecificContext) { - NanScope(); - NanCallback cb(args[0].As()); cb.Call(NanGetCurrentContext()->Global(), 0, NULL); NanReturnUndefined(); } NAN_METHOD(CustomReceiver) { - NanScope(); - NanCallback cb(args[0].As()); cb.Call(args[1].As(), 0, NULL); NanReturnUndefined(); } NAN_METHOD(CompareCallbacks) { - NanScope(); - NanCallback cb1(args[0].As()); NanCallback cb2(args[1].As()); NanCallback cb3(args[2].As()); @@ -43,8 +34,6 @@ NAN_METHOD(CompareCallbacks) { } NAN_METHOD(CallDirect) { - NanScope(); - NanCallback cb(args[0].As()); (*cb)->Call(NanGetCurrentContext()->Global(), 0, NULL); @@ -52,8 +41,6 @@ NAN_METHOD(CallDirect) { } NAN_METHOD(CallAsFunction) { - NanScope(); - NanCallback(args[0].As())(); NanReturnUndefined(); diff --git a/test/cpp/nannew.cpp b/test/cpp/nannew.cpp index bd106df1..1c3932a0 100644 --- a/test/cpp/nannew.cpp +++ b/test/cpp/nannew.cpp @@ -52,7 +52,6 @@ stringMatches(Local value, const char * match) { #define _(e) NAN_TEST_EXPRESSION(e) NAN_METHOD(testArray) { - NanScope(); NanTap t(args[0]); t.plan(3); @@ -65,7 +64,6 @@ NAN_METHOD(testArray) { } NAN_METHOD(testBoolean) { - NanScope(); NanTap t(args[0]); t.plan(6); @@ -87,7 +85,6 @@ NAN_METHOD(testBoolean) { # define V(x) x->ValueOf() #endif NAN_METHOD(testBooleanObject) { - NanScope(); NanTap t(args[0]); t.plan(3); @@ -101,7 +98,6 @@ NAN_METHOD(testBooleanObject) { #undef V NAN_METHOD(testContext) { - NanScope(); NanTap t(args[0]); t.plan(5); @@ -121,7 +117,6 @@ NAN_METHOD(testContext) { } NAN_METHOD(testDate) { - NanScope(); NanTap t(args[0]); t.plan(1); @@ -134,7 +129,6 @@ NAN_METHOD(testDate) { int ttt = 23; NAN_METHOD(testExternal) { - NanScope(); NanTap t(args[0]); t.plan(2); @@ -146,7 +140,6 @@ NAN_METHOD(testExternal) { } NAN_METHOD(testFunction) { - NanScope(); NanTap t(args[0]); t.plan(2); @@ -158,7 +151,6 @@ NAN_METHOD(testFunction) { } NAN_METHOD(testFunctionTemplate) { - NanScope(); NanTap t(args[0]); t.plan(4); @@ -179,7 +171,6 @@ NAN_METHOD(testFunctionTemplate) { const double epsilon = 1e-9; NAN_METHOD(testNumber) { - NanScope(); NanTap t(args[0]); t.plan(17); @@ -214,7 +205,6 @@ NAN_METHOD(testNumber) { } NAN_METHOD(testNumberObject) { - NanScope(); NanTap t(args[0]); t.plan(2); @@ -226,7 +216,6 @@ NAN_METHOD(testNumberObject) { } NAN_METHOD(testObject) { - NanScope(); NanTap t(args[0]); t.plan(1); @@ -237,7 +226,6 @@ NAN_METHOD(testObject) { } NAN_METHOD(testObjectTemplate) { - NanScope(); NanTap t(args[0]); t.plan(1); @@ -248,7 +236,6 @@ NAN_METHOD(testObjectTemplate) { } NAN_METHOD(testScript) { - NanScope(); NanTap t(args[0]); t.plan(6); @@ -272,7 +259,6 @@ NAN_METHOD(testScript) { } NAN_METHOD(testSignature) { - NanScope(); NanTap t(args[0]); t.plan(3); @@ -289,7 +275,6 @@ NAN_METHOD(testSignature) { } NAN_METHOD(testString) { - NanScope(); NanTap t(args[0]); t.plan(14); @@ -328,7 +313,6 @@ NAN_METHOD(testString) { # define V(x) x->ValueOf() #endif NAN_METHOD(testStringObject) { - NanScope(); NanTap t(args[0]); t.plan(2); @@ -345,7 +329,6 @@ NAN_METHOD(testStringObject) { template Handle asHandle(Local l) { return l; } NAN_METHOD(testHandles) { - NanScope(); NanTap t(args[0]); t.plan(2); @@ -357,7 +340,6 @@ NAN_METHOD(testHandles) { } NAN_METHOD(testPersistents) { - NanScope(); NanTap t(args[0]); t.plan(1); @@ -377,7 +359,6 @@ NAN_METHOD(testPersistents) { // See https://github.com/nodejs/nan/issues/212 NAN_METHOD(testRegression212) { - NanScope(); NanTap t(args[0]); t.plan(1); @@ -407,7 +388,6 @@ NAN_METHOD(overloaded) { } NAN_METHOD(testRegression242) { - NanScope(); NanTap t(args[0]); // These lines must *compile*. Not much to test at runtime. @@ -429,37 +409,30 @@ NAN_METHOD(testRegression242) { //============================================================================== NAN_METHOD(newIntegerWithValue) { - NanScope(); return_NanValue(NanNew(args[0]->Int32Value())); } NAN_METHOD(newNumberWithValue) { - NanScope(); return_NanValue(NanNew(args[0]->NumberValue())); } NAN_METHOD(newUint32WithValue) { - NanScope(); return_NanValue(NanNew(args[0]->Uint32Value())); } NAN_METHOD(newStringFromChars) { - NanScope(); return_NanValue(NanNew("hello?")); } NAN_METHOD(newStringFromCharsWithLength) { - NanScope(); return_NanValue(NanNew("hello?", 4)); } NAN_METHOD(newStringFromStdString) { - NanScope(); return_NanValue(NanNew(std::string("hello!"))); } NAN_METHOD(newExternal) { - NanScope(); return_NanValue(NanNew(&ttt)); } diff --git a/test/cpp/news.cpp b/test/cpp/news.cpp index 1af4a9ee..04519ff1 100644 --- a/test/cpp/news.cpp +++ b/test/cpp/news.cpp @@ -19,98 +19,80 @@ static int magic = 1337; NAN_METHOD(NewNumber) { - NanScope(); NanReturnValue(NanNew(0.5)); } NAN_METHOD(NewNegativeInteger) { - NanScope(); NanReturnValue(NanNew(-1)); } NAN_METHOD(NewPositiveInteger) { - NanScope(); NanReturnValue(NanNew(1)); } NAN_METHOD(NewUnsignedInteger) { - NanScope(); NanReturnValue(NanNew(0xFFFFFFFFu)); } NAN_METHOD(NewInt32FromPositive) { - NanScope(); NanReturnValue(NanNew(0xFFFFFFFF)); } NAN_METHOD(NewInt32FromNegative) { - NanScope(); NanReturnValue(NanNew(-1)); } NAN_METHOD(NewUint32FromPositive) { - NanScope(); NanReturnValue(NanNew(0xFFFFFFFF)); } NAN_METHOD(NewUint32FromNegative) { - NanScope(); NanReturnValue(NanNew(-1)); } NAN_METHOD(NewUtf8String) { - NanScope(); const char s[] = "strïng"; NanReturnValue(NanNew(s)); } NAN_METHOD(NewLatin1String) { - NanScope(); const uint8_t s[] = "str\xefng"; NanReturnValue(NanNew(s)); } NAN_METHOD(NewUcs2String) { - NanScope(); const uint16_t s[] = {'s', 't', 'r', 0xef, 'n', 'g', '\0'}; NanReturnValue(NanNew(s)); } NAN_METHOD(NewStdString) { - NanScope(); const std::string s = "strïng"; NanReturnValue(NanNew(s)); } NAN_METHOD(NewRegExp) { - NanScope(); NanReturnValue(NanNew(NanNew("foo"), v8::RegExp::kNone)); } NAN_METHOD(NewStringObject) { - NanScope(); NanReturnValue(NanNew(NanNew("foo"))); } NAN_METHOD(NewNumberObject) { - NanScope(); NanReturnValue(NanNew(0.5)); } NAN_METHOD(NewBooleanObject) { - NanScope(); NanReturnValue(NanNew(true)); } NAN_METHOD(NewExternal) { - NanScope(); v8::Local ext = NanNew(&magic); assert(*static_cast(ext->Value()) == 1337); NanReturnValue(NanNew("passed")); } NAN_METHOD(NewSignature) { - NanScope(); v8::Local tmpl = NanNew(NewSignature); v8::Local sig = NanNew(tmpl); @@ -120,13 +102,11 @@ NAN_METHOD(NewSignature) { } NAN_METHOD(NewScript) { - NanScope(); v8::Local script = NanNew(NanNew("2+4")); NanReturnValue(NanRunScript(script)->ToInt32()); } NAN_METHOD(NewScript2) { - NanScope(); v8::ScriptOrigin origin(NanNew("x")); v8::Local script = NanNew(NanNew("2+4"), origin); @@ -134,37 +114,30 @@ NAN_METHOD(NewScript2) { } NAN_METHOD(CompileScript) { - NanScope(); v8::Local script = NanCompileScript(NanNew("2+4")); NanReturnValue(NanRunScript(script)->ToInt32()); } NAN_METHOD(CompileScript2) { - NanScope(); v8::ScriptOrigin origin(NanNew("x")); v8::Local script = NanCompileScript(NanNew("2+4"), origin); NanReturnValue(NanRunScript(script)->ToInt32()); } NAN_METHOD(NewDate) { - NanScope(); NanReturnValue(NanNew(1337)); } NAN_METHOD(NewArray) { - NanScope(); NanReturnValue(NanNew()); } NAN_METHOD(NewBoolean) { - NanScope(); NanReturnValue(NanNew(true)); } // #212 NAN_METHOD(NewBoolean2) { - NanScope(); - #if defined(_MSC_VER) # pragma warning( push ) # pragma warning( disable : 4800 ) diff --git a/test/cpp/objectwraphandle.cpp b/test/cpp/objectwraphandle.cpp index da25bc25..7f9fc54f 100644 --- a/test/cpp/objectwraphandle.cpp +++ b/test/cpp/objectwraphandle.cpp @@ -11,7 +11,6 @@ class MyObject : public node::ObjectWrap { public: static void Init(v8::Handle exports) { - NanScope(); v8::Local tpl = NanNew(New); tpl->SetClassName(NanNew("MyObject")); tpl->InstanceTemplate()->SetInternalFieldCount(1); @@ -27,8 +26,6 @@ class MyObject : public node::ObjectWrap { ~MyObject() {} static NAN_METHOD(New) { - NanScope(); - if (args.IsConstructCall()) { double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue(); MyObject *obj = new MyObject(value); @@ -43,7 +40,6 @@ class MyObject : public node::ObjectWrap { } static NAN_METHOD(GetHandle) { - NanScope(); MyObject* obj = node::ObjectWrap::Unwrap(args.This()); NanReturnValue(NanObjectWrapHandle(obj)); } diff --git a/test/cpp/persistent.cpp b/test/cpp/persistent.cpp index 6d4b3f1c..92a1d24e 100644 --- a/test/cpp/persistent.cpp +++ b/test/cpp/persistent.cpp @@ -12,48 +12,34 @@ static v8::Persistent persistentTest1; NAN_METHOD(Save1) { - NanScope(); - NanAssignPersistent(persistentTest1, args[0].As()); - NanReturnUndefined(); } NAN_METHOD(Get1) { - NanScope(); - NanReturnValue(NanNew(persistentTest1)); } NAN_METHOD(Dispose1) { - NanScope(); - NanDisposePersistent(persistentTest1); - NanReturnUndefined(); } NAN_METHOD(ToPersistentAndBackAgain) { - NanScope(); - v8::Persistent persistent; NanAssignPersistent(persistent, args[0].As()); v8::Local object = NanNew(persistent); NanDisposePersistent(persistent); memset(&persistent, -1, sizeof(persistent)); // Clobber it good. - NanReturnValue(object); } NAN_METHOD(PersistentToPersistent) { - NanScope(); - v8::Persistent persistent; NanAssignPersistent(persistent, args[0].As()); NanAssignPersistent(persistentTest1, persistent); NanDisposePersistent(persistent); NanDisposePersistent(persistentTest1); - NanReturnUndefined(); } diff --git a/test/cpp/returnemptystring.cpp b/test/cpp/returnemptystring.cpp index 8b1cca08..1f37033c 100644 --- a/test/cpp/returnemptystring.cpp +++ b/test/cpp/returnemptystring.cpp @@ -9,7 +9,6 @@ #include NAN_METHOD(ReturnEmptyString) { - NanScope(); NanReturnEmptyString(); } diff --git a/test/cpp/returnnull.cpp b/test/cpp/returnnull.cpp index 2c774c3e..c283cd5a 100644 --- a/test/cpp/returnnull.cpp +++ b/test/cpp/returnnull.cpp @@ -9,7 +9,6 @@ #include NAN_METHOD(ReturnNull) { - NanScope(); NanReturnNull(); } diff --git a/test/cpp/returnundefined.cpp b/test/cpp/returnundefined.cpp index 38721ce5..2463f2de 100644 --- a/test/cpp/returnundefined.cpp +++ b/test/cpp/returnundefined.cpp @@ -9,7 +9,6 @@ #include NAN_METHOD(ReturnUndefined) { - NanScope(); NanReturnUndefined(); } diff --git a/test/cpp/returnvalue.cpp b/test/cpp/returnvalue.cpp index 5cc6be23..69874845 100644 --- a/test/cpp/returnvalue.cpp +++ b/test/cpp/returnvalue.cpp @@ -11,7 +11,6 @@ v8::Persistent persistent; NAN_METHOD(ReturnValue) { - NanScope(); if (args.Length() == 1) { NanReturnValue(args[0]); } else { @@ -20,17 +19,14 @@ NAN_METHOD(ReturnValue) { } NAN_METHOD(ReturnPrimitive) { - NanScope(); NanReturnValue(true); } NAN_METHOD(ReturnString) { - NanScope(); NanReturnValue("yes, it works"); } NAN_METHOD(ReturnPersistent) { - NanScope(); NanReturnValue(persistent); } diff --git a/test/cpp/settemplate.cpp b/test/cpp/settemplate.cpp index 32fc30b3..79a87880 100644 --- a/test/cpp/settemplate.cpp +++ b/test/cpp/settemplate.cpp @@ -29,8 +29,6 @@ MyObject::~MyObject() { } void MyObject::Init(v8::Handle exports) { - NanScope(); - // Prepare constructor template v8::Local tpl = NanNew(New); tpl->SetClassName(NanNew("MyObject")); @@ -55,8 +53,6 @@ void MyObject::Init(v8::Handle exports) { } NAN_METHOD(MyObject::New) { - NanScope(); - if (args.IsConstructCall()) { MyObject* obj = new MyObject(); obj->Wrap(args.This()); diff --git a/test/cpp/settergetter.cpp b/test/cpp/settergetter.cpp index 1b1e6470..833a990f 100644 --- a/test/cpp/settergetter.cpp +++ b/test/cpp/settergetter.cpp @@ -29,7 +29,6 @@ class SetterGetter : public node::ObjectWrap { static v8::Persistent settergetter_constructor; NAN_METHOD(CreateNew) { - NanScope(); NanReturnValue(SetterGetter::NewInstance()); } @@ -61,16 +60,15 @@ void SetterGetter::Init(v8::Handle target) { } v8::Handle SetterGetter::NewInstance () { + NanEscapableScope(); v8::Local constructorHandle = NanNew(settergetter_constructor); v8::Local instance = constructorHandle->GetFunction()->NewInstance(0, NULL); - return instance; + return NanEscapeScope(instance); } NAN_METHOD(SetterGetter::New) { - NanScope(); - SetterGetter* settergetter = new SetterGetter(); assert(strlen(settergetter->log) < sizeof (settergetter->log)); strncat( @@ -83,8 +81,6 @@ NAN_METHOD(SetterGetter::New) { } NAN_GETTER(SetterGetter::GetProp1) { - NanScope(); - SetterGetter* settergetter = node::ObjectWrap::Unwrap(args.This()); assert(strlen(settergetter->log) < sizeof (settergetter->log)); @@ -107,8 +103,6 @@ NAN_GETTER(SetterGetter::GetProp1) { } NAN_GETTER(SetterGetter::GetProp2) { - NanScope(); - SetterGetter* settergetter = node::ObjectWrap::Unwrap(args.This()); assert(strlen(settergetter->log) < sizeof (settergetter->log)); @@ -131,8 +125,6 @@ NAN_GETTER(SetterGetter::GetProp2) { } NAN_SETTER(SetterGetter::SetProp2) { - NanScope(); - SetterGetter* settergetter = node::ObjectWrap::Unwrap(args.This()); strncpy( @@ -158,8 +150,6 @@ NAN_SETTER(SetterGetter::SetProp2) { } NAN_METHOD(SetterGetter::Log) { - NanScope(); - SetterGetter* settergetter = node::ObjectWrap::Unwrap(args.This()); diff --git a/test/cpp/strings.cpp b/test/cpp/strings.cpp index 4a1f6e07..305aee63 100644 --- a/test/cpp/strings.cpp +++ b/test/cpp/strings.cpp @@ -9,22 +9,18 @@ #include NAN_METHOD(ReturnAsciiString) { - NanScope(); NanReturnValue(NanNew(*NanAsciiString(args[0]))); } NAN_METHOD(ReturnUtf8String) { - NanScope(); NanReturnValue(NanNew(*NanUtf8String(args[0]))); } NAN_METHOD(ReturnUcs2String) { - NanScope(); NanReturnValue(NanNew(*NanUcs2String(args[0]))); } NAN_METHOD(HeapString) { - NanScope(); NanUcs2String *s = new NanUcs2String(args[0]); v8::Local res = NanNew(**s); delete s; @@ -32,12 +28,10 @@ NAN_METHOD(HeapString) { } NAN_METHOD(EncodeHex) { - NanScope(); NanReturnValue(NanEncode("hello", 5, Nan::HEX)); } NAN_METHOD(EncodeUCS2) { - NanScope(); NanReturnValue(NanEncode("h\0e\0l\0l\0o\0", 10, Nan::UCS2)); } @@ -49,8 +43,6 @@ v8::Persistent encodeHex_persistent; v8::Persistent encodeUCS2_persistent; void Init (v8::Handle target) { - NanScope(); - v8::Local returnAsciiString = NanNew(ReturnAsciiString); diff --git a/test/cpp/threadlocal.cpp b/test/cpp/threadlocal.cpp index e0d63794..466e7bb7 100644 --- a/test/cpp/threadlocal.cpp +++ b/test/cpp/threadlocal.cpp @@ -16,6 +16,7 @@ class TlsTest : public NanAsyncWorker { public: explicit TlsTest(NanTap *t) : NanAsyncWorker(NULL), t(t), i(0) { NanScope(); + t->plan(7); t->ok(_(0 == nauv_key_create(&tls_key))); t->ok(_(NULL == nauv_key_get(&tls_key))); @@ -53,7 +54,6 @@ class TlsTest : public NanAsyncWorker { }; NAN_METHOD(thread_local_storage) { - NanScope(); NanTap *t = new NanTap(args[0]); NanAsyncQueueWorker(new TlsTest(t)); return_NanUndefined(); diff --git a/test/cpp/weak.cpp b/test/cpp/weak.cpp index 69f47e96..f202e132 100644 --- a/test/cpp/weak.cpp +++ b/test/cpp/weak.cpp @@ -28,7 +28,6 @@ v8::Handle wrap(v8::Local func) { } NAN_METHOD(Hustle) { - NanScope(); NanReturnValue(wrap(args[0].As())); } From 464f1e17fdf7d016c7a9e781e1caabf2b53ce866 Mon Sep 17 00:00:00 2001 From: Benjamin Byholm Date: Sat, 9 May 2015 16:57:13 +0300 Subject: [PATCH 07/84] Replaced NanObjectWrapHandle with class NanObjectWrap --- nan.h | 20 ++++++++++---------- test/cpp/objectwraphandle.cpp | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/nan.h b/nan.h index b9928928..caf22bdd 100644 --- a/nan.h +++ b/nan.h @@ -387,11 +387,6 @@ namespace Nan { namespace imp { # define NanReturnNull() return args.GetReturnValue().SetNull() # define NanReturnEmptyString() return args.GetReturnValue().SetEmptyString() - NAN_INLINE - v8::Local NanObjectWrapHandle(const node::ObjectWrap *obj) { - return const_cast(obj)->handle(); - } - NAN_INLINE v8::Local NanUndefined() { NanEscapableScope(); return NanEscapeScope(NanNew(v8::Undefined(v8::Isolate::GetCurrent()))); @@ -811,11 +806,6 @@ namespace Nan { namespace imp { # define NanReturnNull() return v8::Null() # define NanReturnEmptyString() return v8::String::Empty() - NAN_INLINE - v8::Local NanObjectWrapHandle(const node::ObjectWrap *obj) { - return v8::Local::New(obj->handle_); - } - NAN_INLINE v8::Local NanUndefined() { NanEscapableScope(); return NanEscapeScope(NanNew(v8::Undefined())); @@ -1725,6 +1715,16 @@ NAN_INLINE void NanSetInstanceTemplate( NanSetTemplate(templ->InstanceTemplate(), name, value, attributes); } +//=== ObjectWrap =============================================================== + +class NanObjectWrap : public node::ObjectWrap { +#if NODE_MODULE_VERSION < NODE_0_12_MODULE_VERSION + public: + inline v8::Local handle() { return NanNew(handle_); } + inline v8::Persistent &persistent() { return handle_; } +#endif +}; + //=== Weak Persistent Handling ================================================= #if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION diff --git a/test/cpp/objectwraphandle.cpp b/test/cpp/objectwraphandle.cpp index 7f9fc54f..a2b33897 100644 --- a/test/cpp/objectwraphandle.cpp +++ b/test/cpp/objectwraphandle.cpp @@ -8,7 +8,7 @@ #include -class MyObject : public node::ObjectWrap { +class MyObject : public NanObjectWrap { public: static void Init(v8::Handle exports) { v8::Local tpl = NanNew(New); @@ -40,8 +40,8 @@ class MyObject : public node::ObjectWrap { } static NAN_METHOD(GetHandle) { - MyObject* obj = node::ObjectWrap::Unwrap(args.This()); - NanReturnValue(NanObjectWrapHandle(obj)); + MyObject* obj = NanObjectWrap::Unwrap(args.This()); + NanReturnValue(obj->handle()); } static v8::Persistent constructor; From 47751c4f904e2f1e13244a8cd86d63ee427a69af Mon Sep 17 00:00:00 2001 From: Benjamin Byholm Date: Sat, 9 May 2015 18:27:38 +0300 Subject: [PATCH 08/84] Replace NanScope and NanEscpableScope macros with classes --- nan.h | 250 ++++++++++++++-------------- test/cpp/asyncprogressworker.cpp | 2 +- test/cpp/bufferworkerpersistent.cpp | 2 +- test/cpp/settergetter.cpp | 4 +- test/cpp/threadlocal.cpp | 2 +- test/cpp/weak.cpp | 4 +- 6 files changed, 136 insertions(+), 128 deletions(-) diff --git a/nan.h b/nan.h index caf22bdd..caca5458 100644 --- a/nan.h +++ b/nan.h @@ -267,6 +267,74 @@ namespace Nan { namespace imp { } // end of namespace imp } // end of namespace Nan +//=== HandleScope ============================================================== + +class NanScope { + v8::HandleScope scope; + + public: +#if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION + inline NanScope() : scope(v8::Isolate::GetCurrent()) {} + inline static int NumberOfHandles() { + return v8::HandleScope::NumberOfHandles(v8::Isolate::GetCurrent()); + } +#else + inline NanScope() : scope() {} + inline static int NumberOfHandles() { + return v8::HandleScope::NumberOfHandles(); + } +#endif + + private: + // Make it hard to create heap-allocated or illegal handle scopes by + // disallowing certain operations. + NanScope(const NanScope &); + void operator=(const NanScope &); + void *operator new(size_t size); + void operator delete(void *, size_t); +}; + +class NanEscapableScope { + public: +#if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION + inline NanEscapableScope() : scope(v8::Isolate::GetCurrent()) {} + + inline static int NumberOfHandles() { + return v8::EscapableHandleScope::NumberOfHandles(v8::Isolate::GetCurrent()); + } + + template + inline v8::Local Escape(v8::Local value) { + return scope.Escape(value); + } + + private: + v8::EscapableHandleScope scope; +#else + inline NanEscapableScope() : scope() {} + + inline static int NumberOfHandles() { + return v8::HandleScope::NumberOfHandles(); + } + + template + inline v8::Local Escape(v8::Local value) { + return scope.Close(value); + } + + private: + v8::HandleScope scope; +#endif + + private: + // Make it hard to create heap-allocated or illegal handle scopes by + // disallowing certain operations. + NanEscapableScope(const NanEscapableScope &); + void operator=(const NanEscapableScope &); + void *operator new(size_t size); + void operator delete(void *, size_t); +}; + /* node 0.12 */ #if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION NAN_INLINE @@ -372,11 +440,6 @@ namespace Nan { namespace imp { NAN_INDEX_QUERY_ARGS_TYPE; typedef void NAN_INDEX_QUERY_RETURN_TYPE; -# define NanScope() v8::HandleScope scope(v8::Isolate::GetCurrent()) -# define NanEscapableScope() \ - v8::EscapableHandleScope scope(v8::Isolate::GetCurrent()) - -# define NanEscapeScope(val) scope.Escape(Nan::imp::NanEnsureLocal(val)) # define NanLocker() v8::Locker locker(v8::Isolate::GetCurrent()) # define NanUnlocker() v8::Unlocker unlocker(v8::Isolate::GetCurrent()) # define NanReturnValue(value) \ @@ -388,23 +451,23 @@ namespace Nan { namespace imp { # define NanReturnEmptyString() return args.GetReturnValue().SetEmptyString() NAN_INLINE v8::Local NanUndefined() { - NanEscapableScope(); - return NanEscapeScope(NanNew(v8::Undefined(v8::Isolate::GetCurrent()))); + NanEscapableScope scope; + return scope.Escape(NanNew(v8::Undefined(v8::Isolate::GetCurrent()))); } NAN_INLINE v8::Local NanNull() { - NanEscapableScope(); - return NanEscapeScope(NanNew(v8::Null(v8::Isolate::GetCurrent()))); + NanEscapableScope scope; + return scope.Escape(NanNew(v8::Null(v8::Isolate::GetCurrent()))); } NAN_INLINE v8::Local NanTrue() { - NanEscapableScope(); - return NanEscapeScope(NanNew(v8::True(v8::Isolate::GetCurrent()))); + NanEscapableScope scope; + return scope.Escape(NanNew(v8::True(v8::Isolate::GetCurrent()))); } NAN_INLINE v8::Local NanFalse() { - NanEscapableScope(); - return NanEscapeScope(NanNew(v8::False(v8::Isolate::GetCurrent()))); + NanEscapableScope scope; + return scope.Escape(NanNew(v8::False(v8::Isolate::GetCurrent()))); } NAN_INLINE int NanAdjustExternalMemory(int bc) { @@ -490,8 +553,8 @@ namespace Nan { namespace imp { # define X(NAME) \ NAN_INLINE v8::Local Nan ## NAME(const char *errmsg) { \ - NanEscapableScope(); \ - return NanEscapeScope(v8::Exception::NAME(NanNew(errmsg))); \ + NanEscapableScope scope; \ + return scope.Escape(v8::Exception::NAME(NanNew(errmsg))); \ } \ \ NAN_INLINE \ @@ -500,13 +563,13 @@ namespace Nan { namespace imp { } \ \ NAN_INLINE void NanThrow ## NAME(const char *errmsg) { \ - NanScope(); \ + NanScope scope; \ v8::Isolate::GetCurrent()->ThrowException( \ v8::Exception::NAME(NanNew(errmsg))); \ } \ \ NAN_INLINE void NanThrow ## NAME(v8::Handle errmsg) { \ - NanScope(); \ + NanScope scope; \ v8::Isolate::GetCurrent()->ThrowException( \ v8::Exception::NAME(NanNew(errmsg))); \ } @@ -523,31 +586,6 @@ namespace Nan { namespace imp { v8::Isolate::GetCurrent()->ThrowException(error); } - namespace Nan { namespace imp { - NAN_INLINE v8::Local E(const char *msg, const int errorNumber) { - NanEscapableScope(); - v8::Local err = v8::Exception::Error(NanNew(msg)); - v8::Local obj = err.As(); - obj->Set(NanNew("code"), NanNew(errorNumber)); - return NanEscapeScope(err); - } - } // end of namespace imp - } // end of namespace Nan - - NAN_DEPRECATED NAN_INLINE v8::Local NanError( - const char *msg - , const int errorNumber - ) { - return Nan::imp::E(msg, errorNumber); - } - - NAN_DEPRECATED NAN_INLINE void NanThrowError( - const char *msg - , const int errorNumber - ) { - NanThrowError(Nan::imp::E(msg, errorNumber)); - } - template NAN_INLINE void NanDisposePersistent( v8::Persistent &handle ) { @@ -793,9 +831,6 @@ namespace Nan { namespace imp { typedef const v8::AccessorInfo& NAN_INDEX_QUERY_ARGS_TYPE; typedef v8::Handle NAN_INDEX_QUERY_RETURN_TYPE; -# define NanScope() v8::HandleScope scope -# define NanEscapableScope() v8::HandleScope scope -# define NanEscapeScope(val) scope.Close(val) # define NanLocker() v8::Locker locker # define NanUnlocker() v8::Unlocker unlocker # define NanReturnValue(value) \ @@ -807,23 +842,23 @@ namespace Nan { namespace imp { # define NanReturnEmptyString() return v8::String::Empty() NAN_INLINE v8::Local NanUndefined() { - NanEscapableScope(); - return NanEscapeScope(NanNew(v8::Undefined())); + NanEscapableScope scope; + return scope.Escape(NanNew(v8::Undefined())); } NAN_INLINE v8::Local NanNull() { - NanEscapableScope(); - return NanEscapeScope(NanNew(v8::Null())); + NanEscapableScope scope; + return scope.Escape(NanNew(v8::Null())); } NAN_INLINE v8::Local NanTrue() { - NanEscapableScope(); - return NanEscapeScope(NanNew(v8::True())); + NanEscapableScope scope; + return scope.Escape(NanNew(v8::True())); } NAN_INLINE v8::Local NanFalse() { - NanEscapableScope(); - return NanEscapeScope(NanNew(v8::False())); + NanEscapableScope scope; + return scope.Escape(NanNew(v8::False())); } NAN_INLINE int NanAdjustExternalMemory(int bc) { @@ -898,8 +933,8 @@ namespace Nan { namespace imp { # define X(NAME) \ NAN_INLINE v8::Local Nan ## NAME(const char *errmsg) { \ - NanEscapableScope(); \ - return NanEscapeScope(v8::Exception::NAME(NanNew(errmsg))); \ + NanEscapableScope scope; \ + return scope.Escape(v8::Exception::NAME(NanNew(errmsg))); \ } \ \ NAN_INLINE \ @@ -908,15 +943,15 @@ namespace Nan { namespace imp { } \ \ NAN_INLINE v8::Local NanThrow ## NAME(const char *errmsg) { \ - NanEscapableScope(); \ - return NanEscapeScope(NanNew(v8::ThrowException( \ + NanEscapableScope scope; \ + return scope.Escape(NanNew(v8::ThrowException( \ v8::Exception::NAME(NanNew(errmsg))))); \ } \ \ NAN_INLINE \ v8::Local NanThrow ## NAME(v8::Handle errmsg) { \ - NanEscapableScope(); \ - return NanEscapeScope(NanNew(v8::ThrowException( \ + NanEscapableScope scope; \ + return scope.Escape(NanNew(v8::ThrowException( \ v8::Exception::NAME(errmsg)))); \ } @@ -929,33 +964,8 @@ namespace Nan { namespace imp { # undef X NAN_INLINE v8::Local NanThrowError(v8::Handle error) { - NanEscapableScope(); - return NanEscapeScope(v8::Local::New(v8::ThrowException(error))); - } - - namespace Nan { namespace imp { - NAN_INLINE v8::Local E(const char *msg, const int errorNumber) { - NanEscapableScope(); - v8::Local err = v8::Exception::Error(NanNew(msg)); - v8::Local obj = err.As(); - obj->Set(NanNew("code"), NanNew(errorNumber)); - return NanEscapeScope(err); - } - } // end of namespace imp - } // end of namespace Nan - - NAN_DEPRECATED NAN_INLINE v8::Local NanError( - const char *msg - , const int errorNumber - ) { - return Nan::imp::E(msg, errorNumber); - } - - NAN_DEPRECATED NAN_INLINE v8::Local NanThrowError( - const char *msg - , const int errorNumber - ) { - return NanThrowError(Nan::imp::E(msg, errorNumber)); + NanEscapableScope scope; + return scope.Escape(v8::Local::New(v8::ThrowException(error))); } template @@ -1219,13 +1229,13 @@ typedef void (*NanFreeCallback)(char *data, void *hint); class NanCallback { public: NanCallback() { - NanScope(); + NanScope scope; v8::Local obj = NanNew(); NanAssignPersistent(handle, obj); } explicit NanCallback(const v8::Handle &fn) { - NanScope(); + NanScope scope; v8::Local obj = NanNew(); NanAssignPersistent(handle, obj); SetFunction(fn); @@ -1237,7 +1247,7 @@ class NanCallback { } bool operator==(const NanCallback &other) const { - NanScope(); + NanScope scope; v8::Local a = NanNew(handle)->Get(kCallbackIndex); v8::Local b = NanNew(other.handle)->Get(kCallbackIndex); return a->StrictEquals(b); @@ -1264,18 +1274,18 @@ class NanCallback { } NAN_INLINE void SetFunction(const v8::Handle &fn) { - NanScope(); + NanScope scope; NanNew(handle)->Set(kCallbackIndex, fn); } NAN_INLINE v8::Local GetFunction() const { - NanEscapableScope(); - return NanEscapeScope(NanNew(handle)->Get(kCallbackIndex) + NanEscapableScope scope; + return scope.Escape(NanNew(handle)->Get(kCallbackIndex) .As()); } NAN_INLINE bool IsEmpty() const { - NanScope(); + NanScope scope; return NanNew(handle)->Get(kCallbackIndex)->IsUndefined(); } @@ -1311,11 +1321,11 @@ class NanCallback { , v8::Handle target , int argc , v8::Handle argv[]) const { - NanEscapableScope(); + NanEscapableScope scope; v8::Local callback = NanNew(handle)-> Get(kCallbackIndex).As(); - return NanEscapeScope(NanNew(node::MakeCallback( + return scope.Escape(Nan::imp::NanEnsureLocal(node::MakeCallback( isolate , target , callback @@ -1327,16 +1337,16 @@ class NanCallback { v8::Local Call_(v8::Handle target , int argc , v8::Handle argv[]) const { - NanEscapableScope(); + NanEscapableScope scope; v8::Local callback = handle-> Get(kCallbackIndex).As(); - return NanEscapeScope(NanNew(node::MakeCallback( + return scope.Escape(Nan::imp::NanEnsureLocal(node::MakeCallback( target , callback , argc , argv - )); + ))); } #endif }; @@ -1347,13 +1357,13 @@ class NanCallback { : callback(callback_), errmsg_(NULL) { request.data = this; - NanScope(); + NanScope scope; v8::Local obj = NanNew(); NanAssignPersistent(persistentHandle, obj); } virtual ~NanAsyncWorker() { - NanScope(); + NanScope scope; if (!persistentHandle.IsEmpty()) NanDisposePersistent(persistentHandle); @@ -1364,7 +1374,7 @@ class NanCallback { } virtual void WorkComplete() { - NanScope(); + NanScope scope; if (errmsg_ == NULL) HandleOKCallback(); @@ -1375,39 +1385,37 @@ class NanCallback { } NAN_INLINE void SaveToPersistent( - const char *key, const v8::Local &obj) { - NanScope(); - NanNew(persistentHandle)->Set(NanNew(key), obj); + const char *key, const v8::Local &value) { + NanScope scope; + NanNew(persistentHandle)->Set(NanNew(key), value); } NAN_INLINE void SaveToPersistent( - const v8::Handle &key, const v8::Local &obj) { - NanScope(); - NanNew(persistentHandle)->Set(key, obj); + const v8::Handle &key, const v8::Local &value) { + NanScope scope; + NanNew(persistentHandle)->Set(key, value); } NAN_INLINE void SaveToPersistent( - uint32_t index, const v8::Local &obj) { - NanScope(); - NanNew(persistentHandle)->Set(index, obj); + uint32_t index, const v8::Local &value) { + NanScope scope; + NanNew(persistentHandle)->Set(index, value); } - NAN_INLINE v8::Local GetFromPersistent(const char *key) const { - NanEscapableScope(); - return NanEscapeScope( - NanNew(persistentHandle)->Get(NanNew(key)).As()); + NAN_INLINE v8::Local GetFromPersistent(const char *key) const { + NanEscapableScope scope; + return scope.Escape(NanNew(persistentHandle)->Get(NanNew(key))); } NAN_INLINE v8::Local GetFromPersistent(const v8::Local &key) const { - NanEscapableScope(); - return NanEscapeScope(NanNew(persistentHandle)->Get(key).As()); + NanEscapableScope scope; + return scope.Escape(NanNew(persistentHandle)->Get(key)); } - NAN_INLINE v8::Local GetFromPersistent(uint32_t index) const { - NanEscapableScope(); - return NanEscapeScope( - NanNew(persistentHandle)->Get(index).As()); + NAN_INLINE v8::Local GetFromPersistent(uint32_t index) const { + NanEscapableScope scope; + return scope.Escape(NanNew(persistentHandle)->Get(index)); } virtual void Execute() = 0; @@ -1427,7 +1435,7 @@ class NanCallback { } virtual void HandleErrorCallback() { - NanScope(); + NanScope scope; v8::Local argv[] = { v8::Exception::Error(NanNew(ErrorMessage())) diff --git a/test/cpp/asyncprogressworker.cpp b/test/cpp/asyncprogressworker.cpp index 68c4a5aa..0639aac8 100644 --- a/test/cpp/asyncprogressworker.cpp +++ b/test/cpp/asyncprogressworker.cpp @@ -31,7 +31,7 @@ class ProgressWorker : public NanAsyncProgressWorker { } void HandleProgressCallback(const char *data, size_t size) { - NanScope(); + NanScope scope; v8::Local argv[] = { NanNew(*reinterpret_cast(const_cast(data))) diff --git a/test/cpp/bufferworkerpersistent.cpp b/test/cpp/bufferworkerpersistent.cpp index 88be47a4..9fe5a46c 100644 --- a/test/cpp/bufferworkerpersistent.cpp +++ b/test/cpp/bufferworkerpersistent.cpp @@ -32,7 +32,7 @@ class BufferWorker : public NanAsyncWorker { } void HandleOKCallback () { - NanScope(); + NanScope scope; v8::Local handle = GetFromPersistent("buffer"); callback->Call(1, &handle); diff --git a/test/cpp/settergetter.cpp b/test/cpp/settergetter.cpp index 833a990f..875db98c 100644 --- a/test/cpp/settergetter.cpp +++ b/test/cpp/settergetter.cpp @@ -60,12 +60,12 @@ void SetterGetter::Init(v8::Handle target) { } v8::Handle SetterGetter::NewInstance () { - NanEscapableScope(); + NanEscapableScope scope; v8::Local constructorHandle = NanNew(settergetter_constructor); v8::Local instance = constructorHandle->GetFunction()->NewInstance(0, NULL); - return NanEscapeScope(instance); + return scope.Escape(instance); } NAN_METHOD(SetterGetter::New) { diff --git a/test/cpp/threadlocal.cpp b/test/cpp/threadlocal.cpp index 466e7bb7..1680e0bf 100644 --- a/test/cpp/threadlocal.cpp +++ b/test/cpp/threadlocal.cpp @@ -31,7 +31,7 @@ class TlsTest : public NanAsyncWorker { ok(_(NULL == nauv_key_get(&tls_key))); } void WorkComplete() { - NanScope(); + NanScope scope; for (unsigned j = 0; j < i; ++j) t->ok(res[j].ok, res[j].msg); nauv_key_delete(&tls_key); diff --git a/test/cpp/weak.cpp b/test/cpp/weak.cpp index f202e132..fc4248e3 100644 --- a/test/cpp/weak.cpp +++ b/test/cpp/weak.cpp @@ -20,11 +20,11 @@ NanWeakCallbackData & data) { // NOLINT(runtime/references) } v8::Handle wrap(v8::Local func) { - NanEscapableScope(); + NanEscapableScope scope; v8::Local lstring = NanNew("result"); int *parameter = new int(0); NanMakeWeakPersistent(func, parameter, weakCallback); - return NanEscapeScope(lstring); + return scope.Escape(lstring); } NAN_METHOD(Hustle) { From dd6e4013914bb8f6b6bb650e4f0eba48b0864116 Mon Sep 17 00:00:00 2001 From: Benjamin Byholm Date: Sat, 9 May 2015 18:49:48 +0300 Subject: [PATCH 09/84] Remove NanLocker and NanUnlocker --- README.md | 28 ---------------------------- nan.h | 4 ---- 2 files changed, 32 deletions(-) diff --git a/README.md b/README.md index 6cb0acc3..5d24595b 100644 --- a/README.md +++ b/README.md @@ -285,8 +285,6 @@ NAN_METHOD(CalculateAsync) { * NanScope * NanEscapableScope * NanEscapeScope - * NanLocker - * NanUnlocker * NanGetInternalFieldPointer * NanSetInternalFieldPointer * NanObjectWrapHandle @@ -667,32 +665,6 @@ Handle Foo::Bar() { ### Local<T> NanEscapeScope(Handle<T> value); Use together with `NanEscapableScope` to escape the scope. Corresponds to `HandleScope::Close` or `EscapableHandleScope::Escape`. - -### NanLocker() - -The introduction of `isolate` references for many V8 calls in Node 0.11 makes `NanLocker()` necessary, use it in place of `Locker locker`: - -```c++ -NAN_METHOD(Foo::Bar) { - NanLocker(); - ... - NanUnlocker(); -} -``` - - -### NanUnlocker() - -The introduction of `isolate` references for many V8 calls in Node 0.11 makes `NanUnlocker()` necessary, use it in place of `Unlocker unlocker`: - -```c++ -NAN_METHOD(Foo::Bar) { - NanLocker(); - ... - NanUnlocker(); -} -``` - ### void * NanGetInternalFieldPointer(Handle<Object>, int) diff --git a/nan.h b/nan.h index caca5458..7dbd6faf 100644 --- a/nan.h +++ b/nan.h @@ -440,8 +440,6 @@ class NanEscapableScope { NAN_INDEX_QUERY_ARGS_TYPE; typedef void NAN_INDEX_QUERY_RETURN_TYPE; -# define NanLocker() v8::Locker locker(v8::Isolate::GetCurrent()) -# define NanUnlocker() v8::Unlocker unlocker(v8::Isolate::GetCurrent()) # define NanReturnValue(value) \ return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) # define NanReturnUndefined() return @@ -831,8 +829,6 @@ class NanEscapableScope { typedef const v8::AccessorInfo& NAN_INDEX_QUERY_ARGS_TYPE; typedef v8::Handle NAN_INDEX_QUERY_RETURN_TYPE; -# define NanLocker() v8::Locker locker -# define NanUnlocker() v8::Unlocker unlocker # define NanReturnValue(value) \ return Nan::imp::NanEnsureHandleOrPersistent(value) # define NanReturnHolder() NanReturnValue(args.Holder()) From b5d00a9147f25c87fad6f2813bfca04b0471f4d8 Mon Sep 17 00:00:00 2001 From: Benjamin Byholm Date: Tue, 26 May 2015 13:34:01 +0300 Subject: [PATCH 10/84] Remove string converters, except NanUtf8String, which now follows node::Utf8Value --- nan.h | 37 ++++++++++++++++++++++++++----------- test/cpp/strings.cpp | 38 +------------------------------------- test/js/strings-test.js | 14 ++++---------- 3 files changed, 31 insertions(+), 58 deletions(-) diff --git a/nan.h b/nan.h index 7dbd6faf..dd53fe7c 100644 --- a/nan.h +++ b/nan.h @@ -737,29 +737,44 @@ class NanEscapableScope { class NanUtf8String { public: - NAN_INLINE explicit NanUtf8String(v8::Handle from) { - v8::Local toStr = from->ToString(); - size = toStr->Utf8Length(); - buf = new char[size + 1]; - toStr->WriteUtf8(buf); + NAN_INLINE explicit NanUtf8String(v8::Handle from) : + length_(0), str_(str_st_) { + if (!from.IsEmpty()) { + v8::Local string = from->ToString(); + if (!string.IsEmpty()) { + size_t len = 3 * string->Length() + 1; + assert(len <= INT_MAX); + if (len > sizeof (str_st_)) { + str_ = static_cast(malloc(len)); + assert(str_ != 0); + } + const int flags = v8::String::NO_NULL_TERMINATION | + v8::String::REPLACE_INVALID_UTF8; + length_ = string->WriteUtf8(str_, static_cast(len), 0, flags); + str_[length_] = '\0'; + } + } } NAN_INLINE int length() const { - return size; + return length_; } - NAN_INLINE char* operator*() { return buf; } - NAN_INLINE const char* operator*() const { return buf; } + NAN_INLINE char* operator*() { return str_; } + NAN_INLINE const char* operator*() const { return str_; } NAN_INLINE ~NanUtf8String() { - delete[] buf; + if (str_ != str_st_) { + free(str_); + } } private: NAN_DISALLOW_ASSIGN_COPY_MOVE(NanUtf8String) - char *buf; - int size; + int length_; + char *str_; + char str_st_[1024]; }; class NanUcs2String { diff --git a/test/cpp/strings.cpp b/test/cpp/strings.cpp index 305aee63..62fc213b 100644 --- a/test/cpp/strings.cpp +++ b/test/cpp/strings.cpp @@ -8,20 +8,12 @@ #include -NAN_METHOD(ReturnAsciiString) { - NanReturnValue(NanNew(*NanAsciiString(args[0]))); -} - NAN_METHOD(ReturnUtf8String) { NanReturnValue(NanNew(*NanUtf8String(args[0]))); } -NAN_METHOD(ReturnUcs2String) { - NanReturnValue(NanNew(*NanUcs2String(args[0]))); -} - NAN_METHOD(HeapString) { - NanUcs2String *s = new NanUcs2String(args[0]); + NanUtf8String *s = new NanUtf8String(args[0]); v8::Local res = NanNew(**s); delete s; NanReturnValue(res); @@ -35,27 +27,12 @@ NAN_METHOD(EncodeUCS2) { NanReturnValue(NanEncode("h\0e\0l\0l\0o\0", 10, Nan::UCS2)); } -v8::Persistent returnAsciiString_persistent; v8::Persistent returnUtf8String_persistent; -v8::Persistent returnUcs2String_persistent; v8::Persistent heapString_persistent; v8::Persistent encodeHex_persistent; v8::Persistent encodeUCS2_persistent; void Init (v8::Handle target) { - v8::Local returnAsciiString = - NanNew(ReturnAsciiString); - - NanAssignPersistent( - returnAsciiString_persistent - , returnAsciiString - ); - - target->Set( - NanNew("returnAsciiString") - , returnAsciiString->GetFunction() - ); - v8::Local returnUtf8String = NanNew(ReturnUtf8String); @@ -69,19 +46,6 @@ void Init (v8::Handle target) { , returnUtf8String->GetFunction() ); - v8::Local returnUcs2String = - NanNew(ReturnUcs2String); - - NanAssignPersistent( - returnUcs2String_persistent - , returnUcs2String - ); - - target->Set( - NanNew("returnUcs2String") - , returnUcs2String->GetFunction() - ); - v8::Local heapString = NanNew(HeapString); diff --git a/test/js/strings-test.js b/test/js/strings-test.js index 334e61bc..c0f5c592 100644 --- a/test/js/strings-test.js +++ b/test/js/strings-test.js @@ -12,22 +12,16 @@ const test = require('tap').test test('FromV8String', function (t) { - t.plan(10); + t.plan(6); - var a = bindings.returnAsciiString; - var b = bindings.returnUtf8String; - var c = bindings.returnUcs2String; - var d = bindings.heapString; + var a = bindings.returnUtf8String; + var b = bindings.heapString; t.type(a, 'function'); t.type(b, 'function'); - t.type(c, 'function'); - t.type(d, 'function'); - t.equal(a('an ascii string'), 'an ascii string'); + t.equal(a('an utf8 strïng'), 'an utf8 strïng'); t.equal(b('an utf8 strïng'), 'an utf8 strïng'); - t.equal(c('an ucs2 strïng'), 'an ucs2 strïng'); - t.equal(d('an utf8 strïng'), 'an utf8 strïng'); t.equal(bindings.encodeHex(), new Buffer('hello').toString('hex')); t.equal(bindings.encodeUCS2(), 'hello'); From 7fed69696bd9811d508bb775eaf8bf8206537ec1 Mon Sep 17 00:00:00 2001 From: Benjamin Byholm Date: Mon, 11 May 2015 02:48:05 +0300 Subject: [PATCH 11/84] Introduce NanPersistent --- Makefile | 10 +- nan.h | 215 +++++------------ nan_implementation_12_inl.h | 9 +- nan_implementation_pre_12_inl.h | 5 + nan_new.h | 7 + nan_persistent_12_inl.h | 58 +++++ nan_persistent_pre_12_inl.h | 180 ++++++++++++++ nan_weak.h | 414 ++++++++++++++++++++++++++++++++ test/binding.gyp | 4 + test/cpp/makecallback.cpp | 6 +- test/cpp/nannew.cpp | 6 +- test/cpp/objectwraphandle.cpp | 6 +- test/cpp/persistent.cpp | 30 ++- test/cpp/returnvalue.cpp | 4 +- test/cpp/settemplate.cpp | 6 +- test/cpp/settergetter.cpp | 4 +- test/cpp/strings.cpp | 28 +-- test/cpp/weak.cpp | 20 +- test/cpp/weak2.cpp | 48 ++++ test/js/persistent-test.js | 29 ++- test/js/weak-test.js | 19 +- test/js/weak2-test.js | 36 +++ 22 files changed, 910 insertions(+), 234 deletions(-) create mode 100644 nan_persistent_12_inl.h create mode 100644 nan_persistent_pre_12_inl.h create mode 100644 nan_weak.h create mode 100644 test/cpp/weak2.cpp create mode 100644 test/js/weak2-test.js diff --git a/Makefile b/Makefile index 2b128f58..8f13a6fe 100644 --- a/Makefile +++ b/Makefile @@ -15,10 +15,13 @@ LINT_SOURCES = \ examples/async_pi_estimate/sync.cc \ examples/async_pi_estimate/sync.h \ nan.h \ - nan_string_bytes.h \ nan_implementation_12_inl.h \ nan_implementation_pre_12_inl.h \ nan_new.h \ + nan_persistent_12_inl.h \ + nan_persistent_pre_12_inl.h \ + nan_string_bytes.h \ + nan_weak.h \ test/cpp/asyncworker.cpp \ test/cpp/asyncprogressworker.cpp \ test/cpp/asyncworkererror.cpp \ @@ -46,6 +49,7 @@ LINT_SOURCES = \ test/cpp/symbols.cpp \ test/cpp/threadlocal.cpp \ test/cpp/weak.cpp \ + test/cpp/weak2.cpp \ node_modules/node-gyp/gyp/data/win/large-pdb-shim.cc FILTER = -whitespace/parens @@ -62,5 +66,7 @@ forcetest: cd test/ && node-gyp rebuild && cd .. npm test -$(ADDONS): nan.h nan_string_bytes.h nan_new.h nan_implementation_pre_12_inl.h nan_implementation_12_inl.h test/binding.gyp $(SOURCES) +$(ADDONS): nan.h nan_new.h nan_implementation_pre_12_inl.h nan_implementation_12_inl.h \ + nan_persistent_12_inl.h nan_persistent_pre_12_inl.h nan_weak.h \ + nan_string_bytes.h test/binding.gyp $(SOURCES) cd test/ && ../node_modules/.bin/node-gyp rebuild diff --git a/nan.h b/nan.h index dd53fe7c..886c1419 100644 --- a/nan.h +++ b/nan.h @@ -89,6 +89,12 @@ #define ATOM_0_21_MODULE_VERSION 41 #define IOJS_1_0_MODULE_VERSION 42 #define IOJS_1_1_MODULE_VERSION 43 +#define IOJS_2_0_MODULE_VERSION 44 + +#define TYPE_CHECK(T, S) \ + while (false) { \ + *(static_cast(0)) = static_cast(0); \ + } #if (NODE_MODULE_VERSION < NODE_0_12_MODULE_VERSION) typedef v8::InvocationCallback NanFunctionCallback; @@ -108,6 +114,28 @@ typedef v8::String::ExternalOneByteStringResource NanExternalOneByteStringResource; #endif +#if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION +template +class NanNonCopyablePersistentTraits : + public v8::NonCopyablePersistentTraits {}; +template +class NanCopyablePersistentTraits : + public v8::CopyablePersistentTraits {}; + +template +class NanPersistentBase : + public v8::PersistentBase {}; + +template > +class NanPersistent; +#else +template class NanNonCopyablePersistentTraits; +template class NanPersistentBase; +template class NanWeakCallbackData; +template > +class NanPersistent; +#endif + #include "nan_new.h" // NOLINT(build/include) // uv helpers @@ -222,6 +250,27 @@ inline void nauv_key_set(nauv_key_t* key, void* value) { template v8::Local NanNew(v8::Handle); +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) + typedef v8::WeakCallbackType NanWeakCallbackType; +#else +struct NanWeakCallbackType { + enum E {kParameter, kInternalFields}; + E type; + NanWeakCallbackType(E other) : type(other) {} // NOLINT(runtime/explicit) + inline bool operator==(E other) { return other == this->type; } + inline bool operator!=(E other) { return !operator==(other); } +}; +#endif + +template class NanWeakCallbackInfo; + +#if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION +# include "nan_persistent_12_inl.h" // NOLINT(build/include) +#else +# include "nan_persistent_pre_12_inl.h" // NOLINT(build/include) +#endif + namespace Nan { namespace imp { template NAN_INLINE v8::Persistent &NanEnsureHandleOrPersistent( @@ -535,20 +584,6 @@ class NanEscapableScope { v8::Isolate::GetCurrent()->GetHeapStatistics(heap_statistics); } - template - NAN_INLINE void NanAssignPersistent( - v8::Persistent& handle - , v8::Handle obj) { - handle.Reset(v8::Isolate::GetCurrent(), obj); - } - - template - NAN_INLINE void NanAssignPersistent( - v8::Persistent& handle - , const v8::Persistent& obj) { - handle.Reset(v8::Isolate::GetCurrent(), obj); - } - # define X(NAME) \ NAN_INLINE v8::Local Nan ## NAME(const char *errmsg) { \ NanEscapableScope scope; \ @@ -584,12 +619,6 @@ class NanEscapableScope { v8::Isolate::GetCurrent()->ThrowException(error); } - template NAN_INLINE void NanDisposePersistent( - v8::Persistent &handle - ) { - handle.Reset(); - } - NAN_INLINE v8::Local NanNewBufferHandle ( char *data , size_t length @@ -934,14 +963,6 @@ class NanEscapableScope { v8::V8::GetHeapStatistics(heap_statistics); } - template - NAN_INLINE void NanAssignPersistent( - v8::Persistent& handle - , v8::Handle obj) { - handle.Dispose(); - handle = v8::Persistent::New(obj); - } - # define X(NAME) \ NAN_INLINE v8::Local Nan ## NAME(const char *errmsg) { \ NanEscapableScope scope; \ @@ -979,13 +1000,6 @@ class NanEscapableScope { return scope.Escape(v8::Local::New(v8::ThrowException(error))); } - template - NAN_INLINE void NanDisposePersistent( - v8::Persistent &handle) { // NOLINT(runtime/references) - handle.Dispose(); - handle.Clear(); - } - NAN_INLINE v8::Local NanNewBufferHandle ( char *data , size_t length @@ -1242,19 +1256,19 @@ class NanCallback { NanCallback() { NanScope scope; v8::Local obj = NanNew(); - NanAssignPersistent(handle, obj); + handle.Reset(obj); } explicit NanCallback(const v8::Handle &fn) { NanScope scope; v8::Local obj = NanNew(); - NanAssignPersistent(handle, obj); + handle.Reset(obj); SetFunction(fn); } ~NanCallback() { if (handle.IsEmpty()) return; - NanDisposePersistent(handle); + handle.Reset(); } bool operator==(const NanCallback &other) const { @@ -1324,7 +1338,7 @@ class NanCallback { private: NAN_DISALLOW_ASSIGN_COPY_MOVE(NanCallback) - v8::Persistent handle; + NanPersistent handle; static const uint32_t kCallbackIndex = 0; #if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION) @@ -1350,7 +1364,7 @@ class NanCallback { , v8::Handle argv[]) const { NanEscapableScope scope; - v8::Local callback = handle-> + v8::Local callback = NanNew(handle)-> Get(kCallbackIndex).As(); return scope.Escape(Nan::imp::NanEnsureLocal(node::MakeCallback( target @@ -1370,14 +1384,14 @@ class NanCallback { NanScope scope; v8::Local obj = NanNew(); - NanAssignPersistent(persistentHandle, obj); + persistentHandle.Reset(obj); } virtual ~NanAsyncWorker() { NanScope scope; if (!persistentHandle.IsEmpty()) - NanDisposePersistent(persistentHandle); + persistentHandle.Reset(); if (callback) delete callback; if (errmsg_) @@ -1438,7 +1452,7 @@ class NanCallback { } protected: - v8::Persistent persistentHandle; + NanPersistent persistentHandle; NanCallback *callback; virtual void HandleOKCallback() { @@ -1746,114 +1760,13 @@ class NanObjectWrap : public node::ObjectWrap { //=== Weak Persistent Handling ================================================= -#if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION -# define NAN_WEAK_CALLBACK_DATA_TYPE_ \ - v8::WeakCallbackData > const& -# define NAN_WEAK_CALLBACK_SIG_ NAN_WEAK_CALLBACK_DATA_TYPE_ -#else -# define NAN_WEAK_CALLBACK_DATA_TYPE_ void * -# define NAN_WEAK_CALLBACK_SIG_ \ - v8::Persistent, NAN_WEAK_CALLBACK_DATA_TYPE_ -#endif - -template -class NanWeakCallbackData { - public: // constructors - typedef void (*Callback)( - NanWeakCallbackData & data // NOLINT(runtime/references) - ); - NanWeakCallbackData(v8::Handle handle, P* param, Callback cb) - : parameter(param), callback(cb) { - NanAssignPersistent(persistent, handle); - Revive(); - } - inline ~NanWeakCallbackData(); - - public: // member functions - v8::Local GetValue() const { return NanNew(persistent); } - v8::Persistent &GetPersistent() const { return persistent; } - P* GetParameter() const { return parameter; } - bool IsNearDeath() const { return persistent.IsNearDeath(); } - inline void Revive(); - - private: // constructors - NAN_DISALLOW_ASSIGN_COPY_MOVE(NanWeakCallbackData) - - private: // static member functions - static - void - invoke(NAN_WEAK_CALLBACK_SIG_ data) { - NanWeakCallbackData * wcbd = unwrap(data); - wcbd->callback(*wcbd); - if (wcbd->IsNearDeath()) { - delete wcbd; - } - } - - static inline - NanWeakCallbackData * - unwrap(NAN_WEAK_CALLBACK_DATA_TYPE_ data); - - private: // data members - P* const parameter; - Callback const callback; - v8::Persistent persistent; -}; - -#undef NAN_WEAK_CALLBACK_DATA_TYPE_ -#undef NAN_WEAK_CALLBACK_SIG_ - -#if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION - -template -NanWeakCallbackData::~NanWeakCallbackData() { persistent.Reset(); } - -template -void -NanWeakCallbackData::Revive() { persistent.SetWeak(this, &invoke); } - -template -NanWeakCallbackData * -NanWeakCallbackData::unwrap( - v8::WeakCallbackData const& data) { - return data.GetParameter(); -} - -#else - -template -NanWeakCallbackData::~NanWeakCallbackData() { - persistent.Dispose(); - persistent.Clear(); -} - -template -void -NanWeakCallbackData::Revive() { persistent.MakeWeak(this, &invoke); } - -template -NanWeakCallbackData * -NanWeakCallbackData::unwrap(void * data) { - return static_cast(data); -} - -#endif - -template -inline -NanWeakCallbackData* -NanMakeWeakPersistent( - v8::Handle handle - , P* parameter - , typename NanWeakCallbackData::Callback callback) { - return new NanWeakCallbackData(handle, parameter, callback); -} +#include "nan_weak.h" // NOLINT(build/include) //=== Export ================================================================== inline void -NanExport(v8::Handle target, const char * name, +NanExport(v8::Handle target, const char *name, NanFunctionCallback f) { target->Set(NanNew(name), NanNew(f)->GetFunction()); @@ -1863,17 +1776,17 @@ NanExport(v8::Handle target, const char * name, struct NanTap { explicit NanTap(v8::Handle t) : t_() { - NanAssignPersistent(t_, t->ToObject()); + t_.Reset(t->ToObject()); } - ~NanTap() { NanDisposePersistent(t_); } // not sure if neccessary + ~NanTap() { t_.Reset(); } // not sure if neccessary inline void plan(int i) { v8::Handle arg = NanNew(i); NanMakeCallback(NanNew(t_), "plan", 1, &arg); } - inline void ok(bool isOk, const char * msg = NULL) { + inline void ok(bool isOk, const char *msg = NULL) { v8::Handle args[2]; args[0] = NanNew(isOk); if (msg) args[1] = NanNew(msg); @@ -1881,7 +1794,7 @@ struct NanTap { } private: - v8::Persistent t_; + NanPersistent t_; }; #define NAN_STRINGIZE2(x) #x @@ -1893,4 +1806,6 @@ struct NanTap { #define return_NanUndefined() NanReturnUndefined() #define NAN_EXPORT(target, function) NanExport(target, #function, function) +#undef TYPE_CHECK + #endif // NAN_H_ diff --git a/nan_implementation_12_inl.h b/nan_implementation_12_inl.h index 68c01f9d..60391ff3 100644 --- a/nan_implementation_12_inl.h +++ b/nan_implementation_12_inl.h @@ -254,8 +254,13 @@ inline v8::Local NanNew(v8::Handle h) { return v8::Local::New(v8::Isolate::GetCurrent(), h); } -template -inline v8::Local NanNew(v8::Persistent const& p) { +template +inline v8::Local NanNew(v8::Persistent const& p) { + return v8::Local::New(v8::Isolate::GetCurrent(), p); +} + +template +inline v8::Local NanNew(NanPersistent const& p) { return v8::Local::New(v8::Isolate::GetCurrent(), p); } diff --git a/nan_implementation_pre_12_inl.h b/nan_implementation_pre_12_inl.h index fe69d440..927c5724 100644 --- a/nan_implementation_pre_12_inl.h +++ b/nan_implementation_pre_12_inl.h @@ -265,4 +265,9 @@ inline v8::Local NanNew(v8::Persistent const& p) { return v8::Local::New(p); } +template +inline v8::Local NanNew(NanPersistent const& p) { + return v8::Local::New(p.persistent); +} + #endif // NAN_IMPLEMENTATION_PRE_12_INL_H_ diff --git a/nan_new.h b/nan_new.h index c52e2a8d..67429f5d 100644 --- a/nan_new.h +++ b/nan_new.h @@ -253,7 +253,14 @@ NanNew( NanFunctionCallback callback // Convenience template inline v8::Local NanNew(v8::Handle h); +#if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION +template + inline v8::Local NanNew(v8::Persistent const& p); +#else template inline v8::Local NanNew(v8::Persistent const& p); +#endif +template +inline v8::Local NanNew(NanPersistent const& p); inline Nan::imp::Factory::return_t diff --git a/nan_persistent_12_inl.h b/nan_persistent_12_inl.h new file mode 100644 index 00000000..41979560 --- /dev/null +++ b/nan_persistent_12_inl.h @@ -0,0 +1,58 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#ifndef NAN_PERSISTENT_12_INL_H_ +#define NAN_PERSISTENT_12_INL_H_ + +template class NanPersistent : + public v8::Persistent { + public: + NAN_INLINE NanPersistent() : v8::Persistent() {} + + template NAN_INLINE NanPersistent(v8::Handle that) : + v8::Persistent(v8::Isolate::GetCurrent(), that) {} + + template + NAN_INLINE NanPersistent(const v8::Persistent &that) : + v8::Persistent(v8::Isolate::GetCurrent(), that) {} + + NAN_INLINE void Reset() { v8::PersistentBase::Reset(); } + + template + NAN_INLINE void Reset(const v8::Handle &other) { + v8::PersistentBase::Reset(v8::Isolate::GetCurrent(), other); + } + + template + NAN_INLINE void Reset(const v8::PersistentBase &other) { + v8::PersistentBase::Reset(v8::Isolate::GetCurrent(), other); + } + + template + NAN_INLINE void SetWeak( + P *parameter + , typename NanWeakCallbackInfo

::Callback callback + , NanWeakCallbackType type); + + private: + NAN_INLINE T *operator*() const { return *NanPersistentBase::persistent; } + + template + NAN_INLINE void Copy(const NanPersistent &that) { + TYPE_CHECK(T, S); + + this->Reset(); + + if (!that.IsEmpty()) { + this->Reset(that); + M::Copy(that, this); + } + } +}; + +#endif // NAN_PERSISTENT_12_INL_H_ diff --git a/nan_persistent_pre_12_inl.h b/nan_persistent_pre_12_inl.h new file mode 100644 index 00000000..7d57dcdc --- /dev/null +++ b/nan_persistent_pre_12_inl.h @@ -0,0 +1,180 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#ifndef NAN_PERSISTENT_PRE_12_INL_H_ +#define NAN_PERSISTENT_PRE_12_INL_H_ + +template +class NanPersistentBase { + v8::Persistent persistent; + template + friend v8::Local NanNew(const NanPersistent &p); + + public: + NAN_INLINE NanPersistentBase() : + persistent() {} + + NAN_INLINE void Reset() { + persistent.Dispose(); + persistent.Clear(); + } + + template + NAN_INLINE void Reset(const v8::Handle &other) { + TYPE_CHECK(T, S); + + if (!persistent.IsEmpty()) { + persistent.Dispose(); + } + + if (other.IsEmpty()) { + persistent.Clear(); + } else { + persistent = v8::Persistent::New(other); + } + } + + template + NAN_INLINE void Reset(const NanPersistentBase &other) { + TYPE_CHECK(T, S); + + if (!persistent.IsEmpty()) { + persistent.Dispose(); + } + + if (other.IsEmpty()) { + persistent.Clear(); + } else { + persistent = v8::Persistent::New(other.persistent); + } + } + + NAN_INLINE bool IsEmpty() const { return persistent.IsEmpty(); } + + NAN_INLINE void Empty() { persistent.Clear(); } + + template + NAN_INLINE bool operator==(const NanPersistentBase &that) { + return this->persistent == that.persistent; + } + + template + NAN_INLINE bool operator==(const v8::Handle &that) { + return this->persistent == that; + } + + template + NAN_INLINE bool operator!=(const NanPersistentBase &that) { + return !operator==(that); + } + + template + NAN_INLINE bool operator!=(const v8::Handle &that) { + return !operator==(that); + } + + template + NAN_INLINE void SetWeak( + P *parameter + , typename NanWeakCallbackInfo

::Callback callback + , NanWeakCallbackType type); + + NAN_INLINE void ClearWeak() { persistent.ClearWeak(); } + + NAN_INLINE void MarkIndependent() { persistent.MarkIndependent(); } + + NAN_INLINE bool IsIndependent() const { return persistent.IsIndependent(); } + + NAN_INLINE bool IsNearDeath() const { return persistent.IsNearDeath(); } + + NAN_INLINE bool IsWeak() const { return persistent.IsWeak(); } + + private: + NAN_INLINE explicit NanPersistentBase(v8::Persistent that) : + persistent(that) { } + template friend class NanPersistent; +}; + +template +class NanNonCopyablePersistentTraits { + public: + typedef NanPersistent > + NonCopyablePersistent; + static const bool kResetInDestructor = false; + template + NAN_INLINE static void Copy(const NanPersistent &source, + NonCopyablePersistent *dest) { + Uncompilable(); + } + + template NAN_INLINE static void Uncompilable() { + TYPE_CHECK(O, v8::Primitive); + } +}; + +template +struct NanCopyablePersistentTraits { + typedef NanPersistent > CopyablePersistent; + static const bool kResetInDestructor = true; + template + static NAN_INLINE void Copy(const NanPersistent &source, + CopyablePersistent *dest) {} +}; + +template class NanPersistent : + public NanPersistentBase { + public: + NAN_INLINE NanPersistent() {} + + template NAN_INLINE NanPersistent(v8::Handle that) + : NanPersistentBase(v8::Persistent::New(that)) { + TYPE_CHECK(T, S); + } + + NAN_INLINE NanPersistent(const NanPersistent &that) : NanPersistentBase() { + Copy(that); + } + + template + NAN_INLINE NanPersistent(const NanPersistent &that) : + NanPersistentBase() { + Copy(that); + } + + NAN_INLINE NanPersistent &operator=(const NanPersistent &that) { + Copy(that); + return *this; + } + + template + NAN_INLINE NanPersistent &operator=(const NanPersistent &that) { + Copy(that); + return *this; + } + + NAN_INLINE ~NanPersistent() { + if (M::kResetInDestructor) this->Reset(); + } + + private: + NAN_INLINE T *operator*() const { return *NanPersistentBase::persistent; } + + template + NAN_INLINE void Copy(const NanPersistent &that) { + TYPE_CHECK(T, S); + + this->Reset(); + + if (!that.IsEmpty()) { + this->persistent = v8::Persistent::New(that.persistent); + M::Copy(that, this); + } + } +}; + +#endif // NAN_PERSISTENT_PRE_12_INL_H_ diff --git a/nan_weak.h b/nan_weak.h new file mode 100644 index 00000000..d34137e1 --- /dev/null +++ b/nan_weak.h @@ -0,0 +1,414 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#ifndef NAN_WEAK_H_ +#define NAN_WEAK_H_ + +static const int kInternalFieldsInWeakCallback = 2; +static const int kNoInternalFieldIndex = -1; + +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) +# define NAN_WEAK_PARAMETER_CALLBACK_DATA_TYPE_ \ + v8::WeakCallbackInfo > const& +# define NAN_WEAK_TWOFIELD_CALLBACK_DATA_TYPE_ \ + NAN_WEAK_PARAMETER_CALLBACK_DATA_TYPE_ +# define NAN_WEAK_PARAMETER_CALLBACK_SIG_ NAN_WEAK_PARAMETER_CALLBACK_DATA_TYPE_ +# define NAN_WEAK_TWOFIELD_CALLBACK_SIG_ NAN_WEAK_TWOFIELD_CALLBACK_DATA_TYPE_ +#elif NODE_MODULE_VERSION > IOJS_1_1_MODULE_VERSION +# define NAN_WEAK_PARAMETER_CALLBACK_DATA_TYPE_ \ + v8::PhantomCallbackData > const& +# define NAN_WEAK_TWOFIELD_CALLBACK_DATA_TYPE_ \ + NAN_WEAK_PARAMETER_CALLBACK_DATA_TYPE_ +# define NAN_WEAK_PARAMETER_CALLBACK_SIG_ NAN_WEAK_PARAMETER_CALLBACK_DATA_TYPE_ +# define NAN_WEAK_TWOFIELD_CALLBACK_SIG_ NAN_WEAK_TWOFIELD_CALLBACK_DATA_TYPE_ +#elif NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION +# define NAN_WEAK_PARAMETER_CALLBACK_DATA_TYPE_ \ + v8::PhantomCallbackData > const& +# define NAN_WEAK_TWOFIELD_CALLBACK_DATA_TYPE_ \ + v8::InternalFieldsCallbackData, void> const& +# define NAN_WEAK_PARAMETER_CALLBACK_SIG_ NAN_WEAK_PARAMETER_CALLBACK_DATA_TYPE_ +# define NAN_WEAK_TWOFIELD_CALLBACK_SIG_ NAN_WEAK_TWOFIELD_CALLBACK_DATA_TYPE_ +#elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION +# define NAN_WEAK_CALLBACK_DATA_TYPE_ \ + v8::WeakCallbackData > const& +# define NAN_WEAK_CALLBACK_SIG_ NAN_WEAK_CALLBACK_DATA_TYPE_ +#else +# define NAN_WEAK_CALLBACK_DATA_TYPE_ void * +# define NAN_WEAK_CALLBACK_SIG_ \ + v8::Persistent, NAN_WEAK_CALLBACK_DATA_TYPE_ +#endif + +template +class NanWeakCallbackInfo { + public: + typedef void (*Callback)(const NanWeakCallbackInfo& data); + NanWeakCallbackInfo( + NanPersistent *persistent + , Callback callback + , void *parameter + , void *field1 = 0 + , void *field2 = 0) : + callback_(callback), isolate_(0), parameter_(parameter) { + std::memcpy(&persistent_, persistent, sizeof (v8::Persistent)); + internal_fields_[0] = field1; + internal_fields_[1] = field2; + } + NAN_INLINE v8::Isolate *GetIsolate() const { return isolate_; } + NAN_INLINE T *GetParameter() const { return static_cast(parameter_); } + NAN_INLINE void *GetInternalField(int index) const { + assert((index == 0 || index == 1) && "internal field index out of bounds"); + if (index == 0) { + return internal_fields_[0]; + } else { + return internal_fields_[1]; + } + } + + private: + Callback callback_; + v8::Isolate *isolate_; + void *parameter_; + void *internal_fields_[kInternalFieldsInWeakCallback]; + v8::Persistent persistent_; + template friend class NanPersistent; + template friend class NanPersistentBase; +#if NODE_MODULE_VERSION <= NODE_0_12_MODULE_VERSION +# if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION + template + static void invoke(NAN_WEAK_CALLBACK_SIG_ data); + template + static NanWeakCallbackInfo *unwrap(NAN_WEAK_CALLBACK_DATA_TYPE_ data); +# else + static void invoke(NAN_WEAK_CALLBACK_SIG_ data); + static NanWeakCallbackInfo *unwrap(NAN_WEAK_CALLBACK_DATA_TYPE_ data); +# endif +#else + static void invokeparameter(NAN_WEAK_PARAMETER_CALLBACK_SIG_ data); + static void invoketwofield(NAN_WEAK_TWOFIELD_CALLBACK_SIG_ data); + static NanWeakCallbackInfo *unwrapparameter( + NAN_WEAK_PARAMETER_CALLBACK_DATA_TYPE_ data); + static NanWeakCallbackInfo *unwraptwofield( + NAN_WEAK_TWOFIELD_CALLBACK_DATA_TYPE_ data); +#endif +}; + + +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) + +template +void +NanWeakCallbackInfo::invokeparameter(NAN_WEAK_PARAMETER_CALLBACK_SIG_ data) { + NanWeakCallbackInfo *cbinfo = unwrapparameter(data); + if (data.IsFirstPass()) { + cbinfo->persistent_.Reset(); + data.SetSecondPassCallback(invokeparameter); + } else { + cbinfo->callback_(*cbinfo); + delete cbinfo; + } +} + +template +void +NanWeakCallbackInfo::invoketwofield(NAN_WEAK_TWOFIELD_CALLBACK_SIG_ data) { + NanWeakCallbackInfo *cbinfo = unwraptwofield(data); + if (data.IsFirstPass()) { + cbinfo->persistent_.Reset(); + data.SetSecondPassCallback(invoketwofield); + } else { + cbinfo->callback_(*cbinfo); + delete cbinfo; + } +} + +template +NanWeakCallbackInfo *NanWeakCallbackInfo::unwrapparameter( + NAN_WEAK_PARAMETER_CALLBACK_DATA_TYPE_ data) { + NanWeakCallbackInfo *cbinfo = + static_cast*>(data.GetParameter()); + cbinfo->isolate_ = data.GetIsolate(); + return cbinfo; +} + +template +NanWeakCallbackInfo *NanWeakCallbackInfo::unwraptwofield( + NAN_WEAK_TWOFIELD_CALLBACK_DATA_TYPE_ data) { + NanWeakCallbackInfo *cbinfo = + static_cast*>(data.GetInternalField(0)); + cbinfo->isolate_ = data.GetIsolate(); + return cbinfo; +} + +#undef NAN_WEAK_PARAMETER_CALLBACK_SIG_ +#undef NAN_WEAK_TWOFIELD_CALLBACK_SIG_ +#undef NAN_WEAK_PARAMETER_CALLBACK_DATA_TYPE_ +#undef NAN_WEAK_TWOFIELD_CALLBACK_DATA_TYPE_ +# elif NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION + +template +void +NanWeakCallbackInfo::invokeparameter(NAN_WEAK_PARAMETER_CALLBACK_SIG_ data) { + NanWeakCallbackInfo *cbinfo = unwrapparameter(data); + cbinfo->persistent_.Reset(); + cbinfo->callback_(*cbinfo); + delete cbinfo; +} + +template +void +NanWeakCallbackInfo::invoketwofield(NAN_WEAK_TWOFIELD_CALLBACK_SIG_ data) { + NanWeakCallbackInfo *cbinfo = unwraptwofield(data); + cbinfo->persistent_.Reset(); + cbinfo->callback_(*cbinfo); + delete cbinfo; +} + +template +NanWeakCallbackInfo *NanWeakCallbackInfo::unwrapparameter( + NAN_WEAK_PARAMETER_CALLBACK_DATA_TYPE_ data) { + NanWeakCallbackInfo *cbinfo = + static_cast*>(data.GetParameter()); + cbinfo->isolate_ = data.GetIsolate(); + return cbinfo; +} + +template +NanWeakCallbackInfo *NanWeakCallbackInfo::unwraptwofield( + NAN_WEAK_TWOFIELD_CALLBACK_DATA_TYPE_ data) { + NanWeakCallbackInfo *cbinfo = + static_cast*>(data.GetInternalField1()); + cbinfo->isolate_ = data.GetIsolate(); + return cbinfo; +} + +#undef NAN_WEAK_PARAMETER_CALLBACK_SIG_ +#undef NAN_WEAK_TWOFIELD_CALLBACK_SIG_ +#undef NAN_WEAK_PARAMETER_CALLBACK_DATA_TYPE_ +#undef NAN_WEAK_TWOFIELD_CALLBACK_DATA_TYPE_ +#elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION + +template +template +void NanWeakCallbackInfo::invoke(NAN_WEAK_CALLBACK_SIG_ data) { + NanWeakCallbackInfo *cbinfo = unwrap(data); + cbinfo->persistent_.Reset(); + cbinfo->callback_(*cbinfo); + delete cbinfo; +} + +template +template +NanWeakCallbackInfo *NanWeakCallbackInfo::unwrap( + NAN_WEAK_CALLBACK_DATA_TYPE_ data) { + void *parameter = data.GetParameter(); + NanWeakCallbackInfo *cbinfo = + static_cast*>(parameter); + cbinfo->isolate_ = data.GetIsolate(); + return cbinfo; +} + +#undef NAN_WEAK_CALLBACK_SIG_ +#undef NAN_WEAK_CALLBACK_DATA_TYPE_ +#else + +template +void NanWeakCallbackInfo::invoke(NAN_WEAK_CALLBACK_SIG_ data) { + NanWeakCallbackInfo *cbinfo = unwrap(data); + cbinfo->persistent_.Dispose(); + cbinfo->persistent_.Clear(); + cbinfo->callback_(*cbinfo); + delete cbinfo; +} + +template +NanWeakCallbackInfo *NanWeakCallbackInfo::unwrap( + NAN_WEAK_CALLBACK_DATA_TYPE_ data) { + NanWeakCallbackInfo *cbinfo = + static_cast*>(data); + cbinfo->isolate_ = v8::Isolate::GetCurrent(); + return cbinfo; +} + +#undef NAN_WEAK_CALLBACK_SIG_ +#undef NAN_WEAK_CALLBACK_DATA_TYPE_ +#endif + +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) +template +template +NAN_INLINE void NanPersistent::SetWeak( + P *parameter + , typename NanWeakCallbackInfo

::Callback callback + , NanWeakCallbackType type) { + NanWeakCallbackInfo

*wcbd; + if (type == NanWeakCallbackType::kParameter) { + wcbd = new NanWeakCallbackInfo

( + reinterpret_cast*>(this) + , callback + , parameter); + v8::PersistentBase::SetWeak(wcbd, wcbd->invokeparameter, type); + } else { + v8::Handle* self = reinterpret_cast*>(this); + assert((*self)->IsObject()); + int count = (*self)->InternalFieldCount(); + void *internal_fields[kInternalFieldsInWeakCallback] = {0, 0}; + for (int i = 0; i < count && i < kInternalFieldsInWeakCallback; i++) { + internal_fields[i] = (*self)->GetAlignedPointerFromInternalField(i); + } + wcbd = new NanWeakCallbackInfo

( + reinterpret_cast*>(this) + , callback + , 0 + , internal_fields[0] + , internal_fields[1]); + (*self)->SetAlignedPointerInInternalField(0, wcbd); + v8::PersistentBase::SetWeak( + static_cast*>(0) + , wcbd->invoketwofield + , type); + } +} +#elif NODE_MODULE_VERSION > IOJS_1_1_MODULE_VERSION +template +template +NAN_INLINE void NanPersistent::SetWeak( + P *parameter + , typename NanWeakCallbackInfo

::Callback callback + , NanWeakCallbackType type) { + NanWeakCallbackInfo

*wcbd; + if (type == NanWeakCallbackType::kParameter) { + wcbd = new NanWeakCallbackInfo

( + reinterpret_cast*>(this) + , callback + , parameter); + v8::PersistentBase::SetPhantom(wcbd, wcbd->invokeparameter); + } else { + v8::Handle* self = reinterpret_cast*>(this); + assert((*self)->IsObject()); + int count = (*self)->InternalFieldCount(); + void *internal_fields[kInternalFieldsInWeakCallback] = {0, 0}; + for (int i = 0; i < count && i < kInternalFieldsInWeakCallback; i++) { + internal_fields[i] = (*self)->GetAlignedPointerFromInternalField(i); + } + wcbd = new NanWeakCallbackInfo

( + reinterpret_cast*>(this) + , callback + , 0 + , internal_fields[0] + , internal_fields[1]); + (*self)->SetAlignedPointerInInternalField(0, wcbd); + v8::PersistentBase::SetPhantom( + static_cast*>(0) + , wcbd->invoketwofield + , 0 + , count > 1 ? 1 : kNoInternalFieldIndex); + } +} +#elif NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION +template +template +NAN_INLINE void NanPersistent::SetWeak( + P *parameter + , typename NanWeakCallbackInfo

::Callback callback + , NanWeakCallbackType type) { + NanWeakCallbackInfo

*wcbd; + if (type == NanWeakCallbackType::kParameter) { + wcbd = new NanWeakCallbackInfo

( + reinterpret_cast*>(this) + , callback + , parameter); + v8::PersistentBase::SetPhantom(wcbd, wcbd->invokeparameter); + } else { + v8::Handle* self = reinterpret_cast*>(this); + assert((*self)->IsObject()); + int count = (*self)->InternalFieldCount(); + void *internal_fields[kInternalFieldsInWeakCallback] = {0, 0}; + for (int i = 0; i < count && i < kInternalFieldsInWeakCallback; i++) { + internal_fields[i] = (*self)->GetAlignedPointerFromInternalField(i); + } + wcbd = new NanWeakCallbackInfo

( + reinterpret_cast*>(this) + , callback + , 0 + , internal_fields[0] + , internal_fields[1]); + (*self)->SetAlignedPointerInInternalField(0, wcbd); + v8::PersistentBase::SetPhantom( + wcbd->invoketwofield + , 0 + , count > 1 ? 1 : kNoInternalFieldIndex); + } +} +#elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION +template +template +NAN_INLINE void NanPersistent::SetWeak( + P *parameter + , typename NanWeakCallbackInfo

::Callback callback + , NanWeakCallbackType type) { + NanWeakCallbackInfo

*wcbd; + if (type == NanWeakCallbackType::kParameter) { + wcbd = new NanWeakCallbackInfo

( + reinterpret_cast*>(this) + , callback + , parameter); + v8::PersistentBase::SetWeak(wcbd, wcbd->invoke); + } else { + v8::Handle* self = reinterpret_cast*>(this); + assert((*self)->IsObject()); + int count = (*self)->InternalFieldCount(); + void *internal_fields[kInternalFieldsInWeakCallback] = {0, 0}; + for (int i = 0; i < count && i < kInternalFieldsInWeakCallback; i++) { + internal_fields[i] = (*self)->GetAlignedPointerFromInternalField(i); + } + wcbd = new NanWeakCallbackInfo

( + reinterpret_cast*>(this) + , callback + , 0 + , internal_fields[0] + , internal_fields[1]); + v8::PersistentBase::SetWeak(wcbd, wcbd->invoke); + } +} +#else +template +template +NAN_INLINE void NanPersistentBase::SetWeak( + P *parameter + , typename NanWeakCallbackInfo

::Callback callback + , NanWeakCallbackType type) { + NanWeakCallbackInfo

*wcbd; + if (type == NanWeakCallbackType::kParameter) { + wcbd = new NanWeakCallbackInfo

( + reinterpret_cast*>(this) + , callback + , parameter); + persistent.MakeWeak(wcbd, wcbd->invoke); + } else { + v8::Handle* self = reinterpret_cast*>(this); + assert((*self)->IsObject()); + int count = (*self)->InternalFieldCount(); + void *internal_fields[kInternalFieldsInWeakCallback] = {0, 0}; + for (int i = 0; i < count && i < kInternalFieldsInWeakCallback; i++) { + internal_fields[i] = (*self)->GetPointerFromInternalField(i); + } + wcbd = new NanWeakCallbackInfo

( + reinterpret_cast*>(this) + , callback + , 0 + , internal_fields[0] + , internal_fields[1]); + persistent.MakeWeak(wcbd, wcbd->invoke); + } +} +#endif + +#endif // NAN_WEAK_H_ diff --git a/test/binding.gyp b/test/binding.gyp index 74392f1e..2db37af9 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -57,6 +57,10 @@ "target_name" : "weak" , "sources" : [ "cpp/weak.cpp" ] } + , { + "target_name" : "weak2" + , "sources" : [ "cpp/weak2.cpp" ] + } , { "target_name" : "bufferworkerpersistent" , "sources" : [ "cpp/bufferworkerpersistent.cpp" ] diff --git a/test/cpp/makecallback.cpp b/test/cpp/makecallback.cpp index b54c1608..51ab2dce 100644 --- a/test/cpp/makecallback.cpp +++ b/test/cpp/makecallback.cpp @@ -18,10 +18,10 @@ class MyObject : public node::ObjectWrap { static NAN_METHOD(New); static NAN_METHOD(CallEmit); - static v8::Persistent constructor; + static NanPersistent constructor; }; -v8::Persistent MyObject::constructor; +NanPersistent MyObject::constructor; MyObject::MyObject() { } @@ -37,7 +37,7 @@ void MyObject::Init(v8::Handle exports) { NODE_SET_PROTOTYPE_METHOD(tpl, "call_emit", CallEmit); - NanAssignPersistent(constructor, tpl->GetFunction()); + constructor.Reset(tpl->GetFunction()); exports->Set(NanNew("MyObject"), tpl->GetFunction()); } diff --git a/test/cpp/nannew.cpp b/test/cpp/nannew.cpp index 1c3932a0..d6c7ff5d 100644 --- a/test/cpp/nannew.cpp +++ b/test/cpp/nannew.cpp @@ -344,10 +344,10 @@ NAN_METHOD(testPersistents) { t.plan(1); - Persistent p; - NanAssignPersistent(p, NanNew("foo")); + NanPersistent p; + p.Reset(NanNew("foo")); t.ok(_( assertType( NanNew(p)))); - NanDisposePersistent(p); + p.Reset(); return_NanUndefined(); } diff --git a/test/cpp/objectwraphandle.cpp b/test/cpp/objectwraphandle.cpp index a2b33897..e4ee1bb4 100644 --- a/test/cpp/objectwraphandle.cpp +++ b/test/cpp/objectwraphandle.cpp @@ -17,7 +17,7 @@ class MyObject : public NanObjectWrap { NODE_SET_PROTOTYPE_METHOD(tpl, "getHandle", GetHandle); - NanAssignPersistent(constructor, tpl->GetFunction()); + constructor.Reset(tpl->GetFunction()); exports->Set(NanNew("MyObject"), tpl->GetFunction()); } @@ -44,11 +44,11 @@ class MyObject : public NanObjectWrap { NanReturnValue(obj->handle()); } - static v8::Persistent constructor; + static NanPersistent constructor; double value_; }; -v8::Persistent MyObject::constructor; +NanPersistent MyObject::constructor; void Init(v8::Handle exports) { MyObject::Init(exports); diff --git a/test/cpp/persistent.cpp b/test/cpp/persistent.cpp index 92a1d24e..512c250d 100644 --- a/test/cpp/persistent.cpp +++ b/test/cpp/persistent.cpp @@ -9,10 +9,10 @@ #include #include // memset() -static v8::Persistent persistentTest1; +static NanPersistent persistentTest1; NAN_METHOD(Save1) { - NanAssignPersistent(persistentTest1, args[0].As()); + persistentTest1.Reset(args[0].As()); NanReturnUndefined(); } @@ -21,28 +21,32 @@ NAN_METHOD(Get1) { } NAN_METHOD(Dispose1) { - NanDisposePersistent(persistentTest1); + persistentTest1.Reset(); NanReturnUndefined(); } NAN_METHOD(ToPersistentAndBackAgain) { - v8::Persistent persistent; - NanAssignPersistent(persistent, args[0].As()); + NanPersistent persistent(args[0].As()); v8::Local object = NanNew(persistent); - NanDisposePersistent(persistent); + persistent.Reset(); memset(&persistent, -1, sizeof(persistent)); // Clobber it good. NanReturnValue(object); } NAN_METHOD(PersistentToPersistent) { - v8::Persistent persistent; - NanAssignPersistent(persistent, args[0].As()); - NanAssignPersistent(persistentTest1, persistent); - NanDisposePersistent(persistent); - NanDisposePersistent(persistentTest1); + NanPersistent persistent(args[0].As()); + persistentTest1.Reset(persistent); + persistent.Reset(); + persistentTest1.Reset(); NanReturnUndefined(); } +NAN_METHOD(CopyablePersistent) { + NanCopyablePersistentTraits::CopyablePersistent p; + p = persistentTest1; + NanReturnValue(p); +} + void Init (v8::Handle target) { target->Set( NanNew("save1") @@ -64,6 +68,10 @@ void Init (v8::Handle target) { NanNew("persistentToPersistent") , NanNew(PersistentToPersistent)->GetFunction() ); + target->Set( + NanNew("copyablePersistent") + , NanNew(CopyablePersistent)->GetFunction() + ); } NODE_MODULE(persistent, Init) diff --git a/test/cpp/returnvalue.cpp b/test/cpp/returnvalue.cpp index 69874845..3a6381eb 100644 --- a/test/cpp/returnvalue.cpp +++ b/test/cpp/returnvalue.cpp @@ -8,7 +8,7 @@ #include -v8::Persistent persistent; +NanPersistent persistent; NAN_METHOD(ReturnValue) { if (args.Length() == 1) { @@ -31,7 +31,7 @@ NAN_METHOD(ReturnPersistent) { } void Init (v8::Handle target) { - NanAssignPersistent(persistent, NanNew(true)); + persistent.Reset(NanNew(true)); target->Set( NanNew("r") diff --git a/test/cpp/settemplate.cpp b/test/cpp/settemplate.cpp index 79a87880..b916f7d1 100644 --- a/test/cpp/settemplate.cpp +++ b/test/cpp/settemplate.cpp @@ -17,10 +17,10 @@ class MyObject : public node::ObjectWrap { ~MyObject(); static NAN_METHOD(New); - static v8::Persistent constructor; + static NanPersistent constructor; }; -v8::Persistent MyObject::constructor; +NanPersistent MyObject::constructor; MyObject::MyObject() { } @@ -48,7 +48,7 @@ void MyObject::Init(v8::Handle exports) { NanSetInstanceTemplate( tpl, NanNew("dontDelete"), NanNew("dontDelete"), v8::DontDelete); - NanAssignPersistent(constructor, tpl->GetFunction()); + constructor.Reset(tpl->GetFunction()); exports->Set(NanNew("MyObject"), tpl->GetFunction()); } diff --git a/test/cpp/settergetter.cpp b/test/cpp/settergetter.cpp index 875db98c..20c4a933 100644 --- a/test/cpp/settergetter.cpp +++ b/test/cpp/settergetter.cpp @@ -26,7 +26,7 @@ class SetterGetter : public node::ObjectWrap { char prop2[256]; }; -static v8::Persistent settergetter_constructor; +static NanPersistent settergetter_constructor; NAN_METHOD(CreateNew) { NanReturnValue(SetterGetter::NewInstance()); @@ -42,7 +42,7 @@ SetterGetter::SetterGetter() { void SetterGetter::Init(v8::Handle target) { v8::Local tpl = NanNew(SetterGetter::New); - NanAssignPersistent(settergetter_constructor, tpl); + settergetter_constructor.Reset(tpl); tpl->SetClassName(NanNew("SetterGetter")); tpl->InstanceTemplate()->SetInternalFieldCount(1); NODE_SET_PROTOTYPE_METHOD(tpl, "log", SetterGetter::Log); diff --git a/test/cpp/strings.cpp b/test/cpp/strings.cpp index 62fc213b..7def25b2 100644 --- a/test/cpp/strings.cpp +++ b/test/cpp/strings.cpp @@ -27,19 +27,16 @@ NAN_METHOD(EncodeUCS2) { NanReturnValue(NanEncode("h\0e\0l\0l\0o\0", 10, Nan::UCS2)); } -v8::Persistent returnUtf8String_persistent; -v8::Persistent heapString_persistent; -v8::Persistent encodeHex_persistent; -v8::Persistent encodeUCS2_persistent; +NanPersistent returnUtf8String_persistent; +NanPersistent heapString_persistent; +NanPersistent encodeHex_persistent; +NanPersistent encodeUCS2_persistent; void Init (v8::Handle target) { v8::Local returnUtf8String = NanNew(ReturnUtf8String); - NanAssignPersistent( - returnUtf8String_persistent - , returnUtf8String - ); + returnUtf8String_persistent.Reset(returnUtf8String); target->Set( NanNew("returnUtf8String") @@ -49,10 +46,7 @@ void Init (v8::Handle target) { v8::Local heapString = NanNew(HeapString); - NanAssignPersistent( - heapString_persistent - , heapString - ); + heapString_persistent.Reset(heapString); target->Set( NanNew("heapString") @@ -62,10 +56,7 @@ void Init (v8::Handle target) { v8::Local encodeHex = NanNew(EncodeHex); - NanAssignPersistent( - encodeHex_persistent - , encodeHex - ); + encodeHex_persistent.Reset(encodeHex); target->Set( NanNew("encodeHex") @@ -75,10 +66,7 @@ void Init (v8::Handle target) { v8::Local encodeUCS2 = NanNew(EncodeUCS2); - NanAssignPersistent( - encodeUCS2_persistent - , encodeUCS2 - ); + encodeUCS2_persistent.Reset(encodeUCS2); target->Set( NanNew("encodeUCS2") diff --git a/test/cpp/weak.cpp b/test/cpp/weak.cpp index fc4248e3..6a6ee68d 100644 --- a/test/cpp/weak.cpp +++ b/test/cpp/weak.cpp @@ -8,26 +8,28 @@ #include +static NanPersistent cb; + void weakCallback( -NanWeakCallbackData & data) { // NOLINT(runtime/references) + const NanWeakCallbackInfo &data) { // NOLINT(runtime/references) int *parameter = data.GetParameter(); - NanMakeCallback(NanGetCurrentContext()->Global(), data.GetValue(), 0, NULL); - if ((*parameter)++ == 0) { - data.Revive(); - } else { - delete parameter; - } + v8::Local val = NanNew(*parameter); + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(cb), 1, &val); + delete parameter; } v8::Handle wrap(v8::Local func) { NanEscapableScope scope; v8::Local lstring = NanNew("result"); - int *parameter = new int(0); - NanMakeWeakPersistent(func, parameter, weakCallback); + int *parameter = new int(42); + NanPersistent persistent(func); + persistent.SetWeak(parameter, weakCallback, NanWeakCallbackType::kParameter); + assert(persistent.IsWeak()); return scope.Escape(lstring); } NAN_METHOD(Hustle) { + cb.Reset(args[1].As()); NanReturnValue(wrap(args[0].As())); } diff --git a/test/cpp/weak2.cpp b/test/cpp/weak2.cpp new file mode 100644 index 00000000..58c9357d --- /dev/null +++ b/test/cpp/weak2.cpp @@ -0,0 +1,48 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#include + +static NanPersistent cb; +void weakCallback( + const NanWeakCallbackInfo &data) { // NOLINT(runtime/references) + int *parameter = static_cast(data.GetInternalField(0)); + v8::Local val = NanNew(*parameter); + NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(cb), 1, &val); + delete parameter; +} + +v8::Handle wrap() { + NanEscapableScope scope; + v8::Local lstring = NanNew("result"); + v8::Local otpl = NanNew(); + otpl->SetInternalFieldCount(1); + v8::Local obj = otpl->NewInstance(); + NanSetInternalFieldPointer(obj, 0, new int(42)); + NanPersistent persistent(obj); + persistent.SetWeak( + static_cast(0) + , weakCallback + , NanWeakCallbackType::kInternalFields); + assert(persistent.IsWeak()); + return scope.Escape(lstring); +} + +NAN_METHOD(Hustle) { + cb.Reset(args[0].As()); + NanReturnValue(wrap()); +} + +void Init (v8::Handle target) { + target->Set( + NanNew("hustle") + , NanNew(Hustle)->GetFunction() + ); +} + +NODE_MODULE(weak2, Init) diff --git a/test/js/persistent-test.js b/test/js/persistent-test.js index 4e4ee4ce..d9105071 100644 --- a/test/js/persistent-test.js +++ b/test/js/persistent-test.js @@ -11,7 +11,7 @@ const test = require('tap').test , bindings = require('bindings')({ module_root: testRoot, bindings: 'persistent' }); test('persistent', function (t) { - t.plan(11) + t.plan(14); var persistent = bindings; t.type(persistent.save1, 'function'); @@ -19,22 +19,27 @@ test('persistent', function (t) { t.type(persistent.dispose1, 'function'); t.type(persistent.toPersistentAndBackAgain, 'function'); t.type(persistent.persistentToPersistent, 'function'); + t.type(persistent.copyablePersistent, 'function'); t.deepEqual(persistent.toPersistentAndBackAgain({ x: 42 }), { x: 42 }); t.ok(persistent.persistentToPersistent('any string') || true); - persistent.save1('a string to save') - t.equal(persistent.get1(), 'a string to save') + persistent.save1('a string to save'); + t.equal(persistent.get1(), 'a string to save'); setTimeout(function () { - t.equal(persistent.get1(), 'a string to save') - }, 25) + t.equal(persistent.get1(), 'a string to save'); + }, 25); setTimeout(function () { - t.equal(persistent.get1(), 'a string to save') - }, 50) + t.equal(persistent.get1(), 'a string to save'); + }, 50); setTimeout(function () { - persistent.dispose1() - }, 75) + t.equal(persistent.copyablePersistent(), 'a string to save'); + }, 75); setTimeout(function () { - t.ok(persistent.get1() === undefined, 'no more persistent') - }, 100) -}) + persistent.dispose1(); + }, 75); + setTimeout(function () { + t.ok(persistent.get1() === undefined, 'no more persistent'); + t.ok(persistent.copyablePersistent() === undefined, 'no more persistent'); + }, 100); +}); diff --git a/test/js/weak-test.js b/test/js/weak-test.js index f6cb192d..6e40499b 100644 --- a/test/js/weak-test.js +++ b/test/js/weak-test.js @@ -13,24 +13,19 @@ const test = require('tap').test test('weak', function (t) { t.plan(3); - var weak = bindings; + var weak = bindings, count = 0; t.type(weak.hustle, 'function'); - function f() { - var count = 0; - weak.hustle(function () { - t.ok(count++ < 2); - }); - }; - - f(); - - // run weak callback, should not dispose - gc(); + weak.hustle(function () {}, function (val) { + t.equal(val, 42); + count++; + }); // run weak callback, should dispose gc(); // do not run weak callback gc(); + + t.equal(count, 1); }); diff --git a/test/js/weak2-test.js b/test/js/weak2-test.js new file mode 100644 index 00000000..dff05b60 --- /dev/null +++ b/test/js/weak2-test.js @@ -0,0 +1,36 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +const test = require('tap').test + , testRoot = require('path').resolve(__dirname, '..') + , bindings = require('bindings')({ module_root: testRoot, bindings: 'weak2' }); + +test('weak2', function (t) { + t.plan(3); + + var weak2 = bindings, count = 0; + t.type(weak2.hustle, 'function'); + + weak2.hustle(function (val) { + t.equal(val, 42); + count++; + }); + + // run weak callback, should dispose + gc(); + + // do not run weak callback + gc(); + + var timeout = setTimeout(function () { + if (count > 0) { + clearTimeout(timeout); + t.equal(count, 1); + } + }, 100); +}); From 4408da1b47f44e788ae23987b088032e4399b8e6 Mon Sep 17 00:00:00 2001 From: Benjamin Byholm Date: Sat, 23 May 2015 15:13:10 +0300 Subject: [PATCH 12/84] Introduce NanGlobal --- nan_persistent_12_inl.h | 71 +++++++++++++++++++++++++++++++++++++ nan_persistent_pre_12_inl.h | 57 +++++++++++++++++++++++++++++ test/cpp/returnvalue.cpp | 10 +++--- 3 files changed, 133 insertions(+), 5 deletions(-) diff --git a/nan_persistent_12_inl.h b/nan_persistent_12_inl.h index 41979560..23a6cc16 100644 --- a/nan_persistent_12_inl.h +++ b/nan_persistent_12_inl.h @@ -55,4 +55,75 @@ template class NanPersistent : } }; +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) +template +class NanGlobal : public v8::Global { + public: + NAN_INLINE NanGlobal() : v8::Global() {} + + template NAN_INLINE NanGlobal(v8::Handle that) : + v8::Global(v8::Isolate::GetCurrent(), that) {} + + template + NAN_INLINE NanGlobal(const v8::PersistentBase &that) : + v8::Global(v8::Isolate::GetCurrent(), that) {} + + NAN_INLINE void Reset() { v8::PersistentBase::Reset(); } + + template + NAN_INLINE void Reset(const v8::Handle &other) { + v8::PersistentBase::Reset(v8::Isolate::GetCurrent(), other); + } + + template + NAN_INLINE void Reset(const v8::PersistentBase &other) { + v8::PersistentBase::Reset(v8::Isolate::GetCurrent(), other); + } + + template + NAN_INLINE void SetWeak( + P *parameter + , typename NanWeakCallbackInfo

::Callback callback + , NanWeakCallbackType type) { + reinterpret_cast*>(this)->SetWeak( + parameter, callback, type); + } +}; +#else +template +class NanGlobal : public v8::UniquePersistent { + public: + NAN_INLINE NanGlobal() : v8::UniquePersistent() {} + + template NAN_INLINE NanGlobal(v8::Handle that) : + v8::UniquePersistent(v8::Isolate::GetCurrent(), that) {} + + template + NAN_INLINE NanGlobal(const v8::PersistentBase &that) : + v8::UniquePersistent(v8::Isolate::GetCurrent(), that) {} + + NAN_INLINE void Reset() { v8::PersistentBase::Reset(); } + + template + NAN_INLINE void Reset(const v8::Handle &other) { + v8::PersistentBase::Reset(v8::Isolate::GetCurrent(), other); + } + + template + NAN_INLINE void Reset(const v8::PersistentBase &other) { + v8::PersistentBase::Reset(v8::Isolate::GetCurrent(), other); + } + + template + NAN_INLINE void SetWeak( + P *parameter + , typename NanWeakCallbackInfo

::Callback callback + , NanWeakCallbackType type) { + reinterpret_cast*>(this)->SetWeak( + parameter, callback, type); + } +}; +#endif + #endif // NAN_PERSISTENT_12_INL_H_ diff --git a/nan_persistent_pre_12_inl.h b/nan_persistent_pre_12_inl.h index 7d57dcdc..51d4e4af 100644 --- a/nan_persistent_pre_12_inl.h +++ b/nan_persistent_pre_12_inl.h @@ -14,6 +14,7 @@ class NanPersistentBase { v8::Persistent persistent; template friend v8::Local NanNew(const NanPersistent &p); + template friend class NanReturnValue; public: NAN_INLINE NanPersistentBase() : @@ -97,7 +98,9 @@ class NanPersistentBase { private: NAN_INLINE explicit NanPersistentBase(v8::Persistent that) : persistent(that) { } + NAN_INLINE explicit NanPersistentBase(T *val) : persistent(val) {} template friend class NanPersistent; + template friend class NanGlobal; }; template @@ -177,4 +180,58 @@ template class NanPersistent : } }; +template +class NanGlobal : public NanPersistentBase { + struct RValue { + NAN_INLINE explicit RValue(NanGlobal* obj) : object(obj) {} + NanGlobal* object; + }; + + public: + NAN_INLINE NanGlobal() : NanPersistentBase(0) { } + + template + NAN_INLINE NanGlobal(v8::Handle that) + : NanPersistentBase(v8::Persistent::New(that)) { + TYPE_CHECK(T, S); + } + + template + NAN_INLINE NanGlobal(const NanPersistentBase &that) + : NanPersistentBase(that) { + TYPE_CHECK(T, S); + } + /** + * Move constructor. + */ + NAN_INLINE NanGlobal(RValue rvalue) + : NanPersistentBase(rvalue.object.persistent) { + rvalue.object->Reset(); + } + NAN_INLINE ~NanGlobal() { this->Reset(); } + /** + * Move via assignment. + */ + template + NAN_INLINE NanGlobal &operator=(NanGlobal rhs) { + TYPE_CHECK(T, S); + this->Reset(rhs.persistent); + rhs.Reset(); + return *this; + } + /** + * Cast operator for moves. + */ + NAN_INLINE operator RValue() { return RValue(this); } + /** + * Pass allows returning uniques from functions, etc. + */ + NanGlobal Pass() { return NanGlobal(RValue(this)); } + + private: + NanGlobal(NanGlobal &); + void operator=(NanGlobal &); + template friend class NanReturnValue; +}; + #endif // NAN_PERSISTENT_PRE_12_INL_H_ diff --git a/test/cpp/returnvalue.cpp b/test/cpp/returnvalue.cpp index 3a6381eb..00c1b1d4 100644 --- a/test/cpp/returnvalue.cpp +++ b/test/cpp/returnvalue.cpp @@ -8,7 +8,7 @@ #include -NanPersistent persistent; +NanGlobal global; NAN_METHOD(ReturnValue) { if (args.Length() == 1) { @@ -26,12 +26,12 @@ NAN_METHOD(ReturnString) { NanReturnValue("yes, it works"); } -NAN_METHOD(ReturnPersistent) { - NanReturnValue(persistent); +NAN_METHOD(ReturnGlobal) { + NanReturnValue(global); } void Init (v8::Handle target) { - persistent.Reset(NanNew(true)); + global.Reset(NanNew(true)); target->Set( NanNew("r") @@ -47,7 +47,7 @@ void Init (v8::Handle target) { ); target->Set( NanNew("q") - , NanNew(ReturnPersistent)->GetFunction() + , NanNew(ReturnGlobal)->GetFunction() ); } From d90a25c3baaba07bb8d45325b7ba05ba4ec04cd2 Mon Sep 17 00:00:00 2001 From: Benjamin Byholm Date: Sat, 9 May 2015 20:06:39 +0300 Subject: [PATCH 13/84] Remove NanReturn* macros --- Makefile | 7 + examples/async_pi_estimate/async.cc | 5 +- examples/async_pi_estimate/sync.cc | 4 +- nan.h | 493 +++++++++++++----- nan_callbacks.h | 133 +++++ nan_callbacks_12_inl.h | 472 +++++++++++++++++ nan_callbacks_pre_12_inl.h | 450 ++++++++++++++++ nan_implementation_12_inl.h | 50 +- nan_implementation_pre_12_inl.h | 26 +- test/binding.gyp | 12 +- test/cpp/{settergetter.cpp => accessors.cpp} | 33 +- test/cpp/asyncprogressworker.cpp | 10 +- test/cpp/asyncworker.cpp | 6 +- test/cpp/asyncworkererror.cpp | 4 +- test/cpp/bufferworkerpersistent.cpp | 8 +- test/cpp/gc.cpp | 6 +- test/cpp/indexedinterceptors.cpp | 114 ++++ test/cpp/isolatedata.cpp | 2 +- test/cpp/makecallback.cpp | 21 +- test/cpp/morenews.cpp | 16 +- test/cpp/multifile2.cpp | 4 +- test/cpp/namedinterceptors.cpp | 114 ++++ test/cpp/nancallback.cpp | 29 +- test/cpp/nannew.cpp | 96 ++-- test/cpp/news.cpp | 53 +- test/cpp/objectwraphandle.cpp | 18 +- test/cpp/persistent.cpp | 18 +- test/cpp/returnemptystring.cpp | 2 +- test/cpp/returnnull.cpp | 2 +- test/cpp/returnundefined.cpp | 2 +- test/cpp/returnvalue.cpp | 20 +- test/cpp/settemplate.cpp | 8 +- test/cpp/strings.cpp | 10 +- test/cpp/threadlocal.cpp | 4 +- test/cpp/weak.cpp | 4 +- test/cpp/weak2.cpp | 4 +- ...settergetter-test.js => accessors-test.js} | 4 +- test/js/indexedinterceptors-test.js | 23 + test/js/namedinterceptors-test.js | 23 + test/js/returnvalue-test.js | 4 +- 40 files changed, 1967 insertions(+), 347 deletions(-) create mode 100644 nan_callbacks.h create mode 100644 nan_callbacks_12_inl.h create mode 100644 nan_callbacks_pre_12_inl.h rename test/cpp/{settergetter.cpp => accessors.cpp} (83%) create mode 100644 test/cpp/indexedinterceptors.cpp create mode 100644 test/cpp/namedinterceptors.cpp rename test/js/{settergetter-test.js => accessors-test.js} (93%) create mode 100644 test/js/indexedinterceptors-test.js create mode 100644 test/js/namedinterceptors-test.js diff --git a/Makefile b/Makefile index 8f13a6fe..665471a4 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,9 @@ LINT_SOURCES = \ examples/async_pi_estimate/sync.cc \ examples/async_pi_estimate/sync.h \ nan.h \ + nan_callbacks.h \ + nan_callbacks_12_inl.h \ + nan_callbacks_pre_12_inl.h \ nan_implementation_12_inl.h \ nan_implementation_pre_12_inl.h \ nan_new.h \ @@ -22,18 +25,21 @@ LINT_SOURCES = \ nan_persistent_pre_12_inl.h \ nan_string_bytes.h \ nan_weak.h \ + test/cpp/accessors.cpp \ test/cpp/asyncworker.cpp \ test/cpp/asyncprogressworker.cpp \ test/cpp/asyncworkererror.cpp \ test/cpp/bufferworkerpersistent.cpp \ test/cpp/error.cpp \ test/cpp/gc.cpp \ + test/cpp/indexedinterceptors.cpp \ test/cpp/isolatedata.cpp \ test/cpp/makecallback.cpp \ test/cpp/morenews.cpp \ test/cpp/multifile1.cpp \ test/cpp/multifile2.cpp \ test/cpp/multifile2.h \ + test/cpp/namedinterceptors.cpp \ test/cpp/nancallback.cpp \ test/cpp/nannew.cpp \ test/cpp/news.cpp \ @@ -67,6 +73,7 @@ forcetest: npm test $(ADDONS): nan.h nan_new.h nan_implementation_pre_12_inl.h nan_implementation_12_inl.h \ + nan_callbacks.h nan_callbacks_12_inl.h nan_callbacks_pre_12_inl.h \ nan_persistent_12_inl.h nan_persistent_pre_12_inl.h nan_weak.h \ nan_string_bytes.h test/binding.gyp $(SOURCES) cd test/ && ../node_modules/.bin/node-gyp rebuild diff --git a/examples/async_pi_estimate/async.cc b/examples/async_pi_estimate/async.cc index 8ce559c8..acab9b65 100644 --- a/examples/async_pi_estimate/async.cc +++ b/examples/async_pi_estimate/async.cc @@ -51,9 +51,8 @@ class PiWorker : public NanAsyncWorker { // Asynchronous access to the `Estimate()` function NAN_METHOD(CalculateAsync) { - int points = args[0]->Uint32Value(); - NanCallback *callback = new NanCallback(args[1].As()); + int points = info[0]->Uint32Value(); + NanCallback *callback = new NanCallback(info[1].As()); NanAsyncQueueWorker(new PiWorker(callback, points)); - NanReturnUndefined(); } diff --git a/examples/async_pi_estimate/sync.cc b/examples/async_pi_estimate/sync.cc index b2e7ba6c..88b04dd4 100644 --- a/examples/async_pi_estimate/sync.cc +++ b/examples/async_pi_estimate/sync.cc @@ -15,8 +15,8 @@ using v8::Number; // Simple synchronous access to the `Estimate()` function NAN_METHOD(CalculateSync) { // expect a number as the first argument - int points = args[0]->Uint32Value(); + int points = info[0]->Uint32Value(); double est = Estimate(points); - NanReturnValue(NanNew(est)); + info.GetReturnValue().Set(est); } diff --git a/nan.h b/nan.h index 886c1419..ecaf6819 100644 --- a/nan.h +++ b/nan.h @@ -96,12 +96,16 @@ *(static_cast(0)) = static_cast(0); \ } +//=== CallbackInfo ============================================================= + +#include "nan_callbacks.h" // NOLINT(build/include) + +//============================================================================== + #if (NODE_MODULE_VERSION < NODE_0_12_MODULE_VERSION) -typedef v8::InvocationCallback NanFunctionCallback; typedef v8::Script NanUnboundScript; typedef v8::Script NanBoundScript; #else -typedef v8::FunctionCallback NanFunctionCallback; typedef v8::UnboundScript NanUnboundScript; typedef v8::Script NanBoundScript; #endif @@ -442,61 +446,6 @@ class NanEscapableScope { #endif #if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION) // Node 0.12 - typedef const v8::FunctionCallbackInfo& NAN_METHOD_ARGS_TYPE; - typedef void NAN_METHOD_RETURN_TYPE; - - typedef const v8::PropertyCallbackInfo& NAN_GETTER_ARGS_TYPE; - typedef void NAN_GETTER_RETURN_TYPE; - - typedef const v8::PropertyCallbackInfo& NAN_SETTER_ARGS_TYPE; - typedef void NAN_SETTER_RETURN_TYPE; - - typedef const v8::PropertyCallbackInfo& - NAN_PROPERTY_GETTER_ARGS_TYPE; - typedef void NAN_PROPERTY_GETTER_RETURN_TYPE; - - typedef const v8::PropertyCallbackInfo& - NAN_PROPERTY_SETTER_ARGS_TYPE; - typedef void NAN_PROPERTY_SETTER_RETURN_TYPE; - - typedef const v8::PropertyCallbackInfo& - NAN_PROPERTY_ENUMERATOR_ARGS_TYPE; - typedef void NAN_PROPERTY_ENUMERATOR_RETURN_TYPE; - - typedef const v8::PropertyCallbackInfo& - NAN_PROPERTY_DELETER_ARGS_TYPE; - typedef void NAN_PROPERTY_DELETER_RETURN_TYPE; - - typedef const v8::PropertyCallbackInfo& - NAN_PROPERTY_QUERY_ARGS_TYPE; - typedef void NAN_PROPERTY_QUERY_RETURN_TYPE; - - typedef const v8::PropertyCallbackInfo& NAN_INDEX_GETTER_ARGS_TYPE; - typedef void NAN_INDEX_GETTER_RETURN_TYPE; - - typedef const v8::PropertyCallbackInfo& NAN_INDEX_SETTER_ARGS_TYPE; - typedef void NAN_INDEX_SETTER_RETURN_TYPE; - - typedef const v8::PropertyCallbackInfo& - NAN_INDEX_ENUMERATOR_ARGS_TYPE; - typedef void NAN_INDEX_ENUMERATOR_RETURN_TYPE; - - typedef const v8::PropertyCallbackInfo& - NAN_INDEX_DELETER_ARGS_TYPE; - typedef void NAN_INDEX_DELETER_RETURN_TYPE; - - typedef const v8::PropertyCallbackInfo& - NAN_INDEX_QUERY_ARGS_TYPE; - typedef void NAN_INDEX_QUERY_RETURN_TYPE; - -# define NanReturnValue(value) \ - return args.GetReturnValue().Set(Nan::imp::NanEnsureHandleOrPersistent(value)) -# define NanReturnUndefined() return -# define NanReturnHolder() NanReturnValue(args.Holder()) -# define NanReturnThis() NanReturnValue(args.This()) -# define NanReturnNull() return args.GetReturnValue().SetNull() -# define NanReturnEmptyString() return args.GetReturnValue().SetEmptyString() - NAN_INLINE v8::Local NanUndefined() { NanEscapableScope scope; return scope.Escape(NanNew(v8::Undefined(v8::Isolate::GetCurrent()))); @@ -834,53 +783,6 @@ class NanEscapableScope { }; #else // Node 0.8 and 0.10 - typedef const v8::Arguments& NAN_METHOD_ARGS_TYPE; - typedef v8::Handle NAN_METHOD_RETURN_TYPE; - - typedef const v8::AccessorInfo & NAN_GETTER_ARGS_TYPE; - typedef v8::Handle NAN_GETTER_RETURN_TYPE; - - typedef const v8::AccessorInfo & NAN_SETTER_ARGS_TYPE; - typedef void NAN_SETTER_RETURN_TYPE; - - typedef const v8::AccessorInfo& NAN_PROPERTY_GETTER_ARGS_TYPE; - typedef v8::Handle NAN_PROPERTY_GETTER_RETURN_TYPE; - - typedef const v8::AccessorInfo& NAN_PROPERTY_SETTER_ARGS_TYPE; - typedef v8::Handle NAN_PROPERTY_SETTER_RETURN_TYPE; - - typedef const v8::AccessorInfo& NAN_PROPERTY_ENUMERATOR_ARGS_TYPE; - typedef v8::Handle NAN_PROPERTY_ENUMERATOR_RETURN_TYPE; - - typedef const v8::AccessorInfo& NAN_PROPERTY_DELETER_ARGS_TYPE; - typedef v8::Handle NAN_PROPERTY_DELETER_RETURN_TYPE; - - typedef const v8::AccessorInfo& NAN_PROPERTY_QUERY_ARGS_TYPE; - typedef v8::Handle NAN_PROPERTY_QUERY_RETURN_TYPE; - - typedef const v8::AccessorInfo& NAN_INDEX_GETTER_ARGS_TYPE; - typedef v8::Handle NAN_INDEX_GETTER_RETURN_TYPE; - - typedef const v8::AccessorInfo& NAN_INDEX_SETTER_ARGS_TYPE; - typedef v8::Handle NAN_INDEX_SETTER_RETURN_TYPE; - - typedef const v8::AccessorInfo& NAN_INDEX_ENUMERATOR_ARGS_TYPE; - typedef v8::Handle NAN_INDEX_ENUMERATOR_RETURN_TYPE; - - typedef const v8::AccessorInfo& NAN_INDEX_DELETER_ARGS_TYPE; - typedef v8::Handle NAN_INDEX_DELETER_RETURN_TYPE; - - typedef const v8::AccessorInfo& NAN_INDEX_QUERY_ARGS_TYPE; - typedef v8::Handle NAN_INDEX_QUERY_RETURN_TYPE; - -# define NanReturnValue(value) \ - return Nan::imp::NanEnsureHandleOrPersistent(value) -# define NanReturnHolder() NanReturnValue(args.Holder()) -# define NanReturnThis() NanReturnValue(args.This()) -# define NanReturnUndefined() return v8::Undefined() -# define NanReturnNull() return v8::Null() -# define NanReturnEmptyString() return v8::String::Empty() - NAN_INLINE v8::Local NanUndefined() { NanEscapableScope scope; return scope.Escape(NanNew(v8::Undefined())); @@ -974,17 +876,14 @@ class NanEscapableScope { return v8::Exception::NAME(errmsg); \ } \ \ - NAN_INLINE v8::Local NanThrow ## NAME(const char *errmsg) { \ - NanEscapableScope scope; \ - return scope.Escape(NanNew(v8::ThrowException( \ - v8::Exception::NAME(NanNew(errmsg))))); \ + NAN_INLINE void NanThrow ## NAME(const char *errmsg) { \ + NanScope scope; \ + v8::ThrowException(v8::Exception::NAME(NanNew(errmsg))); \ } \ \ NAN_INLINE \ - v8::Local NanThrow ## NAME(v8::Handle errmsg) { \ - NanEscapableScope scope; \ - return scope.Escape(NanNew(v8::ThrowException( \ - v8::Exception::NAME(errmsg)))); \ + void NanThrow ## NAME(v8::Handle errmsg) { \ + v8::ThrowException(v8::Exception::NAME(errmsg)); \ } X(Error) @@ -995,9 +894,8 @@ class NanEscapableScope { # undef X - NAN_INLINE v8::Local NanThrowError(v8::Handle error) { - NanEscapableScope scope; - return scope.Escape(v8::Local::New(v8::ThrowException(error))); + NAN_INLINE void NanThrowError(v8::Handle error) { + v8::ThrowException(error); } NAN_INLINE v8::Local NanNewBufferHandle ( @@ -1201,55 +1099,102 @@ class NanEscapableScope { typedef void (*NanFreeCallback)(char *data, void *hint); -#define NAN_METHOD(name) NAN_METHOD_RETURN_TYPE name(NAN_METHOD_ARGS_TYPE args) +typedef const NanFunctionCallbackInfo& NAN_METHOD_ARGS_TYPE; +typedef void NAN_METHOD_RETURN_TYPE; + +typedef const NanPropertyCallbackInfo& NAN_GETTER_ARGS_TYPE; +typedef void NAN_GETTER_RETURN_TYPE; + +typedef const NanPropertyCallbackInfo& NAN_SETTER_ARGS_TYPE; +typedef void NAN_SETTER_RETURN_TYPE; + +typedef const NanPropertyCallbackInfo& + NAN_PROPERTY_GETTER_ARGS_TYPE; +typedef void NAN_PROPERTY_GETTER_RETURN_TYPE; + +typedef const NanPropertyCallbackInfo& + NAN_PROPERTY_SETTER_ARGS_TYPE; +typedef void NAN_PROPERTY_SETTER_RETURN_TYPE; + +typedef const NanPropertyCallbackInfo& + NAN_PROPERTY_ENUMERATOR_ARGS_TYPE; +typedef void NAN_PROPERTY_ENUMERATOR_RETURN_TYPE; + +typedef const NanPropertyCallbackInfo& + NAN_PROPERTY_DELETER_ARGS_TYPE; +typedef void NAN_PROPERTY_DELETER_RETURN_TYPE; + +typedef const NanPropertyCallbackInfo& + NAN_PROPERTY_QUERY_ARGS_TYPE; +typedef void NAN_PROPERTY_QUERY_RETURN_TYPE; + +typedef const NanPropertyCallbackInfo& NAN_INDEX_GETTER_ARGS_TYPE; +typedef void NAN_INDEX_GETTER_RETURN_TYPE; + +typedef const NanPropertyCallbackInfo& NAN_INDEX_SETTER_ARGS_TYPE; +typedef void NAN_INDEX_SETTER_RETURN_TYPE; + +typedef const NanPropertyCallbackInfo& + NAN_INDEX_ENUMERATOR_ARGS_TYPE; +typedef void NAN_INDEX_ENUMERATOR_RETURN_TYPE; + +typedef const NanPropertyCallbackInfo& + NAN_INDEX_DELETER_ARGS_TYPE; +typedef void NAN_INDEX_DELETER_RETURN_TYPE; + +typedef const NanPropertyCallbackInfo& + NAN_INDEX_QUERY_ARGS_TYPE; +typedef void NAN_INDEX_QUERY_RETURN_TYPE; + +#define NAN_METHOD(name) NAN_METHOD_RETURN_TYPE name(NAN_METHOD_ARGS_TYPE info) #define NAN_GETTER(name) \ NAN_GETTER_RETURN_TYPE name( \ v8::Local property \ - , NAN_GETTER_ARGS_TYPE args) + , NAN_GETTER_ARGS_TYPE info) #define NAN_SETTER(name) \ NAN_SETTER_RETURN_TYPE name( \ v8::Local property \ , v8::Local value \ - , NAN_SETTER_ARGS_TYPE args) + , NAN_SETTER_ARGS_TYPE info) #define NAN_PROPERTY_GETTER(name) \ NAN_PROPERTY_GETTER_RETURN_TYPE name( \ v8::Local property \ - , NAN_PROPERTY_GETTER_ARGS_TYPE args) + , NAN_PROPERTY_GETTER_ARGS_TYPE info) #define NAN_PROPERTY_SETTER(name) \ NAN_PROPERTY_SETTER_RETURN_TYPE name( \ v8::Local property \ , v8::Local value \ - , NAN_PROPERTY_SETTER_ARGS_TYPE args) + , NAN_PROPERTY_SETTER_ARGS_TYPE info) #define NAN_PROPERTY_ENUMERATOR(name) \ NAN_PROPERTY_ENUMERATOR_RETURN_TYPE name( \ - NAN_PROPERTY_ENUMERATOR_ARGS_TYPE args) + NAN_PROPERTY_ENUMERATOR_ARGS_TYPE info) #define NAN_PROPERTY_DELETER(name) \ NAN_PROPERTY_DELETER_RETURN_TYPE name( \ v8::Local property \ - , NAN_PROPERTY_DELETER_ARGS_TYPE args) + , NAN_PROPERTY_DELETER_ARGS_TYPE info) #define NAN_PROPERTY_QUERY(name) \ NAN_PROPERTY_QUERY_RETURN_TYPE name( \ v8::Local property \ - , NAN_PROPERTY_QUERY_ARGS_TYPE args) + , NAN_PROPERTY_QUERY_ARGS_TYPE info) # define NAN_INDEX_GETTER(name) \ NAN_INDEX_GETTER_RETURN_TYPE name( \ uint32_t index \ - , NAN_INDEX_GETTER_ARGS_TYPE args) + , NAN_INDEX_GETTER_ARGS_TYPE info) #define NAN_INDEX_SETTER(name) \ NAN_INDEX_SETTER_RETURN_TYPE name( \ uint32_t index \ , v8::Local value \ - , NAN_INDEX_SETTER_ARGS_TYPE args) + , NAN_INDEX_SETTER_ARGS_TYPE info) #define NAN_INDEX_ENUMERATOR(name) \ - NAN_INDEX_ENUMERATOR_RETURN_TYPE name(NAN_INDEX_ENUMERATOR_ARGS_TYPE args) + NAN_INDEX_ENUMERATOR_RETURN_TYPE name(NAN_INDEX_ENUMERATOR_ARGS_TYPE info) #define NAN_INDEX_DELETER(name) \ NAN_INDEX_DELETER_RETURN_TYPE name( \ uint32_t index \ - , NAN_INDEX_DELETER_ARGS_TYPE args) + , NAN_INDEX_DELETER_ARGS_TYPE info) #define NAN_INDEX_QUERY(name) \ NAN_INDEX_QUERY_RETURN_TYPE name( \ uint32_t index \ - , NAN_INDEX_QUERY_ARGS_TYPE args) + , NAN_INDEX_QUERY_ARGS_TYPE info) class NanCallback { public: @@ -1633,7 +1578,7 @@ IsExternal(v8::Local str) { namespace Nan { enum Encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER}; -} +} // end of namespace Nan #if NODE_MODULE_VERSION < NODE_0_10_MODULE_VERSION # include "nan_string_bytes.h" // NOLINT(build/include) @@ -1748,6 +1693,288 @@ NAN_INLINE void NanSetInstanceTemplate( NanSetTemplate(templ->InstanceTemplate(), name, value, attributes); } +template +NAN_INLINE void NanSetMethod( + const T &recv + , const char *name + , NanFunctionCallback callback) { + NanScope scope; + v8::Local fn = NanNew( + callback)->GetFunction(); + v8::Local fn_name = NanNew(name); + fn->SetName(fn_name); + recv->Set(fn_name, fn); +} + +NAN_INLINE void NanSetPrototypeMethod( + v8::Handle recv + , const char* name, NanFunctionCallback callback) { + NanScope scope; + v8::Local fn = NanNew( + callback + , v8::Handle() + , NanNew(recv))->GetFunction(); + v8::Local fn_name = NanNew(name); + recv->PrototypeTemplate()->Set(fn_name, fn); + fn->SetName(fn_name); +} + +//=== Accessors and Such ======================================================= + +inline void NanSetAccessor( + v8::Handle tpl + , v8::Handle name + , NanGetterCallback getter + , NanSetterCallback setter = 0 + , v8::Handle data = v8::Handle() + , v8::AccessControl settings = v8::DEFAULT + , v8::PropertyAttribute attribute = v8::None + , Nan::imp::Sig signature = Nan::imp::Sig()) { + NanScope scope; + + Nan::imp::NativeGetter getter_ = + Nan::imp::GetterCallbackWrapper; + Nan::imp::NativeSetter setter_ = + setter ? Nan::imp::SetterCallbackWrapper : 0; + + v8::Local otpl = NanNew(); + otpl->SetInternalFieldCount(Nan::imp::kAccessorFieldCount); + v8::Local obj = otpl->NewInstance(); + NanSetInternalFieldPointer( + obj + , Nan::imp::kGetterIndex + , Nan::imp::GetWrapper(getter)); + v8::Local val = NanNew(data); + + if (setter != 0) { + NanSetInternalFieldPointer( + obj + , Nan::imp::kSetterIndex + , Nan::imp::GetWrapper(setter)); + } + + if (!val.IsEmpty()) { + obj->SetInternalField(Nan::imp::kDataIndex, val); + } + + tpl->SetAccessor( + name + , getter_ + , setter_ + , obj + , settings + , attribute + , signature); +} + +inline bool NanSetAccessor( + v8::Handle obj + , v8::Handle name + , NanGetterCallback getter + , NanSetterCallback setter = 0 + , v8::Handle data = v8::Handle() + , v8::AccessControl settings = v8::DEFAULT + , v8::PropertyAttribute attribute = v8::None) { + NanEscapableScope scope; + + Nan::imp::NativeGetter getter_ = + Nan::imp::GetterCallbackWrapper; + Nan::imp::NativeSetter setter_ = + setter ? Nan::imp::SetterCallbackWrapper : 0; + + v8::Local otpl = NanNew(); + otpl->SetInternalFieldCount(Nan::imp::kAccessorFieldCount); + v8::Local dataobj = otpl->NewInstance(); + NanSetInternalFieldPointer( + dataobj + , Nan::imp::kGetterIndex + , Nan::imp::GetWrapper(getter)); + v8::Local val = NanNew(data); + + if (!val.IsEmpty()) { + dataobj->SetInternalField(Nan::imp::kDataIndex, val); + } + + if (setter) { + NanSetInternalFieldPointer( + dataobj + , Nan::imp::kPropertySetterIndex + , Nan::imp::GetWrapper(setter)); + } + + return obj->SetAccessor( + name + , getter_ + , setter_ + , dataobj + , settings + , attribute); +} + +inline void NanSetNamedPropertyHandler( + v8::Handle tpl + , NanPropertyGetterCallback getter + , NanPropertySetterCallback setter = 0 + , NanPropertyQueryCallback query = 0 + , NanPropertyDeleterCallback deleter = 0 + , NanPropertyEnumeratorCallback enumerator = 0 + , v8::Handle data = v8::Handle()) { + NanScope scope; + + Nan::imp::NativePropertyGetter getter_ = + Nan::imp::PropertyGetterCallbackWrapper; + Nan::imp::NativePropertySetter setter_ = + setter ? Nan::imp::PropertySetterCallbackWrapper : 0; + Nan::imp::NativePropertyQuery query_ = + query ? Nan::imp::PropertyQueryCallbackWrapper : 0; + Nan::imp::NativePropertyDeleter *deleter_ = + deleter ? Nan::imp::PropertyDeleterCallbackWrapper : 0; + Nan::imp::NativePropertyEnumerator enumerator_ = + enumerator ? Nan::imp::PropertyEnumeratorCallbackWrapper : 0; + + v8::Local otpl = NanNew(); + otpl->SetInternalFieldCount(Nan::imp::kPropertyFieldCount); + v8::Local obj = otpl->NewInstance(); + NanSetInternalFieldPointer( + obj + , Nan::imp::kPropertyGetterIndex + , Nan::imp::GetWrapper(getter)); + v8::Local val = NanNew(data); + + if (setter) { + NanSetInternalFieldPointer( + obj + , Nan::imp::kPropertySetterIndex + , Nan::imp::GetWrapper(setter)); + } + + if (query) { + NanSetInternalFieldPointer( + obj + , Nan::imp::kPropertyQueryIndex + , Nan::imp::GetWrapper(query)); + } + + if (deleter) { + NanSetInternalFieldPointer( + obj + , Nan::imp::kPropertyDeleterIndex + , Nan::imp::GetWrapper(deleter)); + } + + if (enumerator) { + NanSetInternalFieldPointer( + obj + , Nan::imp::kPropertyEnumeratorIndex + , Nan::imp::GetWrapper(enumerator)); + } + + if (!val.IsEmpty()) { + obj->SetInternalField(Nan::imp::kDataIndex, val); + } + +#if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION + tpl->SetHandler(v8::NamedPropertyHandlerConfiguration( + getter_, setter_, query_, deleter_, enumerator_, obj)); +#else + tpl->SetNamedPropertyHandler( + getter_ + , setter_ + , query_ + , deleter_ + , enumerator_ + , obj); +#endif +} + +inline void NanSetIndexedPropertyHandler( + v8::Handle tpl + , NanIndexGetterCallback getter + , NanIndexSetterCallback setter = 0 + , NanIndexQueryCallback query = 0 + , NanIndexDeleterCallback deleter = 0 + , NanIndexEnumeratorCallback enumerator = 0 + , v8::Handle data = v8::Handle()) { + NanScope scope; + + Nan::imp::NativeIndexGetter getter_ = + Nan::imp::IndexGetterCallbackWrapper; + Nan::imp::NativeIndexSetter setter_ = + setter ? Nan::imp::IndexSetterCallbackWrapper : 0; + Nan::imp::NativeIndexQuery query_ = + query ? Nan::imp::IndexQueryCallbackWrapper : 0; + Nan::imp::NativeIndexDeleter deleter_ = + deleter ? Nan::imp::IndexDeleterCallbackWrapper : 0; + Nan::imp::NativeIndexEnumerator enumerator_ = + enumerator ? Nan::imp::IndexEnumeratorCallbackWrapper : 0; + + v8::Local otpl = NanNew(); + otpl->SetInternalFieldCount(Nan::imp::kIndexPropertyFieldCount); + v8::Local obj = otpl->NewInstance(); + NanSetInternalFieldPointer( + obj + , Nan::imp::kIndexPropertyGetterIndex + , Nan::imp::GetWrapper(getter)); + v8::Local val = NanNew(data); + + if (setter) { + NanSetInternalFieldPointer( + obj + , Nan::imp::kIndexPropertySetterIndex + , Nan::imp::GetWrapper(setter)); + } + + if (query) { + NanSetInternalFieldPointer( + obj + , Nan::imp::kIndexPropertyQueryIndex + , Nan::imp::GetWrapper(query)); + } + + if (deleter) { + NanSetInternalFieldPointer( + obj + , Nan::imp::kIndexPropertyDeleterIndex + , Nan::imp::GetWrapper(deleter)); + } + + if (enumerator) { + NanSetInternalFieldPointer( + obj + , Nan::imp::kIndexPropertyEnumeratorIndex + , Nan::imp::GetWrapper(enumerator)); + } + + if (!val.IsEmpty()) { + obj->SetInternalField(Nan::imp::kDataIndex, val); + } + +#if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION + tpl->SetHandler(v8::IndexedPropertyHandlerConfiguration( + getter_, setter_, query_, deleter_, enumerator_, obj)); +#else + tpl->SetIndexedPropertyHandler( + getter_ + , setter_ + , query_ + , deleter_ + , enumerator_ + , obj); +#endif +} + //=== ObjectWrap =============================================================== class NanObjectWrap : public node::ObjectWrap { @@ -1793,6 +2020,12 @@ struct NanTap { NanMakeCallback(NanNew(t_), "ok", msg ? 2 : 1, args); } + inline void pass(const char * msg = NULL) { + v8::Handle hmsg; + if (msg) hmsg = NanNew(msg); + NanMakeCallback(NanNew(t_), "pass", msg ? 1 : 0, &hmsg); + } + private: NanPersistent t_; }; @@ -1802,8 +2035,6 @@ struct NanTap { #define NAN_TEST_EXPRESSION(expression) \ ( expression ), __FILE__ ":" NAN_STRINGIZE(__LINE__) ": " #expression -#define return_NanValue(v) NanReturnValue(v) -#define return_NanUndefined() NanReturnUndefined() #define NAN_EXPORT(target, function) NanExport(target, #function, function) #undef TYPE_CHECK diff --git a/nan_callbacks.h b/nan_callbacks.h new file mode 100644 index 00000000..77ac18de --- /dev/null +++ b/nan_callbacks.h @@ -0,0 +1,133 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#ifndef NAN_CALLBACKS_H_ +#define NAN_CALLBACKS_H_ + +#if defined(_MSC_VER) +# pragma warning( push ) +# pragma warning( disable : 4530 ) +# include +# pragma warning( pop ) +#else +# include +#endif + +template class NanFunctionCallbackInfo; +template class NanPropertyCallbackInfo; +template class NanGlobal; + +typedef void(*NanFunctionCallback)(const NanFunctionCallbackInfo&); +typedef void(*NanGetterCallback) + (v8::Local, const NanPropertyCallbackInfo&); +typedef void(*NanSetterCallback)( + v8::Local, + v8::Local, + const NanPropertyCallbackInfo&); +typedef void(*NanPropertyGetterCallback)( + v8::Local, + const NanPropertyCallbackInfo&); +typedef void(*NanPropertySetterCallback)( + v8::Local, + v8::Local, + const NanPropertyCallbackInfo&); +typedef void(*NanPropertyEnumeratorCallback) + (const NanPropertyCallbackInfo&); +typedef void(*NanPropertyDeleterCallback)( + v8::Local, + const NanPropertyCallbackInfo&); +typedef void(*NanPropertyQueryCallback)( + v8::Local, + const NanPropertyCallbackInfo&); +typedef void(*NanIndexGetterCallback)( + uint32_t, + const NanPropertyCallbackInfo&); +typedef void(*NanIndexSetterCallback)( + uint32_t, + v8::Local, + const NanPropertyCallbackInfo&); +typedef void(*NanIndexEnumeratorCallback) + (const NanPropertyCallbackInfo&); +typedef void(*NanIndexDeleterCallback)( + uint32_t, + const NanPropertyCallbackInfo&); +typedef void(*NanIndexQueryCallback)( + uint32_t, + const NanPropertyCallbackInfo&); + +namespace Nan { namespace imp { +typedef v8::Handle Sig; + +#define X(NAME) \ +struct NAME ## Wrapper { \ + explicit NAME ## Wrapper(Nan ## NAME ## Callback callback_) : \ + callback(callback_) {} \ + Nan ## NAME ## Callback callback; \ +}; + +X(Function) +X(Getter) +X(Setter) +X(PropertyGetter) +X(PropertySetter) +X(PropertyEnumerator) +X(PropertyDeleter) +X(PropertyQuery) +X(IndexGetter) +X(IndexSetter) +X(IndexEnumerator) +X(IndexDeleter) +X(IndexQuery) + +#undef X + +static const int kDataIndex = 0; + +static const int kFunctionIndex = 1; +static const int kFunctionFieldCount = 2; + +static const int kGetterIndex = 1; +static const int kSetterIndex = 2; +static const int kAccessorFieldCount = 3; + +static const int kPropertyGetterIndex = 1; +static const int kPropertySetterIndex = 2; +static const int kPropertyEnumeratorIndex = 3; +static const int kPropertyDeleterIndex = 4; +static const int kPropertyQueryIndex = 5; +static const int kPropertyFieldCount = 6; + +static const int kIndexPropertyGetterIndex = 1; +static const int kIndexPropertySetterIndex = 2; +static const int kIndexPropertyEnumeratorIndex = 3; +static const int kIndexPropertyDeleterIndex = 4; +static const int kIndexPropertyQueryIndex = 5; +static const int kIndexPropertyFieldCount = 6; + +template +P *GetWrapper(T needle) { + static std::map haystack; + typename std::map::iterator it = + haystack.find(needle); + + if (it == haystack.end()) { + return haystack.insert(it, std::make_pair(needle, new P(needle)))->second; + } else { + return it->second; + } +} +} // end of namespace imp +} // end of namespace Nan + +#if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION +# include "nan_callbacks_12_inl.h" // NOLINT(build/include) +#else +# include "nan_callbacks_pre_12_inl.h" // NOLINT(build/include) +#endif + +#endif // NAN_CALLBACKS_H_ diff --git a/nan_callbacks_12_inl.h b/nan_callbacks_12_inl.h new file mode 100644 index 00000000..6740796e --- /dev/null +++ b/nan_callbacks_12_inl.h @@ -0,0 +1,472 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#ifndef NAN_CALLBACKS_12_INL_H_ +#define NAN_CALLBACKS_12_INL_H_ + +template +class NanReturnValue { + v8::ReturnValue value_; + + public: + template + explicit inline NanReturnValue(const v8::ReturnValue &value) : + value_(value) {} + template + explicit inline NanReturnValue(const NanReturnValue& that) + : value_(that.value_) { + TYPE_CHECK(T, S); + } + + // Handle setters + template inline void Set(const v8::Handle &handle) { + TYPE_CHECK(T, S); + value_.Set(handle); + } + + template inline void Set(const NanGlobal &handle) { + TYPE_CHECK(T, S); +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && \ + (V8_MINOR_VERSION > 5 || (V8_MINOR_VERSION == 5 && \ + defined(V8_BUILD_NUMBER) && V8_BUILD_NUMBER >= 8)))) + value_.Set(handle); +#else + value_.Set(*reinterpret_cast*>(&handle)); + const_cast &>(handle).Reset(); +#endif + } + + // Fast primitive setters + inline void Set(bool value) { + TYPE_CHECK(T, v8::Boolean); + value_.Set(value); + } + + inline void Set(double i) { + TYPE_CHECK(T, v8::Number); + value_.Set(i); + } + + inline void Set(int32_t i) { + TYPE_CHECK(T, v8::Integer); + value_.Set(i); + } + + inline void Set(uint32_t i) { + TYPE_CHECK(T, v8::Integer); + value_.Set(i); + } + + // Fast JS primitive setters + inline void SetNull() { + TYPE_CHECK(T, v8::Primitive); + value_.SetNull(); + } + + inline void SetUndefined() { + TYPE_CHECK(T, v8::Primitive); + value_.SetUndefined(); + } + + inline void SetEmptyString() { + TYPE_CHECK(T, v8::String); + value_.SetEmptyString(); + } + + // Convenience getter for isolate + inline v8::Isolate *GetIsolate() const { + return value_.GetIsolate(); + } + + // Pointer setter: Uncompilable to prevent inadvertent misuse. + template + inline void Set(S *whatever) { TYPE_CHECK(S*, v8::Primitive); } +}; + +template +class NanFunctionCallbackInfo { + const v8::FunctionCallbackInfo &info_; + const v8::Local data_; + + public: + explicit inline NanFunctionCallbackInfo( + const v8::FunctionCallbackInfo &info + , v8::Local data) : + info_(info) + , data_(data) {} + + inline NanReturnValue GetReturnValue() const { + return NanReturnValue(info_.GetReturnValue()); + } + + inline v8::Local Callee() const { return info_.Callee(); } + inline v8::Local Data() const { return data_; } + inline v8::Local Holder() const { return info_.Holder(); } + inline bool IsConstructCall() const { return info_.IsConstructCall(); } + inline int Length() const { return info_.Length(); } + inline v8::Local operator[](int i) const { return info_[i]; } + inline v8::Local This() const { return info_.This(); } + inline v8::Isolate *GetIsolate() const { return info_.GetIsolate(); } + + + protected: + static const int kHolderIndex = 0; + static const int kIsolateIndex = 1; + static const int kReturnValueDefaultValueIndex = 2; + static const int kReturnValueIndex = 3; + static const int kDataIndex = 4; + static const int kCalleeIndex = 5; + static const int kContextSaveIndex = 6; + static const int kArgsLength = 7; +}; + +template +class NanPropertyCallbackInfo { + const v8::PropertyCallbackInfo &info_; + const v8::Local data_; + + public: + explicit inline NanPropertyCallbackInfo( + const v8::PropertyCallbackInfo &info + , const v8::Local &data) : + info_(info) + , data_(data) {} + + inline v8::Isolate* GetIsolate() const { return info_.GetIsolate(); } + inline v8::Local Data() const { return data_; } + inline v8::Local This() const { return info_.This(); } + inline v8::Local Holder() const { return info_.Holder(); } + inline NanReturnValue GetReturnValue() const { + return NanReturnValue(info_.GetReturnValue()); + } + + protected: + static const int kHolderIndex = 0; + static const int kIsolateIndex = 1; + static const int kReturnValueDefaultValueIndex = 2; + static const int kReturnValueIndex = 3; + static const int kDataIndex = 4; + static const int kThisIndex = 5; + static const int kArgsLength = 6; +}; + +namespace Nan { namespace imp { +static +void FunctionCallbackWrapper(const v8::FunctionCallbackInfo &info) { + v8::Local obj = info.Data().As(); + FunctionWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kFunctionIndex)); + NanFunctionCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + wrapper->callback(cbinfo); +} + +typedef void (*NativeFunction)(const v8::FunctionCallbackInfo &); + +#if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION +static +void GetterCallbackWrapper( + v8::Local property + , const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + GetterWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kGetterIndex)); + wrapper->callback(property.As(), cbinfo); +} + +typedef void (*NativeGetter) + (v8::Local, const v8::PropertyCallbackInfo &); + +static +void SetterCallbackWrapper( + v8::Local property + , v8::Local value + , const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + SetterWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kSetterIndex)); + wrapper->callback(property.As(), value, cbinfo); +} + +typedef void (*NativeSetter)( + v8::Local + , v8::Local + , const v8::PropertyCallbackInfo &); +#else +static +void GetterCallbackWrapper( + v8::Local property + , const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + GetterWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kGetterIndex)); + wrapper->callback(property, cbinfo); +} + +typedef void (*NativeGetter) + (v8::Local, const v8::PropertyCallbackInfo &); + +static +void SetterCallbackWrapper( + v8::Local property + , v8::Local value + , const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + SetterWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kSetterIndex)); + wrapper->callback(property, value, cbinfo); +} + +typedef void (*NativeSetter)( + v8::Local + , v8::Local + , const v8::PropertyCallbackInfo &); +#endif + +#if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION +static +void PropertyGetterCallbackWrapper( + v8::Local property + , const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + PropertyGetterWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kPropertyGetterIndex)); + wrapper->callback(property.As(), cbinfo); +} + +typedef void (*NativePropertyGetter) + (v8::Local, const v8::PropertyCallbackInfo &); + +static +void PropertySetterCallbackWrapper( + v8::Local property + , v8::Local value + , const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + PropertySetterWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kPropertySetterIndex)); + wrapper->callback(property.As(), value, cbinfo); +} + +typedef void (*NativePropertySetter)( + v8::Local + , v8::Local + , const v8::PropertyCallbackInfo &); + +static +void PropertyEnumeratorCallbackWrapper( + const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + PropertyEnumeratorWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kPropertyEnumeratorIndex)); + wrapper->callback(cbinfo); +} + +typedef void (*NativePropertyEnumerator) + (const v8::PropertyCallbackInfo &); + +static +void PropertyDeleterCallbackWrapper( + v8::Local property + , const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + PropertyDeleterWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kPropertyDeleterIndex)); + wrapper->callback(property.As(), cbinfo); +} + +typedef void (NativePropertyDeleter) + (v8::Local, const v8::PropertyCallbackInfo &); + +static +void PropertyQueryCallbackWrapper( + v8::Local property + , const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + PropertyQueryWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kPropertyQueryIndex)); + wrapper->callback(property.As(), cbinfo); +} + +typedef void (*NativePropertyQuery) + (v8::Local, const v8::PropertyCallbackInfo &); +#else +static +void PropertyGetterCallbackWrapper( + v8::Local property + , const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + PropertyGetterWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kPropertyGetterIndex)); + wrapper->callback(property, cbinfo); +} + +typedef void (*NativePropertyGetter) + (v8::Local, const v8::PropertyCallbackInfo &); + +static +void PropertySetterCallbackWrapper( + v8::Local property + , v8::Local value + , const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + PropertySetterWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kPropertySetterIndex)); + wrapper->callback(property, value, cbinfo); +} + +typedef void (*NativePropertySetter)( + v8::Local + , v8::Local + , const v8::PropertyCallbackInfo &); + +static +void PropertyEnumeratorCallbackWrapper( + const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + PropertyEnumeratorWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kPropertyEnumeratorIndex)); + wrapper->callback(cbinfo); +} + +typedef void (*NativePropertyEnumerator) + (const v8::PropertyCallbackInfo &); + +static +void PropertyDeleterCallbackWrapper( + v8::Local property + , const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + PropertyDeleterWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kPropertyDeleterIndex)); + wrapper->callback(property, cbinfo); +} + +typedef void (NativePropertyDeleter) + (v8::Local, const v8::PropertyCallbackInfo &); + +static +void PropertyQueryCallbackWrapper( + v8::Local property + , const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + PropertyQueryWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kPropertyQueryIndex)); + wrapper->callback(property, cbinfo); +} + +typedef void (*NativePropertyQuery) + (v8::Local, const v8::PropertyCallbackInfo &); +#endif + +static +void IndexGetterCallbackWrapper( + uint32_t index, const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + IndexGetterWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kIndexPropertyGetterIndex)); + wrapper->callback(index, cbinfo); +} + +typedef void (*NativeIndexGetter) + (uint32_t, const v8::PropertyCallbackInfo &); + +static +void IndexSetterCallbackWrapper( + uint32_t index + , v8::Local value + , const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + IndexSetterWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kIndexPropertySetterIndex)); + wrapper->callback(index, value, cbinfo); +} + +typedef void (*NativeIndexSetter)( + uint32_t + , v8::Local + , const v8::PropertyCallbackInfo &); + +static +void IndexEnumeratorCallbackWrapper( + const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + IndexEnumeratorWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kIndexPropertyEnumeratorIndex)); + wrapper->callback(cbinfo); +} + +typedef void (*NativeIndexEnumerator) + (const v8::PropertyCallbackInfo &); + +static +void IndexDeleterCallbackWrapper( + uint32_t index, const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + IndexDeleterWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kIndexPropertyDeleterIndex)); + wrapper->callback(index, cbinfo); +} + +typedef void (*NativeIndexDeleter) + (uint32_t, const v8::PropertyCallbackInfo &); + +static +void IndexQueryCallbackWrapper( + uint32_t index, const v8::PropertyCallbackInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + IndexQueryWrapper *wrapper = static_cast( + obj->GetAlignedPointerFromInternalField(kIndexPropertyQueryIndex)); + wrapper->callback(index, cbinfo); +} + +typedef void (*NativeIndexQuery) + (uint32_t, const v8::PropertyCallbackInfo &); +} // end of namespace imp +} // end of namespace Nan + +#endif // NAN_CALLBACKS_12_INL_H_ diff --git a/nan_callbacks_pre_12_inl.h b/nan_callbacks_pre_12_inl.h new file mode 100644 index 00000000..5394206c --- /dev/null +++ b/nan_callbacks_pre_12_inl.h @@ -0,0 +1,450 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#ifndef NAN_CALLBACKS_PRE_12_INL_H_ +#define NAN_CALLBACKS_PRE_12_INL_H_ + +namespace Nan { namespace imp { +template class NanReturnValueImp; +} // end of namespace imp +} // end of namespace Nan + +template +class NanReturnValue { + v8::Isolate *isolate_; + v8::Persistent *value_; + friend class Nan::imp::NanReturnValueImp; + + public: + template + explicit inline NanReturnValue(v8::Isolate *isolate, v8::Persistent *p) : + isolate_(isolate), value_(p) {} + template + explicit inline NanReturnValue(const NanReturnValue& that) + : isolate_(that.isolate_), value_(that.value_) { + TYPE_CHECK(T, S); + } + + // Handle setters + template inline void Set(const v8::Handle &handle) { + TYPE_CHECK(T, S); + value_->Dispose(); + *value_ = v8::Persistent::New(handle); + } + + template inline void Set(const NanGlobal &handle) { + TYPE_CHECK(T, S); + value_->Dispose(); + *value_ = v8::Persistent::New(handle.persistent); + const_cast &>(handle).Reset(); + } + + // Fast primitive setters + inline void Set(bool value) { + TYPE_CHECK(T, v8::Boolean); + value_->Dispose(); + *value_ = v8::Persistent::New(v8::Boolean::New(value)); + } + + inline void Set(double i) { + TYPE_CHECK(T, v8::Number); + value_->Dispose(); + *value_ = v8::Persistent::New(v8::Number::New(i)); + } + + inline void Set(int32_t i) { + TYPE_CHECK(T, v8::Integer); + value_->Dispose(); + *value_ = v8::Persistent::New(v8::Int32::New(i)); + } + + inline void Set(uint32_t i) { + TYPE_CHECK(T, v8::Integer); + value_->Dispose(); + *value_ = v8::Persistent::New(v8::Uint32::New(i)); + } + + // Fast JS primitive setters + inline void SetNull() { + TYPE_CHECK(T, v8::Primitive); + value_->Dispose(); + *value_ = v8::Persistent::New(v8::Null()); + } + + inline void SetUndefined() { + TYPE_CHECK(T, v8::Primitive); + value_->Dispose(); + *value_ = v8::Persistent::New(v8::Undefined()); + } + + inline void SetEmptyString() { + TYPE_CHECK(T, v8::String); + value_->Dispose(); + *value_ = v8::Persistent::New(v8::String::Empty()); + } + + // Convenience getter for isolate + inline v8::Isolate *GetIsolate() const { + return isolate_; + } + + // Pointer setter: Uncompilable to prevent inadvertent misuse. + template + inline void Set(S *whatever) { TYPE_CHECK(S*, v8::Primitive); } +}; + +template +class NanFunctionCallbackInfo { + const v8::Arguments &args_; + v8::Local data_; + NanReturnValue return_value_; + v8::Persistent retval_; + + public: + explicit inline NanFunctionCallbackInfo( + const v8::Arguments &args + , v8::Local data) : + args_(args) + , data_(data) + , return_value_(args.GetIsolate(), &retval_) + , retval_(v8::Persistent::New(v8::Undefined())) {} + + inline NanReturnValue GetReturnValue() const { + return NanReturnValue(return_value_); + } + + inline v8::Local Callee() const { return args_.Callee(); } + inline v8::Local Data() const { return data_; } + inline v8::Local Holder() const { return args_.Holder(); } + inline bool IsConstructCall() const { return args_.IsConstructCall(); } + inline int Length() const { return args_.Length(); } + inline v8::Local operator[](int i) const { return args_[i]; } + inline v8::Local This() const { return args_.This(); } + inline v8::Isolate *GetIsolate() const { return args_.GetIsolate(); } + + + protected: + static const int kHolderIndex = 0; + static const int kIsolateIndex = 1; + static const int kReturnValueDefaultValueIndex = 2; + static const int kReturnValueIndex = 3; + static const int kDataIndex = 4; + static const int kCalleeIndex = 5; + static const int kContextSaveIndex = 6; + static const int kArgsLength = 7; +}; + +template +class NanPropertyCallbackInfoBase { + const v8::AccessorInfo &info_; + const v8::Local &data_; + + public: + explicit inline NanPropertyCallbackInfoBase( + const v8::AccessorInfo &info + , const v8::Local &data) : + info_(info) + , data_(data) {} + + inline v8::Isolate* GetIsolate() const { return info_.GetIsolate(); } + inline v8::Local Data() const { return data_; } + inline v8::Local This() const { return info_.This(); } + inline v8::Local Holder() const { return info_.Holder(); } + + protected: + static const int kHolderIndex = 0; + static const int kIsolateIndex = 1; + static const int kReturnValueDefaultValueIndex = 2; + static const int kReturnValueIndex = 3; + static const int kDataIndex = 4; + static const int kThisIndex = 5; + static const int kArgsLength = 6; +}; + +template +class NanPropertyCallbackInfo : public NanPropertyCallbackInfoBase { + NanReturnValue return_value_; + v8::Persistent retval_; + + public: + explicit inline NanPropertyCallbackInfo( + const v8::AccessorInfo &info + , const v8::Local &data) : + NanPropertyCallbackInfoBase(info, data) + , return_value_(info.GetIsolate(), &retval_) + , retval_(v8::Persistent::New(v8::Undefined())) {} + inline NanReturnValue GetReturnValue() const { return return_value_; } +}; + +template<> +class NanPropertyCallbackInfo : + public NanPropertyCallbackInfoBase { + NanReturnValue return_value_; + v8::Persistent retval_; + + public: + explicit inline NanPropertyCallbackInfo( + const v8::AccessorInfo &info + , const v8::Local &data) : + NanPropertyCallbackInfoBase(info, data) + , return_value_(info.GetIsolate(), &retval_) + , retval_(v8::Persistent::New(v8::Local())) {} + inline NanReturnValue GetReturnValue() const { + return return_value_; + } +}; + +template<> +class NanPropertyCallbackInfo : + public NanPropertyCallbackInfoBase { + NanReturnValue return_value_; + v8::Persistent retval_; + + public: + explicit inline NanPropertyCallbackInfo( + const v8::AccessorInfo &info + , const v8::Local &data) : + NanPropertyCallbackInfoBase(info, data) + , return_value_(info.GetIsolate(), &retval_) + , retval_(v8::Persistent::New(v8::Local())) {} + inline NanReturnValue GetReturnValue() const { + return return_value_; + } +}; + +template<> +class NanPropertyCallbackInfo : + public NanPropertyCallbackInfoBase { + NanReturnValue return_value_; + v8::Persistent retval_; + + public: + explicit inline NanPropertyCallbackInfo( + const v8::AccessorInfo &info + , const v8::Local &data) : + NanPropertyCallbackInfoBase(info, data) + , return_value_(info.GetIsolate(), &retval_) + , retval_(v8::Persistent::New(v8::Local())) {} + inline NanReturnValue GetReturnValue() const { + return return_value_; + } +}; + +namespace Nan { namespace imp { +template +class NanReturnValueImp : public NanReturnValue { + public: + explicit NanReturnValueImp(NanReturnValue that) : + NanReturnValue(that) {} + NAN_INLINE v8::Handle Value() { + return *NanReturnValue::value_; + } +}; + +static +v8::Handle FunctionCallbackWrapper(const v8::Arguments &args) { + v8::Local obj = args.Data().As(); + FunctionWrapper *wrapper = static_cast( + obj->GetPointerFromInternalField(kFunctionIndex)); + NanFunctionCallbackInfo + cbinfo(args, obj->GetInternalField(kDataIndex)); + wrapper->callback(cbinfo); + return NanReturnValueImp(cbinfo.GetReturnValue()).Value(); +} + +typedef v8::Handle (*NativeFunction)(const v8::Arguments &); + +static +v8::Handle GetterCallbackWrapper( + v8::Local property, const v8::AccessorInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + GetterWrapper *wrapper = static_cast( + obj->GetPointerFromInternalField(kGetterIndex)); + wrapper->callback(property, cbinfo); + return NanReturnValueImp(cbinfo.GetReturnValue()).Value(); +} + +typedef v8::Handle (*NativeGetter) + (v8::Local, const v8::AccessorInfo &); + +static +void SetterCallbackWrapper( + v8::Local property + , v8::Local value + , const v8::AccessorInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + SetterWrapper *wrapper = static_cast( + obj->GetPointerFromInternalField(kSetterIndex)); + wrapper->callback(property, value, cbinfo); +} + +typedef void (*NativeSetter) + (v8::Local, v8::Local, const v8::AccessorInfo &); + +static +v8::Handle PropertyGetterCallbackWrapper( + v8::Local property, const v8::AccessorInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + PropertyGetterWrapper *wrapper = static_cast( + obj->GetPointerFromInternalField(kPropertyGetterIndex)); + wrapper->callback(property, cbinfo); + return NanReturnValueImp(cbinfo.GetReturnValue()).Value(); +} + +typedef v8::Handle (*NativePropertyGetter) + (v8::Local, const v8::AccessorInfo &); + +static +v8::Handle PropertySetterCallbackWrapper( + v8::Local property + , v8::Local value + , const v8::AccessorInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + PropertySetterWrapper *wrapper = static_cast( + obj->GetPointerFromInternalField(kPropertySetterIndex)); + wrapper->callback(property, value, cbinfo); + return NanReturnValueImp(cbinfo.GetReturnValue()).Value(); +} + +typedef v8::Handle (*NativePropertySetter) + (v8::Local, v8::Local, const v8::AccessorInfo &); + +static +v8::Handle PropertyEnumeratorCallbackWrapper( + const v8::AccessorInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + PropertyEnumeratorWrapper *wrapper = static_cast( + obj->GetPointerFromInternalField(kPropertyEnumeratorIndex)); + wrapper->callback(cbinfo); + return NanReturnValueImp(cbinfo.GetReturnValue()).Value(); +} + +typedef v8::Handle (*NativePropertyEnumerator) + (const v8::AccessorInfo &); + +static +v8::Handle PropertyDeleterCallbackWrapper( + v8::Local property + , const v8::AccessorInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + PropertyDeleterWrapper *wrapper = static_cast( + obj->GetPointerFromInternalField(kPropertyDeleterIndex)); + wrapper->callback(property, cbinfo); + return NanReturnValueImp(cbinfo.GetReturnValue()).Value(); +} + +typedef v8::Handle (NativePropertyDeleter) + (v8::Local, const v8::AccessorInfo &); + +static +v8::Handle PropertyQueryCallbackWrapper( + v8::Local property, const v8::AccessorInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + PropertyQueryWrapper *wrapper = static_cast( + obj->GetPointerFromInternalField(kPropertyQueryIndex)); + wrapper->callback(property, cbinfo); + return NanReturnValueImp(cbinfo.GetReturnValue()).Value(); +} + +typedef v8::Handle (*NativePropertyQuery) + (v8::Local, const v8::AccessorInfo &); + +static +v8::Handle IndexGetterCallbackWrapper( + uint32_t index, const v8::AccessorInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + IndexGetterWrapper *wrapper = static_cast( + obj->GetPointerFromInternalField(kIndexPropertyGetterIndex)); + wrapper->callback(index, cbinfo); + return NanReturnValueImp(cbinfo.GetReturnValue()).Value(); +} + +typedef v8::Handle (*NativeIndexGetter) + (uint32_t, const v8::AccessorInfo &); + +static +v8::Handle IndexSetterCallbackWrapper( + uint32_t index + , v8::Local value + , const v8::AccessorInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + IndexSetterWrapper *wrapper = static_cast( + obj->GetPointerFromInternalField(kIndexPropertySetterIndex)); + wrapper->callback(index, value, cbinfo); + return NanReturnValueImp(cbinfo.GetReturnValue()).Value(); +} + +typedef v8::Handle (*NativeIndexSetter) + (uint32_t, v8::Local, const v8::AccessorInfo &); + +static +v8::Handle IndexEnumeratorCallbackWrapper( + const v8::AccessorInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + IndexEnumeratorWrapper *wrapper = static_cast( + obj->GetPointerFromInternalField(kIndexPropertyEnumeratorIndex)); + wrapper->callback(cbinfo); + return NanReturnValueImp(cbinfo.GetReturnValue()).Value(); +} + +typedef v8::Handle (*NativeIndexEnumerator) + (const v8::AccessorInfo &); + +static +v8::Handle IndexDeleterCallbackWrapper( + uint32_t index, const v8::AccessorInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + IndexDeleterWrapper *wrapper = static_cast( + obj->GetPointerFromInternalField(kIndexPropertyDeleterIndex)); + wrapper->callback(index, cbinfo); + return NanReturnValueImp(cbinfo.GetReturnValue()).Value(); +} + +typedef v8::Handle (*NativeIndexDeleter) + (uint32_t, const v8::AccessorInfo &); + +static +v8::Handle IndexQueryCallbackWrapper( + uint32_t index, const v8::AccessorInfo &info) { + v8::Local obj = info.Data().As(); + NanPropertyCallbackInfo + cbinfo(info, obj->GetInternalField(kDataIndex)); + IndexQueryWrapper *wrapper = static_cast( + obj->GetPointerFromInternalField(kIndexPropertyQueryIndex)); + wrapper->callback(index, cbinfo); + return NanReturnValueImp(cbinfo.GetReturnValue()).Value(); +} + +typedef v8::Handle (*NativeIndexQuery) + (uint32_t, const v8::AccessorInfo &); +} // end of namespace imp +} // end of namespace Nan + +#endif // NAN_CALLBACKS_PRE_12_INL_H_ diff --git a/nan_implementation_12_inl.h b/nan_implementation_12_inl.h index 60391ff3..8b5d206b 100644 --- a/nan_implementation_12_inl.h +++ b/nan_implementation_12_inl.h @@ -16,9 +16,11 @@ # pragma warning( push ) # pragma warning( disable : 4530 ) # include +# include # pragma warning( pop ) #else # include +# include #endif namespace Nan { namespace imp { @@ -77,9 +79,26 @@ Factory::New(void * value) { Factory::return_t Factory::New( NanFunctionCallback callback , v8::Handle data) { - return v8::Function::New( v8::Isolate::GetCurrent() - , callback - , data); + v8::Isolate *isolate = v8::Isolate::GetCurrent(); + v8::EscapableHandleScope scope(isolate); + static std::map cbmap; + v8::Local tpl = v8::ObjectTemplate::New(isolate); + tpl->SetInternalFieldCount(Nan::imp::kFunctionFieldCount); + v8::Local obj = tpl->NewInstance(); + + obj->SetAlignedPointerInInternalField( + Nan::imp::kFunctionIndex + , Nan::imp::GetWrapper(callback)); + v8::Local val = v8::Local::New(isolate, data); + + if (!val.IsEmpty()) { + obj->SetInternalField(Nan::imp::kDataIndex, val); + } + + return scope.Escape(v8::Function::New( isolate + , Nan::imp::FunctionCallbackWrapper + , obj)); } //=== Function Template ======================================================== @@ -88,10 +107,27 @@ Factory::return_t Factory::New( NanFunctionCallback callback , v8::Handle data , v8::Handle signature) { - return v8::FunctionTemplate::New( v8::Isolate::GetCurrent() - , callback - , data - , signature); + v8::Isolate *isolate = v8::Isolate::GetCurrent(); + v8::EscapableHandleScope scope(isolate); + static std::map cbmap; + v8::Local tpl = v8::ObjectTemplate::New(isolate); + tpl->SetInternalFieldCount(Nan::imp::kFunctionFieldCount); + v8::Local obj = tpl->NewInstance(); + + obj->SetAlignedPointerInInternalField( + Nan::imp::kFunctionIndex + , Nan::imp::GetWrapper(callback)); + v8::Local val = v8::Local::New(isolate, data); + + if (!val.IsEmpty()) { + obj->SetInternalField(Nan::imp::kDataIndex, val); + } + + return scope.Escape(v8::FunctionTemplate::New( isolate + , Nan::imp::FunctionCallbackWrapper + , obj + , signature)); } //=== Number =================================================================== diff --git a/nan_implementation_pre_12_inl.h b/nan_implementation_pre_12_inl.h index 927c5724..9473b0b8 100644 --- a/nan_implementation_pre_12_inl.h +++ b/nan_implementation_pre_12_inl.h @@ -16,10 +16,12 @@ # pragma warning( disable : 4530 ) # include # include +# include # pragma warning( pop ) #else # include # include +# include #endif //============================================================================== @@ -98,11 +100,29 @@ Factory::return_t Factory::New( NanFunctionCallback callback , v8::Handle data , v8::Handle signature) { + v8::HandleScope scope; + + static std::map cbmap; + v8::Local tpl = v8::ObjectTemplate::New(); + tpl->SetInternalFieldCount(Nan::imp::kFunctionFieldCount); + v8::Local obj = tpl->NewInstance(); + + obj->SetPointerInInternalField( + Nan::imp::kFunctionIndex + , Nan::imp::GetWrapper(callback)); + v8::Local val = v8::Local::New(data); + + if (!val.IsEmpty()) { + obj->SetInternalField(Nan::imp::kDataIndex, val); + } + // Note(agnat): Emulate length argument here. Unfortunately, I couldn't find // a way. Have at it though... - return v8::FunctionTemplate::New( callback - , data - , signature); + return scope.Close( + v8::FunctionTemplate::New( Nan::imp::FunctionCallbackWrapper + , obj + , signature)); } //=== Number =================================================================== diff --git a/test/binding.gyp b/test/binding.gyp index 2db37af9..96066642 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -46,8 +46,8 @@ , "sources" : [ "cpp/asyncworker.cpp" ] } , { - "target_name" : "settergetter" - , "sources" : [ "cpp/settergetter.cpp" ] + "target_name" : "accessors" + , "sources" : [ "cpp/accessors.cpp" ] } , { "target_name" : "persistent" @@ -108,4 +108,12 @@ "target_name" : "error" , "sources" : [ "cpp/error.cpp" ] } + , { + "target_name" : "namedinterceptors" + , "sources" : [ "cpp/namedinterceptors.cpp" ] + } + , { + "target_name" : "indexedinterceptors" + , "sources" : [ "cpp/indexedinterceptors.cpp" ] + } ]} diff --git a/test/cpp/settergetter.cpp b/test/cpp/accessors.cpp similarity index 83% rename from test/cpp/settergetter.cpp rename to test/cpp/accessors.cpp index 20c4a933..255959db 100644 --- a/test/cpp/settergetter.cpp +++ b/test/cpp/accessors.cpp @@ -9,7 +9,7 @@ #include #include -class SetterGetter : public node::ObjectWrap { +class SetterGetter : public NanObjectWrap { public: static void Init (v8::Handle target); static v8::Handle NewInstance (); @@ -29,7 +29,7 @@ class SetterGetter : public node::ObjectWrap { static NanPersistent settergetter_constructor; NAN_METHOD(CreateNew) { - NanReturnValue(SetterGetter::NewInstance()); + info.GetReturnValue().Set(SetterGetter::NewInstance()); } SetterGetter::SetterGetter() { @@ -45,11 +45,12 @@ void SetterGetter::Init(v8::Handle target) { settergetter_constructor.Reset(tpl); tpl->SetClassName(NanNew("SetterGetter")); tpl->InstanceTemplate()->SetInternalFieldCount(1); - NODE_SET_PROTOTYPE_METHOD(tpl, "log", SetterGetter::Log); + NanSetPrototypeMethod(tpl, "log", SetterGetter::Log); v8::Local proto = tpl->PrototypeTemplate(); - proto->SetAccessor(NanNew("prop1"), SetterGetter::GetProp1); - proto->SetAccessor( - NanNew("prop2") + NanSetAccessor(proto, NanNew("prop1"), SetterGetter::GetProp1); + NanSetAccessor( + proto + , NanNew("prop2") , SetterGetter::GetProp2 , SetterGetter::SetProp2 ); @@ -75,14 +76,14 @@ NAN_METHOD(SetterGetter::New) { settergetter->log , "New()\n" , sizeof (settergetter->log) - 1 - strlen(settergetter->log)); - settergetter->Wrap(args.This()); + settergetter->Wrap(info.This()); - NanReturnValue(args.This()); + info.GetReturnValue().Set(info.This()); } NAN_GETTER(SetterGetter::GetProp1) { SetterGetter* settergetter = - node::ObjectWrap::Unwrap(args.This()); + NanObjectWrap::Unwrap(info.This()); assert(strlen(settergetter->log) < sizeof (settergetter->log)); strncat( settergetter->log @@ -99,12 +100,12 @@ NAN_GETTER(SetterGetter::GetProp1) { , ")\n" , sizeof (settergetter->log) - 1 - strlen(settergetter->log)); - NanReturnValue(NanNew(settergetter->prop1)); + info.GetReturnValue().Set(NanNew(settergetter->prop1)); } NAN_GETTER(SetterGetter::GetProp2) { SetterGetter* settergetter = - node::ObjectWrap::Unwrap(args.This()); + NanObjectWrap::Unwrap(info.This()); assert(strlen(settergetter->log) < sizeof (settergetter->log)); strncat( settergetter->log @@ -121,12 +122,12 @@ NAN_GETTER(SetterGetter::GetProp2) { , ")\n" , sizeof (settergetter->log) - 1 - strlen(settergetter->log)); - NanReturnValue(NanNew(settergetter->prop2)); + info.GetReturnValue().Set(NanNew(settergetter->prop2)); } NAN_SETTER(SetterGetter::SetProp2) { SetterGetter* settergetter = - node::ObjectWrap::Unwrap(args.This()); + NanObjectWrap::Unwrap(info.This()); strncpy( settergetter->prop2 , *NanUtf8String(value) @@ -151,9 +152,9 @@ NAN_SETTER(SetterGetter::SetProp2) { NAN_METHOD(SetterGetter::Log) { SetterGetter* settergetter = - node::ObjectWrap::Unwrap(args.This()); + NanObjectWrap::Unwrap(info.This()); - NanReturnValue(NanNew(settergetter->log)); + info.GetReturnValue().Set(NanNew(settergetter->log)); } -NODE_MODULE(settergetter, SetterGetter::Init) +NODE_MODULE(accessors, SetterGetter::Init) diff --git a/test/cpp/asyncprogressworker.cpp b/test/cpp/asyncprogressworker.cpp index 0639aac8..1024755f 100644 --- a/test/cpp/asyncprogressworker.cpp +++ b/test/cpp/asyncprogressworker.cpp @@ -46,14 +46,14 @@ class ProgressWorker : public NanAsyncProgressWorker { }; NAN_METHOD(DoProgress) { - NanCallback *progress = new NanCallback(args[2].As()); - NanCallback *callback = new NanCallback(args[3].As()); + NanCallback *progress = new NanCallback(info[2].As()); + NanCallback *callback = new NanCallback(info[3].As()); NanAsyncQueueWorker(new ProgressWorker( callback , progress - , args[0]->Uint32Value() - , args[1]->Uint32Value())); - NanReturnUndefined(); + , info[0]->Uint32Value() + , info[1]->Uint32Value())); + info.GetReturnValue().SetUndefined(); } void Init(v8::Handle exports) { diff --git a/test/cpp/asyncworker.cpp b/test/cpp/asyncworker.cpp index f11b489a..a66a00b2 100644 --- a/test/cpp/asyncworker.cpp +++ b/test/cpp/asyncworker.cpp @@ -27,9 +27,9 @@ class SleepWorker : public NanAsyncWorker { }; NAN_METHOD(DoSleep) { - NanCallback *callback = new NanCallback(args[1].As()); - NanAsyncQueueWorker(new SleepWorker(callback, args[0]->Uint32Value())); - NanReturnUndefined(); + NanCallback *callback = new NanCallback(info[1].As()); + NanAsyncQueueWorker(new SleepWorker(callback, info[0]->Uint32Value())); + info.GetReturnValue().SetUndefined(); } void Init(v8::Handle exports) { diff --git a/test/cpp/asyncworkererror.cpp b/test/cpp/asyncworkererror.cpp index 6b26a65b..c30935b3 100644 --- a/test/cpp/asyncworkererror.cpp +++ b/test/cpp/asyncworkererror.cpp @@ -19,9 +19,9 @@ class ErrorWorker : public NanAsyncWorker { }; NAN_METHOD(Work) { - NanCallback *callback = new NanCallback(args[0].As()); + NanCallback *callback = new NanCallback(info[0].As()); NanAsyncQueueWorker(new ErrorWorker(callback)); - NanReturnUndefined(); + info.GetReturnValue().SetUndefined(); } void Init (v8::Handle exports) { diff --git a/test/cpp/bufferworkerpersistent.cpp b/test/cpp/bufferworkerpersistent.cpp index 9fe5a46c..fb89ff97 100644 --- a/test/cpp/bufferworkerpersistent.cpp +++ b/test/cpp/bufferworkerpersistent.cpp @@ -49,12 +49,12 @@ class BufferWorker : public NanAsyncWorker { }; NAN_METHOD(DoSleep) { - v8::Local bufferHandle = args[1].As(); - NanCallback *callback = new NanCallback(args[2].As()); + v8::Local bufferHandle = info[1].As(); + NanCallback *callback = new NanCallback(info[2].As()); assert(!callback->IsEmpty() && "Callback shoud not be empty"); NanAsyncQueueWorker( - new BufferWorker(callback, args[0]->Uint32Value(), bufferHandle)); - NanReturnUndefined(); + new BufferWorker(callback, info[0]->Uint32Value(), bufferHandle)); + info.GetReturnValue().SetUndefined(); } void Init(v8::Handle exports) { diff --git a/test/cpp/gc.cpp b/test/cpp/gc.cpp index c046f5a6..b788255a 100644 --- a/test/cpp/gc.cpp +++ b/test/cpp/gc.cpp @@ -22,11 +22,13 @@ NAN_GC_CALLBACK(gcEpilogueCallback) { NAN_METHOD(Hook) { NanAddGCPrologueCallback(gcPrologueCallback); NanAddGCEpilogueCallback(gcEpilogueCallback); - NanReturnUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(Check) { - NanReturnValue(NanNew(prologue_called && epilogue_called)); + NanScope(); + info.GetReturnValue().Set( + NanNew(prologue_called && epilogue_called)); } void Init (v8::Handle target) { diff --git a/test/cpp/indexedinterceptors.cpp b/test/cpp/indexedinterceptors.cpp new file mode 100644 index 00000000..65d56aa7 --- /dev/null +++ b/test/cpp/indexedinterceptors.cpp @@ -0,0 +1,114 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#include +#include + +class IndexedInterceptor : public NanObjectWrap { + char buf[256]; + + public: + IndexedInterceptor() { std::strncpy(this->buf, "foo", sizeof (this->buf)); } + static void Init (v8::Handle target); + static v8::Handle NewInstance (); + static NAN_METHOD(New); + + static NAN_INDEX_GETTER(PropertyGetter); + static NAN_INDEX_SETTER(PropertySetter); + static NAN_INDEX_ENUMERATOR(PropertyEnumerator); + static NAN_INDEX_DELETER(PropertyDeleter); + static NAN_INDEX_QUERY(PropertyQuery); +}; + +static NanPersistent indexedinterceptors_constructor; + +NAN_METHOD(CreateNew) { + info.GetReturnValue().Set(IndexedInterceptor::NewInstance()); +} + +void IndexedInterceptor::Init(v8::Handle target) { + v8::Local tpl = + NanNew(IndexedInterceptor::New); + indexedinterceptors_constructor.Reset(tpl); + tpl->SetClassName(NanNew("IndexedInterceptor")); + tpl->InstanceTemplate()->SetInternalFieldCount(1); + v8::Local inst = tpl->InstanceTemplate(); + + NanSetIndexedPropertyHandler( + inst + , IndexedInterceptor::PropertyGetter + , IndexedInterceptor::PropertySetter + , IndexedInterceptor::PropertyQuery + , IndexedInterceptor::PropertyDeleter + , IndexedInterceptor::PropertyEnumerator); + + v8::Local createnew = + NanNew(CreateNew)->GetFunction(); + target->Set(NanNew("create"), createnew); +} + +v8::Handle IndexedInterceptor::NewInstance () { + NanEscapableScope scope; + v8::Local constructorHandle = + NanNew(indexedinterceptors_constructor); + v8::Local instance = + constructorHandle->GetFunction()->NewInstance(0, NULL); + return scope.Escape(instance); +} + +NAN_METHOD(IndexedInterceptor::New) { + IndexedInterceptor* interceptor = new IndexedInterceptor(); + interceptor->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); +} + + +NAN_INDEX_GETTER(IndexedInterceptor::PropertyGetter) { + IndexedInterceptor* interceptor = + NanObjectWrap::Unwrap(info.This()); + if (index == 0) { + info.GetReturnValue().Set(NanNew(interceptor->buf)); + } else { + info.GetReturnValue().Set(NanNew("bar")); + } +} + +NAN_INDEX_SETTER(IndexedInterceptor::PropertySetter) { + IndexedInterceptor* interceptor = + NanObjectWrap::Unwrap(info.This()); + if (index == 0) { + std::strncpy( + interceptor->buf + , *v8::String::Utf8Value(value) + , sizeof (interceptor->buf)); + info.GetReturnValue().Set(info.This()); + } else { + info.GetReturnValue().Set(info.This()); + } +} + +NAN_INDEX_ENUMERATOR(IndexedInterceptor::PropertyEnumerator) { + v8::Local arr = NanNew(); + arr->Set(0, NanNew("whee")); + info.GetReturnValue().Set(arr); +} + +NAN_INDEX_DELETER(IndexedInterceptor::PropertyDeleter) { + IndexedInterceptor* interceptor = + NanObjectWrap::Unwrap(info.This()); + std::strncpy(interceptor->buf, "goober", sizeof (interceptor->buf)); + info.GetReturnValue().Set(NanTrue()); +} + +NAN_INDEX_QUERY(IndexedInterceptor::PropertyQuery) { + if (index == 1) { + info.GetReturnValue().Set(NanNew(v8::DontEnum)); + } +} + +NODE_MODULE(indexedinterceptors, IndexedInterceptor::Init) diff --git a/test/cpp/isolatedata.cpp b/test/cpp/isolatedata.cpp index a97a8ee5..c47ce7d7 100644 --- a/test/cpp/isolatedata.cpp +++ b/test/cpp/isolatedata.cpp @@ -23,7 +23,7 @@ NAN_METHOD(SetAndGet) { delete d1; - NanReturnValue(NanNew(d0 == d1)); + info.GetReturnValue().Set(NanNew(d0 == d1)); } void Init (v8::Handle target) { diff --git a/test/cpp/makecallback.cpp b/test/cpp/makecallback.cpp index 51ab2dce..2fc0eb25 100644 --- a/test/cpp/makecallback.cpp +++ b/test/cpp/makecallback.cpp @@ -35,21 +35,20 @@ void MyObject::Init(v8::Handle exports) { tpl->SetClassName(NanNew("MyObject")); tpl->InstanceTemplate()->SetInternalFieldCount(1); - NODE_SET_PROTOTYPE_METHOD(tpl, "call_emit", CallEmit); + NanSetPrototypeMethod(tpl, "call_emit", CallEmit); constructor.Reset(tpl->GetFunction()); exports->Set(NanNew("MyObject"), tpl->GetFunction()); } NAN_METHOD(MyObject::New) { - if (args.IsConstructCall()) { - MyObject* obj = new MyObject(); - obj->Wrap(args.This()); - NanReturnValue(args.This()); - } - else { - v8::Local cons = NanNew(constructor); - NanReturnValue(cons->NewInstance()); + if (info.IsConstructCall()) { + MyObject* obj = new MyObject(); + obj->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + } else { + v8::Local cons = NanNew(constructor); + info.GetReturnValue().Set(cons->NewInstance()); } } @@ -58,8 +57,8 @@ NAN_METHOD(MyObject::CallEmit) { NanNew("event"), // event name }; - NanMakeCallback(args.This(), "emit", 1, argv); - NanReturnUndefined(); + NanMakeCallback(info.This(), "emit", 1, argv); + info.GetReturnValue().SetUndefined(); } void Init(v8::Handle exports) { diff --git a/test/cpp/morenews.cpp b/test/cpp/morenews.cpp index 86983da6..a986f679 100644 --- a/test/cpp/morenews.cpp +++ b/test/cpp/morenews.cpp @@ -9,30 +9,30 @@ #include NAN_METHOD(NewNumber) { - NanReturnValue(NanNew(0.5)); + info.GetReturnValue().Set(NanNew(0.5)); } NAN_METHOD(NewNegativeInteger) { - NanReturnValue(NanNew(-1)); + info.GetReturnValue().Set(NanNew(-1)); } NAN_METHOD(NewPositiveInteger) { - NanReturnValue(NanNew(1)); + info.GetReturnValue().Set(NanNew(1)); } NAN_METHOD(NewUtf8String) { const char s[] = "strïng"; - NanReturnValue(NanNew(s)); + info.GetReturnValue().Set(NanNew(s)); } NAN_METHOD(NewLatin1String) { const uint8_t s[] = "str\xefng"; - NanReturnValue(NanNew(s)); + info.GetReturnValue().Set(NanNew(s)); } NAN_METHOD(NewUcs2String) { uint16_t s[] = {'s', 't', 'r', 0xef, 'n', 'g', '\0'}; - NanReturnValue(NanNew(s)); + info.GetReturnValue().Set(NanNew(s)); } static const uint16_t ws[] = {'s', 't', 'r', 0xef, 'n', 'g', '\0'}; @@ -55,12 +55,12 @@ class ExtAsciiString : public NanExternalOneByteStringResource { NAN_METHOD(NewExternalStringResource) { v8::Local ext = NanNew(new ExtString()); - NanReturnValue(ext); + info.GetReturnValue().Set(ext); } NAN_METHOD(NewExternalAsciiStringResource) { v8::Local ext = NanNew(new ExtAsciiString()); - NanReturnValue(ext); + info.GetReturnValue().Set(ext); } void Init(v8::Handle target) { diff --git a/test/cpp/multifile2.cpp b/test/cpp/multifile2.cpp index 7f0c0c51..144a0556 100644 --- a/test/cpp/multifile2.cpp +++ b/test/cpp/multifile2.cpp @@ -9,6 +9,6 @@ #include NAN_METHOD(ReturnString) { - v8::Local s = NanNew(*NanUtf8String(args[0])); - NanReturnValue(s); + v8::Local s = NanNew(*NanUtf8String(info[0])); + info.GetReturnValue().Set(s); } diff --git a/test/cpp/namedinterceptors.cpp b/test/cpp/namedinterceptors.cpp new file mode 100644 index 00000000..01bf5ef4 --- /dev/null +++ b/test/cpp/namedinterceptors.cpp @@ -0,0 +1,114 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#include +#include + +class NamedInterceptor : public NanObjectWrap { + char buf[256]; + + public: + NamedInterceptor() { std::strncpy(this->buf, "foo", sizeof (this->buf)); } + static void Init (v8::Handle target); + static v8::Handle NewInstance (); + static NAN_METHOD(New); + + static NAN_PROPERTY_GETTER(PropertyGetter); + static NAN_PROPERTY_SETTER(PropertySetter); + static NAN_PROPERTY_ENUMERATOR(PropertyEnumerator); + static NAN_PROPERTY_DELETER(PropertyDeleter); + static NAN_PROPERTY_QUERY(PropertyQuery); +}; + +static NanPersistent namedinterceptors_constructor; + +NAN_METHOD(CreateNew) { + info.GetReturnValue().Set(NamedInterceptor::NewInstance()); +} + +void NamedInterceptor::Init(v8::Handle target) { + v8::Local tpl = + NanNew(NamedInterceptor::New); + namedinterceptors_constructor.Reset(tpl); + tpl->SetClassName(NanNew("NamedInterceptor")); + tpl->InstanceTemplate()->SetInternalFieldCount(1); + v8::Local inst = tpl->InstanceTemplate(); + + NanSetNamedPropertyHandler( + inst + , NamedInterceptor::PropertyGetter + , NamedInterceptor::PropertySetter + , NamedInterceptor::PropertyQuery + , NamedInterceptor::PropertyDeleter + , NamedInterceptor::PropertyEnumerator); + + v8::Local createnew = + NanNew(CreateNew)->GetFunction(); + target->Set(NanNew("create"), createnew); +} + +v8::Handle NamedInterceptor::NewInstance () { + NanEscapableScope scope; + v8::Local constructorHandle = + NanNew(namedinterceptors_constructor); + v8::Local instance = + constructorHandle->GetFunction()->NewInstance(0, NULL); + return scope.Escape(instance); +} + +NAN_METHOD(NamedInterceptor::New) { + NamedInterceptor* interceptor = new NamedInterceptor(); + interceptor->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); +} + + +NAN_PROPERTY_GETTER(NamedInterceptor::PropertyGetter) { + NamedInterceptor* interceptor = + NanObjectWrap::Unwrap(info.This()); + if (!std::strcmp(*v8::String::Utf8Value(property), "prop")) { + info.GetReturnValue().Set(NanNew(interceptor->buf)); + } else { + info.GetReturnValue().Set(NanNew("bar")); + } +} + +NAN_PROPERTY_SETTER(NamedInterceptor::PropertySetter) { + NamedInterceptor* interceptor = + NanObjectWrap::Unwrap(info.This()); + if (!std::strcmp(*v8::String::Utf8Value(property), "prop")) { + std::strncpy( + interceptor->buf + , *v8::String::Utf8Value(value) + , sizeof (interceptor->buf)); + info.GetReturnValue().Set(info.This()); + } else { + info.GetReturnValue().Set(info.This()); + } +} + +NAN_PROPERTY_ENUMERATOR(NamedInterceptor::PropertyEnumerator) { + v8::Local arr = NanNew(); + arr->Set(0, NanNew("value")); + info.GetReturnValue().Set(arr); +} + +NAN_PROPERTY_DELETER(NamedInterceptor::PropertyDeleter) { + NamedInterceptor* interceptor = + NanObjectWrap::Unwrap(info.This()); + std::strncpy(interceptor->buf, "goober", sizeof (interceptor->buf)); + info.GetReturnValue().Set(NanTrue()); +} + +NAN_PROPERTY_QUERY(NamedInterceptor::PropertyQuery) { + if (!std::strcmp(*v8::String::Utf8Value(property), "thing")) { + info.GetReturnValue().Set(NanNew(v8::DontEnum)); + } +} + +NODE_MODULE(namedinterceptors, NamedInterceptor::Init) diff --git a/test/cpp/nancallback.cpp b/test/cpp/nancallback.cpp index 4b418149..fe32f685 100644 --- a/test/cpp/nancallback.cpp +++ b/test/cpp/nancallback.cpp @@ -9,41 +9,36 @@ #include NAN_METHOD(GlobalContext) { - NanCallback(args[0].As()).Call(0, NULL); - NanReturnUndefined(); + NanCallback(info[0].As()).Call(0, NULL); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(SpecificContext) { - NanCallback cb(args[0].As()); + NanCallback cb(info[0].As()); cb.Call(NanGetCurrentContext()->Global(), 0, NULL); - NanReturnUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(CustomReceiver) { - NanCallback cb(args[0].As()); - cb.Call(args[1].As(), 0, NULL); - NanReturnUndefined(); + NanCallback cb(info[0].As()); + cb.Call(info[1].As(), 0, NULL); } NAN_METHOD(CompareCallbacks) { - NanCallback cb1(args[0].As()); - NanCallback cb2(args[1].As()); - NanCallback cb3(args[2].As()); + NanCallback cb1(info[0].As()); + NanCallback cb2(info[1].As()); + NanCallback cb3(info[2].As()); - NanReturnValue(NanNew(cb1 == cb2 && cb1 != cb3)); + info.GetReturnValue().Set(NanNew(cb1 == cb2 && cb1 != cb3)); } NAN_METHOD(CallDirect) { - NanCallback cb(args[0].As()); + NanCallback cb(info[0].As()); (*cb)->Call(NanGetCurrentContext()->Global(), 0, NULL); - - NanReturnUndefined(); } NAN_METHOD(CallAsFunction) { - NanCallback(args[0].As())(); - - NanReturnUndefined(); + NanCallback(info[0].As())(); } void Init (v8::Handle target) { diff --git a/test/cpp/nannew.cpp b/test/cpp/nannew.cpp index d6c7ff5d..e1d45484 100644 --- a/test/cpp/nannew.cpp +++ b/test/cpp/nannew.cpp @@ -52,7 +52,7 @@ stringMatches(Local value, const char * match) { #define _(e) NAN_TEST_EXPRESSION(e) NAN_METHOD(testArray) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(3); @@ -60,11 +60,11 @@ NAN_METHOD(testArray) { t.ok(_( NanNew(7)->Length() == 7)); t.ok(_( assertType(NanNew(7)))); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(testBoolean) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(6); @@ -76,7 +76,7 @@ NAN_METHOD(testBoolean) { t.ok(_( NanNew(false)->Value() == false)); t.ok(_( assertType( NanNew(true)))); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } #if (NODE_MODULE_VERSION < 12) @@ -85,7 +85,7 @@ NAN_METHOD(testBoolean) { # define V(x) x->ValueOf() #endif NAN_METHOD(testBooleanObject) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(3); @@ -93,12 +93,12 @@ NAN_METHOD(testBooleanObject) { t.ok(_( V(NanNew(true)) == true)); t.ok(_( V(NanNew(false)) == false)); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } #undef V NAN_METHOD(testContext) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(5); t.ok(_( assertType( NanNew()))); @@ -113,45 +113,45 @@ NAN_METHOD(testContext) { NanNew(&extensions , Handle(), Handle())))); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(testDate) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(1); t.ok(_( assertType( NanNew(static_cast(time(NULL)))))); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } int ttt = 23; NAN_METHOD(testExternal) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(2); t.ok(_(NanNew(&ttt)->Value() == &ttt)); t.ok(_( assertType(NanNew(&ttt)))); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(testFunction) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(2); t.ok(_( assertType(NanNew(testFunction)))); v8::Local data = NanNew("plonk"); t.ok(_( assertType(NanNew(testFunction, data)))); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(testFunctionTemplate) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(4); @@ -165,13 +165,13 @@ NAN_METHOD(testFunctionTemplate) { t.ok(_( assertType( NanNew( testFunctionTemplate, data, signature)))); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } const double epsilon = 1e-9; NAN_METHOD(testNumber) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(17); @@ -201,42 +201,42 @@ NAN_METHOD(testNumber) { t.ok(_( fabs(NanNew(M_PI)->Value() - M_PI) < epsilon)); t.ok(_( assertType( NanNew(M_E) ))); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(testNumberObject) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(2); t.ok(_( assertType( NanNew(M_PI)))); t.ok(_( fabs(NanNew(M_PI)->NumberValue() - M_PI) < epsilon)); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(testObject) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(1); t.ok(_(assertType( NanNew()))); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(testObjectTemplate) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(1); t.ok(_(assertType( NanNew()))); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(testScript) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(6); @@ -255,11 +255,11 @@ NAN_METHOD(testScript) { t.ok(_( NanRunScript( NanNew(NanNew("2 + 3")))->Int32Value() == 5)); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(testSignature) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(3); @@ -271,11 +271,11 @@ NAN_METHOD(testSignature) { t.ok(_( assertType( NanNew(NanNew(testSignature))))); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(testString) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(14); @@ -304,7 +304,7 @@ NAN_METHOD(testString) { t.ok(_( stringMatches( NanNew(std::string("bar")), "bar"))); t.ok(_( assertType( NanNew(std::string("plonk."))))); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } #if (NODE_MODULE_VERSION < 12) @@ -313,7 +313,7 @@ NAN_METHOD(testString) { # define V(x) x->ValueOf() #endif NAN_METHOD(testStringObject) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(2); @@ -323,24 +323,24 @@ NAN_METHOD(testStringObject) { t.ok(_( assertType( NanNew(NanNew("plonk"))))); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } #undef V template Handle asHandle(Local l) { return l; } NAN_METHOD(testHandles) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(2); t.ok(_( assertType( NanNew( asHandle(NanNew("foo")))))); t.ok(_( assertType( NanNew( asHandle(NanNew(5u)))))); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(testPersistents) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(1); @@ -349,7 +349,7 @@ NAN_METHOD(testPersistents) { t.ok(_( assertType( NanNew(p)))); p.Reset(); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } //============================================================================== @@ -359,7 +359,7 @@ NAN_METHOD(testPersistents) { // See https://github.com/nodejs/nan/issues/212 NAN_METHOD(testRegression212) { - NanTap t(args[0]); + NanTap t(info[0]); t.plan(1); @@ -374,7 +374,7 @@ NAN_METHOD(testRegression212) { # pragma warning( pop ) #endif - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } /* Compile time regression test for https://github.com/nodejs/nan/issues/242 @@ -384,11 +384,11 @@ NAN_METHOD(testRegression212) { void overloaded() {} NAN_METHOD(overloaded) { overloaded(); // not unused - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(testRegression242) { - NanTap t(args[0]); + NanTap t(info[0]); // These lines must *compile*. Not much to test at runtime. Local ft = NanNew(overloaded); @@ -400,7 +400,7 @@ NAN_METHOD(testRegression242) { t.ok(true, "compile-time regression test #242"); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } @@ -409,31 +409,31 @@ NAN_METHOD(testRegression242) { //============================================================================== NAN_METHOD(newIntegerWithValue) { - return_NanValue(NanNew(args[0]->Int32Value())); + info.GetReturnValue().Set(NanNew(info[0]->Int32Value())); } NAN_METHOD(newNumberWithValue) { - return_NanValue(NanNew(args[0]->NumberValue())); + info.GetReturnValue().Set(NanNew(info[0]->NumberValue())); } NAN_METHOD(newUint32WithValue) { - return_NanValue(NanNew(args[0]->Uint32Value())); + info.GetReturnValue().Set(NanNew(info[0]->Uint32Value())); } NAN_METHOD(newStringFromChars) { - return_NanValue(NanNew("hello?")); + info.GetReturnValue().Set(NanNew("hello?")); } NAN_METHOD(newStringFromCharsWithLength) { - return_NanValue(NanNew("hello?", 4)); + info.GetReturnValue().Set(NanNew("hello?", 4)); } NAN_METHOD(newStringFromStdString) { - return_NanValue(NanNew(std::string("hello!"))); + info.GetReturnValue().Set(NanNew(std::string("hello!"))); } NAN_METHOD(newExternal) { - return_NanValue(NanNew(&ttt)); + info.GetReturnValue().Set(NanNew(&ttt)); } void Init(Handle exports) { diff --git a/test/cpp/news.cpp b/test/cpp/news.cpp index 04519ff1..d8a3df42 100644 --- a/test/cpp/news.cpp +++ b/test/cpp/news.cpp @@ -19,77 +19,78 @@ static int magic = 1337; NAN_METHOD(NewNumber) { - NanReturnValue(NanNew(0.5)); + info.GetReturnValue().Set(NanNew(0.5)); } NAN_METHOD(NewNegativeInteger) { - NanReturnValue(NanNew(-1)); + info.GetReturnValue().Set(NanNew(-1)); } NAN_METHOD(NewPositiveInteger) { - NanReturnValue(NanNew(1)); + info.GetReturnValue().Set(NanNew(1)); } NAN_METHOD(NewUnsignedInteger) { - NanReturnValue(NanNew(0xFFFFFFFFu)); + info.GetReturnValue().Set(NanNew(0xFFFFFFFFu)); } NAN_METHOD(NewInt32FromPositive) { - NanReturnValue(NanNew(0xFFFFFFFF)); + info.GetReturnValue().Set(NanNew(0xFFFFFFFF)); } NAN_METHOD(NewInt32FromNegative) { - NanReturnValue(NanNew(-1)); + info.GetReturnValue().Set(NanNew(-1)); } NAN_METHOD(NewUint32FromPositive) { - NanReturnValue(NanNew(0xFFFFFFFF)); + info.GetReturnValue().Set(NanNew(0xFFFFFFFF)); } NAN_METHOD(NewUint32FromNegative) { - NanReturnValue(NanNew(-1)); + info.GetReturnValue().Set(NanNew(-1)); } NAN_METHOD(NewUtf8String) { const char s[] = "strïng"; - NanReturnValue(NanNew(s)); + info.GetReturnValue().Set(NanNew(s)); } NAN_METHOD(NewLatin1String) { const uint8_t s[] = "str\xefng"; - NanReturnValue(NanNew(s)); + info.GetReturnValue().Set(NanNew(s)); } NAN_METHOD(NewUcs2String) { const uint16_t s[] = {'s', 't', 'r', 0xef, 'n', 'g', '\0'}; - NanReturnValue(NanNew(s)); + info.GetReturnValue().Set(NanNew(s)); } NAN_METHOD(NewStdString) { const std::string s = "strïng"; - NanReturnValue(NanNew(s)); + info.GetReturnValue().Set(NanNew(s)); } NAN_METHOD(NewRegExp) { - NanReturnValue(NanNew(NanNew("foo"), v8::RegExp::kNone)); + info.GetReturnValue().Set( + NanNew(NanNew("foo"), v8::RegExp::kNone)); } NAN_METHOD(NewStringObject) { - NanReturnValue(NanNew(NanNew("foo"))); + info.GetReturnValue().Set(NanNew(NanNew("foo"))); } NAN_METHOD(NewNumberObject) { - NanReturnValue(NanNew(0.5)); + info.GetReturnValue().Set(NanNew(0.5)); } NAN_METHOD(NewBooleanObject) { - NanReturnValue(NanNew(true)); + info.GetReturnValue().Set(NanNew(true)); } NAN_METHOD(NewExternal) { v8::Local ext = NanNew(&magic); assert(*static_cast(ext->Value()) == 1337); - NanReturnValue(NanNew("passed")); + info.GetReturnValue().Set(NanNew("passed")); } NAN_METHOD(NewSignature) { @@ -98,42 +99,42 @@ NAN_METHOD(NewSignature) { v8::Local sig = NanNew(tmpl); tmpl = NanNew( NewSignature, v8::Handle(), sig); - NanReturnValue(NanNew("string")); + info.GetReturnValue().Set(NanNew("string")); } NAN_METHOD(NewScript) { v8::Local script = NanNew(NanNew("2+4")); - NanReturnValue(NanRunScript(script)->ToInt32()); + info.GetReturnValue().Set(NanRunScript(script)->ToInt32()); } NAN_METHOD(NewScript2) { v8::ScriptOrigin origin(NanNew("x")); v8::Local script = NanNew(NanNew("2+4"), origin); - NanReturnValue(NanRunScript(script)->ToInt32()); + info.GetReturnValue().Set(NanRunScript(script)->ToInt32()); } NAN_METHOD(CompileScript) { v8::Local script = NanCompileScript(NanNew("2+4")); - NanReturnValue(NanRunScript(script)->ToInt32()); + info.GetReturnValue().Set(NanRunScript(script)->ToInt32()); } NAN_METHOD(CompileScript2) { v8::ScriptOrigin origin(NanNew("x")); v8::Local script = NanCompileScript(NanNew("2+4"), origin); - NanReturnValue(NanRunScript(script)->ToInt32()); + info.GetReturnValue().Set(NanRunScript(script)->ToInt32()); } NAN_METHOD(NewDate) { - NanReturnValue(NanNew(1337)); + info.GetReturnValue().Set(NanNew(1337)); } NAN_METHOD(NewArray) { - NanReturnValue(NanNew()); + info.GetReturnValue().Set(NanNew()); } NAN_METHOD(NewBoolean) { - NanReturnValue(NanNew(true)); + info.GetReturnValue().Set(NanNew(true)); } // #212 @@ -142,7 +143,7 @@ NAN_METHOD(NewBoolean2) { # pragma warning( push ) # pragma warning( disable : 4800 ) #endif - NanReturnValue(NanNew(1)); + info.GetReturnValue().Set(NanNew(1)); #if defined(_MSC_VER) # pragma warning( pop ) #endif diff --git a/test/cpp/objectwraphandle.cpp b/test/cpp/objectwraphandle.cpp index e4ee1bb4..ab3c3969 100644 --- a/test/cpp/objectwraphandle.cpp +++ b/test/cpp/objectwraphandle.cpp @@ -15,7 +15,7 @@ class MyObject : public NanObjectWrap { tpl->SetClassName(NanNew("MyObject")); tpl->InstanceTemplate()->SetInternalFieldCount(1); - NODE_SET_PROTOTYPE_METHOD(tpl, "getHandle", GetHandle); + NanSetPrototypeMethod(tpl, "getHandle", GetHandle); constructor.Reset(tpl->GetFunction()); exports->Set(NanNew("MyObject"), tpl->GetFunction()); @@ -26,22 +26,22 @@ class MyObject : public NanObjectWrap { ~MyObject() {} static NAN_METHOD(New) { - if (args.IsConstructCall()) { - double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue(); + if (info.IsConstructCall()) { + double value = info[0]->IsUndefined() ? 0 : info[0]->NumberValue(); MyObject *obj = new MyObject(value); - obj->Wrap(args.This()); - NanReturnThis(); + obj->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); } else { const int argc = 1; - v8::Local argv[argc] = {args[0]}; + v8::Local argv[argc] = {info[0]}; v8::Local cons = NanNew(constructor); - NanReturnValue(cons->NewInstance(argc, argv)); + info.GetReturnValue().Set(cons->NewInstance(argc, argv)); } } static NAN_METHOD(GetHandle) { - MyObject* obj = NanObjectWrap::Unwrap(args.This()); - NanReturnValue(obj->handle()); + MyObject* obj = NanObjectWrap::Unwrap(info.This()); + info.GetReturnValue().Set(obj->handle()); } static NanPersistent constructor; diff --git a/test/cpp/persistent.cpp b/test/cpp/persistent.cpp index 512c250d..4416efea 100644 --- a/test/cpp/persistent.cpp +++ b/test/cpp/persistent.cpp @@ -12,39 +12,39 @@ static NanPersistent persistentTest1; NAN_METHOD(Save1) { - persistentTest1.Reset(args[0].As()); - NanReturnUndefined(); + persistentTest1.Reset(info[0].As()); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(Get1) { - NanReturnValue(NanNew(persistentTest1)); + info.GetReturnValue().Set(NanNew(persistentTest1)); } NAN_METHOD(Dispose1) { persistentTest1.Reset(); - NanReturnUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(ToPersistentAndBackAgain) { - NanPersistent persistent(args[0].As()); + NanPersistent persistent(info[0].As()); v8::Local object = NanNew(persistent); persistent.Reset(); memset(&persistent, -1, sizeof(persistent)); // Clobber it good. - NanReturnValue(object); + info.GetReturnValue().Set(object); } NAN_METHOD(PersistentToPersistent) { - NanPersistent persistent(args[0].As()); + NanPersistent persistent(info[0].As()); persistentTest1.Reset(persistent); persistent.Reset(); persistentTest1.Reset(); - NanReturnUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(CopyablePersistent) { NanCopyablePersistentTraits::CopyablePersistent p; p = persistentTest1; - NanReturnValue(p); + info.GetReturnValue().Set(NanNew(p)); } void Init (v8::Handle target) { diff --git a/test/cpp/returnemptystring.cpp b/test/cpp/returnemptystring.cpp index 1f37033c..646cc573 100644 --- a/test/cpp/returnemptystring.cpp +++ b/test/cpp/returnemptystring.cpp @@ -9,7 +9,7 @@ #include NAN_METHOD(ReturnEmptyString) { - NanReturnEmptyString(); + info.GetReturnValue().SetEmptyString(); } void Init (v8::Handle target) { diff --git a/test/cpp/returnnull.cpp b/test/cpp/returnnull.cpp index c283cd5a..766617bb 100644 --- a/test/cpp/returnnull.cpp +++ b/test/cpp/returnnull.cpp @@ -9,7 +9,7 @@ #include NAN_METHOD(ReturnNull) { - NanReturnNull(); + info.GetReturnValue().SetNull(); } void Init (v8::Handle target) { diff --git a/test/cpp/returnundefined.cpp b/test/cpp/returnundefined.cpp index 2463f2de..9394696c 100644 --- a/test/cpp/returnundefined.cpp +++ b/test/cpp/returnundefined.cpp @@ -9,7 +9,7 @@ #include NAN_METHOD(ReturnUndefined) { - NanReturnUndefined(); + info.GetReturnValue().SetUndefined(); } void Init (v8::Handle target) { diff --git a/test/cpp/returnvalue.cpp b/test/cpp/returnvalue.cpp index 00c1b1d4..7f620d57 100644 --- a/test/cpp/returnvalue.cpp +++ b/test/cpp/returnvalue.cpp @@ -11,23 +11,21 @@ NanGlobal global; NAN_METHOD(ReturnValue) { - if (args.Length() == 1) { - NanReturnValue(args[0]); + const NanFunctionCallbackInfo cbinfo = info; + NanReturnValue ret = cbinfo.GetReturnValue(); + if (cbinfo.Length() == 1) { + ret.Set(info[0].As()); } else { - NanReturnValue(NanNew("default")); + ret.Set(NanNew("default")); } } NAN_METHOD(ReturnPrimitive) { - NanReturnValue(true); -} - -NAN_METHOD(ReturnString) { - NanReturnValue("yes, it works"); + info.GetReturnValue().Set(true); } NAN_METHOD(ReturnGlobal) { - NanReturnValue(global); + info.GetReturnValue().Set(global); } void Init (v8::Handle target) { @@ -41,10 +39,6 @@ void Init (v8::Handle target) { NanNew("p") , NanNew(ReturnPrimitive)->GetFunction() ); - target->Set( - NanNew("s") - , NanNew(ReturnString)->GetFunction() - ); target->Set( NanNew("q") , NanNew(ReturnGlobal)->GetFunction() diff --git a/test/cpp/settemplate.cpp b/test/cpp/settemplate.cpp index b916f7d1..24fcf10d 100644 --- a/test/cpp/settemplate.cpp +++ b/test/cpp/settemplate.cpp @@ -53,13 +53,13 @@ void MyObject::Init(v8::Handle exports) { } NAN_METHOD(MyObject::New) { - if (args.IsConstructCall()) { + if (info.IsConstructCall()) { MyObject* obj = new MyObject(); - obj->Wrap(args.This()); - NanReturnValue(args.This()); + obj->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); } else { v8::Local cons = NanNew(constructor); - NanReturnValue(cons->NewInstance()); + info.GetReturnValue().Set(cons->NewInstance()); } } diff --git a/test/cpp/strings.cpp b/test/cpp/strings.cpp index 7def25b2..f862d668 100644 --- a/test/cpp/strings.cpp +++ b/test/cpp/strings.cpp @@ -9,22 +9,22 @@ #include NAN_METHOD(ReturnUtf8String) { - NanReturnValue(NanNew(*NanUtf8String(args[0]))); + info.GetReturnValue().Set(NanNew(*NanUtf8String(info[0]))); } NAN_METHOD(HeapString) { - NanUtf8String *s = new NanUtf8String(args[0]); + NanUtf8String *s = new NanUtf8String(info[0]); v8::Local res = NanNew(**s); delete s; - NanReturnValue(res); + info.GetReturnValue().Set(res); } NAN_METHOD(EncodeHex) { - NanReturnValue(NanEncode("hello", 5, Nan::HEX)); + info.GetReturnValue().Set(NanEncode("hello", 5, Nan::HEX)); } NAN_METHOD(EncodeUCS2) { - NanReturnValue(NanEncode("h\0e\0l\0l\0o\0", 10, Nan::UCS2)); + info.GetReturnValue().Set(NanEncode("h\0e\0l\0l\0o\0", 10, Nan::UCS2)); } NanPersistent returnUtf8String_persistent; diff --git a/test/cpp/threadlocal.cpp b/test/cpp/threadlocal.cpp index 1680e0bf..10563cc6 100644 --- a/test/cpp/threadlocal.cpp +++ b/test/cpp/threadlocal.cpp @@ -54,9 +54,9 @@ class TlsTest : public NanAsyncWorker { }; NAN_METHOD(thread_local_storage) { - NanTap *t = new NanTap(args[0]); + NanTap *t = new NanTap(info[0]); NanAsyncQueueWorker(new TlsTest(t)); - return_NanUndefined(); + info.GetReturnValue().SetUndefined(); } void Init(v8::Handle exports) { diff --git a/test/cpp/weak.cpp b/test/cpp/weak.cpp index 6a6ee68d..1b8a2ecd 100644 --- a/test/cpp/weak.cpp +++ b/test/cpp/weak.cpp @@ -29,8 +29,8 @@ v8::Handle wrap(v8::Local func) { } NAN_METHOD(Hustle) { - cb.Reset(args[1].As()); - NanReturnValue(wrap(args[0].As())); + cb.Reset(info[1].As()); + info.GetReturnValue().Set(wrap(info[0].As())); } void Init (v8::Handle target) { diff --git a/test/cpp/weak2.cpp b/test/cpp/weak2.cpp index 58c9357d..e63dc44d 100644 --- a/test/cpp/weak2.cpp +++ b/test/cpp/weak2.cpp @@ -34,8 +34,8 @@ v8::Handle wrap() { } NAN_METHOD(Hustle) { - cb.Reset(args[0].As()); - NanReturnValue(wrap()); + cb.Reset(info[0].As()); + info.GetReturnValue().Set(wrap()); } void Init (v8::Handle target) { diff --git a/test/js/settergetter-test.js b/test/js/accessors-test.js similarity index 93% rename from test/js/settergetter-test.js rename to test/js/accessors-test.js index 8ceb6bfc..76506814 100644 --- a/test/js/settergetter-test.js +++ b/test/js/accessors-test.js @@ -8,9 +8,9 @@ const test = require('tap').test , testRoot = require('path').resolve(__dirname, '..') - , bindings = require('bindings')({ module_root: testRoot, bindings: 'settergetter' }); + , bindings = require('bindings')({ module_root: testRoot, bindings: 'accessors' }); -test('settergetter', function (t) { +test('accessors', function (t) { t.plan(4) var settergetter = bindings.create() t.equal(settergetter.prop1, 'this is property 1') diff --git a/test/js/indexedinterceptors-test.js b/test/js/indexedinterceptors-test.js new file mode 100644 index 00000000..2c07ebc4 --- /dev/null +++ b/test/js/indexedinterceptors-test.js @@ -0,0 +1,23 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +const test = require('tap').test + , testRoot = require('path').resolve(__dirname, '..') + , bindings = require('bindings')({ module_root: testRoot, bindings: 'indexedinterceptors' }); + +test('indexedinterceptors', function (t) { + t.plan(5); + var interceptor = bindings.create(); + t.equal(interceptor[2], 'bar'); + interceptor[0] = 'setting a value'; + t.equal(interceptor[0], 'setting a value'); + delete interceptor[0]; + t.equal(interceptor[0], 'goober'); + t.ok(Object.prototype.hasOwnProperty.call(interceptor, 1)); + t.equal(Object.keys(interceptor)[0], 'whee'); +}); diff --git a/test/js/namedinterceptors-test.js b/test/js/namedinterceptors-test.js new file mode 100644 index 00000000..8c548f6f --- /dev/null +++ b/test/js/namedinterceptors-test.js @@ -0,0 +1,23 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +const test = require('tap').test + , testRoot = require('path').resolve(__dirname, '..') + , bindings = require('bindings')({ module_root: testRoot, bindings: 'namedinterceptors' }); + +test('namedinterceptors', function (t) { + t.plan(5); + var interceptor = bindings.create(); + t.equal(interceptor.prop, 'foo'); + interceptor.prop = 'setting a value'; + t.equal(interceptor.prop, 'setting a value'); + delete interceptor.something; + t.equal(interceptor.prop, 'goober'); + t.ok(Object.prototype.hasOwnProperty.call(interceptor, "thing")); + t.ok(Object.keys(interceptor)[0] === 'value'); +}); diff --git a/test/js/returnvalue-test.js b/test/js/returnvalue-test.js index 74af7889..1b818f6c 100644 --- a/test/js/returnvalue-test.js +++ b/test/js/returnvalue-test.js @@ -11,14 +11,12 @@ const test = require('tap').test , bindings = require('bindings')({ module_root: testRoot, bindings: 'returnvalue' }); test('returnvalue', function (t) { - t.plan(9); + t.plan(7); t.type(bindings.r, 'function'); t.type(bindings.p, 'function'); - t.type(bindings.s, 'function'); t.type(bindings.q, 'function'); t.equal(bindings.r('a string value'), 'a string value'); t.equal(bindings.r(), 'default'); t.ok(bindings.p()); - t.equal(bindings.s(), 'yes, it works'); t.ok(bindings.q()); }); From 4b6404af2e0cb6d7fe238688ea9549de17da658d Mon Sep 17 00:00:00 2001 From: Benjamin Byholm Date: Sat, 23 May 2015 15:14:51 +0300 Subject: [PATCH 14/84] Update for V8 v4.3 --- Makefile | 13 ++ examples/async_pi_estimate/addon.cc | 4 +- nan.h | 160 ++++++++++++---- nan_converters.h | 65 +++++++ nan_converters_43_inl.h | 42 ++++ nan_converters_pre_43_inl.h | 42 ++++ nan_implementation_12_inl.h | 159 +++++++++++++-- nan_implementation_pre_12_inl.h | 28 +-- nan_maybe_43_inl.h | 215 +++++++++++++++++++++ nan_maybe_pre_43_inl.h | 287 ++++++++++++++++++++++++++++ nan_new.h | 30 ++- nan_string_bytes.h | 16 +- test/binding.gyp | 8 + test/cpp/accessors.cpp | 23 ++- test/cpp/accessors2.cpp | 161 ++++++++++++++++ test/cpp/asyncprogressworker.cpp | 9 +- test/cpp/asyncworker.cpp | 8 +- test/cpp/asyncworkererror.cpp | 4 +- test/cpp/bufferworkerpersistent.cpp | 15 +- test/cpp/converters.cpp | 128 +++++++++++++ test/cpp/error.cpp | 9 +- test/cpp/gc.cpp | 8 +- test/cpp/indexedinterceptors.cpp | 10 +- test/cpp/isolatedata.cpp | 4 +- test/cpp/makecallback.cpp | 6 +- test/cpp/morenews.cpp | 42 ++-- test/cpp/multifile1.cpp | 4 +- test/cpp/multifile2.cpp | 2 +- test/cpp/namedinterceptors.cpp | 10 +- test/cpp/nancallback.cpp | 26 ++- test/cpp/nannew.cpp | 97 ++++++---- test/cpp/news.cpp | 153 ++++++++------- test/cpp/objectwraphandle.cpp | 4 +- test/cpp/persistent.cpp | 24 +-- test/cpp/returnemptystring.cpp | 4 +- test/cpp/returnnull.cpp | 4 +- test/cpp/returnundefined.cpp | 4 +- test/cpp/returnvalue.cpp | 14 +- test/cpp/settemplate.cpp | 38 +++- test/cpp/strings.cpp | 16 +- test/cpp/symbols.cpp | 5 +- test/cpp/threadlocal.cpp | 6 +- test/cpp/weak.cpp | 6 +- test/cpp/weak2.cpp | 6 +- test/js/accessors2-test.js | 27 +++ test/js/converters-test.js | 45 +++++ 46 files changed, 1652 insertions(+), 339 deletions(-) create mode 100644 nan_converters.h create mode 100644 nan_converters_43_inl.h create mode 100644 nan_converters_pre_43_inl.h create mode 100644 nan_maybe_43_inl.h create mode 100644 nan_maybe_pre_43_inl.h create mode 100644 test/cpp/accessors2.cpp create mode 100644 test/cpp/converters.cpp create mode 100644 test/js/accessors2-test.js create mode 100644 test/js/converters-test.js diff --git a/Makefile b/Makefile index 665471a4..34138dce 100644 --- a/Makefile +++ b/Makefile @@ -18,14 +18,20 @@ LINT_SOURCES = \ nan_callbacks.h \ nan_callbacks_12_inl.h \ nan_callbacks_pre_12_inl.h \ + nan_converters.h \ + nan_converters_43_inl.h \ + nan_converters_pre_43_inl.h \ nan_implementation_12_inl.h \ nan_implementation_pre_12_inl.h \ + nan_maybe_43_inl.h \ + nan_maybe_pre_43_inl.h \ nan_new.h \ nan_persistent_12_inl.h \ nan_persistent_pre_12_inl.h \ nan_string_bytes.h \ nan_weak.h \ test/cpp/accessors.cpp \ + test/cpp/accessors2.cpp \ test/cpp/asyncworker.cpp \ test/cpp/asyncprogressworker.cpp \ test/cpp/asyncworkererror.cpp \ @@ -36,6 +42,10 @@ LINT_SOURCES = \ test/cpp/isolatedata.cpp \ test/cpp/makecallback.cpp \ test/cpp/morenews.cpp \ + test/cpp/converters.cpp \ + test/cpp/isolatedata.cpp \ + test/cpp/makecallback.cpp \ + test/cpp/morenews.cpp \ test/cpp/multifile1.cpp \ test/cpp/multifile2.cpp \ test/cpp/multifile2.h \ @@ -54,6 +64,7 @@ LINT_SOURCES = \ test/cpp/strings.cpp \ test/cpp/symbols.cpp \ test/cpp/threadlocal.cpp \ + test/cpp/trycatch.cpp \ test/cpp/weak.cpp \ test/cpp/weak2.cpp \ node_modules/node-gyp/gyp/data/win/large-pdb-shim.cc @@ -74,6 +85,8 @@ forcetest: $(ADDONS): nan.h nan_new.h nan_implementation_pre_12_inl.h nan_implementation_12_inl.h \ nan_callbacks.h nan_callbacks_12_inl.h nan_callbacks_pre_12_inl.h \ + nan_converters.h nan_converters_43_inl.h nan_converters_pre_43_inl.h \ + nan_maybe_43_inl.h nan_maybe_pre_43_inl.h \ nan_persistent_12_inl.h nan_persistent_pre_12_inl.h nan_weak.h \ nan_string_bytes.h test/binding.gyp $(SOURCES) cd test/ && ../node_modules/.bin/node-gyp rebuild diff --git a/examples/async_pi_estimate/addon.cc b/examples/async_pi_estimate/addon.cc index e8956938..b0e72da3 100644 --- a/examples/async_pi_estimate/addon.cc +++ b/examples/async_pi_estimate/addon.cc @@ -18,10 +18,10 @@ using v8::String; // Expose synchronous and asynchronous access to our // Estimate() function void InitAll(Handle exports) { - exports->Set(NanNew("calculateSync"), + exports->Set(NanNew("calculateSync").ToLocalChecked(), NanNew(CalculateSync)->GetFunction()); - exports->Set(NanNew("calculateAsync"), + exports->Set(NanNew("calculateAsync").ToLocalChecked(), NanNew(CalculateAsync)->GetFunction()); } diff --git a/nan.h b/nan.h index ecaf6819..3316522b 100644 --- a/nan.h +++ b/nan.h @@ -118,7 +118,7 @@ typedef v8::String::ExternalOneByteStringResource NanExternalOneByteStringResource; #endif -#if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION +#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION) template class NanNonCopyablePersistentTraits : public v8::NonCopyablePersistentTraits {}; @@ -138,8 +138,17 @@ template class NanPersistentBase; template class NanWeakCallbackData; template > class NanPersistent; +#endif // NODE_MODULE_VERSION + + +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) +# include "nan_maybe_43_inl.h" // NOLINT(build/include) +#else +# include "nan_maybe_pre_43_inl.h" // NOLINT(build/include) #endif +#include "nan_converters.h" // NOLINT(build/include) #include "nan_new.h" // NOLINT(build/include) // uv helpers @@ -276,6 +285,19 @@ template class NanWeakCallbackInfo; #endif namespace Nan { namespace imp { + template + NAN_INLINE + v8::Local + NanUnwrap(NanMaybeLocal maybe) { + return maybe.ToLocalChecked(); + } + + template + NAN_INLINE + v8::Local NanUnwrap(v8::Local val) { + return val; + } + template NAN_INLINE v8::Persistent &NanEnsureHandleOrPersistent( v8::Persistent &val) { // NOLINT(runtime/references) @@ -294,8 +316,10 @@ namespace Nan { namespace imp { } template - NAN_INLINE v8::Local NanEnsureHandleOrPersistent(const T &val) { - return NanNew(val); + NAN_INLINE + v8::Local + NanEnsureHandleOrPersistent(T val) { + return NanUnwrap(NanNew(val)); } template @@ -314,8 +338,9 @@ namespace Nan { namespace imp { } template - NAN_INLINE v8::Local NanEnsureLocal(const T &val) { - return NanNew(val); + NAN_INLINE + v8::Local NanEnsureLocal(T val) { + return NanUnwrap(NanNew(val)); } } // end of namespace imp } // end of namespace Nan @@ -405,9 +430,17 @@ class NanEscapableScope { v8::Isolate::GetCurrent()->SetAddHistogramSampleFunction(cb); } +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) + NAN_INLINE bool NanIdleNotification(int idle_time_in_ms) { + return v8::Isolate::GetCurrent()->IdleNotificationDeadline( + idle_time_in_ms * 0.001); + } +# else NAN_INLINE bool NanIdleNotification(int idle_time_in_ms) { return v8::Isolate::GetCurrent()->IdleNotification(idle_time_in_ms); } +#endif NAN_INLINE void NanLowMemoryNotification() { v8::Isolate::GetCurrent()->LowMemoryNotification(); @@ -466,6 +499,10 @@ class NanEscapableScope { return scope.Escape(NanNew(v8::False(v8::Isolate::GetCurrent()))); } + NAN_INLINE v8::Local NanEmptyString() { + return v8::String::Empty(v8::Isolate::GetCurrent()); + } + NAN_INLINE int NanAdjustExternalMemory(int bc) { return static_cast( v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(bc)); @@ -534,26 +571,26 @@ class NanEscapableScope { } # define X(NAME) \ - NAN_INLINE v8::Local Nan ## NAME(const char *errmsg) { \ + NAN_INLINE v8::Local Nan ## NAME(const char *msg) { \ NanEscapableScope scope; \ - return scope.Escape(v8::Exception::NAME(NanNew(errmsg))); \ + return scope.Escape(v8::Exception::NAME(NanNew(msg).ToLocalChecked())); \ } \ \ NAN_INLINE \ - v8::Local Nan ## NAME(v8::Handle errmsg) { \ - return v8::Exception::NAME(errmsg); \ + v8::Local Nan ## NAME(v8::Handle msg) { \ + return v8::Exception::NAME(msg); \ } \ \ - NAN_INLINE void NanThrow ## NAME(const char *errmsg) { \ + NAN_INLINE void NanThrow ## NAME(const char *msg) { \ NanScope scope; \ v8::Isolate::GetCurrent()->ThrowException( \ - v8::Exception::NAME(NanNew(errmsg))); \ + v8::Exception::NAME(NanNew(msg).ToLocalChecked())); \ } \ \ - NAN_INLINE void NanThrow ## NAME(v8::Handle errmsg) { \ + NAN_INLINE void NanThrow ## NAME(v8::Handle msg) { \ NanScope scope; \ v8::Isolate::GetCurrent()->ThrowException( \ - v8::Exception::NAME(NanNew(errmsg))); \ + v8::Exception::NAME(NanNew(msg))); \ } X(Error) @@ -603,33 +640,65 @@ class NanEscapableScope { return NanNew(function_template)->HasInstance(value); } - NAN_INLINE v8::Local NanCompileScript( +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) + NAN_INLINE NanMaybeLocal NanCompileScript( v8::Local s , const v8::ScriptOrigin& origin ) { v8::ScriptCompiler::Source source(s, origin); - return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &source); + return v8::ScriptCompiler::Compile(NanGetCurrentContext(), &source); } - NAN_INLINE v8::Local NanCompileScript( + NAN_INLINE NanMaybeLocal NanCompileScript( v8::Local s ) { v8::ScriptCompiler::Source source(s); - return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &source); + return v8::ScriptCompiler::Compile(NanGetCurrentContext(), &source); } - NAN_INLINE v8::Local NanRunScript( + NAN_INLINE NanMaybeLocal NanRunScript( v8::Handle script ) { - return script->BindToCurrentContext()->Run(); + return script->BindToCurrentContext()->Run(NanGetCurrentContext()); } - NAN_INLINE v8::Local NanRunScript( + NAN_INLINE NanMaybeLocal NanRunScript( v8::Handle script ) { - return script->Run(); + return script->Run(NanGetCurrentContext()); + } +#else + NAN_INLINE NanMaybeLocal NanCompileScript( + v8::Local s + , const v8::ScriptOrigin& origin + ) { + v8::ScriptCompiler::Source source(s, origin); + return NanMaybeLocal( + v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &source)); + } + + NAN_INLINE NanMaybeLocal NanCompileScript( + v8::Local s + ) { + v8::ScriptCompiler::Source source(s); + return NanMaybeLocal( + v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &source)); } + NAN_INLINE NanMaybeLocal NanRunScript( + v8::Handle script + ) { + return NanMaybeLocal(script->BindToCurrentContext()->Run()); + } + + NAN_INLINE NanMaybeLocal NanRunScript( + v8::Handle script + ) { + return NanMaybeLocal(script->Run()); + } +#endif + NAN_INLINE v8::Local NanMakeCallback( v8::Handle target , v8::Handle func @@ -803,6 +872,10 @@ class NanEscapableScope { return scope.Escape(NanNew(v8::False())); } + NAN_INLINE v8::Local NanEmptyString() { + return v8::String::Empty(); + } + NAN_INLINE int NanAdjustExternalMemory(int bc) { return static_cast(v8::V8::AdjustAmountOfExternalAllocatedMemory(bc)); } @@ -866,19 +939,19 @@ class NanEscapableScope { } # define X(NAME) \ - NAN_INLINE v8::Local Nan ## NAME(const char *errmsg) { \ + NAN_INLINE v8::Local Nan ## NAME(const char *msg) { \ NanEscapableScope scope; \ - return scope.Escape(v8::Exception::NAME(NanNew(errmsg))); \ + return scope.Escape(v8::Exception::NAME(NanNew(msg).ToLocalChecked())); \ } \ \ NAN_INLINE \ - v8::Local Nan ## NAME(v8::Handle errmsg) { \ - return v8::Exception::NAME(errmsg); \ + v8::Local Nan ## NAME(v8::Handle msg) { \ + return v8::Exception::NAME(msg); \ } \ \ - NAN_INLINE void NanThrow ## NAME(const char *errmsg) { \ + NAN_INLINE void NanThrow ## NAME(const char *msg) { \ NanScope scope; \ - v8::ThrowException(v8::Exception::NAME(NanNew(errmsg))); \ + v8::ThrowException(v8::Exception::NAME(NanNew(msg).ToLocalChecked())); \ } \ \ NAN_INLINE \ @@ -944,21 +1017,23 @@ class NanEscapableScope { return function_template->HasInstance(value); } - NAN_INLINE v8::Local NanCompileScript( + NAN_INLINE NanMaybeLocal NanCompileScript( v8::Local s , const v8::ScriptOrigin& origin ) { - return v8::Script::Compile(s, const_cast(&origin)); + return NanMaybeLocal( + v8::Script::Compile(s, const_cast(&origin))); } - NAN_INLINE v8::Local NanCompileScript( + NAN_INLINE NanMaybeLocal NanCompileScript( v8::Local s ) { - return v8::Script::Compile(s); + return NanMaybeLocal(v8::Script::Compile(s)); } - NAN_INLINE v8::Local NanRunScript(v8::Handle script) { - return script->Run(); + NAN_INLINE + NanMaybeLocal NanRunScript(v8::Handle script) { + return NanMaybeLocal(script->Run()); } NAN_INLINE v8::Local NanMakeCallback( @@ -1245,7 +1320,7 @@ class NanCallback { NAN_INLINE void SetFunction(const v8::Handle &fn) { NanScope scope; - NanNew(handle)->Set(kCallbackIndex, fn); + NanSet(NanNew(handle), kCallbackIndex, fn); } NAN_INLINE v8::Local GetFunction() const { @@ -1357,7 +1432,7 @@ class NanCallback { NAN_INLINE void SaveToPersistent( const char *key, const v8::Local &value) { NanScope scope; - NanNew(persistentHandle)->Set(NanNew(key), value); + NanNew(persistentHandle)->Set(NanNew(key).ToLocalChecked(), value); } NAN_INLINE void SaveToPersistent( @@ -1374,7 +1449,8 @@ class NanCallback { NAN_INLINE v8::Local GetFromPersistent(const char *key) const { NanEscapableScope scope; - return scope.Escape(NanNew(persistentHandle)->Get(NanNew(key))); + return scope.Escape( + NanNew(persistentHandle)->Get(NanNew(key).ToLocalChecked())); } NAN_INLINE v8::Local @@ -1408,7 +1484,7 @@ class NanCallback { NanScope scope; v8::Local argv[] = { - v8::Exception::Error(NanNew(ErrorMessage())) + v8::Exception::Error(NanNew(ErrorMessage()).ToLocalChecked()) }; callback->Call(1, argv); } @@ -1714,7 +1790,7 @@ NAN_INLINE void NanSetPrototypeMethod( callback , v8::Handle() , NanNew(recv))->GetFunction(); - v8::Local fn_name = NanNew(name); + v8::Local fn_name = NanNew(name).ToLocalChecked(); recv->PrototypeTemplate()->Set(fn_name, fn); fn->SetName(fn_name); } @@ -1995,7 +2071,7 @@ inline void NanExport(v8::Handle target, const char *name, NanFunctionCallback f) { - target->Set(NanNew(name), + NanSet(target, NanNew(name).ToLocalChecked(), NanNew(f)->GetFunction()); } @@ -2003,7 +2079,7 @@ NanExport(v8::Handle target, const char *name, struct NanTap { explicit NanTap(v8::Handle t) : t_() { - t_.Reset(t->ToObject()); + t_.Reset(NanTo(t).ToLocalChecked()); } ~NanTap() { t_.Reset(); } // not sure if neccessary @@ -2016,13 +2092,13 @@ struct NanTap { inline void ok(bool isOk, const char *msg = NULL) { v8::Handle args[2]; args[0] = NanNew(isOk); - if (msg) args[1] = NanNew(msg); + if (msg) args[1] = NanNew(msg).ToLocalChecked(); NanMakeCallback(NanNew(t_), "ok", msg ? 2 : 1, args); } inline void pass(const char * msg = NULL) { v8::Handle hmsg; - if (msg) hmsg = NanNew(msg); + if (msg) hmsg = NanNew(msg).ToLocalChecked(); NanMakeCallback(NanNew(t_), "pass", msg ? 1 : 0, &hmsg); } diff --git a/nan_converters.h b/nan_converters.h new file mode 100644 index 00000000..776e6d8e --- /dev/null +++ b/nan_converters.h @@ -0,0 +1,65 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#ifndef NAN_CONVERTERS_H_ +#define NAN_CONVERTERS_H_ + +namespace Nan { namespace imp { +template struct ToFactoryBase { + typedef NanMaybeLocal return_t; +}; +template struct ValueFactoryBase { typedef NanMaybe return_t; }; + +template struct ToFactory; + +#define X(TYPE) \ + template<> \ + struct ToFactory : ToFactoryBase { \ + static inline return_t convert(v8::Handle val); \ + }; + +X(Boolean) +X(Number) +X(String) +X(Object) +X(Integer) +X(Uint32) +X(Int32) + +#undef X + +#define X(TYPE) \ + template<> \ + struct ToFactory : ValueFactoryBase { \ + static inline return_t convert(v8::Handle val); \ + }; + +X(bool) +X(double) +X(int64_t) +X(uint32_t) +X(int32_t) + +#undef X +} // end of namespace imp +} // end of namespace Nan + +template +NAN_INLINE +typename Nan::imp::ToFactory::return_t NanTo(v8::Handle val) { + return Nan::imp::ToFactory::convert(val); +} + +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) +# include "nan_converters_43_inl.h" +#else +# include "nan_converters_pre_43_inl.h" +#endif + +#endif // NAN_CONVERTERS_H_ diff --git a/nan_converters_43_inl.h b/nan_converters_43_inl.h new file mode 100644 index 00000000..ba5fe776 --- /dev/null +++ b/nan_converters_43_inl.h @@ -0,0 +1,42 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#ifndef NAN_CONVERTERS_43_INL_H_ +#define NAN_CONVERTERS_43_INL_H_ + +#define X(TYPE) \ +Nan::imp::ToFactory::return_t \ +Nan::imp::ToFactory::convert(v8::Handle val) { \ + return val->To ## TYPE(NanGetCurrentContext()); \ +} + +X(Boolean) +X(Number) +X(String) +X(Object) +X(Integer) +X(Uint32) +X(Int32) + +#undef X + +#define X(TYPE, NAME) \ +Nan::imp::ToFactory::return_t \ +Nan::imp::ToFactory::convert(v8::Handle val) { \ + return val->NAME ##Value(NanGetCurrentContext()); \ +} + +X(bool, Boolean) +X(double, Number) +X(int64_t, Integer) +X(uint32_t, Uint32) +X(int32_t, Int32) + +#undef X + +#endif // NAN_CONVERTERS_43_INL_H_ diff --git a/nan_converters_pre_43_inl.h b/nan_converters_pre_43_inl.h new file mode 100644 index 00000000..f3772da7 --- /dev/null +++ b/nan_converters_pre_43_inl.h @@ -0,0 +1,42 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#ifndef NAN_CONVERTERS_PRE_43_INL_H_ +#define NAN_CONVERTERS_PRE_43_INL_H_ + +#define X(TYPE) \ +Nan::imp::ToFactory::return_t \ +Nan::imp::ToFactory::convert(v8::Handle val) { \ + return NanMaybeLocal(val->To ## TYPE()); \ +} + +X(Boolean) +X(Number) +X(String) +X(Object) +X(Integer) +X(Uint32) +X(Int32) + +#undef X + +#define X(TYPE, NAME) \ +Nan::imp::ToFactory::return_t \ +Nan::imp::ToFactory::convert(v8::Handle val) { \ + return NanJust(val->NAME ##Value()); \ +} + +X(bool, Boolean) +X(double, Number) +X(int64_t, Integer) +X(uint32_t, Uint32) +X(int32_t, Int32) + +#undef X + +#endif // NAN_CONVERTERS_PRE_43_INL_H_ diff --git a/nan_implementation_12_inl.h b/nan_implementation_12_inl.h index 8b5d206b..f9af72ce 100644 --- a/nan_implementation_12_inl.h +++ b/nan_implementation_12_inl.h @@ -62,10 +62,25 @@ Factory::New( v8::ExtensionConfiguration* extensions //=== Date ===================================================================== +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) Factory::return_t Factory::New(double value) { - return v8::Date::New(v8::Isolate::GetCurrent(), value).As(); + v8::Local ret; + if (v8::Date::New(NanGetCurrentContext(), value). + ToLocal(reinterpret_cast*>(&ret))) { + return v8::MaybeLocal(ret); + } else { + return v8::MaybeLocal(ret); + } +} +#else +Factory::return_t +Factory::New(double value) { + return Factory::return_t( + v8::Date::New(v8::Isolate::GetCurrent(), value).As()); } +#endif //=== External ================================================================= @@ -84,7 +99,13 @@ Factory::New( NanFunctionCallback callback static std::map cbmap; v8::Local tpl = v8::ObjectTemplate::New(isolate); tpl->SetInternalFieldCount(Nan::imp::kFunctionFieldCount); +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) + v8::Local obj = + tpl->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); +#else v8::Local obj = tpl->NewInstance(); +#endif obj->SetAlignedPointerInInternalField( Nan::imp::kFunctionIndex @@ -112,7 +133,13 @@ Factory::New( NanFunctionCallback callback static std::map cbmap; v8::Local tpl = v8::ObjectTemplate::New(isolate); tpl->SetInternalFieldCount(Nan::imp::kFunctionFieldCount); +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) + v8::Local obj = + tpl->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); +#else v8::Local obj = tpl->NewInstance(); +#endif obj->SetAlignedPointerInInternalField( Nan::imp::kFunctionIndex @@ -187,28 +214,56 @@ Factory::New() { //=== RegExp =================================================================== +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) +Factory::return_t +Factory::New( + v8::Handle pattern + , v8::RegExp::Flags flags) { + return v8::RegExp::New(NanGetCurrentContext(), pattern, flags); +} +#else Factory::return_t Factory::New( v8::Handle pattern , v8::RegExp::Flags flags) { - return v8::RegExp::New(pattern, flags); + return Factory::return_t(v8::RegExp::New(pattern, flags)); } +#endif //=== Script =================================================================== +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) Factory::return_t Factory::New( v8::Local source) { v8::ScriptCompiler::Source src(source); - return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &src); + return v8::ScriptCompiler::Compile(NanGetCurrentContext(), &src); } Factory::return_t Factory::New( v8::Local source , v8::ScriptOrigin const& origin) { v8::ScriptCompiler::Source src(source, origin); - return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &src); + return v8::ScriptCompiler::Compile(NanGetCurrentContext(), &src); +} +#else +Factory::return_t +Factory::New( v8::Local source) { + v8::ScriptCompiler::Source src(source); + return Factory::return_t( + v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &src)); } +Factory::return_t +Factory::New( v8::Local source + , v8::ScriptOrigin const& origin) { + v8::ScriptCompiler::Source src(source, origin); + return Factory::return_t( + v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &src)); +} +#endif + //=== Signature ================================================================ Factory::return_t @@ -220,44 +275,100 @@ Factory::New(Factory::FTH receiver) { Factory::return_t Factory::New() { - return v8::String::Empty(v8::Isolate::GetCurrent()); + return Factory::return_t( + v8::String::Empty(v8::Isolate::GetCurrent())); } +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) Factory::return_t Factory::New(const char * value, int length) { - return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), value, - v8::String::kNormalString, length); + return v8::String::NewFromUtf8( + v8::Isolate::GetCurrent(), value, v8::NewStringType::kNormal, length); } Factory::return_t Factory::New(std::string const& value) { assert(value.size() <= INT_MAX && "string too long"); return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), - value.data(), v8::String::kNormalString, static_cast(value.size())); + value.data(), v8::NewStringType::kNormal, static_cast(value.size())); } Factory::return_t Factory::New(const uint8_t * value, int length) { return v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), value, - v8::String::kNormalString, length); + v8::NewStringType::kNormal, length); } Factory::return_t Factory::New(const uint16_t * value, int length) { return v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), value, - v8::String::kNormalString, length); + v8::NewStringType::kNormal, length); } Factory::return_t Factory::New(v8::String::ExternalStringResource * value) { - return v8::String::NewExternal(v8::Isolate::GetCurrent(), value); + return v8::String::NewExternalTwoByte(v8::Isolate::GetCurrent(), value); } Factory::return_t Factory::New(NanExternalOneByteStringResource * value) { - return v8::String::NewExternal(v8::Isolate::GetCurrent(), value); + return v8::String::NewExternalOneByte(v8::Isolate::GetCurrent(), value); +} +#else +Factory::return_t +Factory::New(const char * value, int length) { + return Factory::return_t( + v8::String::NewFromUtf8( + v8::Isolate::GetCurrent() + , value + , v8::String::kNormalString + , length)); } +Factory::return_t +Factory::New(std::string const& value) { + assert(value.size() <= INT_MAX && "string too long"); + return Factory::return_t( + v8::String::NewFromUtf8( + v8::Isolate::GetCurrent() + , value.data() + , v8::String::kNormalString + , static_cast(value.size()))); +} + +Factory::return_t +Factory::New(const uint8_t * value, int length) { + return Factory::return_t( + v8::String::NewFromOneByte( + v8::Isolate::GetCurrent() + , value + , v8::String::kNormalString, length)); +} + +Factory::return_t +Factory::New(const uint16_t * value, int length) { + return Factory::return_t( + v8::String::NewFromTwoByte( + v8::Isolate::GetCurrent() + , value + , v8::String::kNormalString + , length)); +} + +Factory::return_t +Factory::New(v8::String::ExternalStringResource * value) { + return Factory::return_t( + v8::String::NewExternal(v8::Isolate::GetCurrent(), value)); +} + +Factory::return_t +Factory::New(NanExternalOneByteStringResource * value) { + return Factory::return_t( + v8::String::NewExternal(v8::Isolate::GetCurrent(), value)); +} +#endif + //=== String Object ============================================================ Factory::return_t @@ -267,18 +378,38 @@ Factory::New(v8::Handle value) { //=== Unbound Script =========================================================== +#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ + (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) +Factory::return_t +Factory::New(v8::Local source) { + v8::ScriptCompiler::Source src(source); + return v8::ScriptCompiler::CompileUnboundScript( + v8::Isolate::GetCurrent(), &src); +} + +Factory::return_t +Factory::New( v8::Local source + , v8::ScriptOrigin const& origin) { + v8::ScriptCompiler::Source src(source, origin); + return v8::ScriptCompiler::CompileUnboundScript( + v8::Isolate::GetCurrent(), &src); +} +#else Factory::return_t Factory::New(v8::Local source) { v8::ScriptCompiler::Source src(source); - return v8::ScriptCompiler::CompileUnbound(v8::Isolate::GetCurrent(), &src); + return Factory::return_t( + v8::ScriptCompiler::CompileUnbound(v8::Isolate::GetCurrent(), &src)); } Factory::return_t Factory::New( v8::Local source , v8::ScriptOrigin const& origin) { v8::ScriptCompiler::Source src(source, origin); - return v8::ScriptCompiler::CompileUnbound(v8::Isolate::GetCurrent(), &src); + return Factory::return_t( + v8::ScriptCompiler::CompileUnbound(v8::Isolate::GetCurrent(), &src)); } +#endif } // end of namespace imp } // end of namespace Nan diff --git a/nan_implementation_pre_12_inl.h b/nan_implementation_pre_12_inl.h index 9473b0b8..6b222b8c 100644 --- a/nan_implementation_pre_12_inl.h +++ b/nan_implementation_pre_12_inl.h @@ -72,7 +72,7 @@ Factory::New( v8::ExtensionConfiguration* extensions Factory::return_t Factory::New(double value) { - return v8::Date::New(value).As(); + return Factory::return_t(v8::Date::New(value).As()); } //=== External ================================================================= @@ -184,19 +184,20 @@ Factory::return_t Factory::New( v8::Handle pattern , v8::RegExp::Flags flags) { - return v8::RegExp::New(pattern, flags); + return Factory::return_t(v8::RegExp::New(pattern, flags)); } //=== Script =================================================================== Factory::return_t Factory::New( v8::Local source) { - return v8::Script::New(source); + return Factory::return_t(v8::Script::New(source)); } Factory::return_t Factory::New( v8::Local source , v8::ScriptOrigin const& origin) { - return v8::Script::New(source, const_cast(&origin)); + return Factory::return_t( + v8::Script::New(source, const_cast(&origin))); } //=== Signature ================================================================ @@ -210,18 +211,19 @@ Factory::New(Factory::FTH receiver) { Factory::return_t Factory::New() { - return v8::String::Empty(); + return Factory::return_t(v8::String::Empty()); } Factory::return_t Factory::New(const char * value, int length) { - return v8::String::New(value, length); + return Factory::return_t(v8::String::New(value, length)); } Factory::return_t Factory::New(std::string const& value) { assert(value.size() <= INT_MAX && "string too long"); - return v8::String::New( value.data(), static_cast(value.size())); + return Factory::return_t( + v8::String::New( value.data(), static_cast(value.size()))); } inline @@ -238,7 +240,7 @@ widenString(std::vector *ws, const uint8_t *s, int l = -1) { Factory::return_t Factory::New(const uint16_t * value, int length) { - return v8::String::New(value, length); + return Factory::return_t(v8::String::New(value, length)); } Factory::return_t @@ -246,21 +248,21 @@ Factory::New(const uint8_t * value, int length) { std::vector wideString; widenString(&wideString, value, length); if (wideString.size() == 0) { - return v8::String::Empty(); + return Factory::return_t(v8::String::Empty()); } else { - return v8::String::New(&wideString.front() - , static_cast(wideString.size())); + return Factory::return_t(v8::String::New(&wideString.front() + , static_cast(wideString.size()))); } } Factory::return_t Factory::New(v8::String::ExternalStringResource * value) { - return v8::String::NewExternal(value); + return Factory::return_t(v8::String::NewExternal(value)); } Factory::return_t Factory::New(v8::String::ExternalAsciiStringResource * value) { - return v8::String::NewExternal(value); + return Factory::return_t(v8::String::NewExternal(value)); } //=== String Object ============================================================ diff --git a/nan_maybe_43_inl.h b/nan_maybe_43_inl.h new file mode 100644 index 00000000..dd8d47bc --- /dev/null +++ b/nan_maybe_43_inl.h @@ -0,0 +1,215 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#ifndef NAN_MAYBE_43_INL_H_ +#define NAN_MAYBE_43_INL_H_ + +template +using NanMaybeLocal = v8::MaybeLocal; + +template +using NanMaybe = v8::Maybe; + +template +NAN_INLINE NanMaybe NanNothing() { + return v8::Nothing(); +} + +template +NAN_INLINE NanMaybe NanJust(const T& t) { + return v8::Just(t); +} + +v8::Local NanGetCurrentContext(); + +NAN_INLINE +NanMaybeLocal NanToDetailString(v8::Handle val) { + return val->ToDetailString(NanGetCurrentContext()); +} + +NAN_INLINE +NanMaybeLocal NanToArrayIndex(v8::Handle val) { + return val->ToArrayIndex(NanGetCurrentContext()); +} + +NAN_INLINE +NanMaybe NanEquals(v8::Handle a, v8::Handle(b)) { + return a->Equals(NanGetCurrentContext(), b); +} + +NAN_INLINE +NanMaybeLocal NanNewInstance(v8::Handle h) { + return NanMaybeLocal(h->NewInstance(NanGetCurrentContext())); +} + +NAN_INLINE +NanMaybeLocal NanNewInstance(v8::Handle h) { + return NanMaybeLocal(h->NewInstance(NanGetCurrentContext())); +} + + +NAN_INLINE NanMaybeLocal NanGetFunction( + v8::Handle t) { + return t->GetFunction(NanGetCurrentContext()); +} + +NAN_INLINE NanMaybe NanSet( + v8::Handle obj + , v8::Handle key + , v8::Handle value) { + return obj->Set(NanGetCurrentContext(), key, value); +} + +NAN_INLINE NanMaybe NanSet( + v8::Handle obj + , uint32_t index + , v8::Handle value) { + return obj->Set(NanGetCurrentContext(), index, value); +} + +NAN_INLINE NanMaybe NanForceSet( + v8::Handle obj + , v8::Handle key + , v8::Handle value + , v8::PropertyAttribute attribs = v8::None) { + return obj->ForceSet(NanGetCurrentContext(), key, value, attribs); +} + +NAN_INLINE NanMaybeLocal NanGet( + v8::Handle obj + , v8::Handle key) { + return obj->Get(NanGetCurrentContext(), key); +} + +NAN_INLINE +NanMaybeLocal NanGet(v8::Handle obj, uint32_t index) { + return obj->Get(NanGetCurrentContext(), index); +} + +NAN_INLINE v8::PropertyAttribute NanGetPropertyAttributes( + v8::Handle obj + , v8::Handle key) { + return obj->GetPropertyAttributes(NanGetCurrentContext(), key).FromJust(); +} + +NAN_INLINE NanMaybe NanHas( + v8::Handle obj + , v8::Handle key) { + return obj->Has(NanGetCurrentContext(), key); +} + +NAN_INLINE NanMaybe NanHas(v8::Handle obj, uint32_t index) { + return obj->Has(NanGetCurrentContext(), index); +} + +NAN_INLINE NanMaybe NanDelete( + v8::Handle obj + , v8::Handle key) { + return obj->Delete(NanGetCurrentContext(), key); +} + +NAN_INLINE +NanMaybe NanDelete(v8::Handle obj, uint32_t index) { + return obj->Delete(NanGetCurrentContext(), index); +} + +NAN_INLINE +NanMaybeLocal NanGetPropertyNames(v8::Handle obj) { + return obj->GetPropertyNames(NanGetCurrentContext()); +} + +NAN_INLINE +NanMaybeLocal NanGetOwnPropertyNames(v8::Handle obj) { + return obj->GetOwnPropertyNames(NanGetCurrentContext()); +} + +NAN_INLINE NanMaybe NanSetPrototype( + v8::Handle obj + , v8::Handle prototype) { + return obj->SetPrototype(NanGetCurrentContext(), prototype); +} + +NAN_INLINE NanMaybeLocal NanObjectProtoToString( + v8::Handle obj) { + return obj->ObjectProtoToString(NanGetCurrentContext()); +} + +NAN_INLINE NanMaybe NanHasOwnProperty( + v8::Handle obj + , v8::Handle key) { + return obj->HasOwnProperty(NanGetCurrentContext(), key); +} + +NAN_INLINE NanMaybe NanHasRealNamedProperty( + v8::Handle obj + , v8::Handle key) { + return obj->HasRealNamedProperty(NanGetCurrentContext(), key); +} + +NAN_INLINE NanMaybe NanHasRealIndexedProperty( + v8::Handle obj + , uint32_t index) { + return obj->HasRealIndexedProperty(NanGetCurrentContext(), index); +} + +NAN_INLINE NanMaybe NanHasRealNamedCallbackProperty( + v8::Handle obj + , v8::Handle key) { + return obj->HasRealNamedCallbackProperty(NanGetCurrentContext(), key); +} + +NAN_INLINE NanMaybeLocal NanGetRealNamedPropertyInPrototypeChain( + v8::Handle obj + , v8::Handle key) { + return obj->GetRealNamedPropertyInPrototypeChain(NanGetCurrentContext(), key); +} + +NAN_INLINE NanMaybeLocal NanGetRealNamedProperty( + v8::Handle obj + , v8::Handle key) { + return obj->GetRealNamedProperty(NanGetCurrentContext(), key); +} + +NAN_INLINE NanMaybeLocal NanCallAsFunction( + v8::Handle obj + , v8::Handle recv + , int argc + , v8::Handle argv[]) { + return obj->CallAsFunction(NanGetCurrentContext(), recv, argc, argv); +} + +NAN_INLINE NanMaybeLocal NanCallAsConstructor( + v8::Handle obj + , int argc, v8::Local argv[]) { + return obj->CallAsConstructor(NanGetCurrentContext(), argc, argv); +} + +NAN_INLINE +NanMaybeLocal NanGetSourceLine(v8::Handle msg) { + return msg->GetSourceLine(NanGetCurrentContext()); +} + +NAN_INLINE NanMaybe NanGetLineNumber(v8::Handle msg) { + return msg->GetLineNumber(NanGetCurrentContext()); +} + +NAN_INLINE NanMaybe NanGetStartColumn(v8::Handle msg) { + return msg->GetStartColumn(NanGetCurrentContext()); +} + +NAN_INLINE NanMaybe NanGetEndColumn(v8::Handle msg) { + return msg->GetEndColumn(NanGetCurrentContext()); +} + +NAN_INLINE NanMaybeLocal NanCloneElementAt( + v8::Handle array + , uint32_t index) { + return array->CloneElementAt(NanGetCurrentContext(), index); +} + +#endif // NAN_MAYBE_43_INL_H_ diff --git a/nan_maybe_pre_43_inl.h b/nan_maybe_pre_43_inl.h new file mode 100644 index 00000000..ef096da3 --- /dev/null +++ b/nan_maybe_pre_43_inl.h @@ -0,0 +1,287 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#ifndef NAN_MAYBE_PRE_43_INL_H_ +#define NAN_MAYBE_PRE_43_INL_H_ + +template +class NanMaybeLocal { + public: + NAN_INLINE NanMaybeLocal() : val_(v8::Local()) {} + + template +# if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION + NAN_INLINE NanMaybeLocal(v8::Local that) : val_(that) {} +# else + NAN_INLINE NanMaybeLocal(v8::Local that) : + val_(*reinterpret_cast*>(&that)) {} +# endif + + NAN_INLINE bool IsEmpty() const { return val_.IsEmpty(); } + + template + NAN_INLINE bool ToLocal(v8::Local *out) const { + *out = val_; + return !IsEmpty(); + } + + NAN_INLINE v8::Local ToLocalChecked() const { +#if defined(V8_ENABLE_CHECKS) + assert(!IsEmpty() && "ToLocalChecked is Empty"); +#endif // V8_ENABLE_CHECKS + return val_; + } + + template + NAN_INLINE v8::Local FromMaybe(v8::Local default_value) const { + return IsEmpty() ? default_value : val_; + } + + private: + v8::Local val_; +}; + +template +class NanMaybe { + public: + NAN_INLINE bool IsNothing() const { return !has_value_; } + NAN_INLINE bool IsJust() const { return has_value_; } + + NAN_INLINE T FromJust() const { +#if defined(V8_ENABLE_CHECKS) + assert(IsJust() && "FromJust is Nothing"); +#endif // V8_ENABLE_CHECKS + return value_; + } + + NAN_INLINE T FromMaybe(const T& default_value) const { + return has_value_ ? value_ : default_value; + } + + NAN_INLINE bool operator==(const NanMaybe &other) const { + return (IsJust() == other.IsJust()) && + (!IsJust() || FromJust() == other.FromJust()); + } + + NAN_INLINE bool operator!=(const NanMaybe &other) const { + return !operator==(other); + } + + private: + NanMaybe() : has_value_(false) {} + explicit NanMaybe(const T& t) : has_value_(true), value_(t) {} + bool has_value_; + T value_; + + template + friend NanMaybe NanNothing(); + template + friend NanMaybe NanJust(const U& u); +}; + +template +inline NanMaybe NanNothing() { + return NanMaybe(); +} + +template +inline NanMaybe NanJust(const T& t) { + return NanMaybe(t); +} + +NAN_INLINE +NanMaybeLocal NanToDetailString(v8::Handle val) { + return NanMaybeLocal(val->ToDetailString()); +} + +NAN_INLINE +NanMaybeLocal NanToArrayIndex(v8::Handle val) { + return NanMaybeLocal(val->ToArrayIndex()); +} + +NAN_INLINE +NanMaybe NanEquals(v8::Handle a, v8::Handle(b)) { + return NanJust(a->Equals(b)); +} + +NAN_INLINE +NanMaybeLocal NanNewInstance(v8::Handle h) { + return NanMaybeLocal(h->NewInstance()); +} + +NAN_INLINE +NanMaybeLocal NanNewInstance(v8::Handle h) { + return NanMaybeLocal(h->NewInstance()); +} + +NAN_INLINE +NanMaybeLocal NanGetFunction(v8::Handle t) { + return NanMaybeLocal(t->GetFunction()); +} + +NAN_INLINE NanMaybe NanSet( + v8::Handle obj + , v8::Handle key + , v8::Handle value) { + return NanJust(obj->Set(key, value)); +} + +NAN_INLINE NanMaybe NanSet( + v8::Handle obj + , uint32_t index + , v8::Handle value) { + return NanJust(obj->Set(index, value)); +} + +NAN_INLINE NanMaybe NanForceSet( + v8::Handle obj + , v8::Handle key + , v8::Handle value + , v8::PropertyAttribute attribs = v8::None) { + return NanJust(obj->ForceSet(key, value, attribs)); +} + +NAN_INLINE NanMaybeLocal NanGet( + v8::Handle obj + , v8::Handle key) { + return NanMaybeLocal(obj->Get(key)); +} + +NAN_INLINE NanMaybeLocal NanGet( + v8::Handle obj + , uint32_t index) { + return NanMaybeLocal(obj->Get(index)); +} + +NAN_INLINE NanMaybe NanGetPropertyAttributes( + v8::Handle obj + , v8::Handle key) { + return NanJust(obj->GetPropertyAttributes(key)); +} + +NAN_INLINE NanMaybe NanHas( + v8::Handle obj + , v8::Handle key) { + return NanJust(obj->Has(key)); +} + +NAN_INLINE NanMaybe NanHas( + v8::Handle obj + , uint32_t index) { + return NanJust(obj->Has(index)); +} + +NAN_INLINE NanMaybe NanDelete( + v8::Handle obj + , v8::Handle key) { + return NanJust(obj->Delete(key)); +} + +NAN_INLINE NanMaybe NanDelete( + v8::Handle obj + , uint32_t index) { + return NanJust(obj->Delete(index)); +} + +NAN_INLINE +NanMaybeLocal NanGetPropertyNames(v8::Handle obj) { + return NanMaybeLocal(obj->GetPropertyNames()); +} + +NAN_INLINE +NanMaybeLocal NanGetOwnPropertyNames(v8::Handle obj) { + return NanMaybeLocal(obj->GetOwnPropertyNames()); +} + +NAN_INLINE NanMaybe NanSetPrototype( + v8::Handle obj + , v8::Handle prototype) { + return NanJust(obj->SetPrototype(prototype)); +} + +NAN_INLINE NanMaybeLocal NanObjectProtoToString( + v8::Handle obj) { + return NanMaybeLocal(obj->ObjectProtoToString()); +} + +NAN_INLINE NanMaybe NanHasOwnProperty( + v8::Handle obj + , v8::Handle key) { + return NanJust(obj->HasOwnProperty(key)); +} + +NAN_INLINE NanMaybe NanHasRealNamedProperty( + v8::Handle obj + , v8::Handle key) { + return NanJust(obj->HasRealNamedProperty(key)); +} + +NAN_INLINE NanMaybe NanHasRealIndexedProperty( + v8::Handle obj + , uint32_t index) { + return NanJust(obj->HasRealIndexedProperty(index)); +} + +NAN_INLINE NanMaybe NanHasRealNamedCallbackProperty( + v8::Handle obj + , v8::Handle key) { + return NanJust(obj->HasRealNamedCallbackProperty(key)); +} + +NAN_INLINE NanMaybeLocal NanGetRealNamedPropertyInPrototypeChain( + v8::Handle obj + , v8::Handle key) { + return NanMaybeLocal( + obj->GetRealNamedPropertyInPrototypeChain(key)); +} + +NAN_INLINE NanMaybeLocal NanGetRealNamedProperty( + v8::Handle obj + , v8::Handle key) { + return NanMaybeLocal(obj->GetRealNamedProperty(key)); +} + +NAN_INLINE NanMaybeLocal NanCallAsFunction( + v8::Handle obj + , v8::Handle recv + , int argc + , v8::Handle argv[]) { + return NanMaybeLocal(obj->CallAsFunction(recv, argc, argv)); +} + +NAN_INLINE NanMaybeLocal NanCallAsConstructor( + v8::Handle obj + , int argc + , v8::Local argv[]) { + return NanMaybeLocal(obj->CallAsConstructor(argc, argv)); +} + +NAN_INLINE +NanMaybeLocal NanGetSourceLine(v8::Handle msg) { + return NanMaybeLocal(msg->GetSourceLine()); +} + +NAN_INLINE NanMaybe NanGetLineNumber(v8::Handle msg) { + return NanJust(msg->GetLineNumber()); +} + +NAN_INLINE NanMaybe NanGetStartColumn(v8::Handle msg) { + return NanJust(msg->GetStartColumn()); +} + +NAN_INLINE NanMaybe NanGetEndColumn(v8::Handle msg) { + return NanJust(msg->GetEndColumn()); +} + +NAN_INLINE NanMaybeLocal NanCloneElementAt( + v8::Handle array + , uint32_t index) { + return NanMaybeLocal(array->CloneElementAt(index)); +} + +#endif // NAN_MAYBE_PRE_43_INL_H_ diff --git a/nan_new.h b/nan_new.h index 67429f5d..3d5bcf71 100644 --- a/nan_new.h +++ b/nan_new.h @@ -26,19 +26,31 @@ template v8::Local To(v8::Handle i); template <> inline v8::Local -To(v8::Handle i) { return i->ToInteger(); } +To(v8::Handle i) { + return NanTo(i).ToLocalChecked(); +} template <> inline v8::Local -To(v8::Handle i) { return i->ToInt32(); } +To(v8::Handle i) { + return NanTo(i).ToLocalChecked(); +} template <> inline v8::Local -To(v8::Handle i) { return i->ToUint32(); } +To(v8::Handle i) { + return NanTo(i).ToLocalChecked(); +} -template struct FactoryBase { typedef v8::Local return_t; }; +template struct FactoryBase { + typedef v8::Local return_t; +}; + +template struct MaybeFactoryBase { + typedef NanMaybeLocal return_t; +}; template struct Factory; @@ -68,7 +80,7 @@ struct Factory : FactoryBase { }; template <> -struct Factory : FactoryBase { +struct Factory : MaybeFactoryBase { static inline return_t New(double value); }; @@ -134,13 +146,13 @@ struct Factory : FactoryBase { }; template <> -struct Factory : FactoryBase { +struct Factory : MaybeFactoryBase { static inline return_t New( v8::Handle pattern, v8::RegExp::Flags flags); }; template <> -struct Factory : FactoryBase { +struct Factory : MaybeFactoryBase { static inline return_t New( v8::Local source); static inline return_t New( v8::Local source , v8::ScriptOrigin const& origin); @@ -153,7 +165,7 @@ struct Factory : FactoryBase { }; template <> -struct Factory : FactoryBase { +struct Factory : MaybeFactoryBase { static inline return_t New(); static inline return_t New(const char *value, int length = -1); static inline return_t New(const uint16_t *value, int length = -1); @@ -179,7 +191,7 @@ struct Factory : FactoryBase { namespace Nan { namespace imp { template <> -struct Factory : FactoryBase { +struct Factory : MaybeFactoryBase { static inline return_t New( v8::Local source); static inline return_t New( v8::Local source , v8::ScriptOrigin const& origin); diff --git a/nan_string_bytes.h b/nan_string_bytes.h index 44122f07..f5040a5e 100644 --- a/nan_string_bytes.h +++ b/nan_string_bytes.h @@ -233,7 +233,7 @@ static Local Encode(const char* buf, enum Nan::Encoding encoding) { assert(buflen <= node::Buffer::kMaxLength); if (!buflen && encoding != Nan::BUFFER) - return NanNew(""); + return NanNew("").ToLocalChecked(); Local val; switch (encoding) { @@ -244,15 +244,15 @@ static Local Encode(const char* buf, if (contains_non_ascii(buf, buflen)) { char* out = new char[buflen]; force_ascii(buf, out, buflen); - val = NanNew(out, buflen); + val = NanNew(out, buflen).ToLocalChecked(); delete[] out; } else { - val = NanNew(buf, buflen); + val = NanNew(buf, buflen).ToLocalChecked(); } break; case Nan::UTF8: - val = NanNew(buf, buflen); + val = NanNew(buf, buflen).ToLocalChecked(); break; case Nan::BINARY: { @@ -263,7 +263,7 @@ static Local Encode(const char* buf, // XXX is the following line platform independent? twobytebuf[i] = cbuf[i]; } - val = NanNew(twobytebuf, buflen); + val = NanNew(twobytebuf, buflen).ToLocalChecked(); delete[] twobytebuf; break; } @@ -275,14 +275,14 @@ static Local Encode(const char* buf, size_t written = base64_encode(buf, buflen, dst, dlen); assert(written == dlen); - val = NanNew(dst, dlen); + val = NanNew(dst, dlen).ToLocalChecked(); delete[] dst; break; } case Nan::UCS2: { const uint16_t* data = reinterpret_cast(buf); - val = NanNew(data, buflen / 2); + val = NanNew(data, buflen / 2).ToLocalChecked(); break; } @@ -292,7 +292,7 @@ static Local Encode(const char* buf, size_t written = hex_encode(buf, buflen, dst, dlen); assert(written == dlen); - val = NanNew(dst, dlen); + val = NanNew(dst, dlen).ToLocalChecked(); delete[] dst; break; } diff --git a/test/binding.gyp b/test/binding.gyp index 96066642..4277337e 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -49,6 +49,10 @@ "target_name" : "accessors" , "sources" : [ "cpp/accessors.cpp" ] } + , { + "target_name" : "accessors2" + , "sources" : [ "cpp/accessors2.cpp" ] + } , { "target_name" : "persistent" , "sources" : [ "cpp/persistent.cpp" ] @@ -116,4 +120,8 @@ "target_name" : "indexedinterceptors" , "sources" : [ "cpp/indexedinterceptors.cpp" ] } + , { + "target_name" : "converters" + , "sources" : [ "cpp/converters.cpp" ] + } ]} diff --git a/test/cpp/accessors.cpp b/test/cpp/accessors.cpp index 255959db..46927815 100644 --- a/test/cpp/accessors.cpp +++ b/test/cpp/accessors.cpp @@ -43,21 +43,24 @@ void SetterGetter::Init(v8::Handle target) { v8::Local tpl = NanNew(SetterGetter::New); settergetter_constructor.Reset(tpl); - tpl->SetClassName(NanNew("SetterGetter")); + tpl->SetClassName(NanNew("SetterGetter").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); NanSetPrototypeMethod(tpl, "log", SetterGetter::Log); v8::Local proto = tpl->PrototypeTemplate(); - NanSetAccessor(proto, NanNew("prop1"), SetterGetter::GetProp1); NanSetAccessor( - proto - , NanNew("prop2") - , SetterGetter::GetProp2 - , SetterGetter::SetProp2 + proto + , NanNew("prop1").ToLocalChecked() + , SetterGetter::GetProp1); + NanSetAccessor( + proto + , NanNew("prop2").ToLocalChecked() + , SetterGetter::GetProp2 + , SetterGetter::SetProp2 ); v8::Local createnew = NanNew(CreateNew)->GetFunction(); - target->Set(NanNew("create"), createnew); + NanSet(target, NanNew("create").ToLocalChecked(), createnew); } v8::Handle SetterGetter::NewInstance () { @@ -100,7 +103,7 @@ NAN_GETTER(SetterGetter::GetProp1) { , ")\n" , sizeof (settergetter->log) - 1 - strlen(settergetter->log)); - info.GetReturnValue().Set(NanNew(settergetter->prop1)); + info.GetReturnValue().Set(NanNew(settergetter->prop1).ToLocalChecked()); } NAN_GETTER(SetterGetter::GetProp2) { @@ -122,7 +125,7 @@ NAN_GETTER(SetterGetter::GetProp2) { , ")\n" , sizeof (settergetter->log) - 1 - strlen(settergetter->log)); - info.GetReturnValue().Set(NanNew(settergetter->prop2)); + info.GetReturnValue().Set(NanNew(settergetter->prop2).ToLocalChecked()); } NAN_SETTER(SetterGetter::SetProp2) { @@ -154,7 +157,7 @@ NAN_METHOD(SetterGetter::Log) { SetterGetter* settergetter = NanObjectWrap::Unwrap(info.This()); - info.GetReturnValue().Set(NanNew(settergetter->log)); + info.GetReturnValue().Set(NanNew(settergetter->log).ToLocalChecked()); } NODE_MODULE(accessors, SetterGetter::Init) diff --git a/test/cpp/accessors2.cpp b/test/cpp/accessors2.cpp new file mode 100644 index 00000000..f50c5cce --- /dev/null +++ b/test/cpp/accessors2.cpp @@ -0,0 +1,161 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#include +#include + +class SetterGetter : public NanObjectWrap { + public: + static void Init (v8::Handle target); + static v8::Handle NewInstance (); + static NAN_METHOD(New); + static NAN_METHOD(Log); + static NAN_GETTER(GetProp1); + static NAN_GETTER(GetProp2); + static NAN_SETTER(SetProp2); + + SetterGetter(); + + char log[1024]; + char prop1[256]; + char prop2[256]; +}; + +static NanPersistent settergetter_constructor; + +NAN_METHOD(CreateNew) { + info.GetReturnValue().Set(SetterGetter::NewInstance()); +} + +SetterGetter::SetterGetter() { + log[0] = '\0'; + strncpy(prop1, "this is property 1", sizeof (prop1) - 1); + prop1[sizeof (prop1) - 1] = '\0'; + prop2[0] = '\0'; +} + +void SetterGetter::Init(v8::Handle target) { + v8::Local tpl = + NanNew(SetterGetter::New); + settergetter_constructor.Reset(tpl); + tpl->SetClassName(NanNew("SetterGetter").ToLocalChecked()); + tpl->InstanceTemplate()->SetInternalFieldCount(1); + NanSetPrototypeMethod(tpl, "log", SetterGetter::Log); + v8::Local createnew = + NanNew(CreateNew)->GetFunction(); + NanSet(target, NanNew("create").ToLocalChecked(), createnew); +} + +v8::Handle SetterGetter::NewInstance () { + NanEscapableScope scope; + v8::Local constructorHandle = + NanNew(settergetter_constructor); + v8::Local instance = + constructorHandle->GetFunction()->NewInstance(0, NULL); + NanSetAccessor( + instance + , NanNew("prop1").ToLocalChecked() + , SetterGetter::GetProp1); + NanSetAccessor( + instance + , NanNew("prop2").ToLocalChecked() + , SetterGetter::GetProp2 + , SetterGetter::SetProp2 + ); + return scope.Escape(instance); +} + +NAN_METHOD(SetterGetter::New) { + SetterGetter* settergetter = new SetterGetter(); + assert(strlen(settergetter->log) < sizeof (settergetter->log)); + strncat( + settergetter->log + , "New()\n" + , sizeof (settergetter->log) - 1 - strlen(settergetter->log)); + settergetter->Wrap(info.This()); + + info.GetReturnValue().Set(info.This()); +} + +NAN_GETTER(SetterGetter::GetProp1) { + SetterGetter* settergetter = + NanObjectWrap::Unwrap(info.This()); + assert(strlen(settergetter->log) < sizeof (settergetter->log)); + strncat( + settergetter->log + , "Prop1:GETTER(" + , sizeof (settergetter->log) - 1 - strlen(settergetter->log)); + assert(strlen(settergetter->log) < sizeof (settergetter->log)); + strncat( + settergetter->log + , settergetter->prop1 + , sizeof (settergetter->log) - 1 - strlen(settergetter->log)); + assert(strlen(settergetter->log) < sizeof (settergetter->log)); + strncat( + settergetter->log + , ")\n" + , sizeof (settergetter->log) - 1 - strlen(settergetter->log)); + + info.GetReturnValue().Set(NanNew(settergetter->prop1).ToLocalChecked()); +} + +NAN_GETTER(SetterGetter::GetProp2) { + SetterGetter* settergetter = + NanObjectWrap::Unwrap(info.This()); + assert(strlen(settergetter->log) < sizeof (settergetter->log)); + strncat( + settergetter->log + , "Prop2:GETTER(" + , sizeof (settergetter->log) - 1 - strlen(settergetter->log)); + assert(strlen(settergetter->log) < sizeof (settergetter->log)); + strncat( + settergetter->log + , settergetter->prop2 + , sizeof (settergetter->log) - 1 - strlen(settergetter->log)); + assert(strlen(settergetter->log) < sizeof (settergetter->log)); + strncat( + settergetter->log + , ")\n" + , sizeof (settergetter->log) - 1 - strlen(settergetter->log)); + + info.GetReturnValue().Set(NanNew(settergetter->prop2).ToLocalChecked()); +} + +NAN_SETTER(SetterGetter::SetProp2) { + SetterGetter* settergetter = + NanObjectWrap::Unwrap(info.This()); + strncpy( + settergetter->prop2 + , *v8::String::Utf8Value(value) + , sizeof (settergetter->prop2)); + settergetter->prop2[sizeof (settergetter->prop2) - 1] = '\0'; + assert(strlen(settergetter->log) < sizeof (settergetter->log)); + strncat( + settergetter->log + , "Prop2:SETTER(" + , sizeof (settergetter->log) - 1 - strlen(settergetter->log)); + assert(strlen(settergetter->log) < sizeof (settergetter->log)); + strncat( + settergetter->log + , settergetter->prop2 + , sizeof (settergetter->log) - 1 - strlen(settergetter->log)); + assert(strlen(settergetter->log) < sizeof (settergetter->log)); + strncat( + settergetter->log + , ")\n" + , sizeof (settergetter->log) - 1 - strlen(settergetter->log)); +} + +NAN_METHOD(SetterGetter::Log) { + SetterGetter* settergetter = + NanObjectWrap::Unwrap(info.This()); + + info.GetReturnValue().Set(NanNew(settergetter->log).ToLocalChecked()); +} + +NODE_MODULE(accessors2, SetterGetter::Init) diff --git a/test/cpp/asyncprogressworker.cpp b/test/cpp/asyncprogressworker.cpp index 1024755f..46bf2f06 100644 --- a/test/cpp/asyncprogressworker.cpp +++ b/test/cpp/asyncprogressworker.cpp @@ -51,14 +51,13 @@ NAN_METHOD(DoProgress) { NanAsyncQueueWorker(new ProgressWorker( callback , progress - , info[0]->Uint32Value() - , info[1]->Uint32Value())); - info.GetReturnValue().SetUndefined(); + , NanTo(info[0]).FromJust() + , NanTo(info[1]).FromJust())); } void Init(v8::Handle exports) { - exports->Set( - NanNew("a") + NanSet(exports + , NanNew("a").ToLocalChecked() , NanNew(DoProgress)->GetFunction()); } diff --git a/test/cpp/asyncworker.cpp b/test/cpp/asyncworker.cpp index a66a00b2..f7ddb326 100644 --- a/test/cpp/asyncworker.cpp +++ b/test/cpp/asyncworker.cpp @@ -28,13 +28,13 @@ class SleepWorker : public NanAsyncWorker { NAN_METHOD(DoSleep) { NanCallback *callback = new NanCallback(info[1].As()); - NanAsyncQueueWorker(new SleepWorker(callback, info[0]->Uint32Value())); - info.GetReturnValue().SetUndefined(); + NanAsyncQueueWorker( + new SleepWorker(callback, NanTo(info[0]).FromJust())); } void Init(v8::Handle exports) { - exports->Set( - NanNew("a") + NanSet(exports + , NanNew("a").ToLocalChecked() , NanNew(DoSleep)->GetFunction()); } diff --git a/test/cpp/asyncworkererror.cpp b/test/cpp/asyncworkererror.cpp index c30935b3..244368ae 100644 --- a/test/cpp/asyncworkererror.cpp +++ b/test/cpp/asyncworkererror.cpp @@ -25,7 +25,9 @@ NAN_METHOD(Work) { } void Init (v8::Handle exports) { - exports->Set(NanNew("a"), NanNew(Work)->GetFunction()); + NanSet(exports + , NanNew("a").ToLocalChecked() + , NanNew(Work)->GetFunction()); } NODE_MODULE(asyncworkererror, Init) diff --git a/test/cpp/bufferworkerpersistent.cpp b/test/cpp/bufferworkerpersistent.cpp index fb89ff97..31606b7b 100644 --- a/test/cpp/bufferworkerpersistent.cpp +++ b/test/cpp/bufferworkerpersistent.cpp @@ -21,7 +21,7 @@ class BufferWorker : public NanAsyncWorker { ) : NanAsyncWorker(callback), milliseconds(milliseconds) { SaveToPersistent("buffer", bufferHandle); - SaveToPersistent(NanNew("puffer"), bufferHandle); + SaveToPersistent(NanNew("puffer").ToLocalChecked(), bufferHandle); SaveToPersistent(0u, bufferHandle); } @@ -37,7 +37,7 @@ class BufferWorker : public NanAsyncWorker { v8::Local handle = GetFromPersistent("buffer"); callback->Call(1, &handle); - handle = GetFromPersistent(NanNew("puffer")); + handle = GetFromPersistent(NanNew("puffer").ToLocalChecked()); callback->Call(1, &handle); handle = GetFromPersistent(0u); @@ -52,14 +52,15 @@ NAN_METHOD(DoSleep) { v8::Local bufferHandle = info[1].As(); NanCallback *callback = new NanCallback(info[2].As()); assert(!callback->IsEmpty() && "Callback shoud not be empty"); - NanAsyncQueueWorker( - new BufferWorker(callback, info[0]->Uint32Value(), bufferHandle)); - info.GetReturnValue().SetUndefined(); + NanAsyncQueueWorker(new BufferWorker( + callback + , NanTo(info[0]).FromJust() + , bufferHandle)); } void Init(v8::Handle exports) { - exports->Set( - NanNew("a") + NanSet(exports + , NanNew("a").ToLocalChecked() , NanNew(DoSleep)->GetFunction()); } diff --git a/test/cpp/converters.cpp b/test/cpp/converters.cpp new file mode 100644 index 00000000..1418d2bd --- /dev/null +++ b/test/cpp/converters.cpp @@ -0,0 +1,128 @@ +/********************************************************************* + * NAN - Native Abstractions for Node.js + * + * Copyright (c) 2015 NAN contributors + * + * MIT License + ********************************************************************/ + +#include + +NAN_METHOD(ToBoolean) { + info.GetReturnValue().Set(NanTo(info[0]).ToLocalChecked()); +} + +NAN_METHOD(ToNumber) { + info.GetReturnValue().Set(NanTo(info[0]).ToLocalChecked()); +} + +NAN_METHOD(ToString) { + info.GetReturnValue().Set(NanTo(info[0]).ToLocalChecked()); +} + +NAN_METHOD(ToDetailString) { + info.GetReturnValue().Set(NanToDetailString(info[0]).ToLocalChecked()); +} + +NAN_METHOD(ToObject) { + info.GetReturnValue().Set(NanTo(info[0]).ToLocalChecked()); +} + +NAN_METHOD(ToInteger) { + info.GetReturnValue().Set(NanTo(info[0]).ToLocalChecked()); +} + +NAN_METHOD(ToUint32) { + info.GetReturnValue().Set(NanTo(info[0]).ToLocalChecked()); +} + +NAN_METHOD(ToInt32) { + info.GetReturnValue().Set(NanTo(info[0]).ToLocalChecked()); +} + +NAN_METHOD(ToArrayIndex) { + NanScope(); + info.GetReturnValue().Set(NanToArrayIndex(info[0]).ToLocalChecked()); +} + +NAN_METHOD(BooleanValue) { + info.GetReturnValue().Set(NanNew(NanTo(info[0]).FromJust())); +} + +NAN_METHOD(NumberValue) { + info.GetReturnValue().Set(NanNew(NanTo(info[0]).FromJust())); +} + +NAN_METHOD(IntegerValue) { + info.GetReturnValue().Set(NanNew(static_cast( + NanTo(info[0]).FromJust()))); +} + +NAN_METHOD(Uint32Value) { + info.GetReturnValue().Set(NanNew(NanTo(info[0]).FromJust())); +} + +NAN_METHOD(Int32Value) { + info.GetReturnValue().Set(NanNew(NanTo(info[0]).FromJust())); +} + +void Init (v8::Handle target) { + NanSet(target + , NanNew("toBoolean").ToLocalChecked() + , NanNew(ToBoolean)->GetFunction() + ); + NanSet(target + , NanNew("toNumber").ToLocalChecked() + , NanNew(ToNumber)->GetFunction() + ); + NanSet(target + , NanNew("toString").ToLocalChecked() + , NanNew(ToString)->GetFunction() + ); + NanSet(target + , NanNew("toDetailString").ToLocalChecked() + , NanNew(ToDetailString)->GetFunction() + ); + NanSet(target + , NanNew("toObject").ToLocalChecked() + , NanNew(ToObject)->GetFunction() + ); + NanSet(target + , NanNew("toInteger").ToLocalChecked() + , NanNew(ToInteger)->GetFunction() + ); + NanSet(target + , NanNew("toUint32").ToLocalChecked() + , NanNew(ToUint32)->GetFunction() + ); + NanSet(target + , NanNew("toInt32").ToLocalChecked() + , NanNew(ToInt32)->GetFunction() + ); + NanSet(target + , NanNew("toArrayIndex").ToLocalChecked() + , NanNew(ToArrayIndex)->GetFunction() + ); + NanSet(target + , NanNew("booleanValue").ToLocalChecked() + , NanNew(BooleanValue)->GetFunction() + ); + NanSet(target + , NanNew("numberValue").ToLocalChecked() + , NanNew(NumberValue)->GetFunction() + ); + NanSet(target + , NanNew("integerValue").ToLocalChecked() + , NanNew(IntegerValue)->GetFunction() + ); + NanSet(target + , NanNew("uint32Value").ToLocalChecked() + , NanNew(Uint32Value)->GetFunction() + ); + NanSet(target + , NanNew("int32Value").ToLocalChecked() + , NanNew(Int32Value)->GetFunction() + ); +} + +NODE_MODULE(converters, Init) diff --git a/test/cpp/error.cpp b/test/cpp/error.cpp index 608a04f1..57f43d3a 100644 --- a/test/cpp/error.cpp +++ b/test/cpp/error.cpp @@ -14,7 +14,7 @@ } \ \ NAN_METHOD(Throw ## NAME ## 2) { \ - return NanThrow ## NAME(NanNew("errmsg")); \ + return NanThrow ## NAME(NanNew("errmsg").ToLocalChecked()); \ } \ \ NAN_METHOD(Throw ## NAME ## 3) { \ @@ -22,7 +22,7 @@ } \ \ NAN_METHOD(Throw ## NAME ## 4) { \ - return NanThrowError(Nan ## NAME(NanNew("errmsg"))); \ + return NanThrowError(Nan ## NAME(NanNew("errmsg").ToLocalChecked())); \ } X(Error) @@ -40,7 +40,10 @@ X(TypeError) X(Throw ## NAME ## 4) #define X(NAME) \ - target->Set(NanNew(#NAME), NanNew(NAME)->GetFunction()); + NanSet( \ + target \ + , NanNew(#NAME).ToLocalChecked() \ + , NanNew(NAME)->GetFunction()); void Init (v8::Handle target) { diff --git a/test/cpp/gc.cpp b/test/cpp/gc.cpp index b788255a..ef89744c 100644 --- a/test/cpp/gc.cpp +++ b/test/cpp/gc.cpp @@ -32,12 +32,12 @@ NAN_METHOD(Check) { } void Init (v8::Handle target) { - target->Set( - NanNew("hook") + NanSet(target + , NanNew("hook").ToLocalChecked() , NanNew(Hook)->GetFunction() ); - target->Set( - NanNew("check") + NanSet(target + , NanNew("check").ToLocalChecked() , NanNew(Check)->GetFunction() ); } diff --git a/test/cpp/indexedinterceptors.cpp b/test/cpp/indexedinterceptors.cpp index 65d56aa7..729b1f14 100644 --- a/test/cpp/indexedinterceptors.cpp +++ b/test/cpp/indexedinterceptors.cpp @@ -35,7 +35,7 @@ void IndexedInterceptor::Init(v8::Handle target) { v8::Local tpl = NanNew(IndexedInterceptor::New); indexedinterceptors_constructor.Reset(tpl); - tpl->SetClassName(NanNew("IndexedInterceptor")); + tpl->SetClassName(NanNew("IndexedInterceptor").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); v8::Local inst = tpl->InstanceTemplate(); @@ -49,7 +49,7 @@ void IndexedInterceptor::Init(v8::Handle target) { v8::Local createnew = NanNew(CreateNew)->GetFunction(); - target->Set(NanNew("create"), createnew); + NanSet(target, NanNew("create").ToLocalChecked(), createnew); } v8::Handle IndexedInterceptor::NewInstance () { @@ -72,9 +72,9 @@ NAN_INDEX_GETTER(IndexedInterceptor::PropertyGetter) { IndexedInterceptor* interceptor = NanObjectWrap::Unwrap(info.This()); if (index == 0) { - info.GetReturnValue().Set(NanNew(interceptor->buf)); + info.GetReturnValue().Set(NanNew(interceptor->buf).ToLocalChecked()); } else { - info.GetReturnValue().Set(NanNew("bar")); + info.GetReturnValue().Set(NanNew("bar").ToLocalChecked()); } } @@ -94,7 +94,7 @@ NAN_INDEX_SETTER(IndexedInterceptor::PropertySetter) { NAN_INDEX_ENUMERATOR(IndexedInterceptor::PropertyEnumerator) { v8::Local arr = NanNew(); - arr->Set(0, NanNew("whee")); + NanSet(arr, 0, NanNew("whee").ToLocalChecked()); info.GetReturnValue().Set(arr); } diff --git a/test/cpp/isolatedata.cpp b/test/cpp/isolatedata.cpp index c47ce7d7..87607725 100644 --- a/test/cpp/isolatedata.cpp +++ b/test/cpp/isolatedata.cpp @@ -27,8 +27,8 @@ NAN_METHOD(SetAndGet) { } void Init (v8::Handle target) { - target->Set( - NanNew("setAndGet") + NanSet(target + , NanNew("setAndGet").ToLocalChecked() , NanNew(SetAndGet)->GetFunction() ); } diff --git a/test/cpp/makecallback.cpp b/test/cpp/makecallback.cpp index 2fc0eb25..8239ede9 100644 --- a/test/cpp/makecallback.cpp +++ b/test/cpp/makecallback.cpp @@ -32,13 +32,13 @@ MyObject::~MyObject() { void MyObject::Init(v8::Handle exports) { // Prepare constructor template v8::Local tpl = NanNew(New); - tpl->SetClassName(NanNew("MyObject")); + tpl->SetClassName(NanNew("MyObject").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); NanSetPrototypeMethod(tpl, "call_emit", CallEmit); constructor.Reset(tpl->GetFunction()); - exports->Set(NanNew("MyObject"), tpl->GetFunction()); + NanSet(exports, NanNew("MyObject").ToLocalChecked(), tpl->GetFunction()); } NAN_METHOD(MyObject::New) { @@ -54,7 +54,7 @@ NAN_METHOD(MyObject::New) { NAN_METHOD(MyObject::CallEmit) { v8::Handle argv[1] = { - NanNew("event"), // event name + NanNew("event").ToLocalChecked(), // event name }; NanMakeCallback(info.This(), "emit", 1, argv); diff --git a/test/cpp/morenews.cpp b/test/cpp/morenews.cpp index a986f679..eef689a5 100644 --- a/test/cpp/morenews.cpp +++ b/test/cpp/morenews.cpp @@ -22,17 +22,17 @@ NAN_METHOD(NewPositiveInteger) { NAN_METHOD(NewUtf8String) { const char s[] = "strïng"; - info.GetReturnValue().Set(NanNew(s)); + info.GetReturnValue().Set(NanNew(s).ToLocalChecked()); } NAN_METHOD(NewLatin1String) { const uint8_t s[] = "str\xefng"; - info.GetReturnValue().Set(NanNew(s)); + info.GetReturnValue().Set(NanNew(s).ToLocalChecked()); } NAN_METHOD(NewUcs2String) { uint16_t s[] = {'s', 't', 'r', 0xef, 'n', 'g', '\0'}; - info.GetReturnValue().Set(NanNew(s)); + info.GetReturnValue().Set(NanNew(s).ToLocalChecked()); } static const uint16_t ws[] = {'s', 't', 'r', 0xef, 'n', 'g', '\0'}; @@ -54,46 +54,46 @@ class ExtAsciiString : public NanExternalOneByteStringResource { }; NAN_METHOD(NewExternalStringResource) { - v8::Local ext = NanNew(new ExtString()); + v8::Local ext = NanNew(new ExtString()).ToLocalChecked(); info.GetReturnValue().Set(ext); } NAN_METHOD(NewExternalAsciiStringResource) { - v8::Local ext = NanNew(new ExtAsciiString()); + v8::Local ext = NanNew(new ExtAsciiString()).ToLocalChecked(); info.GetReturnValue().Set(ext); } void Init(v8::Handle target) { - target->Set( - NanNew("newNumber") + NanSet(target + , NanNew("newNumber").ToLocalChecked() , NanNew(NewNumber)->GetFunction() ); - target->Set( - NanNew("newNegativeInteger") + NanSet(target + , NanNew("newNegativeInteger").ToLocalChecked() , NanNew(NewNegativeInteger)->GetFunction() ); - target->Set( - NanNew("newPositiveInteger") + NanSet(target + , NanNew("newPositiveInteger").ToLocalChecked() , NanNew(NewPositiveInteger)->GetFunction() ); - target->Set( - NanNew("newUtf8String") + NanSet(target + , NanNew("newUtf8String").ToLocalChecked() , NanNew(NewUtf8String)->GetFunction() ); - target->Set( - NanNew("newLatin1String") + NanSet(target + , NanNew("newLatin1String").ToLocalChecked() , NanNew(NewLatin1String)->GetFunction() ); - target->Set( - NanNew("newUcs2String") + NanSet(target + , NanNew("newUcs2String").ToLocalChecked() , NanNew(NewUcs2String)->GetFunction() ); - target->Set( - NanNew("newExternalStringResource") + NanSet(target + , NanNew("newExternalStringResource").ToLocalChecked() , NanNew(NewExternalStringResource)->GetFunction() ); - target->Set( - NanNew("newExternalAsciiStringResource") + NanSet(target + , NanNew("newExternalAsciiStringResource").ToLocalChecked() , NanNew(NewExternalAsciiStringResource) ->GetFunction() ); diff --git a/test/cpp/multifile1.cpp b/test/cpp/multifile1.cpp index a1ce38f1..748168a4 100644 --- a/test/cpp/multifile1.cpp +++ b/test/cpp/multifile1.cpp @@ -10,8 +10,8 @@ #include "./multifile2.h" void Init (v8::Handle target) { - target->Set( - NanNew("r") + NanSet(target + , NanNew("r").ToLocalChecked() , NanNew(ReturnString)->GetFunction() ); } diff --git a/test/cpp/multifile2.cpp b/test/cpp/multifile2.cpp index 144a0556..83bc8852 100644 --- a/test/cpp/multifile2.cpp +++ b/test/cpp/multifile2.cpp @@ -9,6 +9,6 @@ #include NAN_METHOD(ReturnString) { - v8::Local s = NanNew(*NanUtf8String(info[0])); + v8::Local s = NanNew(*NanUtf8String(info[0])).ToLocalChecked(); info.GetReturnValue().Set(s); } diff --git a/test/cpp/namedinterceptors.cpp b/test/cpp/namedinterceptors.cpp index 01bf5ef4..342309f5 100644 --- a/test/cpp/namedinterceptors.cpp +++ b/test/cpp/namedinterceptors.cpp @@ -35,7 +35,7 @@ void NamedInterceptor::Init(v8::Handle target) { v8::Local tpl = NanNew(NamedInterceptor::New); namedinterceptors_constructor.Reset(tpl); - tpl->SetClassName(NanNew("NamedInterceptor")); + tpl->SetClassName(NanNew("NamedInterceptor").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); v8::Local inst = tpl->InstanceTemplate(); @@ -49,7 +49,7 @@ void NamedInterceptor::Init(v8::Handle target) { v8::Local createnew = NanNew(CreateNew)->GetFunction(); - target->Set(NanNew("create"), createnew); + NanSet(target, NanNew("create").ToLocalChecked(), createnew); } v8::Handle NamedInterceptor::NewInstance () { @@ -72,9 +72,9 @@ NAN_PROPERTY_GETTER(NamedInterceptor::PropertyGetter) { NamedInterceptor* interceptor = NanObjectWrap::Unwrap(info.This()); if (!std::strcmp(*v8::String::Utf8Value(property), "prop")) { - info.GetReturnValue().Set(NanNew(interceptor->buf)); + info.GetReturnValue().Set(NanNew(interceptor->buf).ToLocalChecked()); } else { - info.GetReturnValue().Set(NanNew("bar")); + info.GetReturnValue().Set(NanNew("bar").ToLocalChecked()); } } @@ -94,7 +94,7 @@ NAN_PROPERTY_SETTER(NamedInterceptor::PropertySetter) { NAN_PROPERTY_ENUMERATOR(NamedInterceptor::PropertyEnumerator) { v8::Local arr = NanNew(); - arr->Set(0, NanNew("value")); + NanSet(arr, 0, NanNew("value").ToLocalChecked()); info.GetReturnValue().Set(arr); } diff --git a/test/cpp/nancallback.cpp b/test/cpp/nancallback.cpp index fe32f685..8ec68be9 100644 --- a/test/cpp/nancallback.cpp +++ b/test/cpp/nancallback.cpp @@ -10,13 +10,11 @@ NAN_METHOD(GlobalContext) { NanCallback(info[0].As()).Call(0, NULL); - info.GetReturnValue().SetUndefined(); } NAN_METHOD(SpecificContext) { NanCallback cb(info[0].As()); cb.Call(NanGetCurrentContext()->Global(), 0, NULL); - info.GetReturnValue().SetUndefined(); } NAN_METHOD(CustomReceiver) { @@ -42,28 +40,28 @@ NAN_METHOD(CallAsFunction) { } void Init (v8::Handle target) { - target->Set( - NanNew("globalContext") + NanSet(target + , NanNew("globalContext").ToLocalChecked() , NanNew(GlobalContext)->GetFunction() ); - target->Set( - NanNew("specificContext") + NanSet(target + , NanNew("specificContext").ToLocalChecked() , NanNew(SpecificContext)->GetFunction() ); - target->Set( - NanNew("customReceiver") + NanSet(target + , NanNew("customReceiver").ToLocalChecked() , NanNew(CustomReceiver)->GetFunction() ); - target->Set( - NanNew("compareCallbacks") + NanSet(target + , NanNew("compareCallbacks").ToLocalChecked() , NanNew(CompareCallbacks)->GetFunction() ); - target->Set( - NanNew("callDirect") + NanSet(target + , NanNew("callDirect").ToLocalChecked() , NanNew(CallDirect)->GetFunction() ); - target->Set( - NanNew("callAsFunction") + NanSet(target + , NanNew("callAsFunction").ToLocalChecked() , NanNew(CallAsFunction)->GetFunction() ); } diff --git a/test/cpp/nannew.cpp b/test/cpp/nannew.cpp index e1d45484..dc6f0557 100644 --- a/test/cpp/nannew.cpp +++ b/test/cpp/nannew.cpp @@ -121,7 +121,8 @@ NAN_METHOD(testDate) { t.plan(1); - t.ok(_( assertType( NanNew(static_cast(time(NULL)))))); + t.ok(_( assertType( + NanNew(static_cast(time(NULL))).ToLocalChecked()))); info.GetReturnValue().SetUndefined(); } @@ -144,7 +145,7 @@ NAN_METHOD(testFunction) { t.plan(2); t.ok(_( assertType(NanNew(testFunction)))); - v8::Local data = NanNew("plonk"); + v8::Local data = NanNew("plonk").ToLocalChecked(); t.ok(_( assertType(NanNew(testFunction, data)))); info.GetReturnValue().SetUndefined(); @@ -158,7 +159,7 @@ NAN_METHOD(testFunctionTemplate) { t.ok(_( assertType( NanNew()))); t.ok(_( assertType( NanNew(testFunctionTemplate)))); - v8::Local data = NanNew("plonk"); + v8::Local data = NanNew("plonk").ToLocalChecked(); t.ok(_( assertType( NanNew( testFunctionTemplate, data)))); v8::Local signature = NanNew(); @@ -210,7 +211,10 @@ NAN_METHOD(testNumberObject) { t.plan(2); t.ok(_( assertType( NanNew(M_PI)))); - t.ok(_( fabs(NanNew(M_PI)->NumberValue() - M_PI) < epsilon)); + t.ok(_( + fabs( + NanTo(NanNew(M_PI)).FromJust() - M_PI + ) < epsilon)); info.GetReturnValue().SetUndefined(); } @@ -240,20 +244,26 @@ NAN_METHOD(testScript) { t.plan(6); - ScriptOrigin origin(NanNew("foo"), NanNew(5)); + ScriptOrigin origin(NanNew("foo").ToLocalChecked(), NanNew(5)); - t.ok(_( assertType