From 82ce454698c7989cd64840f464dd01546347d0fc Mon Sep 17 00:00:00 2001 From: King Koopa Date: Fri, 16 Jan 2015 18:58:12 +0200 Subject: [PATCH 1/5] Added utility functions fully moved from V8 to Isolate. --- README.md | 36 +++++++++++++++++++++++++++++++++++ nan.h | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/README.md b/README.md index f91d08b9..a54f2e5e 100644 --- a/README.md +++ b/README.md @@ -302,6 +302,12 @@ NAN_METHOD(CalculateAsync) { * NanRemoveGCEpilogueCallback * NanRemoveGCPrologueCallback * NanGetHeapStatistics + * NanSetCounterFunction + * NanSetCreateHistogramFunction + * NanSetAddHistogramSampleFunction + * NanIdleNotification + * NanLowMemoryNotification + * NanContextDisposedNotification * NanCallback * NanAsyncWorker * NanAsyncQueueWorker @@ -1098,6 +1104,36 @@ Simply does `RemoveGCPrologueCallback` Simply does `GetHeapStatistics` + +### NanSetCounterFunction(CounterLookupCallback cb) + +Simply does `SetCounterFunction` + + +### NanSetCreateHistogramFunction(CreateHistogramCallback cb) + +Simply does `SetCreateHistogramFunction` + + +### NanSetAddHistogramSampleFunction(AddHistogramSampleCallback cb) + +Simply does `SetAddHistogramSampleFunction` + + +### NanIdleNotification(int idle_time_in_ms) + +Simply does `IdleNotification` + + +### NanLowMemoryNotification() + +Simply does `LowMemoryNotification` + + +### NanContextDisposedNotification() + +Simply does `ContextDisposedNotification` + ### NanCallback diff --git a/nan.h b/nan.h index 531b3aca..af75b3f8 100644 --- a/nan.h +++ b/nan.h @@ -166,6 +166,62 @@ NAN_INLINE v8::Local _NanEnsureLocal(v8::Local val) { return val; } +#if NODE_MODULE_VERSION >= 42 // io.js 1.0 + NAN_INLINE + void NanSetCounterFunction(v8::CounterLookupCallback cb) { + v8::Isolate::GetCurrent()->SetCounterFunction(cb); + } + + NAN_INLINE + void NanSetCreateHistogramFunction(v8::CreateHistogramCallback cb) { + v8::Isolate::GetCurrent()->SetCreateHistogramFunction(cb); + } + + NAN_INLINE + void NanSetAddHistogramSampleFunction(v8::AddHistogramSampleCallback cb) { + v8::Isolate::GetCurrent()->SetAddHistogramSampleFunction(cb); + } + + NAN_INLINE bool NanIdleNotification(int idle_time_in_ms) { + return v8::Isolate::GetCurrent()->IdleNotification(idle_time_in_ms); + } + + NAN_INLINE void NanLowMemoryNotification() { + v8::Isolate::GetCurrent()->LowMemoryNotification(); + } + + NAN_INLINE void NanContextDisposedNotification() { + v8::Isolate::GetCurrent()->ContextDisposedNotification(); + } +#else + NAN_INLINE + void NanSetCounterFunction(v8::CounterLookupCallback cb) { + v8::V8::SetCounterFunction(cb); + } + + NAN_INLINE + void NanSetCreateHistogramFunction(v8::CreateHistogramCallback cb) { + v8::V8::SetCreateHistogramFunction(cb); + } + + NAN_INLINE + void NanSetAddHistogramSampleFunction(v8::AddHistogramSampleCallback cb) { + v8::V8::SetAddHistogramSampleFunction(cb); + } + + NAN_INLINE bool NanIdleNotification(int idle_time_in_ms) { + return v8::V8::IdleNotification(idle_time_in_ms); + } + + NAN_INLINE void NanLowMemoryNotification() { + v8::V8::LowMemoryNotification(); + } + + NAN_INLINE void NanContextDisposedNotification() { + v8::V8::ContextDisposedNotification(); + } +#endif + #if (NODE_MODULE_VERSION > 0x000B) // Node 0.11+ (0.11.12 and below won't compile with these) From e50c9b988dfe03af63f41045fca748cfab191007 Mon Sep 17 00:00:00 2001 From: King Koopa Date: Sun, 18 Jan 2015 18:01:23 +0200 Subject: [PATCH 2/5] Replace NanNewContextHandle with NanNew --- README.md | 9 ++++++--- nan.h | 4 ++-- nan_implementation_12_inl.h | 9 +++++++++ nan_implementation_pre_12_inl.h | 12 ++++++++++++ nan_new.h | 9 +++++++++ test/cpp/nannew.cpp | 18 ++++++++++++++++++ 6 files changed, 56 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a54f2e5e..5d8d8efe 100644 --- a/README.md +++ b/README.md @@ -284,7 +284,7 @@ NAN_METHOD(CalculateAsync) { * NanThrowError, NanThrowTypeError, NanThrowRangeError, NanThrowError(Handle), NanThrowError(Handle, int) * NanNewBufferHandle(char *, size_t, FreeCallback, void *), NanNewBufferHandle(char *, uint32_t), NanNewBufferHandle(uint32_t) * NanBufferUse(char *, uint32_t) - * NanNewContextHandle + * NanNewContextHandle * NanGetCurrentContext * NanHasInstance * NanDisposePersistent @@ -980,8 +980,11 @@ careless use can lead to "double free or corruption" and other cryptic failures. Can be used to check the type of an object to determine it is of a particular class you have already defined and have a `Persistent` handle for. -### Local<Context> NanNewContextHandle([ExtensionConfiguration*, Handle<ObjectTemplate>, Handle<Value>]) -Creates a new `Local` handle. +### ~~Local<Context> NanNewContextHandle([ExtensionConfiguration*, Handle<ObjectTemplate>, Handle<Value>]) + +Deprecated. Use `NanNew` instead. + +~~Creates a new `Local` handle. ```c++ Local ftmpl = NanNew(); diff --git a/nan.h b/nan.h index af75b3f8..9412f98f 100644 --- a/nan.h +++ b/nan.h @@ -585,7 +585,7 @@ NAN_INLINE _NanWeakCallbackInfo* NanMakeWeakPersistent( return NanNew(function_template)->HasInstance(value); } - NAN_INLINE v8::Local NanNewContextHandle( + NAN_DEPRECATED NAN_INLINE v8::Local NanNewContextHandle( v8::ExtensionConfiguration* extensions = NULL , v8::Handle tmpl = v8::Handle() , v8::Handle obj = v8::Handle() @@ -1131,7 +1131,7 @@ NAN_INLINE _NanWeakCallbackInfo* NanMakeWeakPersistent( return function_template->HasInstance(value); } - NAN_INLINE v8::Local NanNewContextHandle( + NAN_DEPRECATED NAN_INLINE v8::Local NanNewContextHandle( v8::ExtensionConfiguration* extensions = NULL , v8::Handle tmpl = v8::Handle() , v8::Handle obj = v8::Handle() diff --git a/nan_implementation_12_inl.h b/nan_implementation_12_inl.h index 5f0bff05..ff63ec0c 100644 --- a/nan_implementation_12_inl.h +++ b/nan_implementation_12_inl.h @@ -48,6 +48,15 @@ Factory::New(bool value) { return v8::BooleanObject::New(value).As(); } +//=== Context ================================================================== + +Factory::return_t +Factory::New( v8::ExtensionConfiguration* extensions + , v8::Handle tmpl + , v8::Handle obj) { + return v8::Context::New(v8::Isolate::GetCurrent(), extensions, tmpl, obj); +} + //=== Date ===================================================================== Factory::return_t diff --git a/nan_implementation_pre_12_inl.h b/nan_implementation_pre_12_inl.h index 4c4ae116..85dd2754 100644 --- a/nan_implementation_pre_12_inl.h +++ b/nan_implementation_pre_12_inl.h @@ -53,6 +53,18 @@ Factory::New(bool value) { return v8::BooleanObject::New(value).As(); } +//=== Context ================================================================== + +Factory::return_t +Factory::New( v8::ExtensionConfiguration* extensions + , v8::Handle tmpl + , v8::Handle obj) { + v8::Persistent ctx = v8::Context::New(extensions, tmpl, obj); + v8::Local lctx = v8::Local::New(ctx); + ctx.Dispose(); + return lctx; +} + //=== Date ===================================================================== Factory::return_t diff --git a/nan_new.h b/nan_new.h index 7058d123..95b6b51e 100644 --- a/nan_new.h +++ b/nan_new.h @@ -57,6 +57,15 @@ struct Factory : FactoryBase { static inline return_t New(bool value); }; +template <> +struct Factory : FactoryBase { + static inline + return_t + New( v8::ExtensionConfiguration* extensions = NULL + , v8::Handle tmpl = v8::Handle() + , v8::Handle obj = v8::Handle()); +}; + template <> struct Factory : FactoryBase { static inline return_t New(double value); diff --git a/test/cpp/nannew.cpp b/test/cpp/nannew.cpp index 854e6bd5..b7b5d39b 100644 --- a/test/cpp/nannew.cpp +++ b/test/cpp/nannew.cpp @@ -99,6 +99,23 @@ NAN_METHOD(testBooleanObject) { } #undef V +NAN_METHOD(testContext) { + NanScope(); + NanTap t(args[0]); + + t.plan(4); + t.ok(_( assertType( NanNew()))); + ExtensionConfiguration extensions(0, NULL); + t.ok(_( assertType( NanNew(&extensions)))); + t.ok(_( assertType( + NanNew(&extensions, Handle())))); + t.ok(_( assertType( + NanNew(&extensions + , Handle(), Handle())))); + + return_NanUndefined(); +} + NAN_METHOD(testDate) { NanScope(); NanTap t(args[0]); @@ -446,6 +463,7 @@ void Init(Handle exports) { NAN_EXPORT(exports, testArray); NAN_EXPORT(exports, testBoolean); NAN_EXPORT(exports, testBooleanObject); + NAN_EXPORT(exports, testContext); NAN_EXPORT(exports, testDate); NAN_EXPORT(exports, testExternal); NAN_EXPORT(exports, testFunction); From 26db37757be1b48d88925da78c07ecf5f09b7dc5 Mon Sep 17 00:00:00 2001 From: King Koopa Date: Mon, 19 Jan 2015 14:50:24 +0200 Subject: [PATCH 3/5] Add support for node::Encode/DecodeBytes/DecodeWrite. Fixes #250 --- README.md | 18 ++++++++++++++++ nan.h | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5d8d8efe..1e6a1b22 100644 --- a/README.md +++ b/README.md @@ -294,6 +294,9 @@ NAN_METHOD(CalculateAsync) { * NanSetPrototypeTemplate * NanSetInstanceTemplate * NanMakeCallback + * NanEncode + * NanDecodeBytes + * NanDecodeWrite * NanCompileScript * NanRunScript * NanAdjustExternalMemory @@ -1067,6 +1070,21 @@ Use to add instance properties on function templates. Use instead of `node::MakeCallback` to call javascript functions. This (or `NanCallback`) is the only proper way of calling functions. You must _*never, ever*_ directly use `Function::Call`, it will lead to run-time failures. + +### NanEncode(const void*, size_t[, enum Nan::Encoding]) + +Replaces `node::Encode`. + + +### NanDecodeBytes(v8::Handle<v8::Value>[, enum Nan::Encoding]) + +Replaces `node::DecodeBytes`. + + +### NanDecodeWrite(char *, size_t, v8::Handle<v8::Value>[, enum Nan::Encoding]) + +Replaces `node::DecodeWrite`. + ### NanCompileScript(Handle s [, const ScriptOrigin& origin]) diff --git a/nan.h b/nan.h index 9412f98f..39ed7235 100644 --- a/nan.h +++ b/nan.h @@ -166,7 +166,7 @@ NAN_INLINE v8::Local _NanEnsureLocal(v8::Local val) { return val; } -#if NODE_MODULE_VERSION >= 42 // io.js 1.0 +#if NODE_MODULE_VERSION >= 42 // io.js 1.0 NAN_INLINE void NanSetCounterFunction(v8::CounterLookupCallback cb) { v8::Isolate::GetCurrent()->SetCounterFunction(cb); @@ -1841,6 +1841,68 @@ namespace Nan { enum Encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER}; } +NAN_INLINE v8::Local NanEncode( + const void *buf, size_t len, enum Nan::Encoding encoding = Nan::BINARY) { +#if (NODE_MODULE_VERSION > 0x000B) + return node::Encode( + v8::Isolate::GetCurrent() + , buf, len + , static_cast(encoding)); +#else +# if (NODE_MODULE_VERSION < 0x000B) + if (encoding == Nan::BUFFER) { + assert(len <= node::Buffer::kMaxLength); + return v8::Local::New(node::Buffer::New( + static_cast(const_cast(buf)), len)->handle_); + } +# endif + return node::Encode(buf, len, static_cast(encoding)); +#endif +} + +NAN_INLINE ssize_t NanDecodeBytes( + v8::Handle val, enum Nan::Encoding encoding = Nan::BINARY) { +#if (NODE_MODULE_VERSION > 0x000B) + return node::DecodeBytes( + v8::Isolate::GetCurrent() + , val + , static_cast(encoding)); +#else +# if (NODE_MODULE_VERSION < 0x000B) + if (encoding == Nan::BUFFER) { + return node::DecodeBytes(val, node::BINARY); + } +# endif + return node::DecodeBytes(val, static_cast(encoding)); +#endif +} + +NAN_INLINE ssize_t NanDecodeWrite( + char *buf + , size_t len + , v8::Handle val + , enum Nan::Encoding encoding = Nan::BINARY) { +#if (NODE_MODULE_VERSION > 0x000B) + return node::DecodeWrite( + v8::Isolate::GetCurrent() + , buf + , len + , val + , static_cast(encoding)); +#else +# if (NODE_MODULE_VERSION < 0x000B) + if (encoding == Nan::BUFFER) { + return node::DecodeWrite(buf, len, val, node::BINARY); + } +# endif + return node::DecodeWrite( + buf + , len + , val + , static_cast(encoding)); +#endif +} + /* NAN_DEPRECATED */ NAN_INLINE void* _NanRawString( v8::Handle from , enum Nan::Encoding encoding From 40476420f47ee6edfb9ec5bab545a974f5f8dece Mon Sep 17 00:00:00 2001 From: King Koopa Date: Wed, 21 Jan 2015 13:37:00 +0200 Subject: [PATCH 4/5] Support joyent/node 0.11.15 --- nan.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nan.h b/nan.h index 39ed7235..390d8597 100644 --- a/nan.h +++ b/nan.h @@ -166,7 +166,8 @@ NAN_INLINE v8::Local _NanEnsureLocal(v8::Local val) { return val; } -#if NODE_MODULE_VERSION >= 42 // io.js 1.0 +/* io.js 1.0 */ +#if NODE_MODULE_VERSION >= 42 || NODE_VERSION_AT_LEAST(0, 11, 15) NAN_INLINE void NanSetCounterFunction(v8::CounterLookupCallback cb) { v8::Isolate::GetCurrent()->SetCounterFunction(cb); From 2ffebd56528974b4a8efdaa19f04c60a188ce5cc Mon Sep 17 00:00:00 2001 From: King Koopa Date: Wed, 21 Jan 2015 14:41:10 +0200 Subject: [PATCH 5/5] Prepare for 1.6.0 --- CHANGELOG.md | 8 ++++++-- README.md | 9 ++++++++- nan.h | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a0fdbe9..dab47bf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # NAN ChangeLog -**Version 1.5.2: current Node unstable: 0.11.15, Node stable: 0.10.35, io.js: 1.0.3** +**Version 1.6.0: current Node unstable: 0.11.15, Node stable: 0.10.35, io.js: 1.0.3** + +### 1.6.0 Jan 23 2015 + - Deprecated `NanNewContextHandle` in favor of `NanNew` 49259af + - Support utility functions moved in newer v8 versions (Node 0.11.15, io.js 1.0) a0aa179 + - Added `NanEncode`, `NanDecodeBytes` and `NanDecodeWrite` 75e6fb9 ### 1.5.2 Jan 23 2015 @@ -14,7 +19,6 @@ - Correctness: Make explicit downcasts of String lengths 00074e6 - Windows: Limit the scope of disabled warning C4530 83d7deb - ### 1.5.1 Jan 15 2015 - Build: version bump diff --git a/README.md b/README.md index 1e6a1b22..0b025af0 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Native Abstractions for Node.js **A header file filled with macro and utility goodness for making add-on development for Node.js easier across versions 0.8, 0.10 and 0.11, and eventually 0.12.** -***Current version: 1.5.2*** +***Current version: 1.6.0*** *(See [CHANGELOG.md](https://github.com/rvagg/nan/blob/master/CHANGELOG.md) for complete ChangeLog)* @@ -25,6 +25,13 @@ This project also contains some helper utilities that make addon development a b ## News & Updates +### Jan-2015: 1.6.0 release + +* Deprecated `NanNewContextHandle` in favor of `NanNew` +* Added `NanSetCounterFunction`, `NanSetCreateHistogramFunction`, `NanSetAddHistogramSampleFunction` +* Added `NanIdleNotification`, `NanLowMemoryNotification`, `NanContextDisposedNotification` +* Added `NanEncode`, `NanDecodeBytes` and `NanDecodeWrite` + ### Jan-2015: 1.5.0 release * Support [io.js](https://github.com/iojs/io.js) thanks to [Ben Noordhuis](bnoordhuis) diff --git a/nan.h b/nan.h index 390d8597..fa15e266 100644 --- a/nan.h +++ b/nan.h @@ -12,7 +12,7 @@ * * MIT License * - * Version 1.5.2: current Node unstable: 0.11.15, Node stable: 0.10.35, io.js: 1.0.3 + * Version 1.6.0: current Node unstable: 0.11.15, Node stable: 0.10.35, io.js: 1.0.3 * * See https://github.com/rvagg/nan for the latest update to this file **********************************************************************************/