[Pkg-javascript-commits] [node-nan] 01/04: Imported Upstream version 2.1.0
Jérémy Lal
kapouer at moszumanska.debian.org
Tue Oct 27 09:57:58 UTC 2015
This is an automated email from the git hooks/post-receive script.
kapouer pushed a commit to branch master
in repository node-nan.
commit 0f957b0b30ed4fb2946d85b102411b4d3198e6fc
Author: Jérémy Lal <kapouer at melix.org>
Date: Tue Oct 27 10:51:44 2015 +0100
Imported Upstream version 2.1.0
---
.travis.yml | 9 ++-
CHANGELOG.md | 12 +++-
Makefile | 2 +
README.md | 10 ++-
appveyor.yml | 17 +++--
doc/callback.md | 2 +-
doc/maybe_types.md | 20 ++++++
doc/methods.md | 59 ++++++++++++----
doc/new.md | 20 ++++--
doc/persistent.md | 15 ++--
doc/v8_internals.md | 2 +-
doc/v8_misc.md | 22 ++++++
nan.h | 125 ++++++++++++++++++++++++++++++----
nan_callbacks_12_inl.h | 2 +-
nan_callbacks_pre_12_inl.h | 12 ++--
nan_implementation_12_inl.h | 5 ++
nan_implementation_pre_12_inl.h | 5 ++
nan_new.h | 8 +++
nan_persistent_pre_12_inl.h | 4 ++
nan_typedarray_contents.h | 87 ++++++++++++++++++++++++
package.json | 7 +-
test/binding.gyp | 17 ++++-
test/cpp/nannew.cpp | 29 +++++++-
test/cpp/objectwraphandle.cpp | 12 ++--
test/cpp/setcallhandler.cpp | 45 ++++++++++++
test/cpp/typedarrays.cpp | 66 ++++++++++++++++++
test/cpp/wrappedobjectfactory.cpp | 128 +++++++++++++++++++++++++++++++++++
test/js/nannew-test.js | 5 ++
test/js/setcallhandler-test.js | 19 ++++++
test/js/typedarrays-test.js | 74 ++++++++++++++++++++
test/js/wrappedobjectfactory-test.js | 24 +++++++
31 files changed, 786 insertions(+), 78 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index 6bdf5d0..958e1c0 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -14,19 +14,18 @@ env:
- TRAVIS_NODE_VERSION="0.8"
- TRAVIS_NODE_VERSION="0.10"
- TRAVIS_NODE_VERSION="0.12"
- - TRAVIS_NODE_VERSION="iojs-1"
- - TRAVIS_NODE_VERSION="iojs-2"
- TRAVIS_NODE_VERSION="iojs-3"
+ - TRAVIS_NODE_VERSION="4"
notifications:
email:
- rod at vagg.org
install:
- - rm -rf ~/.nvm && git clone https://github.com/creationix/nvm.git ~/.nvm && source ~/.nvm/nvm.sh && nvm install $TRAVIS_NODE_VERSION
- - npm install npm
+ - rm -rf ~/.nvm && git clone https://github.com/creationix/nvm.git ~/.nvm && (cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`) && source ~/.nvm/nvm.sh && nvm install $TRAVIS_NODE_VERSION
+ - if [[ $TRAVIS_NODE_VERSION == "0.8" ]]; then npm install npm at 2 && node_modules/.bin/npm install npm; else npm install npm; fi
- mv node_modules npm
- npm/.bin/npm --version
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then export CXX=g++-4.8; fi
- $CXX --version
- npm/.bin/npm install
- - if [[ $TRAVIS_NODE_VERSION == "0.8" ]]; then node_modules/.bin/node-gyp rebuild --directory test; else node_modules/.bin/pangyp rebuild --directory test; fi
+ - node_modules/.bin/node-gyp rebuild --directory test
script: node_modules/.bin/tap --gc test/js/*-test.js
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 457e7c4..8d3c618 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,16 @@
# NAN ChangeLog
-**Version 2.0.9: current Node 4.0.0, Node 12: 0.12.7, Node 10: 0.10.40, iojs: 3.2.0**
+**Version 2.1.0: current Node 4.1.2, Node 12: 0.12.7, Node 10: 0.10.40, iojs: 3.3.1**
+
+### 2.1.0 Oct 8 2015
+
+ - Deprecation: Deprecate NanErrnoException in favor of ErrnoException 0af1ca4cf8b3f0f65ed31bc63a663ab3319da55c
+ - Feature: added helper class for accessing contents of typedarrays 17b51294c801e534479d5463697a73462d0ca555
+ - Feature: [Maybe types] Add MakeMaybe(...) 48d7b53d9702b0c7a060e69ea10fea8fb48d814d
+ - Feature: new: allow utf16 string with length 66ac6e65c8ab9394ef588adfc59131b3b9d8347b
+ - Feature: Introduce SetCallHandler and SetCallAsFunctionHandler 7764a9a115d60ba10dc24d86feb0fbc9b4f75537
+ - Bugfix: Enable creating Locals from Globals under Node 0.10. 9bf9b8b190821af889790fdc18ace57257e4f9ff
+ - Bugfix: Fix issue #462 where PropertyCallbackInfo data is not stored safely. 55f50adedd543098526c7b9f4fffd607d3f9861f
### 2.0.9 Sep 8 2015
diff --git a/Makefile b/Makefile
index e9d5455..8bdd09c 100644
--- a/Makefile
+++ b/Makefile
@@ -61,6 +61,7 @@ LINT_SOURCES = \
test/cpp/returnnull.cpp \
test/cpp/returnundefined.cpp \
test/cpp/returnvalue.cpp \
+ test/cpp/setcallhandler.cpp \
test/cpp/settemplate.cpp \
test/cpp/strings.cpp \
test/cpp/symbols.cpp \
@@ -68,6 +69,7 @@ LINT_SOURCES = \
test/cpp/trycatch.cpp \
test/cpp/weak.cpp \
test/cpp/weak2.cpp \
+ test/cpp/wrappedobjectfactory.cpp \
node_modules/node-gyp/gyp/data/win/large-pdb-shim.cc
FILTER = -whitespace/parens
diff --git a/README.md b/README.md
index db3daec..7167ae3 100644
--- a/README.md
+++ b/README.md
@@ -1,9 +1,9 @@
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.12 as well as io.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, 0.12 and 4.**
-***Current version: 2.0.9***
+***Current version: 2.1.0***
*(See [CHANGELOG.md](https://github.com/nodejs/nan/blob/master/CHANGELOG.md) for complete ChangeLog)*
@@ -12,7 +12,7 @@ Native Abstractions for Node.js
[![Build Status](https://api.travis-ci.org/nodejs/nan.svg?branch=master)](http://travis-ci.org/nodejs/nan)
[![Build status](https://ci.appveyor.com/api/projects/status/kh73pbm9dsju7fgh)](https://ci.appveyor.com/project/RodVagg/nan)
-Thanks to the crazy changes in V8 (and some in Node core), keeping native addons compiling happily across versions, particularly 0.10 to 0.12, is a minor nightmare. The goal of this project is to store all logic necessary to develop native Node.js addons without having to inspect `NODE_MODULE_VERSION` and get yourself into a macro-tangle.
+Thanks to the crazy changes in V8 (and some in Node core), keeping native addons compiling happily across versions, particularly 0.10 to 0.12 to 4.0, is a minor nightmare. The goal of this project is to store all logic necessary to develop native Node.js addons without having to inspect `NODE_MODULE_VERSION` and get yourself into a macro-tangle.
This project also contains some helper utilities that make addon development a bit more pleasant.
@@ -97,6 +97,8 @@ In order to expose functionality to JavaScript via a template, you must provide
- <a href="doc/methods.md#api_nan_set_template"><b><code>Nan::SetTemplate()</code></b></a>
- <a href="doc/methods.md#api_nan_set_prototype_template"><b><code>Nan::SetPrototypeTemplate()</code></b></a>
- <a href="doc/methods.md#api_nan_set_instance_template"><b><code>Nan::SetInstanceTemplate()</code></b></a>
+ - <a href="doc/methods.md#api_nan_set_call_handler"><b><code>Nan::SetCallHandler()</code></b></a>
+ - <a href="doc/methods.md#api_nan_set_call_as_function_handler"><b><code>Nan::SetCallAsFunctionHandler()</code></b></a>
### Scopes
@@ -183,6 +185,7 @@ The `Nan::MaybeLocal` and `Nan::Maybe` types are monads that encapsulate `v8::Lo
- <a href="doc/maybe_types.md#api_nan_get_start_column"><b><code>Nan::GetStartColumn()</code></b></a>
- <a href="doc/maybe_types.md#api_nan_get_end_column"><b><code>Nan::GetEndColumn()</code></b></a>
- <a href="doc/maybe_types.md#api_nan_clone_element_at"><b><code>Nan::CloneElementAt()</code></b></a>
+ - <a href="doc/maybe_types.md#api_nan_make_maybe"><b><code>Nan::MakeMaybe()</code></b></a>
### Script
@@ -274,6 +277,7 @@ The hooks to access V8 internals—including GC and statistics—are different a
- <a href="doc/v8_misc.md#api_nan_get_current_context"><b><code>Nan::GetCurrentContext()</code></b></a>
- <a href="doc/v8_misc.md#api_nan_set_isolate_data"><b><code>Nan::SetIsolateData()</code></b></a>
- <a href="doc/v8_misc.md#api_nan_get_isolate_data"><b><code>Nan::GetIsolateData()</code></b></a>
+ - <a href="doc/v8_misc.md#api_nan_typedarray_contents"><b><code>Nan::TypedArrayContents</code></b></a>
### Miscellaneous Node Helpers
diff --git a/appveyor.yml b/appveyor.yml
index 1378d31..694f84e 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -7,21 +7,20 @@ environment:
- nodejs_version: "0.8"
- nodejs_version: "0.10"
- nodejs_version: "0.12"
- # io.js
- - nodejs_version: "1"
- - nodejs_version: "2"
- nodejs_version: "3"
+ - nodejs_version: "4"
# Install scripts. (runs after repo cloning)
install:
# Get the latest stable version of Node 0.STABLE.latest
- - ps: if($env:nodejs_version -eq "0.8") {Install-Product node $env:nodejs_version}
- - ps: if($env:nodejs_version -ne "0.8") {Update-NodeJsInstallation (Get-NodeJsLatestBuild $env:nodejs_version)}
- - IF %nodejs_version% LSS 1 npm -g install npm
- - IF %nodejs_version% LSS 1 set PATH=%APPDATA%\npm;%PATH%
+ - ps: Install-Product node $env:nodejs_version
+ - IF %nodejs_version% EQU 0.8 npm -g install npm at 2
+ - IF %nodejs_version% EQU 0.8 set PATH=%APPDATA%\npm;%PATH%
+ - npm -g install npm
+ - IF %nodejs_version% NEQ 0.8 set PATH=%APPDATA%\npm;%PATH%
# Typical npm stuff.
- npm install
- - IF %nodejs_version% EQU 0.8 (node node_modules\node-gyp\bin\node-gyp.js rebuild --msvs_version=2013 --directory test) ELSE (npm run rebuild-tests)
+ - npm run rebuild-tests
# Post-install test scripts.
test_script:
@@ -29,7 +28,7 @@ test_script:
- node --version
- npm --version
# run tests
- - IF %nodejs_version% LSS 1 (npm test) ELSE (iojs node_modules\tap\bin\tap.js --gc test/js/*-test.js)
+ - IF %nodejs_version% LSS 1 (npm test) ELSE (IF %nodejs_version% LSS 4 (iojs node_modules\tap\bin\tap.js --gc test/js/*-test.js) ELSE (node node_modules\tap\bin\tap.js --gc test/js/*-test.js))
# Don't actually build.
build: off
diff --git a/doc/callback.md b/doc/callback.md
index 9e0f0a2..d10aeac 100644
--- a/doc/callback.md
+++ b/doc/callback.md
@@ -48,5 +48,5 @@ Example usage:
```c++
v8::Local<v8::Function> function;
Nan::Callback callback(function);
-callback->Call(0, 0);
+callback.Call(0, 0);
```
diff --git a/doc/maybe_types.md b/doc/maybe_types.md
index 2f2e6b3..d2e5b7d 100644
--- a/doc/maybe_types.md
+++ b/doc/maybe_types.md
@@ -36,6 +36,7 @@ The `Nan::MaybeLocal` and `Nan::Maybe` types are monads that encapsulate `v8::Lo
- <a href="#api_nan_get_start_column"><b><code>Nan::GetStartColumn()</code></b></a>
- <a href="#api_nan_get_end_column"><b><code>Nan::GetEndColumn()</code></b></a>
- <a href="#api_nan_clone_element_at"><b><code>Nan::CloneElementAt()</code></b></a>
+ - <a href="#api_nan_make_maybe"><b><code>Nan::MakeMaybe()</code></b></a>
<a name="api_nan_maybe_local"></a>
### Nan::MaybeLocal
@@ -478,3 +479,22 @@ Signature:
```c++
Nan::MaybeLocal<v8::Object> Nan::CloneElementAt(v8::Local<v8::Array> array, uint32_t index);
```
+
+<a name="api_nan_make_maybe"></a>
+### Nan::MakeMaybe()
+
+Wraps a `v8::Local<>` in a `Nan::MaybeLocal<>`. When called with a `Nan::MaybeLocal<>` it just returns its argument. This is useful in generic template code that builds on NAN.
+
+Synopsis:
+
+```c++
+ MaybeLocal<v8::Number> someNumber = MakeMaybe(New<v8::Number>(3.141592654));
+ MaybeLocal<v8::String> someString = MakeMaybe(New<v8::String>("probably"));
+```
+
+Signature:
+
+```c++
+template <typename T, template <typename> class MaybeMaybe>
+Nan::MaybeLocal<T> Nan::MakeMaybe(MaybeMaybe<T> v);
+```
diff --git a/doc/methods.md b/doc/methods.md
index 6e9f9de..068feed 100644
--- a/doc/methods.md
+++ b/doc/methods.md
@@ -30,6 +30,8 @@ In order to expose functionality to JavaScript via a template, you must provide
- <a href="#api_nan_set_template"><b><code>Nan::SetTemplate()</code></b></a>
- <a href="#api_nan_set_prototype_template"><b><code>Nan::SetPrototypeTemplate()</code></b></a>
- <a href="#api_nan_set_instance_template"><b><code>Nan::SetInstanceTemplate()</code></b></a>
+ - <a href="#api_nan_set_call_handler"><b><code>Nan::SetCallHandler()</code></b></a>
+ - <a href="#api_nan_set_call_as_function_handler"><b><code>Nan::SetCallAsFunctionHandler()</code></b></a>
<a name="api_nan_function_callback_info"></a>
### Nan::FunctionCallbackInfo
@@ -73,7 +75,7 @@ template<typename T> class PropertyCallbackInfo : public PropertyCallbackInfoBas
};
```
-See the [`v8::PropertyCallbackInfo](https://v8docs.nodesource.com/io.js-3.0/d7/dc5/classv8_1_1_property_callback_info.html) documentation for usage details on these. See [`Nan::ReturnValue`](#api_nan_return_value) for further information on how to set a return value from property accessor methods.
+See the [`v8::PropertyCallbackInfo`](https://v8docs.nodesource.com/io.js-3.0/d7/dc5/classv8_1_1_property_callback_info.html) documentation for usage details on these. See [`Nan::ReturnValue`](#api_nan_return_value) for further information on how to set a return value from property accessor methods.
<a name="api_nan_return_value"></a>
### Nan::ReturnValue
@@ -130,6 +132,7 @@ Example:
void MethodName(const Nan::FunctionCallbackInfo<v8::Value>& info) {
...
}
+```
You do not need to declare a new `HandleScope` within a method as one is implicitly created for you.
@@ -222,7 +225,7 @@ Example:
```c++
void SetterName(v8::Local<v8::String> property,
v8::Local<v8::Value> value,
- const Nan::PropertyCallbackInfo<v8::Value>& info) {
+ const Nan::PropertyCallbackInfo<void>& info) {
...
}
```
@@ -471,8 +474,8 @@ Signature:
```c++
template<typename T> void Nan::SetMethod(const T &recv,
- const char *name,
- Nan::FunctionCallback callback)
+ const char *name,
+ Nan::FunctionCallback callback)
```
<a name="api_nan_set_prototype_method"></a>
@@ -503,7 +506,7 @@ void SetAccessor(v8::Local<v8::ObjectTemplate> tpl,
v8::Local<v8::Value> data = v8::Local<v8::Value>(),
v8::AccessControl settings = v8::DEFAULT,
v8::PropertyAttribute attribute = v8::None,
- imp::Sig signature = imp::Sig())
+ imp::Sig signature = imp::Sig());
bool SetAccessor(v8::Local<v8::Object> obj,
v8::Local<v8::String> name,
Nan::GetterCallback getter,
@@ -555,12 +558,12 @@ Signature:
```c++
void SetIndexedPropertyHandler(v8::Local<v8::ObjectTemplate> tpl,
- Nan::IndexGetterCallback getter,
- Nan::IndexSetterCallback setter = 0,
- Nan::IndexQueryCallback query = 0,
- Nan::IndexDeleterCallback deleter = 0,
- Nan::IndexEnumeratorCallback enumerator = 0,
- v8::Local<v8::Value> data = v8::Local<v8::Value>())
+ Nan::IndexGetterCallback getter,
+ Nan::IndexSetterCallback setter = 0,
+ Nan::IndexQueryCallback query = 0,
+ Nan::IndexDeleterCallback deleter = 0,
+ Nan::IndexEnumeratorCallback enumerator = 0,
+ v8::Local<v8::Value> data = v8::Local<v8::Value>())
```
See the V8 [`ObjectTemplate#SetIndexedPropertyHandler()`](https://v8docs.nodesource.com/io.js-3.0/db/d5f/classv8_1_1_object_template.html#ac0234cbede45d51778bb5f6a32a9e125) for further information about how to use `Nan::SetIndexedPropertyHandler()`.
@@ -575,7 +578,7 @@ Signature:
```c++
void Nan::SetTemplate(v8::Local<v8::Template> templ,
const char *name,
- v8::Local<v8::Data> value)
+ v8::Local<v8::Data> value);
void Nan::SetTemplate(v8::Local<v8::Template> templ,
v8::Local<v8::String> name,
v8::Local<v8::Data> value,
@@ -594,7 +597,7 @@ Signature:
```c++
void Nan::SetPrototypeTemplate(v8::Local<v8::FunctionTemplate> templ,
const char *name,
- v8::Local<v8::Data> value)
+ v8::Local<v8::Data> value);
void Nan::SetPrototypeTemplate(v8::Local<v8::FunctionTemplate> templ,
v8::Local<v8::String> name,
v8::Local<v8::Data> value,
@@ -613,7 +616,7 @@ Signature:
```c++
void Nan::SetInstanceTemplate(v8::Local<v8::FunctionTemplate> templ,
const char *name,
- v8::Local<v8::Data> value)
+ v8::Local<v8::Data> value);
void Nan::SetInstanceTemplate(v8::Local<v8::FunctionTemplate> templ,
v8::Local<v8::String> name,
v8::Local<v8::Data> value,
@@ -622,3 +625,31 @@ void Nan::SetInstanceTemplate(v8::Local<v8::FunctionTemplate> templ,
Calls the `FunctionTemplate`'s _InstanceTemplate's_ [`Set()`](https://v8docs.nodesource.com/io.js-3.0/db/df7/classv8_1_1_template.html#a2db6a56597bf23c59659c0659e564ddf).
+<a name="api_nan_set_call_handler"></a>
+### Nan::SetCallHandler()
+
+Set the call-handler callback for a `v8::FunctionTemplate`.
+This callback is called whenever the function created from this FunctionTemplate is called.
+
+Signature:
+
+```c++
+void Nan::SetCallHandler(v8::Local<v8::FunctionTemplate> templ, Nan::FunctionCallback callback, v8::Local<v8::Value> data = v8::Local<v8::Value>())
+```
+
+Calls the `FunctionTemplate`'s [`SetCallHandler()`](https://v8docs.nodesource.com/io.js-3.0/d8/d83/classv8_1_1_function_template.html#a26cf14e36aa1a47091b98536d08ea821).
+
+<a name="api_nan_set_call_as_function_handler"></a>
+### Nan::SetCallAsFunctionHandler()
+
+Sets the callback to be used when calling instances created from the `v8::ObjectTemplate` as a function.
+If no callback is set, instances behave like normal JavaScript objects that cannot be called as a function.
+
+Signature:
+
+```c++
+void Nan::SetCallAsFunctionHandler(v8::Local<v8::ObjectTemplate> templ, Nan::FunctionCallback callback, v8::Local<v8::Value> data = v8::Local<v8::Value>())
+```
+
+Calls the `ObjectTemplate`'s [`SetCallAsFunctionHandler()`](https://v8docs.nodesource.com/io.js-3.0/db/d5f/classv8_1_1_object_template.html#ae0a0e72fb0c5e5f32e255fe5bcc7316a).
+
diff --git a/doc/new.md b/doc/new.md
index 318af37..c0734bd 100644
--- a/doc/new.md
+++ b/doc/new.md
@@ -48,17 +48,23 @@ Nan::New<T>(Nan::FunctionCallback callback,
A2 a2 = A2());
```
-Native types:
+Native number types:
```c++
v8::Local<v8::Boolean> Nan::New<T>(bool value);
v8::Local<v8::Int32> Nan::New<T>(int32_t value);
v8::Local<v8::Uint32> Nan::New<T>(uint32_t value);
v8::Local<v8::Number> Nan::New<T>(double value);
-v8::Local<v8::String> Nan::New<T>(std::string const& value);
-v8::Local<v8::String> Nan::New<T>(const char * value, int length);
-v8::Local<v8::String> Nan::New<T>(const char * value);
-v8::Local<v8::String> Nan::New<T>(const uint16_t * value);
+```
+
+String types:
+
+```c++
+Nan::MaybeLocal<v8::String> Nan::New<T>(std::string const& value);
+Nan::MaybeLocal<v8::String> Nan::New<T>(const char * value, int length);
+Nan::MaybeLocal<v8::String> Nan::New<T>(const char * value);
+Nan::MaybeLocal<v8::String> Nan::New<T>(const uint16_t * value);
+Nan::MaybeLocal<v8::String> Nan::New<T>(const uint16_t * value, int length);
```
Specialized types:
@@ -124,7 +130,7 @@ Call [`v8::String::Empty`](https://v8docs.nodesource.com/io.js-3.0/d2/db3/classv
Signature:
```c++
-v8::Local<v8::String> Nan::EmptyString()
+v8::Local<v8::String> Nan::EmptyString()
```
@@ -137,5 +143,5 @@ Signature:
```c++
Nan::MaybeLocal<v8::String> Nan::NewOneByteString(const uint8_t * value,
- int length = -1)
+ int length = -1)
```
diff --git a/doc/persistent.md b/doc/persistent.md
index 7ac0f35..743b44a 100644
--- a/doc/persistent.md
+++ b/doc/persistent.md
@@ -95,6 +95,12 @@ template<typename T> class PersistentBase {
See the V8 documentation for [`PersistentBase`](https://v8docs.nodesource.com/io.js-3.0/d4/dca/classv8_1_1_persistent_base.html) for further information.
+**Tip:** To get a `v8::Local` reference to the original object back from a `PersistentBase` or `Persistent` object:
+
+```c++
+v8::Local<v8::Object> object = Nan::New(persistent);
+```
+
<a name="api_nan_non_copyable_persistent_traits"></a>
### Nan::NonCopyablePersistentTraits & v8::NonCopyablePersistentTraits
@@ -108,10 +114,10 @@ _(note: this is implemented as `Nan::NonCopyablePersistentTraits` for older vers
template<typename T> class NonCopyablePersistentTraits {
public:
typedef Persistent<T, NonCopyablePersistentTraits<T> > NonCopyablePersistent;
-
+
static const bool kResetInDestructor = false;
-
- template<typename S, typename M>
+
+ template<typename S, typename M>
static void Copy(const Persistent<S, M> &source,
NonCopyablePersistent *dest);
@@ -233,9 +239,6 @@ template<typename T> class Global : public PersistentBase<T> {
See the V8 documentation for [`Global`](https://v8docs.nodesource.com/io.js-3.0/d5/d40/classv8_1_1_global.html) for further information.
-<a name="api_nan_weak_callback_type"></a>
-### Nan::WeakCallbackType
-
<a name="api_nan_weak_callback_info"></a>
### Nan::WeakCallbackInfo
diff --git a/doc/v8_internals.md b/doc/v8_internals.md
index b188209..8e6319b 100644
--- a/doc/v8_internals.md
+++ b/doc/v8_internals.md
@@ -195,5 +195,5 @@ Signature:
int Nan::AdjustExternalMemory(int bytesChange)
```
-Calls V8's [`AdjustAmountOfExternalAllocatedMemory()` or `AdjustAmountOfExternalAllocatedMemory()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#ae1a59cac60409d3922582c4af675473e) depending on the version of V8.
+Calls V8's [`AdjustAmountOfExternalAllocatedMemory()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#ae1a59cac60409d3922582c4af675473e).
diff --git a/doc/v8_misc.md b/doc/v8_misc.md
index e87af3f..653ef61 100644
--- a/doc/v8_misc.md
+++ b/doc/v8_misc.md
@@ -4,6 +4,7 @@
- <a href="#api_nan_get_current_context"><b><code>Nan::GetCurrentContext()</code></b></a>
- <a href="#api_nan_set_isolate_data"><b><code>Nan::SetIsolateData()</code></b></a>
- <a href="#api_nan_get_isolate_data"><b><code>Nan::GetIsolateData()</code></b></a>
+ - <a href="#api_nan_typedarray_contents"><b><code>Nan::TypedArrayContents<T></code></b></a>
<a name="api_nan_utf8_string"></a>
@@ -61,3 +62,24 @@ Signature:
T *Nan::GetIsolateData(v8::Isolate *isolate)
```
+<a name="api_nan_typedarray_contents"></a>
+### Nan::TypedArrayContents<T>
+
+A helper class for accessing the contents of an ArrayBufferView (aka a typedarray) from C++. If the input array is not a valid typedarray, then the data pointer of TypedArrayContents will default to `NULL` and the length will be 0. If the data pointer is not compatible with the alignment requirements of type, an assertion error will fail.
+
+Note that you must store a reference to the `array` object while you are accessing its contents.
+
+Definition:
+
+```c++
+template<typename T>
+class Nan::TypedArrayContents {
+ public:
+ TypedArrayContents(v8::Local<Value> array);
+
+ size_t length() const;
+
+ T* const operator*();
+ const T* const operator*() const;
+};
+```
diff --git a/nan.h b/nan.h
index 3f09c8d..a5cd895 100644
--- a/nan.h
+++ b/nan.h
@@ -12,7 +12,7 @@
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
*
- * Version 2.0.9: current Node 4.0.0, Node 12: 0.12.7, Node 10: 0.10.40, iojs: 3.2.0
+ * Version 2.1.0: current Node 4.1.2, Node 12: 0.12.7, Node 10: 0.10.40, iojs: 3.3.1
*
* See https://github.com/nodejs/nan for the latest update to this file
**********************************************************************************/
@@ -20,10 +20,30 @@
#ifndef NAN_H_
#define NAN_H_
+#include <node_version.h>
+
+#define NODE_0_10_MODULE_VERSION 11
+#define NODE_0_12_MODULE_VERSION 14
+#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 IOJS_3_0_MODULE_VERSION 45
+#define NODE_4_0_MODULE_VERSION 46
+
+#ifdef _MSC_VER
+# define NAN_HAS_CPLUSPLUS_11 (_MSC_VER >= 1800)
+#else
+# define NAN_HAS_CPLUSPLUS_11 (__cplusplus >= 201103L)
+#endif
+
+#if NODE_MODULE_VERSION >= IOJS_3_0_MODULE_VERSION && ! NAN_HAS_CPLUSPLUS_11
+# error This version of node/NAN/v8 requires a C++11 compiler
+#endif
+
#include <uv.h>
#include <node.h>
#include <node_buffer.h>
-#include <node_version.h>
#include <node_object_wrap.h>
#include <algorithm>
#include <cstring>
@@ -109,14 +129,6 @@ namespace Nan {
NAN_DISALLOW_COPY(CLASS) \
NAN_DISALLOW_MOVE(CLASS)
-#define NODE_0_10_MODULE_VERSION 11
-#define NODE_0_12_MODULE_VERSION 14
-#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 IOJS_3_0_MODULE_VERSION 45
-
#define TYPE_CHECK(T, S) \
while (false) { \
*(static_cast<T *volatile *>(0)) = static_cast<S*>(0); \
@@ -834,7 +846,7 @@ class TryCatch {
node::FatalException(v8::Isolate::GetCurrent(), try_catch.try_catch_);
}
- NAN_INLINE v8::Local<v8::Value> NanErrnoException(
+ NAN_INLINE v8::Local<v8::Value> ErrnoException(
int errorno
, const char* syscall = NULL
, const char* message = NULL
@@ -843,6 +855,14 @@ class TryCatch {
message, path);
}
+ NAN_DEPRECATED NAN_INLINE v8::Local<v8::Value> NanErrnoException(
+ int errorno
+ , const char* syscall = NULL
+ , const char* message = NULL
+ , const char* path = NULL) {
+ return ErrnoException(errorno, syscall, message, path);
+ }
+
template<typename T>
NAN_INLINE void SetIsolateData(
v8::Isolate *isolate
@@ -1145,7 +1165,7 @@ widenString(std::vector<uint16_t> *ws, const uint8_t *s, int l) {
node::FatalException(const_cast<v8::TryCatch &>(try_catch.try_catch_));
}
- NAN_INLINE v8::Local<v8::Value> NanErrnoException(
+ NAN_INLINE v8::Local<v8::Value> ErrnoException(
int errorno
, const char* syscall = NULL
, const char* message = NULL
@@ -1153,6 +1173,14 @@ widenString(std::vector<uint16_t> *ws, const uint8_t *s, int l) {
return node::ErrnoException(errorno, syscall, message, path);
}
+ NAN_DEPRECATED NAN_INLINE v8::Local<v8::Value> NanErrnoException(
+ int errorno
+ , const char* syscall = NULL
+ , const char* message = NULL
+ , const char* path = NULL) {
+ return ErrnoException(errorno, syscall, message, path);
+ }
+
template<typename T>
NAN_INLINE void SetIsolateData(
@@ -2073,6 +2101,49 @@ inline void SetIndexedPropertyHandler(
#endif
}
+inline void SetCallHandler(
+ v8::Local<v8::FunctionTemplate> tpl
+ , FunctionCallback callback
+ , v8::Local<v8::Value> data = v8::Local<v8::Value>()) {
+ HandleScope scope;
+
+ v8::Local<v8::ObjectTemplate> otpl = New<v8::ObjectTemplate>();
+ otpl->SetInternalFieldCount(imp::kFunctionFieldCount);
+ v8::Local<v8::Object> obj = NewInstance(otpl).ToLocalChecked();
+
+ obj->SetInternalField(
+ imp::kFunctionIndex
+ , New<v8::External>(reinterpret_cast<void *>(callback)));
+
+ if (!data.IsEmpty()) {
+ obj->SetInternalField(imp::kDataIndex, data);
+ }
+
+ tpl->SetCallHandler(imp::FunctionCallbackWrapper, obj);
+}
+
+
+inline void SetCallAsFunctionHandler(
+ v8::Local<v8::ObjectTemplate> tpl,
+ FunctionCallback callback,
+ v8::Local<v8::Value> data = v8::Local<v8::Value>()) {
+ HandleScope scope;
+
+ v8::Local<v8::ObjectTemplate> otpl = New<v8::ObjectTemplate>();
+ otpl->SetInternalFieldCount(imp::kFunctionFieldCount);
+ v8::Local<v8::Object> obj = NewInstance(otpl).ToLocalChecked();
+
+ obj->SetInternalField(
+ imp::kFunctionIndex
+ , New<v8::External>(reinterpret_cast<void *>(callback)));
+
+ if (!data.IsEmpty()) {
+ obj->SetInternalField(imp::kDataIndex, data);
+ }
+
+ tpl->SetCallAsFunctionHandler(imp::FunctionCallbackWrapper, obj);
+}
+
//=== Weak Persistent Handling =================================================
#include "nan_weak.h" // NOLINT(build/include)
@@ -2131,6 +2202,36 @@ struct Tap {
#undef TYPE_CHECK
+//=== Generic Maybefication ===================================================
+
+namespace imp {
+
+template <typename T> struct Maybefier;
+
+template <typename T> struct Maybefier<v8::Local<T> > {
+ static MaybeLocal<T> convert(v8::Local<T> v) {
+ return MaybeLocal<T>(v);
+ }
+};
+
+template <typename T> struct Maybefier<MaybeLocal<T> > {
+ static MaybeLocal<T> convert(MaybeLocal<T> v) {
+ return v;
+ }
+};
+
+} // end of namespace imp
+
+template <typename T, template <typename> class MaybeMaybe>
+MaybeLocal<T>
+MakeMaybe(MaybeMaybe<T> v) {
+ return imp::Maybefier<MaybeMaybe<T> >::convert(v);
+}
+
+//=== TypedArrayContents =======================================================
+
+#include "nan_typedarray_contents.h" // NOLINT(build/include)
+
} // end of namespace Nan
#endif // NAN_H_
diff --git a/nan_callbacks_12_inl.h b/nan_callbacks_12_inl.h
index 311e49a..b20d68f 100644
--- a/nan_callbacks_12_inl.h
+++ b/nan_callbacks_12_inl.h
@@ -137,7 +137,7 @@ class PropertyCallbackInfo {
public:
explicit inline PropertyCallbackInfo(
const v8::PropertyCallbackInfo<T> &info
- , const v8::Local<v8::Value> &data) :
+ , const v8::Local<v8::Value> data) :
info_(info)
, data_(data) {}
diff --git a/nan_callbacks_pre_12_inl.h b/nan_callbacks_pre_12_inl.h
index 316f506..9a01443 100644
--- a/nan_callbacks_pre_12_inl.h
+++ b/nan_callbacks_pre_12_inl.h
@@ -149,12 +149,12 @@ class FunctionCallbackInfo {
template<typename T>
class PropertyCallbackInfoBase {
const v8::AccessorInfo &info_;
- const v8::Local<v8::Value> &data_;
+ const v8::Local<v8::Value> data_;
public:
explicit inline PropertyCallbackInfoBase(
const v8::AccessorInfo &info
- , const v8::Local<v8::Value> &data) :
+ , const v8::Local<v8::Value> data) :
info_(info)
, data_(data) {}
@@ -184,7 +184,7 @@ class PropertyCallbackInfo : public PropertyCallbackInfoBase<T> {
public:
explicit inline PropertyCallbackInfo(
const v8::AccessorInfo &info
- , const v8::Local<v8::Value> &data) :
+ , const v8::Local<v8::Value> data) :
PropertyCallbackInfoBase<T>(info, data)
, return_value_(info.GetIsolate(), &retval_)
, retval_(v8::Persistent<T>::New(v8::Undefined())) {}
@@ -206,7 +206,7 @@ class PropertyCallbackInfo<v8::Array> :
public:
explicit inline PropertyCallbackInfo(
const v8::AccessorInfo &info
- , const v8::Local<v8::Value> &data) :
+ , const v8::Local<v8::Value> data) :
PropertyCallbackInfoBase<v8::Array>(info, data)
, return_value_(info.GetIsolate(), &retval_)
, retval_(v8::Persistent<v8::Array>::New(v8::Local<v8::Array>())) {}
@@ -230,7 +230,7 @@ class PropertyCallbackInfo<v8::Boolean> :
public:
explicit inline PropertyCallbackInfo(
const v8::AccessorInfo &info
- , const v8::Local<v8::Value> &data) :
+ , const v8::Local<v8::Value> data) :
PropertyCallbackInfoBase<v8::Boolean>(info, data)
, return_value_(info.GetIsolate(), &retval_)
, retval_(v8::Persistent<v8::Boolean>::New(v8::Local<v8::Boolean>())) {}
@@ -254,7 +254,7 @@ class PropertyCallbackInfo<v8::Integer> :
public:
explicit inline PropertyCallbackInfo(
const v8::AccessorInfo &info
- , const v8::Local<v8::Value> &data) :
+ , const v8::Local<v8::Value> data) :
PropertyCallbackInfoBase<v8::Integer>(info, data)
, return_value_(info.GetIsolate(), &retval_)
, retval_(v8::Persistent<v8::Integer>::New(v8::Local<v8::Integer>())) {}
diff --git a/nan_implementation_12_inl.h b/nan_implementation_12_inl.h
index f71d7fe..a140f85 100644
--- a/nan_implementation_12_inl.h
+++ b/nan_implementation_12_inl.h
@@ -396,4 +396,9 @@ inline v8::Local<T> New(Persistent<T, M> const& p) {
return v8::Local<T>::New(v8::Isolate::GetCurrent(), p);
}
+template <typename T>
+inline v8::Local<T> New(Global<T> const& p) {
+ return v8::Local<T>::New(v8::Isolate::GetCurrent(), p);
+}
+
#endif // NAN_IMPLEMENTATION_12_INL_H_
diff --git a/nan_implementation_pre_12_inl.h b/nan_implementation_pre_12_inl.h
index b666150..15a815c 100644
--- a/nan_implementation_pre_12_inl.h
+++ b/nan_implementation_pre_12_inl.h
@@ -256,4 +256,9 @@ inline v8::Local<T> New(Persistent<T, M> const& p) {
return v8::Local<T>::New(p.persistent);
}
+template <typename T>
+inline v8::Local<T> New(Global<T> const& p) {
+ return v8::Local<T>::New(p.persistent);
+}
+
#endif // NAN_IMPLEMENTATION_PRE_12_INL_H_
diff --git a/nan_new.h b/nan_new.h
index bc799cc..c6c4236 100644
--- a/nan_new.h
+++ b/nan_new.h
@@ -262,6 +262,8 @@ template <typename T> inline v8::Local<T> New(v8::Persistent<T> const& p);
#endif
template <typename T, typename M>
inline v8::Local<T> New(Persistent<T, M> const& p);
+template <typename T>
+inline v8::Local<T> New(Global<T> const& p);
inline
imp::Factory<v8::Boolean>::return_t
@@ -301,6 +303,12 @@ New(const char * value, int length) {
inline
imp::Factory<v8::String>::return_t
+New(const uint16_t * value, int length) {
+ return New<v8::String>(value, length);
+}
+
+inline
+imp::Factory<v8::String>::return_t
New(const char * value) {
return New<v8::String>(value);
}
diff --git a/nan_persistent_pre_12_inl.h b/nan_persistent_pre_12_inl.h
index cca1162..8212f58 100644
--- a/nan_persistent_pre_12_inl.h
+++ b/nan_persistent_pre_12_inl.h
@@ -12,8 +12,12 @@
template<typename T>
class PersistentBase {
v8::Persistent<T> persistent;
+ template<typename U>
+ friend v8::Local<U> New(const PersistentBase<U> &p);
template<typename U, typename M>
friend v8::Local<U> New(const Persistent<U, M> &p);
+ template<typename U>
+ friend v8::Local<U> New(const Global<U> &p);
template<typename S> friend class ReturnValue;
public:
diff --git a/nan_typedarray_contents.h b/nan_typedarray_contents.h
new file mode 100644
index 0000000..114eed4
--- /dev/null
+++ b/nan_typedarray_contents.h
@@ -0,0 +1,87 @@
+/*********************************************************************
+ * NAN - Native Abstractions for Node.js
+ *
+ * Copyright (c) 2015 NAN contributors
+ *
+ * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
+ ********************************************************************/
+
+#ifndef NAN_TYPEDARRAY_CONTENTS_H_
+#define NAN_TYPEDARRAY_CONTENTS_H_
+
+template<typename T>
+class TypedArrayContents {
+ public:
+ NAN_INLINE explicit TypedArrayContents(v8::Local<v8::Value> from) :
+ length_(0), data_(NULL) {
+
+ size_t length = 0;
+ void* data = NULL;
+
+#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
+ (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
+
+ if (from->IsArrayBufferView()) {
+ v8::Local<v8::ArrayBufferView> array =
+ v8::Local<v8::ArrayBufferView>::Cast(from);
+
+ const size_t byte_length = array->ByteLength();
+ const ptrdiff_t byte_offset = array->ByteOffset();
+ v8::Local<v8::ArrayBuffer> buffer = array->Buffer();
+
+ length = byte_length / sizeof(T);
+ data = static_cast<char*>(buffer->GetContents().Data()) + byte_offset;
+ }
+
+#else
+
+ if (from->IsObject() && !from->IsNull()) {
+ v8::Local<v8::Object> array = v8::Local<v8::Object>::Cast(from);
+
+ MaybeLocal<v8::Value> buffer = Get(array,
+ New<v8::String>("buffer").ToLocalChecked());
+ MaybeLocal<v8::Value> byte_length = Get(array,
+ New<v8::String>("byteLength").ToLocalChecked());
+ MaybeLocal<v8::Value> byte_offset = Get(array,
+ New<v8::String>("byteOffset").ToLocalChecked());
+
+ if (!buffer.IsEmpty() &&
+ !byte_length.IsEmpty() && byte_length.ToLocalChecked()->IsUint32() &&
+ !byte_offset.IsEmpty() && byte_offset.ToLocalChecked()->IsUint32()) {
+ data = array->GetIndexedPropertiesExternalArrayData();
+ if(data) {
+ length = byte_length.ToLocalChecked()->Uint32Value() / sizeof(T);
+ }
+ }
+ }
+
+#endif
+
+#if defined(_MSC_VER) || defined(__GNUC__)
+ assert(reinterpret_cast<uintptr_t>(data) % __alignof(T) == 0);
+#elif __cplusplus >= 201103L
+ assert(reinterpret_cast<uintptr_t>(data) % alignof(T) == 0);
+#else
+ assert(reinterpret_cast<uintptr_t>(data) % sizeof(T) == 0);
+#endif
+
+ length_ = length;
+ data_ = static_cast<T*>(data);
+ }
+
+ NAN_INLINE size_t length() const { return length_; }
+ NAN_INLINE T* operator*() { return data_; }
+ NAN_INLINE const T* operator*() const { return data_; }
+
+ private:
+ NAN_DISALLOW_ASSIGN_COPY_MOVE(TypedArrayContents)
+
+ //Disable heap allocation
+ void *operator new(size_t size);
+ void operator delete(void *, size_t);
+
+ size_t length_;
+ T* data_;
+};
+
+#endif // NAN_TYPEDARRAY_CONTENTS_H_
diff --git a/package.json b/package.json
index 9b391e5..19515ce 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "nan",
- "version": "2.0.9",
+ "version": "2.1.0",
"description": "Native Abstractions for Node.js: C++ header for Node 0.8 -> 4 compatibility",
"main": "include_dirs.js",
"repository": {
@@ -9,7 +9,7 @@
},
"scripts": {
"test": "tap --gc test/js/*-test.js",
- "rebuild-tests": "pangyp rebuild --msvs_version=2013 --directory test",
+ "rebuild-tests": "node-gyp rebuild --msvs_version=2013 --directory test",
"docs": "doc/.build.sh"
},
"contributors": [
@@ -25,8 +25,7 @@
"bindings": "~1.2.1",
"commander": "^2.8.1",
"glob": "^5.0.14",
- "node-gyp": "~2.0.2",
- "pangyp": "~2.2.0",
+ "node-gyp": "~3.0.1",
"tap": "~0.7.1",
"xtend": "~4.0.0"
},
diff --git a/test/binding.gyp b/test/binding.gyp
index e3d3f6c..278666a 100644
--- a/test/binding.gyp
+++ b/test/binding.gyp
@@ -22,8 +22,9 @@
, "sources" : [ "cpp/news.cpp" ]
}
, {
- "target_name" : "morenews"
- , "sources" : [ "cpp/morenews.cpp" ]
+ "target_name" : "morenews"
+ , "win_delay_load_hook" : "false"
+ , "sources" : [ "cpp/morenews.cpp" ]
}
, {
"target_name" : "returnvalue"
@@ -131,4 +132,16 @@
"target_name" : "trycatch"
, "sources" : [ "cpp/trycatch.cpp" ]
}
+ , {
+ "target_name" : "wrappedobjectfactory"
+ , "sources" : [ "cpp/wrappedobjectfactory.cpp" ]
+ }
+ , {
+ "target_name" : "setcallhandler"
+ , "sources" : [ "cpp/setcallhandler.cpp" ]
+ }
+ , {
+ "target_name" : "typedarrays"
+ , "sources" : [ "cpp/typedarrays.cpp" ]
+ }
]}
diff --git a/test/cpp/nannew.cpp b/test/cpp/nannew.cpp
index 6d57f62..3934588 100644
--- a/test/cpp/nannew.cpp
+++ b/test/cpp/nannew.cpp
@@ -289,7 +289,7 @@ NAN_METHOD(testSignature) {
NAN_METHOD(testString) {
Tap t(info[0]);
- t.plan(14);
+ t.plan(16);
t.ok(_( stringMatches(
New<String>("Hello World").ToLocalChecked(), "Hello World")));
@@ -319,6 +319,10 @@ NAN_METHOD(testString) {
t.ok(_( stringMatches( New("Hello World", 4).ToLocalChecked(), "Hell")));
t.ok(_( assertType<String>( New("plonk.", 4).ToLocalChecked())));
+ const uint16_t *widestring = reinterpret_cast<const uint16_t *>("H\0e\0l\0l\0o\0");
+ t.ok(_( stringMatches( New(widestring, 4).ToLocalChecked(), "Hell")));
+ t.ok(_( assertType<String>( New(widestring, 4).ToLocalChecked())));
+
t.ok(_( stringMatches( New(std::string("bar")).ToLocalChecked(), "bar")));
t.ok(_( assertType<String>( New(std::string("plonk.")).ToLocalChecked())));
@@ -358,6 +362,19 @@ NAN_METHOD(testPersistents) {
info.GetReturnValue().SetUndefined();
}
+NAN_METHOD(testGlobals) {
+ Tap t(info[0]);
+
+ t.plan(1);
+
+ Nan::Global<String> p;
+ p.Reset(New("foo").ToLocalChecked());
+ t.ok(_( assertType<String>( New(p))));
+ p.Reset();
+
+ info.GetReturnValue().SetUndefined();
+}
+
//==============================================================================
// Regression Tests
//==============================================================================
@@ -443,6 +460,13 @@ NAN_METHOD(newExternal) {
info.GetReturnValue().Set(New<External>(&ttt));
}
+NAN_METHOD(invokeMakeMaybe) {
+ Nan::MaybeLocal<v8::Number> number = MakeMaybe(New<v8::Number>(3.141592654));
+ Nan::MaybeLocal<v8::String> string = MakeMaybe(New<v8::String>("probably"));
+ (void)string;
+ info.GetReturnValue().Set(number.ToLocalChecked());
+}
+
NAN_MODULE_INIT(Init) {
NAN_EXPORT(target, testArray);
NAN_EXPORT(target, testBoolean);
@@ -462,6 +486,7 @@ NAN_MODULE_INIT(Init) {
NAN_EXPORT(target, testStringObject);
NAN_EXPORT(target, testPersistents);
+ NAN_EXPORT(target, testGlobals);
NAN_EXPORT(target, testRegression212);
NAN_EXPORT(target, testRegression242);
@@ -474,6 +499,8 @@ NAN_MODULE_INIT(Init) {
NAN_EXPORT(target, newStringFromStdString);
NAN_EXPORT(target, newExternal);
+
+ NAN_EXPORT(target, invokeMakeMaybe);
}
} // end of anonymous namespace
diff --git a/test/cpp/objectwraphandle.cpp b/test/cpp/objectwraphandle.cpp
index e163622..ca8a07a 100644
--- a/test/cpp/objectwraphandle.cpp
+++ b/test/cpp/objectwraphandle.cpp
@@ -20,7 +20,7 @@ class MyObject : public ObjectWrap {
SetPrototypeMethod(tpl, "getHandle", GetHandle);
SetPrototypeMethod(tpl, "getValue", GetValue);
- constructor.Reset(tpl->GetFunction());
+ constructor().Reset(tpl->GetFunction());
Set(target, Nan::New("MyObject").ToLocalChecked(), tpl->GetFunction());
}
@@ -37,7 +37,7 @@ class MyObject : public ObjectWrap {
} else {
const int argc = 1;
v8::Local<v8::Value> argv[argc] = {info[0]};
- v8::Local<v8::Function> cons = Nan::New(constructor);
+ v8::Local<v8::Function> cons = Nan::New(constructor());
info.GetReturnValue().Set(cons->NewInstance(argc, argv));
}
}
@@ -52,10 +52,12 @@ class MyObject : public ObjectWrap {
info.GetReturnValue().Set(obj->value_);
}
- static Persistent<v8::Function> constructor;
+ static inline Persistent<v8::Function> & constructor() {
+ static Persistent<v8::Function> my_constructor;
+ return my_constructor;
+ }
+
double value_;
};
-Persistent<v8::Function> MyObject::constructor;
-
NODE_MODULE(objectwraphandle, MyObject::Init)
diff --git a/test/cpp/setcallhandler.cpp b/test/cpp/setcallhandler.cpp
new file mode 100644
index 0000000..c0d1f60
--- /dev/null
+++ b/test/cpp/setcallhandler.cpp
@@ -0,0 +1,45 @@
+/*********************************************************************
+ * NAN - Native Abstractions for Node.js
+ *
+ * Copyright (c) 2015 NAN contributors
+ *
+ * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
+ ********************************************************************/
+
+#include <nan.h>
+
+using namespace Nan; // NOLINT(build/namespaces)
+
+NAN_METHOD(CallHandler) {
+ info.GetReturnValue().Set(12);
+}
+
+NAN_METHOD(CallHandlerSetter) {
+ v8::Local<v8::FunctionTemplate> tpl = New<v8::FunctionTemplate>();
+ SetCallHandler(tpl, CallHandler);
+ info.GetReturnValue().Set(GetFunction(tpl).ToLocalChecked());
+}
+
+NAN_METHOD(CallAsFunctionHandler) {
+ info.GetReturnValue().Set(15);
+}
+
+NAN_METHOD(CallAsFunctionHandlerSetter) {
+ v8::Local<v8::ObjectTemplate> tpl = New<v8::ObjectTemplate>();
+ SetCallAsFunctionHandler(tpl, CallAsFunctionHandler);
+ info.GetReturnValue().Set(NewInstance(tpl).ToLocalChecked());
+}
+
+NAN_MODULE_INIT(Init) {
+ Set(target
+ , New("a").ToLocalChecked()
+ , New<v8::FunctionTemplate>(CallHandlerSetter)->GetFunction()
+ );
+ Set(target
+ , New("b").ToLocalChecked()
+ , New<v8::FunctionTemplate>(CallAsFunctionHandlerSetter)->GetFunction()
+ );
+}
+
+NODE_MODULE(setcallhandler, Init)
+
diff --git a/test/cpp/typedarrays.cpp b/test/cpp/typedarrays.cpp
new file mode 100644
index 0000000..0923347
--- /dev/null
+++ b/test/cpp/typedarrays.cpp
@@ -0,0 +1,66 @@
+/*********************************************************************
+ * NAN - Native Abstractions for Node.js
+ *
+ * Copyright (c) 2015 NAN contributors
+ *
+ * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
+ ********************************************************************/
+
+#include <nan.h>
+
+#include <stdint.h>
+
+using namespace Nan; // NOLINT(build/namespaces)
+
+NAN_METHOD(ReadU8) {
+ TypedArrayContents<uint8_t> data(info[0]);
+
+ v8::Local<v8::Array> result = New<v8::Array>(data.length());
+ for (size_t i=0; i<data.length(); i++) {
+ Set(result, i, New<v8::Number>((*data)[i]));
+ }
+
+ info.GetReturnValue().Set(result);
+}
+
+NAN_METHOD(ReadI32) {
+ TypedArrayContents<int32_t> data(info[0]);
+
+ v8::Local<v8::Array> result = New<v8::Array>(data.length());
+ for (size_t i=0; i<data.length(); i++) {
+ Set(result, i, New<v8::Number>((*data)[i]));
+ }
+
+ info.GetReturnValue().Set(result);
+}
+
+NAN_METHOD(ReadFloat) {
+ TypedArrayContents<float> data(info[0]);
+
+ v8::Local<v8::Array> result = New<v8::Array>(data.length());\
+ for (size_t i=0; i<data.length(); i++) {
+ Set(result, i, New<v8::Number>((*data)[i]));
+ }
+
+ info.GetReturnValue().Set(result);
+}
+
+NAN_METHOD(ReadDouble) {
+ TypedArrayContents<double> data(info[0]);
+
+ v8::Local<v8::Array> result = New<v8::Array>(data.length());
+ for (size_t i=0; i<data.length(); i++) {
+ Set(result, i, New<v8::Number>((*data)[i]));
+ }
+
+ info.GetReturnValue().Set(result);
+}
+
+NAN_MODULE_INIT(Init) {
+ NAN_EXPORT(target, ReadU8);
+ NAN_EXPORT(target, ReadI32);
+ NAN_EXPORT(target, ReadFloat);
+ NAN_EXPORT(target, ReadDouble);
+}
+
+NODE_MODULE(typedarrays, Init)
diff --git a/test/cpp/wrappedobjectfactory.cpp b/test/cpp/wrappedobjectfactory.cpp
new file mode 100644
index 0000000..9833b4f
--- /dev/null
+++ b/test/cpp/wrappedobjectfactory.cpp
@@ -0,0 +1,128 @@
+/*********************************************************************
+ * NAN - Native Abstractions for Node.js
+ *
+ * Copyright (c) 2015 NAN contributors
+ *
+ * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
+ ********************************************************************/
+
+#include <nan.h>
+
+using namespace Nan; // NOLINT(build/namespaces)
+
+// ref: https://nodejs.org/api/addons.html#addons_factory_of_wrapped_objects
+
+class InnerObject : public ObjectWrap {
+ public:
+ static NAN_MODULE_INIT(Init) {
+ v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
+ tpl->InstanceTemplate()->SetInternalFieldCount(1);
+
+ SetPrototypeMethod(tpl, "getValue", GetValue);
+
+ constructor().Reset(GetFunction(tpl).ToLocalChecked());
+ }
+
+ static v8::Local<v8::Object> NewInstance(int argc, v8::Local<v8::Value> argv[]) {
+ v8::Local<v8::Function> cons = Nan::New(constructor());
+ return Nan::NewInstance(cons, argc, argv).ToLocalChecked();
+ }
+
+ private:
+ explicit InnerObject(double value = 0) : value_(value) {}
+ ~InnerObject() {}
+
+ static NAN_METHOD(New) {
+ if (info.IsConstructCall()) {
+ double value = info[0]->IsNumber() ? To<double>(info[0]).FromJust() : 0;
+ InnerObject * obj = new InnerObject(value);
+ obj->Wrap(info.This());
+ info.GetReturnValue().Set(info.This());
+ } else {
+ const int argc = 1;
+ v8::Local<v8::Value> argv[argc] = {info[0]};
+ v8::Local<v8::Function> cons = Nan::New(constructor());
+ info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
+ }
+ }
+
+ static NAN_METHOD(GetValue) {
+ InnerObject* obj = ObjectWrap::Unwrap<InnerObject>(info.This());
+ info.GetReturnValue().Set(obj->value_);
+ }
+
+ static inline Persistent<v8::Function> & constructor() {
+ static Persistent<v8::Function> my_constructor;
+ return my_constructor;
+ }
+
+ double value_;
+};
+
+class MyObject : public ObjectWrap {
+ public:
+ static NAN_MODULE_INIT(Init) {
+ v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
+ tpl->InstanceTemplate()->SetInternalFieldCount(1);
+
+ SetPrototypeMethod(tpl, "getValue", GetValue);
+ SetPrototypeMethod(tpl, "newInnerObject", NewInnerObject);
+
+ constructor().Reset(GetFunction(tpl).ToLocalChecked());
+ }
+
+ static NAN_METHOD(NewInstance) {
+ v8::Local<v8::Function> cons = Nan::New(constructor());
+ double value = info[0]->IsNumber() ? To<double>(info[0]).FromJust() : 0;
+ const int argc = 1;
+ v8::Local<v8::Value> argv[1] = {Nan::New(value)};
+ info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
+ }
+
+ private:
+ explicit MyObject(double value = 0) : value_(value) {}
+ ~MyObject() {}
+
+ static NAN_METHOD(New) {
+ if (info.IsConstructCall()) {
+ double value = info[0]->IsNumber() ? To<double>(info[0]).FromJust() : 0;
+ MyObject * obj = new MyObject(value);
+ obj->Wrap(info.This());
+ info.GetReturnValue().Set(info.This());
+ } else {
+ const int argc = 1;
+ v8::Local<v8::Value> argv[argc] = {info[0]};
+ v8::Local<v8::Function> cons = Nan::New(constructor());
+ info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
+ }
+ }
+
+ static NAN_METHOD(GetValue) {
+ MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.This());
+ info.GetReturnValue().Set(obj->value_);
+ }
+
+ static NAN_METHOD(NewInnerObject) {
+ const int argc = 1;
+ v8::Local<v8::Value> argv[argc] = {info[0]};
+ info.GetReturnValue().Set(InnerObject::NewInstance(argc, argv));
+ }
+
+ static inline Persistent<v8::Function> & constructor() {
+ static Persistent<v8::Function> my_constructor;
+ return my_constructor;
+ }
+
+ double value_;
+};
+
+NAN_MODULE_INIT(Init) {
+ InnerObject::Init(target);
+ MyObject::Init(target);
+ Set(target
+ , New<v8::String>("newFactoryObjectInstance").ToLocalChecked()
+ , GetFunction(New<v8::FunctionTemplate>(MyObject::NewInstance)).ToLocalChecked()
+ );
+}
+
+NODE_MODULE(wrappedobjectfactory, Init)
diff --git a/test/js/nannew-test.js b/test/js/nannew-test.js
index 9544d72..87b8dbc 100644
--- a/test/js/nannew-test.js
+++ b/test/js/nannew-test.js
@@ -49,3 +49,8 @@ test('strings', function (t) {
t.end();
});
+test('test MakeMaybe(...)', function (t) {
+ t.plan(1);
+ t.ok(bindings.invokeMakeMaybe() - Math.PI < 10e-8);
+ t.end();
+});
diff --git a/test/js/setcallhandler-test.js b/test/js/setcallhandler-test.js
new file mode 100644
index 0000000..d39ab47
--- /dev/null
+++ b/test/js/setcallhandler-test.js
@@ -0,0 +1,19 @@
+/*********************************************************************
+ * NAN - Native Abstractions for Node.js
+ *
+ * Copyright (c) 2015 NAN contributors
+ *
+ * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
+ ********************************************************************/
+
+const test = require('tap').test
+ , testRoot = require('path').resolve(__dirname, '..')
+ , bindings = require('bindings')({ module_root: testRoot, bindings: 'setcallhandler' });
+
+test('setcallhandler', function (t) {
+ t.plan(4);
+ t.type(bindings.a, 'function');
+ t.type(bindings.b, 'function');
+ t.equal(bindings.a()(), 12);
+ t.equal(bindings.b()(), 15);
+});
diff --git a/test/js/typedarrays-test.js b/test/js/typedarrays-test.js
new file mode 100644
index 0000000..0b02b5c
--- /dev/null
+++ b/test/js/typedarrays-test.js
@@ -0,0 +1,74 @@
+const test = require('tap').test
+ , testRoot = require('path').resolve(__dirname, '..')
+ , bindings = require('bindings')({ module_root: testRoot, bindings: 'typedarrays' });
+
+test('typedarrays - simple cases', function (t) {
+ if (typeof Uint8Array !== 'function') {
+
+ t.pass('typedarrays not supported');
+ t.end();
+
+ } else {
+
+ var zeros = new Uint8Array(5)
+ t.same(bindings.ReadU8(zeros), [0,0,0,0,0])
+
+ var y = zeros[0]
+ t.equals(y, 0)
+ t.same(bindings.ReadU8(zeros), [0,0,0,0,0])
+
+ var u8array = new Uint8Array([1, 255, 3]);
+ t.same(bindings.ReadU8(u8array), [1, 255, 3]);
+ t.same(bindings.ReadU8(u8array.subarray(1)), [255, 3]);
+ t.same(bindings.ReadU8(u8array.subarray(0, 2)), [1, 255]);
+ t.same(bindings.ReadU8(u8array.subarray(1, 2)), [255]);
+
+ t.same(bindings.ReadU8(new Uint8Array(u8array)), [1, 255, 3]);
+ t.same(bindings.ReadU8(new Uint8Array(u8array.subarray(1))), [255, 3]);
+ t.same(bindings.ReadU8(new Uint8Array(u8array.subarray(0, 2))), [1, 255]);
+ t.same(bindings.ReadU8(new Uint8Array(u8array.subarray(1, 2))), [255]);
+
+ t.same(bindings.ReadU8((new Uint8Array(u8array.buffer)).subarray(1)), [255, 3]);
+
+
+ var i32array = new Int32Array([0, 1, -1, 1073741824, -1073741824]);
+ t.same(bindings.ReadI32(i32array), [0, 1, -1, 1073741824, -1073741824]);
+
+ var f32array = new Float32Array([1, -1, Infinity, -Infinity, 0, +0, -0]);
+ t.same(bindings.ReadFloat(f32array), [1, -1, Infinity, -Infinity, 0, +0, -0]);
+ t.same(bindings.ReadFloat(f32array.subarray(1)), [-1, Infinity, -Infinity, 0, +0, -0]);
+ t.same(bindings.ReadFloat(f32array.subarray(0,4)), [1, -1, Infinity, -Infinity]);
+ t.same(bindings.ReadFloat(f32array.subarray(1,3)), [-1, Infinity]);
+
+ t.end();
+ }
+});
+
+test('typedarrays - bad arguments', function (t) {
+ if (typeof Uint8Array !== 'function') {
+
+ t.pass('typedarrays not supported');
+ t.end();
+
+ } else {
+
+ t.same(bindings.ReadU8(0), []);
+ t.same(bindings.ReadU8(1), []);
+ t.same(bindings.ReadU8(null), []);
+ t.same(bindings.ReadU8(), []);
+ t.same(bindings.ReadU8('foobar'), []);
+ t.same(bindings.ReadU8([]), []);
+ t.same(bindings.ReadU8([1,2]), []);
+ t.same(bindings.ReadU8({}), []);
+ t.same(bindings.ReadU8(Uint8Array), []);
+ t.same(bindings.ReadU8(new Float32Array(0)), []);
+
+ t.same(bindings.ReadU8({
+ byteLength: 10000000,
+ byteOffset: 100000,
+ buffer: null
+ }), [])
+
+ t.end();
+ }
+});
diff --git a/test/js/wrappedobjectfactory-test.js b/test/js/wrappedobjectfactory-test.js
new file mode 100644
index 0000000..6b8eafa
--- /dev/null
+++ b/test/js/wrappedobjectfactory-test.js
@@ -0,0 +1,24 @@
+/*********************************************************************
+ * NAN - Native Abstractions for Node.js
+ *
+ * Copyright (c) 2015 NAN contributors
+ *
+ * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
+ ********************************************************************/
+
+const test = require('tap').test
+ , testRoot = require('path').resolve(__dirname, '..')
+ , bindings = require('bindings')({ module_root: testRoot, bindings: 'wrappedobjectfactory' });
+
+test('wrappedobjectfactory', function (t) {
+ t.plan(4);
+
+ var obj = bindings.newFactoryObjectInstance(123);
+
+ t.type(obj.getValue, 'function');
+ t.type(obj.getValue(), 'number');
+ t.equal(obj.getValue(), 123);
+
+ var inner = obj.newInnerObject(456);
+ t.equal(inner.getValue(), 456);
+});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-nan.git
More information about the Pkg-javascript-commits
mailing list