probando parpadeo led

parent 7364629a
Showing with 4894 additions and 0 deletions
1.1.8 - Oct 15 2017
===================
* update dependencies (epoll v1.0.0)
1.1.7 - Aug 26 2017
===================
* only check permissions for edge file if edge specified [#77](https://github.com/fivdi/onoff/issues/77)
1.1.5 - Jul 30 2017
===================
* wait until unprivileged file access allowed
1.1.4 - Jul 15 2017
===================
* improve examples
1.1.3 - Jun 18 2017
===================
* upgrade to epoll v0.1.22
* document related packages
1.1.2 - Feb 12 2017
===================
* documentation improved
* upgrade to epoll v0.1.21
1.1.1 - Jun 05 2016
===================
* avoid exceptions when cape_universal is enabled on the bbb [#50](https://github.com/fivdi/onoff/issues/50)
1.1.0 - May 04 2016
===================
* activeLow option
* documentation improved
1.0.4 - Jan 29 2016
===================
* documentation improved
* epoll v0.1.17
1.0.3 - Oct 10 2015
===================
* documentation improved
* epoll v0.1.16
1.0.2 - Feb 18 2015
===================
* documentation improved
1.0.1 - Feb 15 2015
===================
* refactored tests to avoid relying in interrupt generating outputs as linux 3.13 and above no longer supports them
* new wiring for tests and examples
* pullup and pulldown resistor configuration documented
1.0.0 - Jan 10 2015
===================
* use strict mode
* jslint improvements
* updated dependencies: epoll 0.1.4 -> 0.1.10
* new wiring for tests on pi
* GPIO access without superuser privileges on Raspbian
0.3.2 - Apr 18 2014
===================
* Documented BeagleBone Ångström prerequisites
* Updated dependencies: epoll 0.1.2 -> 0.1.4
0.3.1 - Mar 22 2014
===================
* Added setDirection functionality [#19](https://github.com/fivdi/onoff/pull/19)
* Added setEdge functionality
* Updated dependencies: epoll 0.1.0 -> 0.1.2
0.3.0 - Nov 18 2013
===================
* Updated dependencies: epoll 0.0.8 -> 0.1.0
* Removed persistentWatch option
0.2.3 - Oct 14 2013
===================
* Use epoll 0.0.8
* onoff now plays well with the quick2wire gpio-admin and the WiringPi gpio utilities on the Pi [#14](https://github.com/fivdi/onoff/issues/14)
* Documentation improved
* New test to monitor interrupt performance
* New light switch example
0.2.2 - Oct 05 2013
===================
* Use epoll 0.0.7
* Removed timeout hack in many-interrupts test
0.2.1 - Sep 25 2013
===================
* Use epoll 0.0.3
* Improved five-inputs test
0.2.0 - Sep 22 2013
===================
* Use epoll module for interrupt detection [#15](https://github.com/fivdi/onoff/issues/15)
* 0.11.4+ compatability [#11](https://github.com/fivdi/onoff/issues/10)
* One thread for watching all GPIOs rather than one thread per GPIO [#5](https://github.com/fivdi/onoff/issues/5)
* Unwatch API added [#4](https://github.com/fivdi/onoff/issues/4)
0.1.7 - Sep 17 2013
===================
* Remove OS limitations for installing [#12](https://github.com/fivdi/onoff/issues/12)
0.1.6 - July 15 2013
===================
* Fixed typos
* Documented how to watch five or more inputs
0.1.5 - May 26 2013
===================
* Added test with five inputs
0.1.0 - Nov 11 2012
===================
* Added Gpio objects
* Removed functions, use Gpio objects instead
* Performance improvements
* Synchronous or asynchronous access to a GPIOs value
* Allow applications to handle superuser issues
0.0.1 - Oct 28 2012
===================
* Initial release
(The MIT License)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"use strict";
var Gpio = require('../onoff').Gpio, // Constructor function for Gpio objects.
led = new Gpio(17, 'out'); // Export GPIO #17 as an output.
// Toggle the state of the LED on GPIO #17 every 200ms 'count' times.
// Here asynchronous methods are used. Synchronous methods are also available.
(function blink(count) {
if (count <= 0) {
return led.unexport();
}
led.read(function (err, value) { // Asynchronous read.
if (err) {
throw err;
}
led.write(value ^ 1, function (err) { // Asynchronous write.
if (err) {
throw err;
}
});
});
setTimeout(function () {
blink(count - 1);
}, 200);
}(25));
"use strict";
var Gpio = require('../onoff').Gpio, // Constructor function for Gpio objects.
led = new Gpio(17, 'out'), // Export GPIO #17 as an output.
iv;
// Toggle the state of the LED on GPIO #17 every 200ms.
// Here synchronous methods are used. Asynchronous methods are also available.
iv = setInterval(function () {
led.writeSync(led.readSync() ^ 1); // 1 = on, 0 = off :)
}, 200);
// Stop blinking the LED and turn it off after 5 seconds.
setTimeout(function () {
clearInterval(iv); // Stop blinking
led.writeSync(0); // Turn LED off.
led.unexport(); // Unexport GPIO and free resources
}, 5000);
"use strict";
var Gpio = require('../onoff').Gpio,
led = new Gpio(17, 'out'),
button = new Gpio(4, 'in', 'both');
button.watch(function (err, value) {
if (err) {
throw err;
}
led.writeSync(value);
});
process.on('SIGINT', function () {
led.unexport();
button.unexport();
});
/dts-v1/;
/plugin/;
/ {
compatible = "brcm,bcm2708";
fragment@0 {
target = <&gpio>;
__overlay__ {
pinctrl-names = "default";
pinctrl-0 = <&my_pins>;
my_pins: my_pins {
brcm,pins = <7 8 9>; /* gpio no. */
brcm,function = <0 0 0>; /* 0:in, 1:out */
brcm,pull = <1 1 2>; /* 2:up 1:down 0:none */
};
};
};
};
#!/bin/sh
node blink-led
node blink-led-async
node wait-for-interrupt
node light-switch
"use strict";
var Gpio = require('../onoff').Gpio, // Constructor function for Gpio objects.
button = new Gpio(4, 'in', 'both'); // Export GPIO #4 as an interrupt
// generating input.
console.log('Please press the button on GPIO #4...');
// The callback passed to watch will be called when the button on GPIO #4 is
// pressed.
button.watch(function (err, value) {
if (err) {
throw err;
}
console.log('Button pressed!, its value was ' + value);
button.unexport(); // Unexport GPIO and free resources
});
1.0.1 - Nov 04 2017
===================
* suppress deprecated-declaration warnings
* document node 9 support
1.0.0 - Oct 15 2017
===================
* update dependencies (bindings v1.3.0, nan v2.7.0)
* document supported node versions
0.1.22 - Jun 18 2017
====================
* nan 2.6.2
0.1.21 - Feb 12 2017
====================
* require node v0.10 or higher
* nan 2.5.1
0.1.20 - Jul 22 2016
====================
* document dependency on Linux
* nan 2.4.0
0.1.19 - May 03 2016
====================
* add accessor for closed property to instance template
0.1.18 - Apr 27 2016
====================
* upgrade to NAN v2.3.2 for Node.js v6.0.0 compatability
0.1.17 - Jan 29 2016
====================
* nan 2.2.0
* documentation
0.1.16 - Oct 10 2015
====================
* documentation
* nan 2.1.0
0.1.15 - Aug 24 2015
====================
* fix null passed to callback
0.1.14 - Aug 24 2015
====================
* nan2 migration
0.1.13 - May 07 2015
====================
* io.js v2.0.0+ compatibility [#13](https://github.com/fivdi/epoll/issues/13)
0.1.12 - Feb 10 2015
====================
* nan 1.6.2
* refactored interrupts-per-second example [#11](https://github.com/fivdi/epoll/issues/11)
0.1.11 - Jan 17 2015
====================
* support io.js [#10](https://github.com/fivdi/epoll/issues/10)
0.1.10 - Nov 02 2014
====================
* nan 1.4.0
0.1.9 - Aug 09 2014
===================
* nan 1.3.0
0.1.8 - Jul 12 2014
===================
* nan 1.2.0
* bindings 1.2.1
0.1.7 - May 29 2014
===================
* Fixed date in History.md
0.1.6 - May 29 2014
===================
* Replace NanSymbol with NanNew<v8:String> [#9](https://github.com/fivdi/epoll/issues/9)
0.1.5 - May 04 2014
===================
* nan 1.0.0 alignment [#8](https://github.com/fivdi/epoll/issues/8)
0.1.4 - Apr 18 2014
===================
* Documented BeagleBone Ångström prerequisites
* Use bindings for laoding
0.1.3 - Mar 23 2014
===================
* 0.11.5+ compatibility [#7](https://github.com/fivdi/epoll/issues/7)
* Updated dependencies: nan 0.6.2 -> 0.8.0
0.1.2 - Nov 21 2013
===================
* 0.11.9+ compatibility [#6](https://github.com/fivdi/epoll/issues/6)
* Updated dependencies: nan 0.5.2 -> 0.6.0
0.1.1 - Nov 19 2013
===================
* A hopefully successfull attempt to fix an npm install issue
0.1.0 - Nov 18 2013
===================
* Updated dependencies: nan 0.4.1 -> 0.5.2
* removed using namespace v8 (bad practice)
0.0.8 - Oct 14 2013
===================
* Epoll thread code improved (#4)
* EINTR handling (#3)
0.0.7 - Oct 05 2013
===================
* closed property added (#1)
* Segfault issue fixed (#5)
* add and modify now accept Epoll.EPOLLET as an event (#2)
0.0.6 - Oct 01 2013
===================
* Example for watching outputs added
* Tests improved
0.0.5 - Sep 25 2013
===================
* Link removed from readme
0.0.4 - Sep 25 2013
===================
* Url in readme fixed so that it displays correctly at npmjs.org, hopefully
0.0.3 - Sep 25 2013
===================
* Content added to readme
* Examples for the BeagleBone and RaspberryPi
* Minor bug fixes
* Tests improved
0.0.2 - Sep 22 2013
===================
* Tests extended and improved
* Allow installation on non-Linux systems but provide no functionality (needed for firmata-pi tests)
0.0.1 - Sep 22 2013
===================
* Initial release
The MIT License (MIT)
Copyright (c) 2013 fivdi
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
## epoll
A low-level **Node.js** binding for the Linux epoll API for monitoring multiple
file descriptors to see if I/O is possible on any of them.
This module was initially written to detect EPOLLPRI events indicating that
urgent data is available for reading. EPOLLPRI events are triggered by
interrupt generating [GPIO](https://www.kernel.org/doc/Documentation/gpio/)
pins. The epoll module is used by [onoff](https://github.com/fivdi/onoff)
to detect such interrupts.
epoll supports Node.js versions 0.10, 0.12, 4, 5, 6, 7, 8 and 9.
## Installation
Note that epoll can only be installed successfully on Linux systems. Attempting
to install epoll on other systems will result in compile errors.
$ npm install epoll
If you're using Node.js v4 or higher and seeing lots of compile errors when
installing epoll, it's very likely that gcc/g++ 4.8 or higher are not
installed. See
[Node.js v4 and native addons](https://github.com/fivdi/onoff/wiki/Node.js-v4-and-native-addons)
for details.
If you're using Node.js v0.10.29 on the Raspberry Pi and seeing a compile
error saying that `‘REPLACE_INVALID_UTF8’ is not a member of ‘v8::String’`
see [Node.js v0.10.29 and native addons on the Raspberry Pi](https://github.com/fivdi/onoff/wiki/Node.js-v0.10.29-and-native-addons-on-the-Raspberry-Pi).
If you're using Node.js v0.10.29 on the BeagleBone Black and seeing a compile
error saying that `‘REPLACE_INVALID_UTF8’ is not a member of ‘v8::String’`
see [Node.js v0.10.29 and native addons on the BeagleBone Black](https://github.com/fivdi/onoff/wiki/Node.js-v0.10.29-and-native-addons-on-the-BeagleBone-Black).
## API
* Epoll(callback) - Constructor. The callback is called when epoll events
occur and it gets three arguments (err, fd, events).
* add(fd, events) - Register file descriptor fd for the event types specified
by events.
* remove(fd) - Deregister file descriptor fd.
* modify(fd, events) - Change the event types associated with file descriptor
fd to those specified by events.
* close() - Deregisters all file descriptors and free resources.
Event Types
* Epoll.EPOLLIN
* Epoll.EPOLLOUT
* Epoll.EPOLLRDHUP
* Epoll.EPOLLPRI
* Epoll.EPOLLERR
* Epoll.EPOLLHUP
* Epoll.EPOLLET
* Epoll.EPOLLONESHOT
Event types can be combined with | when calling add or modify. For example,
Epoll.EPOLLPRI | Epoll.EPOLLONESHOT could be passed to add to detect a single
GPIO interrupt.
## Example - Watching Buttons
The following example shows how epoll can be used to detect interrupts from a
momentary push-button connected to GPIO #4 (pin P1-7) on the Raspberry Pi.
The source code is available in the [example directory]
(https://github.com/fivdi/epoll/tree/master/example/watch-button) and can
easily be modified for using a different GPIO on the Pi or a different platform
such as the BeagleBone.
The first step is to export GPIO #4 as an interrupt generating input using
the export bash script from the examples directory.
$ [sudo] ./export
export:
```bash
#!/bin/sh
echo 4 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio4/direction
echo both > /sys/class/gpio/gpio4/edge
```
Then run watch-button to be notified every time the button is pressed and
released. If there is no hardware debounce circuit for the push-button, contact
bounce issues are very likely to be visible on the console output.
watch-button terminates automatically after 30 seconds.
$ [sudo] node watch-button
watch-button:
```js
var Epoll = require('../../build/Release/epoll').Epoll,
fs = require('fs'),
valuefd = fs.openSync('/sys/class/gpio/gpio4/value', 'r'),
buffer = new Buffer(1);
// Create a new Epoll. The callback is the interrupt handler.
var poller = new Epoll(function (err, fd, events) {
// Read GPIO value file. Reading also clears the interrupt.
fs.readSync(fd, buffer, 0, 1, 0);
console.log(buffer.toString() === '1' ? 'released' : 'pressed');
});
// Read the GPIO value file before watching to
// prevent an initial unauthentic interrupt.
fs.readSync(valuefd, buffer, 0, 1, 0);
// Start watching for interrupts.
poller.add(valuefd, Epoll.EPOLLPRI);
// Stop watching after 30 seconds.
setTimeout(function () {
poller.remove(valuefd).close();
}, 30000);
```
When watch-button has terminated, GPIO #4 can be unexported using the
unexport bash script.
$ [sudo] ./unexport
unexport:
```bash
#!/bin/sh
echo 4 > /sys/class/gpio/unexport
```
## Example - Interrupts Per Second
The following example shows how epoll can be used to determine the number of
hardware interrupts that can be handled per second on the Raspberry Pi.
The source code is available in the [example directory]
(https://github.com/fivdi/epoll/tree/master/example/interrupts-per-second) and
can easily be modified to use different GPIOs on the Raspberry Pi or a
different platform such as the BeagleBone.
In this example, GPIO #7 is wired to one end of a 1kΩ current limiting
resistor and GPIO #8 is wired to the other end of the resistor. GPIO #7 is an
input and GPIO #8 is an output.
The first step is to export GPIOs #7 and #8 using the export bash script from
the examples directory.
$ [sudo] ./export
export:
```bash
#!/bin/sh
echo 7 > /sys/class/gpio/export
echo 8 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio7/direction
echo both > /sys/class/gpio/gpio7/edge
echo out > /sys/class/gpio/gpio8/direction
```
Then run interrupts-per-second. interrupts-per-second toggles the state of the
output every time it detects an interrupt on the input. Each toggle will
trigger the next interrupt. After five seconds, interrupts-per-second prints
the number of interrupts it detected per second.
$ [sudo] node interrupts-per-second
interrupts-per-second:
```js
var Epoll = require('../../build/Release/epoll').Epoll,
fs = require('fs'),
inputfd = fs.openSync('/sys/class/gpio/gpio7/value', 'r+'),
outputfd = fs.openSync('/sys/class/gpio/gpio8/value', 'r+'),
value = new Buffer(1), // The three Buffers here are global
zero = new Buffer('0'), // to improve performance.
one = new Buffer('1'),
count = 0,
time;
// Create a new Epoll. The callback is the interrupt handler.
var poller = new Epoll(function (err, fd, events) {
var nextValue;
count++;
// Read GPIO value file. Reading also clears the interrupt.
fs.readSync(inputfd, value, 0, 1, 0);
// Toggle GPIO value. This will eventually result
// in the next interrupt being triggered.
nextValue = value[0] === zero[0] ? one : zero;
fs.writeSync(outputfd, nextValue, 0, nextValue.length, 0);
});
time = process.hrtime(); // Get start time.
// Start watching for interrupts. This will trigger the first interrupt
// as the value file already has data waiting for a read.
poller.add(inputfd, Epoll.EPOLLPRI);
// Print interrupt rate to console after 5 seconds.
setTimeout(function () {
var rate;
time = process.hrtime(time); // Get run time.
rate = Math.floor(count / (time[0] + time[1] / 1E9));
console.log(rate + ' interrupts per second');
// Stop watching.
poller.remove(inputfd).close();
}, 5000);
```
When interrupts-per-second has terminated, GPIOs #7 and #8 can be unexported
using the unexport bash script.
$ [sudo] ./unexport
unexport:
```bash
#!/bin/sh
echo 7 > /sys/class/gpio/unexport
echo 8 > /sys/class/gpio/unexport
```
Here are some results from the "Interrupts Per Second" example.
**BeagleBone, 720MHz, Ångström v2012.12, Kernel 3.8.13, epoll v0.0.6:**
Node.js | Interrupts / Second
:---: | ---:
v0.11.7 | 7152
v0.10.20 | 5861
v0.8.22 | 6098
**BeagleBone Black, 1GHz, Debian, Kernel 3.8.13, epoll v0.1.11:**
Node.js | Interrupts / Second
:---: | ---:
v0.10.25 | 9133
**Raspberry Pi, 700Mhz, Raspbian, Kernel 3.2.27+, epoll v0.0.6:**
Node.js | Interrupts / Second
:---: | ---:
v0.11.07 | 4071
v0.10.16 | 3530
v0.8.14 | 3591
**Raspberry Pi 2, 900Mhz, Raspbian, Kernel 3.18.5-v7+, epoll v0.1.11:**
Node.js | Interrupts / Second
:---: | ---:
v0.10.36 | 10438
{
"targets": [{
"target_name": "epoll",
"conditions": [[
"OS == \"linux\"", {
"cflags": [
"-Wno-unused-local-typedefs",
"-Wno-deprecated-declarations"
]
}]
],
"include_dirs" : [
"<!(node -e \"require('nan')\")"
],
"sources": [
"./src/epoll.cc"
]
}]
}
cmd_Release/epoll.node := ln -f "Release/obj.target/epoll.node" "Release/epoll.node" 2>/dev/null || (rm -rf "Release/epoll.node" && cp -af "Release/obj.target/epoll.node" "Release/epoll.node")
cmd_Release/obj.target/epoll.node := g++ -shared -pthread -rdynamic -Wl,-soname=epoll.node -o Release/obj.target/epoll.node -Wl,--start-group Release/obj.target/epoll/src/epoll.o -Wl,--end-group
cmd_Release/obj.target/epoll/src/epoll.o := g++ '-DNODE_GYP_MODULE_NAME=epoll' '-DUSING_UV_SHARED=1' '-DUSING_V8_SHARED=1' '-DV8_DEPRECATION_WARNINGS=1' '-D_LARGEFILE_SOURCE' '-D_FILE_OFFSET_BITS=64' '-DBUILDING_NODE_EXTENSION' -I/usr/include/nodejs/include/node -I/usr/include/nodejs/src -I/usr/include/nodejs/deps/uv/include -I/usr/include/nodejs/deps/v8/include -I../node_modules/nan -fPIC -pthread -Wall -Wextra -Wno-unused-parameter -Wno-unused-local-typedefs -Wno-deprecated-declarations -O3 -ffunction-sections -fdata-sections -fno-omit-frame-pointer -fno-rtti -fno-exceptions -std=gnu++0x -MMD -MF ./Release/.deps/Release/obj.target/epoll/src/epoll.o.d.raw -c -o Release/obj.target/epoll/src/epoll.o ../src/epoll.cc
Release/obj.target/epoll/src/epoll.o: ../src/epoll.cc \
/usr/include/nodejs/deps/uv/include/uv.h \
/usr/include/nodejs/deps/uv/include/uv-errno.h \
/usr/include/nodejs/deps/uv/include/uv-version.h \
/usr/include/nodejs/deps/uv/include/uv-unix.h \
/usr/include/nodejs/deps/uv/include/uv-threadpool.h \
/usr/include/nodejs/deps/uv/include/uv-linux.h \
/usr/include/nodejs/deps/v8/include/v8.h \
/usr/include/nodejs/deps/v8/include/v8-version.h \
/usr/include/nodejs/deps/v8/include/v8config.h \
/usr/include/nodejs/src/node.h /usr/include/nodejs/src/node_version.h \
/usr/include/nodejs/src/node_object_wrap.h \
/usr/include/nodejs/src/node_version.h ../node_modules/nan/nan.h \
/usr/include/nodejs/src/node_buffer.h /usr/include/nodejs/src/node.h \
../node_modules/nan/nan_callbacks.h \
../node_modules/nan/nan_callbacks_12_inl.h \
../node_modules/nan/nan_maybe_43_inl.h \
../node_modules/nan/nan_converters.h \
../node_modules/nan/nan_converters_43_inl.h \
../node_modules/nan/nan_new.h \
../node_modules/nan/nan_implementation_12_inl.h \
../node_modules/nan/nan_persistent_12_inl.h \
../node_modules/nan/nan_weak.h ../node_modules/nan/nan_object_wrap.h \
../node_modules/nan/nan_private.h \
../node_modules/nan/nan_typedarray_contents.h \
../node_modules/nan/nan_json.h ../src/epoll.h
../src/epoll.cc:
/usr/include/nodejs/deps/uv/include/uv.h:
/usr/include/nodejs/deps/uv/include/uv-errno.h:
/usr/include/nodejs/deps/uv/include/uv-version.h:
/usr/include/nodejs/deps/uv/include/uv-unix.h:
/usr/include/nodejs/deps/uv/include/uv-threadpool.h:
/usr/include/nodejs/deps/uv/include/uv-linux.h:
/usr/include/nodejs/deps/v8/include/v8.h:
/usr/include/nodejs/deps/v8/include/v8-version.h:
/usr/include/nodejs/deps/v8/include/v8config.h:
/usr/include/nodejs/src/node.h:
/usr/include/nodejs/src/node_version.h:
/usr/include/nodejs/src/node_object_wrap.h:
/usr/include/nodejs/src/node_version.h:
../node_modules/nan/nan.h:
/usr/include/nodejs/src/node_buffer.h:
/usr/include/nodejs/src/node.h:
../node_modules/nan/nan_callbacks.h:
../node_modules/nan/nan_callbacks_12_inl.h:
../node_modules/nan/nan_maybe_43_inl.h:
../node_modules/nan/nan_converters.h:
../node_modules/nan/nan_converters_43_inl.h:
../node_modules/nan/nan_new.h:
../node_modules/nan/nan_implementation_12_inl.h:
../node_modules/nan/nan_persistent_12_inl.h:
../node_modules/nan/nan_weak.h:
../node_modules/nan/nan_object_wrap.h:
../node_modules/nan/nan_private.h:
../node_modules/nan/nan_typedarray_contents.h:
../node_modules/nan/nan_json.h:
../src/epoll.h:
# This file is generated by gyp; do not edit.
export builddir_name ?= ./build/.
.PHONY: all
all:
$(MAKE) epoll
# Do not edit. File was generated by node-gyp's "configure" step
{
"target_defaults": {
"cflags": [],
"default_configuration": "Release",
"defines": [],
"include_dirs": [],
"libraries": []
},
"variables": {
"arm_float_abi": "hard",
"arm_fpu": "vfp",
"arm_thumb": 0,
"arm_version": "6",
"asan": 0,
"coverage": "false",
"force_dynamic_crt": 0,
"host_arch": "arm",
"icu_gyp_path": "tools/icu/icu-system.gyp",
"icu_small": "false",
"node_byteorder": "little",
"node_enable_d8": "false",
"node_enable_v8_vtunejit": "false",
"node_install_npm": "false",
"node_module_version": 46,
"node_prefix": "/usr",
"node_release_urlbase": "",
"node_shared": "false",
"node_shared_http_parser": "false",
"node_shared_libuv": "true",
"node_shared_openssl": "true",
"node_shared_zlib": "true",
"node_tag": "",
"node_use_bundled_v8": "true",
"node_use_dtrace": "false",
"node_use_etw": "false",
"node_use_lttng": "false",
"node_use_openssl": "true",
"node_use_perfctr": "false",
"node_use_v8_platform": "true",
"openssl_fips": "",
"openssl_no_asm": 0,
"shlib_suffix": "so.46",
"target_arch": "arm",
"uv_parent_path": "/deps/uv/",
"uv_use_dtrace": "false",
"v8_enable_gdbjit": 0,
"v8_enable_i18n_support": 1,
"v8_no_strict_aliasing": 1,
"v8_optimized_debug": 0,
"v8_random_seed": 0,
"v8_use_snapshot": "true",
"want_separate_host_toolset": 0,
"nodedir": "/usr/include/nodejs",
"copy_dev_lib": "true",
"standalone_static_library": 1,
"cache_lock_stale": "60000",
"sign_git_tag": "",
"user_agent": "npm/1.4.21 node/v4.8.2 linux arm",
"always_auth": "",
"bin_links": "true",
"key": "",
"description": "true",
"fetch_retries": "2",
"heading": "npm",
"user": "",
"force": "",
"cache_min": "10",
"init_license": "ISC",
"editor": "vi",
"rollback": "true",
"cache_max": "Infinity",
"userconfig": "/home/pi/.npmrc",
"engine_strict": "",
"init_author_name": "",
"init_author_url": "",
"tmp": "/tmp",
"depth": "Infinity",
"save_dev": "",
"usage": "",
"https_proxy": "",
"onload_script": "",
"rebuild_bundle": "true",
"save_bundle": "",
"shell": "/bin/bash",
"prefix": "/usr/local",
"registry": "https://registry.npmjs.org/",
"__DO_NOT_MODIFY_THIS_FILE___use__etc_npmrc_instead_": "true",
"browser": "",
"cache_lock_wait": "10000",
"save_optional": "",
"searchopts": "",
"versions": "",
"cache": "/home/pi/.npm",
"ignore_scripts": "",
"searchsort": "name",
"version": "",
"local_address": "",
"viewer": "man",
"color": "true",
"fetch_retry_mintimeout": "10000",
"umask": "18",
"fetch_retry_maxtimeout": "60000",
"message": "%s",
"ca": "",
"cert": "",
"global": "",
"link": "",
"save": "",
"unicode": "true",
"long": "",
"production": "",
"unsafe_perm": "true",
"node_version": "4.8.2",
"tag": "latest",
"git_tag_version": "true",
"shrinkwrap": "true",
"fetch_retry_factor": "10",
"npat": "",
"proprietary_attribs": "true",
"save_exact": "",
"strict_ssl": "true",
"username": "",
"globalconfig": "/etc/npmrc",
"dev": "",
"init_module": "/home/pi/.npm-init.js",
"parseable": "",
"globalignorefile": "/etc/npmignore",
"cache_lock_retries": "10",
"save_prefix": "^",
"group": "1000",
"init_author_email": "",
"searchexclude": "",
"git": "git",
"optional": "true",
"email": "",
"json": "",
"spin": "true"
}
}
# This file is generated by gyp; do not edit.
TOOLSET := target
TARGET := epoll
DEFS_Debug := \
'-DNODE_GYP_MODULE_NAME=epoll' \
'-DUSING_UV_SHARED=1' \
'-DUSING_V8_SHARED=1' \
'-DV8_DEPRECATION_WARNINGS=1' \
'-D_LARGEFILE_SOURCE' \
'-D_FILE_OFFSET_BITS=64' \
'-DBUILDING_NODE_EXTENSION' \
'-DDEBUG' \
'-D_DEBUG'
# Flags passed to all source files.
CFLAGS_Debug := \
-fPIC \
-pthread \
-Wall \
-Wextra \
-Wno-unused-parameter \
-Wno-unused-local-typedefs \
-Wno-deprecated-declarations \
-g \
-O0
# Flags passed to only C files.
CFLAGS_C_Debug :=
# Flags passed to only C++ files.
CFLAGS_CC_Debug := \
-fno-rtti \
-fno-exceptions \
-std=gnu++0x
INCS_Debug := \
-I/usr/include/nodejs/include/node \
-I/usr/include/nodejs/src \
-I/usr/include/nodejs/deps/uv/include \
-I/usr/include/nodejs/deps/v8/include \
-I$(srcdir)/node_modules/nan
DEFS_Release := \
'-DNODE_GYP_MODULE_NAME=epoll' \
'-DUSING_UV_SHARED=1' \
'-DUSING_V8_SHARED=1' \
'-DV8_DEPRECATION_WARNINGS=1' \
'-D_LARGEFILE_SOURCE' \
'-D_FILE_OFFSET_BITS=64' \
'-DBUILDING_NODE_EXTENSION'
# Flags passed to all source files.
CFLAGS_Release := \
-fPIC \
-pthread \
-Wall \
-Wextra \
-Wno-unused-parameter \
-Wno-unused-local-typedefs \
-Wno-deprecated-declarations \
-O3 \
-ffunction-sections \
-fdata-sections \
-fno-omit-frame-pointer
# Flags passed to only C files.
CFLAGS_C_Release :=
# Flags passed to only C++ files.
CFLAGS_CC_Release := \
-fno-rtti \
-fno-exceptions \
-std=gnu++0x
INCS_Release := \
-I/usr/include/nodejs/include/node \
-I/usr/include/nodejs/src \
-I/usr/include/nodejs/deps/uv/include \
-I/usr/include/nodejs/deps/v8/include \
-I$(srcdir)/node_modules/nan
OBJS := \
$(obj).target/$(TARGET)/src/epoll.o
# Add to the list of files we specially track dependencies for.
all_deps += $(OBJS)
# CFLAGS et al overrides must be target-local.
# See "Target-specific Variable Values" in the GNU Make manual.
$(OBJS): TOOLSET := $(TOOLSET)
$(OBJS): GYP_CFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_C_$(BUILDTYPE))
$(OBJS): GYP_CXXFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_CC_$(BUILDTYPE))
# Suffix rules, putting all outputs into $(obj).
$(obj).$(TOOLSET)/$(TARGET)/%.o: $(srcdir)/%.cc FORCE_DO_CMD
@$(call do_cmd,cxx,1)
# Try building from generated source, too.
$(obj).$(TOOLSET)/$(TARGET)/%.o: $(obj).$(TOOLSET)/%.cc FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/$(TARGET)/%.o: $(obj)/%.cc FORCE_DO_CMD
@$(call do_cmd,cxx,1)
# End of this set of suffix rules
### Rules for final target.
LDFLAGS_Debug := \
-pthread \
-rdynamic
LDFLAGS_Release := \
-pthread \
-rdynamic
LIBS :=
$(obj).target/epoll.node: GYP_LDFLAGS := $(LDFLAGS_$(BUILDTYPE))
$(obj).target/epoll.node: LIBS := $(LIBS)
$(obj).target/epoll.node: TOOLSET := $(TOOLSET)
$(obj).target/epoll.node: $(OBJS) FORCE_DO_CMD
$(call do_cmd,solink_module)
all_deps += $(obj).target/epoll.node
# Add target alias
.PHONY: epoll
epoll: $(builddir)/epoll.node
# Copy this to the executable output path.
$(builddir)/epoll.node: TOOLSET := $(TOOLSET)
$(builddir)/epoll.node: $(obj).target/epoll.node FORCE_DO_CMD
$(call do_cmd,copy)
all_deps += $(builddir)/epoll.node
# Short alias for building this executable.
.PHONY: epoll.node
epoll.node: $(obj).target/epoll.node $(builddir)/epoll.node
# Add executable to "all" target.
.PHONY: all
all: $(builddir)/epoll.node
module.exports = require('bindings')('epoll.node');
#!/bin/sh
echo 7 > /sys/class/gpio/export
echo 8 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio7/direction
echo both > /sys/class/gpio/gpio7/edge
echo out > /sys/class/gpio/gpio8/direction
'use strict';
var Epoll = require('../../build/Release/epoll').Epoll,
fs = require('fs'),
inputfd = fs.openSync('/sys/class/gpio/gpio7/value', 'r+'),
outputfd = fs.openSync('/sys/class/gpio/gpio8/value', 'r+'),
value = new Buffer(1), // The three Buffers here are global
zero = new Buffer('0'), // to improve performance.
one = new Buffer('1'),
count = 0,
time;
// Create a new Epoll. The callback is the interrupt handler.
var poller = new Epoll(function (err, fd, events) {
var nextValue;
count += 1;
// Read GPIO value file. Reading also clears the interrupt.
fs.readSync(inputfd, value, 0, 1, 0);
// Toggle GPIO value. This will eventually result
// in the next interrupt being triggered.
nextValue = value[0] === zero[0] ? one : zero;
fs.writeSync(outputfd, nextValue, 0, nextValue.length, 0);
});
time = process.hrtime(); // Get start time.
// Start watching for interrupts. This will trigger the first interrupt
// as the value file already has data waiting for a read.
poller.add(inputfd, Epoll.EPOLLPRI);
// Print interrupt rate to console after 5 seconds.
setTimeout(function () {
var rate;
time = process.hrtime(time); // Get run time.
rate = Math.floor(count / (time[0] + time[1] / 1E9));
console.log(rate + ' interrupts per second');
// Stop watching.
poller.remove(inputfd).close();
}, 5000);
#!/bin/sh
echo 7 > /sys/class/gpio/unexport
echo 8 > /sys/class/gpio/unexport
#!/bin/sh
echo 4 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio4/direction
echo both > /sys/class/gpio/gpio4/edge
'use strict';
var Epoll = require('../../build/Release/epoll').Epoll,
fs = require('fs'),
valuefd = fs.openSync('/sys/class/gpio/gpio4/value', 'r'),
buffer = new Buffer(1);
// Create a new Epoll. The callback is the interrupt handler.
var poller = new Epoll(function (err, fd, events) {
// Read GPIO value file. Reading also clears the interrupt.
fs.readSync(fd, buffer, 0, 1, 0);
console.log(buffer.toString() === '1' ? 'pressed' : 'released');
});
// Read the GPIO value file before watching to
// prevent an initial unauthentic interrupt.
fs.readSync(valuefd, buffer, 0, 1, 0);
// Start watching for interrupts.
poller.add(valuefd, Epoll.EPOLLPRI);
// Stop watching after 30 seconds.
setTimeout(function () {
poller.remove(valuefd).close();
}, 30000);
node-bindings
=============
### Helper module for loading your native module's .node file
This is a helper module for authors of Node.js native addon modules.
It is basically the "swiss army knife" of `require()`ing your native module's
`.node` file.
Throughout the course of Node's native addon history, addons have ended up being
compiled in a variety of different places, depending on which build tool and which
version of node was used. To make matters worse, now the _gyp_ build tool can
produce either a _Release_ or _Debug_ build, each being built into different
locations.
This module checks _all_ the possible locations that a native addon would be built
at, and returns the first one that loads successfully.
Installation
------------
Install with `npm`:
``` bash
$ npm install bindings
```
Or add it to the `"dependencies"` section of your _package.json_ file.
Example
-------
`require()`ing the proper bindings file for the current node version, platform
and architecture is as simple as:
``` js
var bindings = require('bindings')('binding.node')
// Use your bindings defined in your C files
bindings.your_c_function()
```
Nice Error Output
-----------------
When the `.node` file could not be loaded, `node-bindings` throws an Error with
a nice error message telling you exactly what was tried. You can also check the
`err.tries` Array property.
```
Error: Could not load the bindings file. Tried:
→ /Users/nrajlich/ref/build/binding.node
→ /Users/nrajlich/ref/build/Debug/binding.node
→ /Users/nrajlich/ref/build/Release/binding.node
→ /Users/nrajlich/ref/out/Debug/binding.node
→ /Users/nrajlich/ref/Debug/binding.node
→ /Users/nrajlich/ref/out/Release/binding.node
→ /Users/nrajlich/ref/Release/binding.node
→ /Users/nrajlich/ref/build/default/binding.node
→ /Users/nrajlich/ref/compiled/0.8.2/darwin/x64/binding.node
at bindings (/Users/nrajlich/ref/node_modules/bindings/bindings.js:84:13)
at Object.<anonymous> (/Users/nrajlich/ref/lib/ref.js:5:47)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
...
```
The searching for the `.node` file will originate from the first directory in which has a `package.json` file is found.
License
-------
(The MIT License)
Copyright (c) 2012 Nathan Rajlich &lt;nathan@tootallnate.net&gt;
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/**
* Module dependencies.
*/
var fs = require('fs')
, path = require('path')
, join = path.join
, dirname = path.dirname
, exists = ((fs.accessSync && function (path) { try { fs.accessSync(path); } catch (e) { return false; } return true; })
|| fs.existsSync || path.existsSync)
, defaults = {
arrow: process.env.NODE_BINDINGS_ARROW || ' → '
, compiled: process.env.NODE_BINDINGS_COMPILED_DIR || 'compiled'
, platform: process.platform
, arch: process.arch
, version: process.versions.node
, bindings: 'bindings.node'
, try: [
// node-gyp's linked version in the "build" dir
[ 'module_root', 'build', 'bindings' ]
// node-waf and gyp_addon (a.k.a node-gyp)
, [ 'module_root', 'build', 'Debug', 'bindings' ]
, [ 'module_root', 'build', 'Release', 'bindings' ]
// Debug files, for development (legacy behavior, remove for node v0.9)
, [ 'module_root', 'out', 'Debug', 'bindings' ]
, [ 'module_root', 'Debug', 'bindings' ]
// Release files, but manually compiled (legacy behavior, remove for node v0.9)
, [ 'module_root', 'out', 'Release', 'bindings' ]
, [ 'module_root', 'Release', 'bindings' ]
// Legacy from node-waf, node <= 0.4.x
, [ 'module_root', 'build', 'default', 'bindings' ]
// Production "Release" buildtype binary (meh...)
, [ 'module_root', 'compiled', 'version', 'platform', 'arch', 'bindings' ]
]
}
/**
* The main `bindings()` function loads the compiled bindings for a given module.
* It uses V8's Error API to determine the parent filename that this function is
* being invoked from, which is then used to find the root directory.
*/
function bindings (opts) {
// Argument surgery
if (typeof opts == 'string') {
opts = { bindings: opts }
} else if (!opts) {
opts = {}
}
// maps `defaults` onto `opts` object
Object.keys(defaults).map(function(i) {
if (!(i in opts)) opts[i] = defaults[i];
});
// Get the module root
if (!opts.module_root) {
opts.module_root = exports.getRoot(exports.getFileName())
}
// Ensure the given bindings name ends with .node
if (path.extname(opts.bindings) != '.node') {
opts.bindings += '.node'
}
var tries = []
, i = 0
, l = opts.try.length
, n
, b
, err
for (; i<l; i++) {
n = join.apply(null, opts.try[i].map(function (p) {
return opts[p] || p
}))
tries.push(n)
try {
b = opts.path ? require.resolve(n) : require(n)
if (!opts.path) {
b.path = n
}
return b
} catch (e) {
if (!/not find/i.test(e.message)) {
throw e
}
}
}
err = new Error('Could not locate the bindings file. Tried:\n'
+ tries.map(function (a) { return opts.arrow + a }).join('\n'))
err.tries = tries
throw err
}
module.exports = exports = bindings
/**
* Gets the filename of the JavaScript file that invokes this function.
* Used to help find the root directory of a module.
* Optionally accepts an filename argument to skip when searching for the invoking filename
*/
exports.getFileName = function getFileName (calling_file) {
var origPST = Error.prepareStackTrace
, origSTL = Error.stackTraceLimit
, dummy = {}
, fileName
Error.stackTraceLimit = 10
Error.prepareStackTrace = function (e, st) {
for (var i=0, l=st.length; i<l; i++) {
fileName = st[i].getFileName()
if (fileName !== __filename) {
if (calling_file) {
if (fileName !== calling_file) {
return
}
} else {
return
}
}
}
}
// run the 'prepareStackTrace' function above
Error.captureStackTrace(dummy)
dummy.stack
// cleanup
Error.prepareStackTrace = origPST
Error.stackTraceLimit = origSTL
return fileName
}
/**
* Gets the root directory of a module, given an arbitrary filename
* somewhere in the module tree. The "root directory" is the directory
* containing the `package.json` file.
*
* In: /home/nate/node-native-module/lib/index.js
* Out: /home/nate/node-native-module
*/
exports.getRoot = function getRoot (file) {
var dir = dirname(file)
, prev
while (true) {
if (dir === '.') {
// Avoids an infinite loop in rare cases, like the REPL
dir = process.cwd()
}
if (exists(join(dir, 'package.json')) || exists(join(dir, 'node_modules'))) {
// Found the 'package.json' file or 'node_modules' dir; we're done
return dir
}
if (prev === dir) {
// Got to the top
throw new Error('Could not find module root given file: "' + file
+ '". Do you have a `package.json` file? ')
}
// Try the parent dir next
prev = dir
dir = join(dir, '..')
}
}
{
"name": "bindings",
"description": "Helper module for loading your native module's .node file",
"keywords": [
"native",
"addon",
"bindings",
"gyp",
"waf",
"c",
"c++"
],
"version": "1.3.0",
"author": {
"name": "Nathan Rajlich",
"email": "nathan@tootallnate.net",
"url": "http://tootallnate.net"
},
"repository": {
"type": "git",
"url": "git://github.com/TooTallNate/node-bindings.git"
},
"main": "./bindings.js",
"bugs": {
"url": "https://github.com/TooTallNate/node-bindings/issues"
},
"homepage": "https://github.com/TooTallNate/node-bindings",
"license": "MIT",
"gitHead": "7fd065ee85386ad3d074d2506e03abe8f9b1588b",
"_id": "bindings@1.3.0",
"_npmVersion": "5.0.3",
"_nodeVersion": "8.1.3",
"_npmUser": {
"name": "tootallnate",
"email": "nathan@tootallnate.net"
},
"dist": {
"integrity": "sha512-DpLh5EzMR2kzvX1KIlVC0VkC3iZtHKTgdtZ0a3pglBZdaQFjt5S9g9xd1lE+YvXyfd6mtCeRnrUfOLYiTMlNSw==",
"shasum": "b346f6ecf6a95f5a815c5839fc7cdb22502f1ed7",
"tarball": "https://registry.npmjs.org/bindings/-/bindings-1.3.0.tgz"
},
"maintainers": [
{
"email": "nathan@tootallnate.net",
"name": "tootallnate"
}
],
"_npmOperationalInternal": {
"host": "s3://npm-registry-packages",
"tmp": "tmp/bindings-1.3.0.tgz_1500923768710_0.3334669852629304"
},
"directories": {},
"_shasum": "b346f6ecf6a95f5a815c5839fc7cdb22502f1ed7",
"_from": "bindings@~1.3.0",
"_resolved": "https://registry.npmjs.org/bindings/-/bindings-1.3.0.tgz"
}
The MIT License (MIT)
=====================
Copyright (c) 2017 NAN contributors
-----------------------------------
*NAN contributors listed at <https://github.com/nodejs/nan#contributors>*
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
## Asynchronous work helpers
`Nan::AsyncWorker` and `Nan::AsyncProgressWorker` are helper classes that make working with asynchronous code easier.
- <a href="#api_nan_async_worker"><b><code>Nan::AsyncWorker</code></b></a>
- <a href="#api_nan_async_progress_worker"><b><code>Nan::AsyncProgressWorkerBase &amp; Nan::AsyncProgressWorker</code></b></a>
- <a href="#api_nan_async_queue_worker"><b><code>Nan::AsyncQueueWorker</code></b></a>
<a name="api_nan_async_worker"></a>
### Nan::AsyncWorker
`Nan::AsyncWorker` is an _abstract_ class that you can subclass to have much of the annoying asynchronous queuing and handling taken care of for you. It can even store arbitrary V8 objects for you and have them persist while the asynchronous work is in progress.
Definition:
```c++
class AsyncWorker {
public:
explicit AsyncWorker(Callback *callback_);
virtual ~AsyncWorker();
virtual void WorkComplete();
void SaveToPersistent(const char *key, const v8::Local<v8::Value> &value);
void SaveToPersistent(const v8::Local<v8::String> &key,
const v8::Local<v8::Value> &value);
void SaveToPersistent(uint32_t index,
const v8::Local<v8::Value> &value);
v8::Local<v8::Value> GetFromPersistent(const char *key) const;
v8::Local<v8::Value> GetFromPersistent(const v8::Local<v8::String> &key) const;
v8::Local<v8::Value> GetFromPersistent(uint32_t index) const;
virtual void Execute() = 0;
uv_work_t request;
virtual void Destroy();
protected:
Persistent<v8::Object> persistentHandle;
Callback *callback;
virtual void HandleOKCallback();
virtual void HandleErrorCallback();
void SetErrorMessage(const char *msg);
const char* ErrorMessage();
};
```
<a name="api_nan_async_progress_worker"></a>
### Nan::AsyncProgressWorkerBase &amp; Nan::AsyncProgressWorker
`Nan::AsyncProgressWorkerBase` is an _abstract_ class template that extends `Nan::AsyncWorker` and adds additional progress reporting callbacks that can be used during the asynchronous work execution to provide progress data back to JavaScript.
Previously the definiton of `Nan::AsyncProgressWorker` only allowed sending `const char` data. Now extending `Nan::AsyncProgressWorker` will yield an instance of the implicit `Nan::AsyncProgressWorkerBase` template with type `<char>` for compatibility.
Definition:
```c++
template<class T>
class AsyncProgressWorkerBase<T> : public AsyncWorker {
public:
explicit AsyncProgressWorker(Callback *callback_);
virtual ~AsyncProgressWorker();
void WorkProgress();
class ExecutionProgress {
public:
void Signal() const;
void Send(const T* data, size_t count) const;
};
virtual void Execute(const ExecutionProgress& progress) = 0;
virtual void HandleProgressCallback(const T *data, size_t count) = 0;
virtual void Destroy();
};
typedef AsyncProgressWorkerBase<T> AsyncProgressWorker;
```
<a name="api_nan_async_queue_worker"></a>
### Nan::AsyncQueueWorker
`Nan::AsyncQueueWorker` will run a `Nan::AsyncWorker` asynchronously via libuv. Both the `execute` and `after_work` steps are taken care of for you. Most of the logic for this is embedded in `Nan::AsyncWorker`.
Definition:
```c++
void AsyncQueueWorker(AsyncWorker *);
```
## Buffers
NAN's `node::Buffer` helpers exist as the API has changed across supported Node versions. Use these methods to ensure compatibility.
- <a href="#api_nan_new_buffer"><b><code>Nan::NewBuffer()</code></b></a>
- <a href="#api_nan_copy_buffer"><b><code>Nan::CopyBuffer()</code></b></a>
- <a href="#api_nan_free_callback"><b><code>Nan::FreeCallback()</code></b></a>
<a name="api_nan_new_buffer"></a>
### Nan::NewBuffer()
Allocate a new `node::Buffer` object with the specified size and optional data. Calls `node::Buffer::New()`.
Note that when creating a `Buffer` using `Nan::NewBuffer()` and an existing `char*`, it is assumed that the ownership of the pointer is being transferred to the new `Buffer` for management.
When a `node::Buffer` instance is garbage collected and a `FreeCallback` has not been specified, `data` will be disposed of via a call to `free()`.
You _must not_ free the memory space manually once you have created a `Buffer` in this way.
Signature:
```c++
Nan::MaybeLocal<v8::Object> Nan::NewBuffer(uint32_t size)
Nan::MaybeLocal<v8::Object> Nan::NewBuffer(char* data, uint32_t size)
Nan::MaybeLocal<v8::Object> Nan::NewBuffer(char *data,
size_t length,
Nan::FreeCallback callback,
void *hint)
```
<a name="api_nan_copy_buffer"></a>
### Nan::CopyBuffer()
Similar to [`Nan::NewBuffer()`](#api_nan_new_buffer) except that an implicit memcpy will occur within Node. Calls `node::Buffer::Copy()`.
Management of the `char*` is left to the user, you should manually free the memory space if necessary as the new `Buffer` will have its own copy.
Signature:
```c++
Nan::MaybeLocal<v8::Object> Nan::CopyBuffer(const char *data, uint32_t size)
```
<a name="api_nan_free_callback"></a>
### Nan::FreeCallback()
A free callback that can be provided to [`Nan::NewBuffer()`](#api_nan_new_buffer).
The supplied callback will be invoked when the `Buffer` undergoes garbage collection.
Signature:
```c++
typedef void (*FreeCallback)(char *data, void *hint);
```
## Nan::Callback
`Nan::Callback` makes it easier to use `v8::Function` handles as callbacks. A class that wraps a `v8::Function` handle, protecting it from garbage collection and making it particularly useful for storage and use across asynchronous execution.
- <a href="#api_nan_callback"><b><code>Nan::Callback</code></b></a>
<a name="api_nan_callback"></a>
### Nan::Callback
```c++
class Callback {
public:
Callback();
explicit Callback(const v8::Local<v8::Function> &fn);
~Callback();
bool operator==(const Callback &other) const;
bool operator!=(const Callback &other) const;
v8::Local<v8::Function> operator*() const;
v8::Local<v8::Value> operator()(v8::Local<v8::Object> target,
int argc = 0,
v8::Local<v8::Value> argv[] = 0) const;
v8::Local<v8::Value> operator()(int argc = 0,
v8::Local<v8::Value> argv[] = 0) const;
void SetFunction(const v8::Local<v8::Function> &fn);
v8::Local<v8::Function> GetFunction() const;
bool IsEmpty() const;
void Reset(const v8::Local<v8::Function> &fn);
void Reset();
v8::Local<v8::Value> Call(v8::Local<v8::Object> target,
int argc,
v8::Local<v8::Value> argv[]) const;
v8::Local<v8::Value> Call(int argc, v8::Local<v8::Value> argv[]) const;
};
```
Example usage:
```c++
v8::Local<v8::Function> function;
Nan::Callback callback(function);
callback.Call(0, 0);
```
## Converters
NAN contains functions that convert `v8::Value`s to other `v8::Value` types and native types. Since type conversion is not guaranteed to succeed, they return `Nan::Maybe` types. These converters can be used in place of `value->ToX()` and `value->XValue()` (where `X` is one of the types, e.g. `Boolean`) in a way that provides a consistent interface across V8 versions. Newer versions of V8 use the new `v8::Maybe` and `v8::MaybeLocal` types for these conversions, older versions don't have this functionality so it is provided by NAN.
- <a href="#api_nan_to"><b><code>Nan::To()</code></b></a>
<a name="api_nan_to"></a>
### Nan::To()
Converts a `v8::Local<v8::Value>` to a different subtype of `v8::Value` or to a native data type. Returns a `Nan::MaybeLocal<>` or a `Nan::Maybe<>` accordingly.
See [maybe_types.md](./maybe_types.md) for more information on `Nan::Maybe` types.
Signatures:
```c++
// V8 types
Nan::MaybeLocal<v8::Boolean> Nan::To<v8::Boolean>(v8::Local<v8::Value> val);
Nan::MaybeLocal<v8::Int32> Nan::To<v8::Int32>(v8::Local<v8::Value> val);
Nan::MaybeLocal<v8::Integer> Nan::To<v8::Integer>(v8::Local<v8::Value> val);
Nan::MaybeLocal<v8::Object> Nan::To<v8::Object>(v8::Local<v8::Value> val);
Nan::MaybeLocal<v8::Number> Nan::To<v8::Number>(v8::Local<v8::Value> val);
Nan::MaybeLocal<v8::String> Nan::To<v8::String>(v8::Local<v8::Value> val);
Nan::MaybeLocal<v8::Uint32> Nan::To<v8::Uint32>(v8::Local<v8::Value> val);
// Native types
Nan::Maybe<bool> Nan::To<bool>(v8::Local<v8::Value> val);
Nan::Maybe<double> Nan::To<double>(v8::Local<v8::Value> val);
Nan::Maybe<int32_t> Nan::To<int32_t>(v8::Local<v8::Value> val);
Nan::Maybe<int64_t> Nan::To<int64_t>(v8::Local<v8::Value> val);
Nan::Maybe<uint32_t> Nan::To<uint32_t>(v8::Local<v8::Value> val);
```
### Example
```c++
v8::Local<v8::Value> val;
Nan::MaybeLocal<v8::String> str = Nan::To<v8::String>(val);
Nan::Maybe<double> d = Nan::To<double>(val);
```
## Errors
NAN includes helpers for creating, throwing and catching Errors as much of this functionality varies across the supported versions of V8 and must be abstracted.
Note that an Error object is simply a specialized form of `v8::Value`.
Also consult the V8 Embedders Guide section on [Exceptions](https://developers.google.com/v8/embed#exceptions) for more information.
- <a href="#api_nan_error"><b><code>Nan::Error()</code></b></a>
- <a href="#api_nan_range_error"><b><code>Nan::RangeError()</code></b></a>
- <a href="#api_nan_reference_error"><b><code>Nan::ReferenceError()</code></b></a>
- <a href="#api_nan_syntax_error"><b><code>Nan::SyntaxError()</code></b></a>
- <a href="#api_nan_type_error"><b><code>Nan::TypeError()</code></b></a>
- <a href="#api_nan_throw_error"><b><code>Nan::ThrowError()</code></b></a>
- <a href="#api_nan_throw_range_error"><b><code>Nan::ThrowRangeError()</code></b></a>
- <a href="#api_nan_throw_reference_error"><b><code>Nan::ThrowReferenceError()</code></b></a>
- <a href="#api_nan_throw_syntax_error"><b><code>Nan::ThrowSyntaxError()</code></b></a>
- <a href="#api_nan_throw_type_error"><b><code>Nan::ThrowTypeError()</code></b></a>
- <a href="#api_nan_fatal_exception"><b><code>Nan::FatalException()</code></b></a>
- <a href="#api_nan_errno_exception"><b><code>Nan::ErrnoException()</code></b></a>
- <a href="#api_nan_try_catch"><b><code>Nan::TryCatch</code></b></a>
<a name="api_nan_error"></a>
### Nan::Error()
Create a new Error object using the [v8::Exception](https://v8docs.nodesource.com/io.js-3.0/da/d6a/classv8_1_1_exception.html) class in a way that is compatible across the supported versions of V8.
Note that an Error object is simply a specialized form of `v8::Value`.
Signature:
```c++
v8::Local<v8::Value> Nan::Error(const char *msg);
v8::Local<v8::Value> Nan::Error(v8::Local<v8::String> msg);
```
<a name="api_nan_range_error"></a>
### Nan::RangeError()
Create a new RangeError object using the [v8::Exception](https://v8docs.nodesource.com/io.js-3.0/da/d6a/classv8_1_1_exception.html) class in a way that is compatible across the supported versions of V8.
Note that an RangeError object is simply a specialized form of `v8::Value`.
Signature:
```c++
v8::Local<v8::Value> Nan::RangeError(const char *msg);
v8::Local<v8::Value> Nan::RangeError(v8::Local<v8::String> msg);
```
<a name="api_nan_reference_error"></a>
### Nan::ReferenceError()
Create a new ReferenceError object using the [v8::Exception](https://v8docs.nodesource.com/io.js-3.0/da/d6a/classv8_1_1_exception.html) class in a way that is compatible across the supported versions of V8.
Note that an ReferenceError object is simply a specialized form of `v8::Value`.
Signature:
```c++
v8::Local<v8::Value> Nan::ReferenceError(const char *msg);
v8::Local<v8::Value> Nan::ReferenceError(v8::Local<v8::String> msg);
```
<a name="api_nan_syntax_error"></a>
### Nan::SyntaxError()
Create a new SyntaxError object using the [v8::Exception](https://v8docs.nodesource.com/io.js-3.0/da/d6a/classv8_1_1_exception.html) class in a way that is compatible across the supported versions of V8.
Note that an SyntaxError object is simply a specialized form of `v8::Value`.
Signature:
```c++
v8::Local<v8::Value> Nan::SyntaxError(const char *msg);
v8::Local<v8::Value> Nan::SyntaxError(v8::Local<v8::String> msg);
```
<a name="api_nan_type_error"></a>
### Nan::TypeError()
Create a new TypeError object using the [v8::Exception](https://v8docs.nodesource.com/io.js-3.0/da/d6a/classv8_1_1_exception.html) class in a way that is compatible across the supported versions of V8.
Note that an TypeError object is simply a specialized form of `v8::Value`.
Signature:
```c++
v8::Local<v8::Value> Nan::TypeError(const char *msg);
v8::Local<v8::Value> Nan::TypeError(v8::Local<v8::String> msg);
```
<a name="api_nan_throw_error"></a>
### Nan::ThrowError()
Throw an Error object (a specialized `v8::Value` as above) in the current context. If a `msg` is provided, a new Error object will be created.
Signature:
```c++
void Nan::ThrowError(const char *msg);
void Nan::ThrowError(v8::Local<v8::String> msg);
void Nan::ThrowError(v8::Local<v8::Value> error);
```
<a name="api_nan_throw_range_error"></a>
### Nan::ThrowRangeError()
Throw an RangeError object (a specialized `v8::Value` as above) in the current context. If a `msg` is provided, a new RangeError object will be created.
Signature:
```c++
void Nan::ThrowRangeError(const char *msg);
void Nan::ThrowRangeError(v8::Local<v8::String> msg);
void Nan::ThrowRangeError(v8::Local<v8::Value> error);
```
<a name="api_nan_throw_reference_error"></a>
### Nan::ThrowReferenceError()
Throw an ReferenceError object (a specialized `v8::Value` as above) in the current context. If a `msg` is provided, a new ReferenceError object will be created.
Signature:
```c++
void Nan::ThrowReferenceError(const char *msg);
void Nan::ThrowReferenceError(v8::Local<v8::String> msg);
void Nan::ThrowReferenceError(v8::Local<v8::Value> error);
```
<a name="api_nan_throw_syntax_error"></a>
### Nan::ThrowSyntaxError()
Throw an SyntaxError object (a specialized `v8::Value` as above) in the current context. If a `msg` is provided, a new SyntaxError object will be created.
Signature:
```c++
void Nan::ThrowSyntaxError(const char *msg);
void Nan::ThrowSyntaxError(v8::Local<v8::String> msg);
void Nan::ThrowSyntaxError(v8::Local<v8::Value> error);
```
<a name="api_nan_throw_type_error"></a>
### Nan::ThrowTypeError()
Throw an TypeError object (a specialized `v8::Value` as above) in the current context. If a `msg` is provided, a new TypeError object will be created.
Signature:
```c++
void Nan::ThrowTypeError(const char *msg);
void Nan::ThrowTypeError(v8::Local<v8::String> msg);
void Nan::ThrowTypeError(v8::Local<v8::Value> error);
```
<a name="api_nan_fatal_exception"></a>
### Nan::FatalException()
Replaces `node::FatalException()` which has a different API across supported versions of Node. For use with [`Nan::TryCatch`](#api_nan_try_catch).
Signature:
```c++
void Nan::FatalException(const Nan::TryCatch& try_catch);
```
<a name="api_nan_errno_exception"></a>
### Nan::ErrnoException()
Replaces `node::ErrnoException()` which has a different API across supported versions of Node.
Signature:
```c++
v8::Local<v8::Value> Nan::ErrnoException(int errorno,
const char* syscall = NULL,
const char* message = NULL,
const char* path = NULL);
```
<a name="api_nan_try_catch"></a>
### Nan::TryCatch
A simple wrapper around [`v8::TryCatch`](https://v8docs.nodesource.com/io.js-3.0/d4/dc6/classv8_1_1_try_catch.html) compatible with all supported versions of V8. Can be used as a direct replacement in most cases. See also [`Nan::FatalException()`](#api_nan_fatal_exception) for an internal use compatible with `node::FatalException`.
Signature:
```c++
class Nan::TryCatch {
public:
Nan::TryCatch();
bool HasCaught() const;
bool CanContinue() const;
v8::Local<v8::Value> ReThrow();
v8::Local<v8::Value> Exception() const;
// Nan::MaybeLocal for older versions of V8
v8::MaybeLocal<v8::Value> StackTrace() const;
v8::Local<v8::Message> Message() const;
void Reset();
void SetVerbose(bool value);
void SetCaptureMessage(bool value);
};
```
## JSON
The _JSON_ object provides the c++ versions of the methods offered by the `JSON` object in javascript. V8 exposes these methods via the `v8::JSON` object.
- <a href="#api_nan_json_parse"><b><code>Nan::JSON.Parse</code></b></a>
- <a href="#api_nan_json_stringify"><b><code>Nan::JSON.Stringify</code></b></a>
Refer to the V8 JSON object in the [V8 documentation](https://v8docs.nodesource.com/node-7.4/da/d6f/classv8_1_1_j_s_o_n.html) for more information about these methods and their arguments.
<a name="api_nan_json_parse"></a>
### Nan::JSON.Parse
A simple wrapper around [`v8::JSON::Parse`](https://v8docs.nodesource.com/node-7.4/da/d6f/classv8_1_1_j_s_o_n.html#a936310d2540fb630ed37d3ee3ffe4504).
Definition:
```c++
Nan::MaybeLocal<v8::Value> Nan::JSON::Parse(v8::Local<v8::String> json_string);
```
Use `JSON.Parse(json_string)` to parse a string into a `v8::Value`.
Example:
```c++
v8::Local<v8::String> json_string = Nan::New("{ \"JSON\": \"object\" }").ToLocalChecked();
Nan::JSON NanJSON;
Nan::MaybeLocal<v8::Value> result = NanJSON.Parse(json_string);
if (!result.IsEmpty()) {
v8::Local<v8::Value> val = result.ToLocalChecked();
}
```
<a name="api_nan_json_stringify"></a>
### Nan::JSON.Stringify
A simple wrapper around [`v8::JSON::Stringify`](https://v8docs.nodesource.com/node-7.4/da/d6f/classv8_1_1_j_s_o_n.html#a44b255c3531489ce43f6110209138860).
Definition:
```c++
Nan::MaybeLocal<v8::String> Nan::JSON::Stringify(v8::Local<v8::Object> json_object, v8::Local<v8::String> gap = v8::Local<v8::String>());
```
Use `JSON.Stringify(value)` to stringify a `v8::Object`.
Example:
```c++
// using `v8::Local<v8::Value> val` from the `JSON::Parse` example
v8::Local<v8::Object> obj = Nan::To<v8::Object>(val).ToLocalChecked();
Nan::JSON NanJSON;
Nan::MaybeLocal<v8::String> result = NanJSON.Stringify(obj);
if (!result.IsEmpty()) {
v8::Local<v8::String> stringified = result.ToLocalChecked();
}
```
## New
NAN provides a `Nan::New()` helper for the creation of new JavaScript objects in a way that's compatible across the supported versions of V8.
- <a href="#api_nan_new"><b><code>Nan::New()</code></b></a>
- <a href="#api_nan_undefined"><b><code>Nan::Undefined()</code></b></a>
- <a href="#api_nan_null"><b><code>Nan::Null()</code></b></a>
- <a href="#api_nan_true"><b><code>Nan::True()</code></b></a>
- <a href="#api_nan_false"><b><code>Nan::False()</code></b></a>
- <a href="#api_nan_empty_string"><b><code>Nan::EmptyString()</code></b></a>
<a name="api_nan_new"></a>
### Nan::New()
`Nan::New()` should be used to instantiate new JavaScript objects.
Refer to the specific V8 type in the [V8 documentation](https://v8docs.nodesource.com/io.js-3.0/d1/d83/classv8_1_1_data.html) for information on the types of arguments required for instantiation.
Signatures:
Return types are mostly omitted from the signatures for simplicity. In most cases the type will be contained within a `v8::Local<T>`. The following types will be contained within a `Nan::MaybeLocal<T>`: `v8::String`, `v8::Date`, `v8::RegExp`, `v8::Script`, `v8::UnboundScript`.
Empty objects:
```c++
Nan::New<T>();
```
Generic single and multiple-argument:
```c++
Nan::New<T>(A0 arg0);
Nan::New<T>(A0 arg0, A1 arg1);
Nan::New<T>(A0 arg0, A1 arg1, A2 arg2);
Nan::New<T>(A0 arg0, A1 arg1, A2 arg2, A3 arg3);
```
For creating `v8::FunctionTemplate` and `v8::Function` objects:
_The definition of `Nan::FunctionCallback` can be found in the [Method declaration](./methods.md#api_nan_method) documentation._
```c++
Nan::New<T>(Nan::FunctionCallback callback,
v8::Local<v8::Value> data = v8::Local<v8::Value>());
Nan::New<T>(Nan::FunctionCallback callback,
v8::Local<v8::Value> data = v8::Local<v8::Value>(),
A2 a2 = A2());
```
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);
```
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:
```c++
v8::Local<v8::String> Nan::New<T>(v8::String::ExternalStringResource * value);
v8::Local<v8::String> Nan::New<T>(Nan::ExternalOneByteStringResource * value);
v8::Local<v8::RegExp> Nan::New<T>(v8::Local<v8::String> pattern, v8::RegExp::Flags flags);
```
Note that `Nan::ExternalOneByteStringResource` maps to [`v8::String::ExternalOneByteStringResource`](https://v8docs.nodesource.com/io.js-3.0/d9/db3/classv8_1_1_string_1_1_external_one_byte_string_resource.html), and `v8::String::ExternalAsciiStringResource` in older versions of V8.
<a name="api_nan_undefined"></a>
### Nan::Undefined()
A helper method to reference the `v8::Undefined` object in a way that is compatible across all supported versions of V8.
Signature:
```c++
v8::Local<v8::Primitive> Nan::Undefined()
```
<a name="api_nan_null"></a>
### Nan::Null()
A helper method to reference the `v8::Null` object in a way that is compatible across all supported versions of V8.
Signature:
```c++
v8::Local<v8::Primitive> Nan::Null()
```
<a name="api_nan_true"></a>
### Nan::True()
A helper method to reference the `v8::Boolean` object representing the `true` value in a way that is compatible across all supported versions of V8.
Signature:
```c++
v8::Local<v8::Boolean> Nan::True()
```
<a name="api_nan_false"></a>
### Nan::False()
A helper method to reference the `v8::Boolean` object representing the `false` value in a way that is compatible across all supported versions of V8.
Signature:
```c++
v8::Local<v8::Boolean> Nan::False()
```
<a name="api_nan_empty_string"></a>
### Nan::EmptyString()
Call [`v8::String::Empty`](https://v8docs.nodesource.com/io.js-3.0/d2/db3/classv8_1_1_string.html#a7c1bc8886115d7ee46f1d571dd6ebc6d) to reference the empty string in a way that is compatible across all supported versions of V8.
Signature:
```c++
v8::Local<v8::String> Nan::EmptyString()
```
<a name="api_nan_new_one_byte_string"></a>
### Nan::NewOneByteString()
An implementation of [`v8::String::NewFromOneByte()`](https://v8docs.nodesource.com/io.js-3.0/d2/db3/classv8_1_1_string.html#a5264d50b96d2c896ce525a734dc10f09) provided for consistent availability and API across supported versions of V8. Allocates a new string from Latin-1 data.
Signature:
```c++
Nan::MaybeLocal<v8::String> Nan::NewOneByteString(const uint8_t * value,
int length = -1)
```
## Miscellaneous Node Helpers
- <a href="#api_nan_make_callback"><b><code>Nan::MakeCallback()</code></b></a>
- <a href="#api_nan_module_init"><b><code>NAN_MODULE_INIT()</code></b></a>
- <a href="#api_nan_export"><b><code>Nan::Export()</code></b></a>
<a name="api_nan_make_callback"></a>
### Nan::MakeCallback()
Wrappers around `node::MakeCallback()` providing a consistent API across all supported versions of Node.
Use `MakeCallback()` rather than using `v8::Function#Call()` directly in order to properly process internal Node functionality including domains, async hooks, the microtask queue, and other debugging functionality.
Signatures:
```c++
v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target,
v8::Local<v8::Function> func,
int argc,
v8::Local<v8::Value>* argv);
v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target,
v8::Local<v8::String> symbol,
int argc,
v8::Local<v8::Value>* argv);
v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target,
const char* method,
int argc,
v8::Local<v8::Value>* argv);
```
<a name="api_nan_module_init"></a>
### NAN_MODULE_INIT()
Used to define the entry point function to a Node add-on. Creates a function with a given `name` that receives a `target` object representing the equivalent of the JavaScript `exports` object.
See example below.
<a name="api_nan_export"></a>
### Nan::Export()
A simple helper to register a `v8::FunctionTemplate` from a JavaScript-accessible method (see [Methods](./methods.md)) as a property on an object. Can be used in a way similar to assigning properties to `module.exports` in JavaScript.
Signature:
```c++
void Export(v8::Local<v8::Object> target, const char *name, Nan::FunctionCallback f)
```
Also available as the shortcut `NAN_EXPORT` macro.
Example:
```c++
NAN_METHOD(Foo) {
...
}
NAN_MODULE_INIT(Init) {
NAN_EXPORT(target, Foo);
}
```
## Object Wrappers
The `ObjectWrap` class can be used to make wrapped C++ objects and a factory of wrapped objects.
- <a href="#api_nan_object_wrap"><b><code>Nan::ObjectWrap</code></b></a>
<a name="api_nan_object_wrap"></a>
### Nan::ObjectWrap()
A reimplementation of `node::ObjectWrap` that adds some API not present in older versions of Node. Should be preferred over `node::ObjectWrap` in all cases for consistency.
Definition:
```c++
class ObjectWrap {
public:
ObjectWrap();
virtual ~ObjectWrap();
template <class T>
static inline T* Unwrap(v8::Local<v8::Object> handle);
inline v8::Local<v8::Object> handle();
inline Nan::Persistent<v8::Object>& persistent();
protected:
inline void Wrap(v8::Local<v8::Object> handle);
inline void MakeWeak();
/* Ref() marks the object as being attached to an event loop.
* Refed objects will not be garbage collected, even if
* all references are lost.
*/
virtual void Ref();
/* Unref() marks an object as detached from the event loop. This is its
* default state. When an object with a "weak" reference changes from
* attached to detached state it will be freed. Be careful not to access
* the object after making this call as it might be gone!
* (A "weak reference" means an object that only has a
* persistant handle.)
*
* DO NOT CALL THIS FROM DESTRUCTOR
*/
virtual void Unref();
int refs_; // ro
};
```
See the Node documentation on [Wrapping C++ Objects](https://nodejs.org/api/addons.html#addons_wrapping_c_objects) for more details.
### This vs. Holder
When calling `Unwrap`, it is important that the argument is indeed some JavaScript object which got wrapped by a `Wrap` call for this class or any derived class.
The `Signature` installed by [`Nan::SetPrototypeMethod()`](methods.md#api_nan_set_prototype_method) does ensure that `info.Holder()` is just such an instance.
In Node 0.12 and later, `info.This()` will also be of such a type, since otherwise the invocation will get rejected.
However, in Node 0.10 and before it was possible to invoke a method on a JavaScript object which just had the extension type in its prototype chain.
In such a situation, calling `Unwrap` on `info.This()` will likely lead to a failed assertion causing a crash, but could lead to even more serious corruption.
On the other hand, calling `Unwrap` in an [accessor](methods.md#api_nan_set_accessor) should not use `Holder()` if the accessor is defined on the prototype.
So either define your accessors on the instance template,
or use `This()` after verifying that it is indeed a valid object.
### Examples
#### Basic
```c++
class MyObject : public Nan::ObjectWrap {
public:
static NAN_MODULE_INIT(Init) {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("MyObject").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(tpl, "getHandle", GetHandle);
Nan::SetPrototypeMethod(tpl, "getValue", GetValue);
constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
Nan::Set(target, Nan::New("MyObject").ToLocalChecked(),
Nan::GetFunction(tpl).ToLocalChecked());
}
private:
explicit MyObject(double value = 0) : value_(value) {}
~MyObject() {}
static NAN_METHOD(New) {
if (info.IsConstructCall()) {
double value = info[0]->IsUndefined() ? 0 : Nan::To<double>(info[0]).FromJust();
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(cons->NewInstance(argc, argv));
}
}
static NAN_METHOD(GetHandle) {
MyObject* obj = Nan::ObjectWrap::Unwrap<MyObject>(info.Holder());
info.GetReturnValue().Set(obj->handle());
}
static NAN_METHOD(GetValue) {
MyObject* obj = Nan::ObjectWrap::Unwrap<MyObject>(info.Holder());
info.GetReturnValue().Set(obj->value_);
}
static inline Nan::Persistent<v8::Function> & constructor() {
static Nan::Persistent<v8::Function> my_constructor;
return my_constructor;
}
double value_;
};
NODE_MODULE(objectwrapper, MyObject::Init)
```
To use in Javascript:
```Javascript
var objectwrapper = require('bindings')('objectwrapper');
var obj = new objectwrapper.MyObject(5);
console.log('Should be 5: ' + obj.getValue());
```
#### Factory of wrapped objects
```c++
class MyFactoryObject : public Nan::ObjectWrap {
public:
static NAN_MODULE_INIT(Init) {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(tpl, "getValue", GetValue);
constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
}
static NAN_METHOD(NewInstance) {
v8::Local<v8::Function> cons = Nan::New(constructor());
double value = info[0]->IsNumber() ? Nan::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());
}
// Needed for the next example:
inline double value() const {
return value_;
}
private:
explicit MyFactoryObject(double value = 0) : value_(value) {}
~MyFactoryObject() {}
static NAN_METHOD(New) {
if (info.IsConstructCall()) {
double value = info[0]->IsNumber() ? Nan::To<double>(info[0]).FromJust() : 0;
MyFactoryObject * obj = new MyFactoryObject(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) {
MyFactoryObject* obj = ObjectWrap::Unwrap<MyFactoryObject>(info.Holder());
info.GetReturnValue().Set(obj->value_);
}
static inline Nan::Persistent<v8::Function> & constructor() {
static Nan::Persistent<v8::Function> my_constructor;
return my_constructor;
}
double value_;
};
NAN_MODULE_INIT(Init) {
MyFactoryObject::Init(target);
Nan::Set(target,
Nan::New<v8::String>("newFactoryObjectInstance").ToLocalChecked(),
Nan::GetFunction(
Nan::New<v8::FunctionTemplate>(MyFactoryObject::NewInstance)).ToLocalChecked()
);
}
NODE_MODULE(wrappedobjectfactory, Init)
```
To use in Javascript:
```Javascript
var wrappedobjectfactory = require('bindings')('wrappedobjectfactory');
var obj = wrappedobjectfactory.newFactoryObjectInstance(10);
console.log('Should be 10: ' + obj.getValue());
```
#### Passing wrapped objects around
Use the `MyFactoryObject` class above along with the following:
```c++
static NAN_METHOD(Sum) {
Nan::MaybeLocal<v8::Object> maybe1 = Nan::To<v8::Object>(info[0]);
Nan::MaybeLocal<v8::Object> maybe2 = Nan::To<v8::Object>(info[1]);
// Quick check:
if (maybe1.IsEmpty() || maybe2.IsEmpty()) {
// return value is undefined by default
return;
}
MyFactoryObject* obj1 =
Nan::ObjectWrap::Unwrap<MyFactoryObject>(maybe1.ToLocalChecked());
MyFactoryObject* obj2 =
Nan::ObjectWrap::Unwrap<MyFactoryObject>(maybe2.ToLocalChecked());
info.GetReturnValue().Set(Nan::New<v8::Number>(obj1->value() + obj2->value()));
}
NAN_MODULE_INIT(Init) {
MyFactoryObject::Init(target);
Nan::Set(target,
Nan::New<v8::String>("newFactoryObjectInstance").ToLocalChecked(),
Nan::GetFunction(
Nan::New<v8::FunctionTemplate>(MyFactoryObject::NewInstance)).ToLocalChecked()
);
Nan::Set(target,
Nan::New<v8::String>("sum").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Sum)).ToLocalChecked()
);
}
NODE_MODULE(myaddon, Init)
```
To use in Javascript:
```Javascript
var myaddon = require('bindings')('myaddon');
var obj1 = myaddon.newFactoryObjectInstance(5);
var obj2 = myaddon.newFactoryObjectInstance(10);
console.log('sum of object values: ' + myaddon.sum(obj1, obj2));
```
## Scopes
A _local handle_ is a pointer to an object. All V8 objects are accessed using handles, they are necessary because of the way the V8 garbage collector works.
A handle scope can be thought of as a container for any number of handles. When you've finished with your handles, instead of deleting each one individually you can simply delete their scope.
The creation of `HandleScope` objects is different across the supported versions of V8. Therefore, NAN provides its own implementations that can be used safely across these.
- <a href="#api_nan_handle_scope"><b><code>Nan::HandleScope</code></b></a>
- <a href="#api_nan_escapable_handle_scope"><b><code>Nan::EscapableHandleScope</code></b></a>
Also see the V8 Embedders Guide section on [Handles and Garbage Collection](https://github.com/v8/v8/wiki/Embedder%27s%20Guide#handles-and-garbage-collection).
<a name="api_nan_handle_scope"></a>
### Nan::HandleScope
A simple wrapper around [`v8::HandleScope`](https://v8docs.nodesource.com/io.js-3.0/d3/d95/classv8_1_1_handle_scope.html).
Definition:
```c++
class Nan::HandleScope {
public:
Nan::HandleScope();
static int NumberOfHandles();
};
```
Allocate a new `Nan::HandleScope` whenever you are creating new V8 JavaScript objects. Note that an implicit `HandleScope` is created for you on JavaScript-accessible methods so you do not need to insert one yourself.
Example:
```c++
// new object is created, it needs a new scope:
void Pointless() {
Nan::HandleScope scope;
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
}
// JavaScript-accessible method already has a HandleScope
NAN_METHOD(Pointless2) {
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
}
```
<a name="api_nan_escapable_handle_scope"></a>
### Nan::EscapableHandleScope
Similar to [`Nan::HandleScope`](#api_nan_handle_scope) but should be used in cases where a function needs to return a V8 JavaScript type that has been created within it.
Definition:
```c++
class Nan::EscapableHandleScope {
public:
Nan::EscapableHandleScope();
static int NumberOfHandles();
template<typename T> v8::Local<T> Escape(v8::Local<T> value);
}
```
Use `Escape(value)` to return the object.
Example:
```c++
v8::Local<v8::Object> EmptyObj() {
Nan::EscapableHandleScope scope;
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
return scope.Escape(obj);
}
```
## Script
NAN provides a `v8::Script` helpers as the API has changed over the supported versions of V8.
- <a href="#api_nan_compile_script"><b><code>Nan::CompileScript()</code></b></a>
- <a href="#api_nan_run_script"><b><code>Nan::RunScript()</code></b></a>
<a name="api_nan_compile_script"></a>
### Nan::CompileScript()
A wrapper around [`v8::Script::Compile()`](https://v8docs.nodesource.com/io.js-3.0/da/da5/classv8_1_1_script_compiler.html#a93f5072a0db55d881b969e9fc98e564b).
Note that `Nan::BoundScript` is an alias for `v8::Script`.
Signature:
```c++
Nan::MaybeLocal<Nan::BoundScript> Nan::CompileScript(
v8::Local<v8::String> s,
const v8::ScriptOrigin& origin);
Nan::MaybeLocal<Nan::BoundScript> Nan::CompileScript(v8::Local<v8::String> s);
```
<a name="api_nan_run_script"></a>
### Nan::RunScript()
Calls `script->Run()` or `script->BindToCurrentContext()->Run(Nan::GetCurrentContext())`.
Note that `Nan::BoundScript` is an alias for `v8::Script` and `Nan::UnboundScript` is an alias for `v8::UnboundScript` where available and `v8::Script` on older versions of V8.
Signature:
```c++
Nan::MaybeLocal<v8::Value> Nan::RunScript(v8::Local<Nan::UnboundScript> script)
Nan::MaybeLocal<v8::Value> Nan::RunScript(v8::Local<Nan::BoundScript> script)
```
## Strings & Bytes
Miscellaneous string & byte encoding and decoding functionality provided for compatibility across supported versions of V8 and Node. Implemented by NAN to ensure that all encoding types are supported, even for older versions of Node where they are missing.
- <a href="#api_nan_encoding"><b><code>Nan::Encoding</code></b></a>
- <a href="#api_nan_encode"><b><code>Nan::Encode()</code></b></a>
- <a href="#api_nan_decode_bytes"><b><code>Nan::DecodeBytes()</code></b></a>
- <a href="#api_nan_decode_write"><b><code>Nan::DecodeWrite()</code></b></a>
<a name="api_nan_encoding"></a>
### Nan::Encoding
An enum representing the supported encoding types. A copy of `node::encoding` that is consistent across versions of Node.
Definition:
```c++
enum Nan::Encoding { ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER }
```
<a name="api_nan_encode"></a>
### Nan::Encode()
A wrapper around `node::Encode()` that provides a consistent implementation across supported versions of Node.
Signature:
```c++
v8::Local<v8::Value> Nan::Encode(const void *buf,
size_t len,
enum Nan::Encoding encoding = BINARY);
```
<a name="api_nan_decode_bytes"></a>
### Nan::DecodeBytes()
A wrapper around `node::DecodeBytes()` that provides a consistent implementation across supported versions of Node.
Signature:
```c++
ssize_t Nan::DecodeBytes(v8::Local<v8::Value> val,
enum Nan::Encoding encoding = BINARY);
```
<a name="api_nan_decode_write"></a>
### Nan::DecodeWrite()
A wrapper around `node::DecodeWrite()` that provides a consistent implementation across supported versions of Node.
Signature:
```c++
ssize_t Nan::DecodeWrite(char *buf,
size_t len,
v8::Local<v8::Value> val,
enum Nan::Encoding encoding = BINARY);
```
## V8 internals
The hooks to access V8 internals—including GC and statistics—are different across the supported versions of V8, therefore NAN provides its own hooks that call the appropriate V8 methods.
- <a href="#api_nan_gc_callback"><b><code>NAN_GC_CALLBACK()</code></b></a>
- <a href="#api_nan_add_gc_epilogue_callback"><b><code>Nan::AddGCEpilogueCallback()</code></b></a>
- <a href="#api_nan_remove_gc_epilogue_callback"><b><code>Nan::RemoveGCEpilogueCallback()</code></b></a>
- <a href="#api_nan_add_gc_prologue_callback"><b><code>Nan::AddGCPrologueCallback()</code></b></a>
- <a href="#api_nan_remove_gc_prologue_callback"><b><code>Nan::RemoveGCPrologueCallback()</code></b></a>
- <a href="#api_nan_get_heap_statistics"><b><code>Nan::GetHeapStatistics()</code></b></a>
- <a href="#api_nan_set_counter_function"><b><code>Nan::SetCounterFunction()</code></b></a>
- <a href="#api_nan_set_create_histogram_function"><b><code>Nan::SetCreateHistogramFunction()</code></b></a>
- <a href="#api_nan_set_add_histogram_sample_function"><b><code>Nan::SetAddHistogramSampleFunction()</code></b></a>
- <a href="#api_nan_idle_notification"><b><code>Nan::IdleNotification()</code></b></a>
- <a href="#api_nan_low_memory_notification"><b><code>Nan::LowMemoryNotification()</code></b></a>
- <a href="#api_nan_context_disposed_notification"><b><code>Nan::ContextDisposedNotification()</code></b></a>
- <a href="#api_nan_get_internal_field_pointer"><b><code>Nan::GetInternalFieldPointer()</code></b></a>
- <a href="#api_nan_set_internal_field_pointer"><b><code>Nan::SetInternalFieldPointer()</code></b></a>
- <a href="#api_nan_adjust_external_memory"><b><code>Nan::AdjustExternalMemory()</code></b></a>
<a name="api_nan_gc_callback"></a>
### NAN_GC_CALLBACK(callbackname)
Use `NAN_GC_CALLBACK` to declare your callbacks for `Nan::AddGCPrologueCallback()` and `Nan::AddGCEpilogueCallback()`. Your new method receives the arguments `v8::GCType type` and `v8::GCCallbackFlags flags`.
```c++
static Nan::Persistent<Function> callback;
NAN_GC_CALLBACK(gcPrologueCallback) {
v8::Local<Value> argv[] = { Nan::New("prologue").ToLocalChecked() };
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), Nan::New(callback), 1, argv);
}
NAN_METHOD(Hook) {
callback.Reset(args[0].As<Function>());
Nan::AddGCPrologueCallback(gcPrologueCallback);
info.GetReturnValue().Set(info.Holder());
}
```
<a name="api_nan_add_gc_epilogue_callback"></a>
### Nan::AddGCEpilogueCallback()
Signature:
```c++
void Nan::AddGCEpilogueCallback(v8::Isolate::GCEpilogueCallback callback, v8::GCType gc_type_filter = v8::kGCTypeAll)
```
Calls V8's [`AddGCEpilogueCallback()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#a90d1860babc76059c62514b422f56960).
<a name="api_nan_remove_gc_epilogue_callback"></a>
### Nan::RemoveGCEpilogueCallback()
Signature:
```c++
void Nan::RemoveGCEpilogueCallback(v8::Isolate::GCEpilogueCallback callback)
```
Calls V8's [`RemoveGCEpilogueCallback()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#a05c60859fd4b8e96bfcd451281ed6c7c).
<a name="api_nan_add_gc_prologue_callback"></a>
### Nan::AddGCPrologueCallback()
Signature:
```c++
void Nan::AddGCPrologueCallback(v8::Isolate::GCPrologueCallback, v8::GCType gc_type_filter callback)
```
Calls V8's [`AddGCPrologueCallback()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#ab4b87b8f9f8e5bf95eba4009357e001f).
<a name="api_nan_remove_gc_prologue_callback"></a>
### Nan::RemoveGCPrologueCallback()
Signature:
```c++
void Nan::RemoveGCPrologueCallback(v8::Isolate::GCPrologueCallback callback)
```
Calls V8's [`RemoveGCEpilogueCallback()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#a9f6c51932811593f81ff30b949124186).
<a name="api_nan_get_heap_statistics"></a>
### Nan::GetHeapStatistics()
Signature:
```c++
void Nan::GetHeapStatistics(v8::HeapStatistics *heap_statistics)
```
Calls V8's [`GetHeapStatistics()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#a5593ac74687b713095c38987e5950b34).
<a name="api_nan_set_counter_function"></a>
### Nan::SetCounterFunction()
Signature:
```c++
void Nan::SetCounterFunction(v8::CounterLookupCallback cb)
```
Calls V8's [`SetCounterFunction()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#a045d7754e62fa0ec72ae6c259b29af94).
<a name="api_nan_set_create_histogram_function"></a>
### Nan::SetCreateHistogramFunction()
Signature:
```c++
void Nan::SetCreateHistogramFunction(v8::CreateHistogramCallback cb)
```
Calls V8's [`SetCreateHistogramFunction()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#a542d67e85089cb3f92aadf032f99e732).
<a name="api_nan_set_add_histogram_sample_function"></a>
### Nan::SetAddHistogramSampleFunction()
Signature:
```c++
void Nan::SetAddHistogramSampleFunction(v8::AddHistogramSampleCallback cb)
```
Calls V8's [`SetAddHistogramSampleFunction()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#aeb420b690bc2c216882d6fdd00ddd3ea).
<a name="api_nan_idle_notification"></a>
### Nan::IdleNotification()
Signature:
```c++
void Nan::IdleNotification(v8::HeapStatistics *heap_statistics)
```
Calls V8's [`IdleNotification()` or `IdleNotificationDeadline()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#ad6a2a02657f5425ad460060652a5a118) depending on V8 version.
<a name="api_nan_low_memory_notification"></a>
### Nan::LowMemoryNotification()
Signature:
```c++
void Nan::LowMemoryNotification()
```
Calls V8's [`LowMemoryNotification()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#a24647f61d6b41f69668094bdcd6ea91f).
<a name="api_nan_context_disposed_notification"></a>
### Nan::ContextDisposedNotification()
Signature:
```c++
void Nan::ContextDisposedNotification()
```
Calls V8's [`ContextDisposedNotification()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#ad7f5dc559866343fe6cd8db1f134d48b).
<a name="api_nan_get_internal_field_pointer"></a>
### Nan::GetInternalFieldPointer()
Gets a pointer to the internal field with at `index` from a V8 `Object` handle.
Signature:
```c++
void* Nan::GetInternalFieldPointer(v8::Local<v8::Object> object, int index)
```
Calls the Object's [`GetAlignedPointerFromInternalField()` or `GetPointerFromInternalField()`](https://v8docs.nodesource.com/io.js-3.0/db/d85/classv8_1_1_object.html#ab3c57184263cf29963ef0017bec82281) depending on the version of V8.
<a name="api_nan_set_internal_field_pointer"></a>
### Nan::SetInternalFieldPointer()
Sets the value of the internal field at `index` on a V8 `Object` handle.
Signature:
```c++
void Nan::SetInternalFieldPointer(v8::Local<v8::Object> object, int index, void* value)
```
Calls the Object's [`SetAlignedPointerInInternalField()` or `SetPointerInInternalField()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#ad7f5dc559866343fe6cd8db1f134d48b) depending on the version of V8.
<a name="api_nan_adjust_external_memory"></a>
### Nan::AdjustExternalMemory()
Signature:
```c++
int Nan::AdjustExternalMemory(int bytesChange)
```
Calls V8's [`AdjustAmountOfExternalAllocatedMemory()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#ae1a59cac60409d3922582c4af675473e).
## Miscellaneous V8 Helpers
- <a href="#api_nan_utf8_string"><b><code>Nan::Utf8String</code></b></a>
- <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</code></b></a>
<a name="api_nan_utf8_string"></a>
### Nan::Utf8String
Converts an object to a UTF-8-encoded character array. If conversion to a string fails (e.g. due to an exception in the toString() method of the object) then the length() method returns 0 and the * operator returns NULL. The underlying memory used for this object is managed by the object.
An implementation of [`v8::String::Utf8Value`](https://v8docs.nodesource.com/io.js-3.0/d4/d1b/classv8_1_1_string_1_1_utf8_value.html) that is consistent across all supported versions of V8.
Definition:
```c++
class Nan::Utf8String {
public:
Nan::Utf8String(v8::Local<v8::Value> from);
int length() const;
char* operator*();
const char* operator*() const;
};
```
<a name="api_nan_get_current_context"></a>
### Nan::GetCurrentContext()
A call to [`v8::Isolate::GetCurrent()->GetCurrentContext()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#a81c7a1ed7001ae2a65e89107f75fd053) that works across all supported versions of V8.
Signature:
```c++
v8::Local<v8::Context> Nan::GetCurrentContext()
```
<a name="api_nan_set_isolate_data"></a>
### Nan::SetIsolateData()
A helper to provide a consistent API to [`v8::Isolate#SetData()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#a7acadfe7965997e9c386a05f098fbe36).
Signature:
```c++
void Nan::SetIsolateData(v8::Isolate *isolate, T *data)
```
<a name="api_nan_get_isolate_data"></a>
### Nan::GetIsolateData()
A helper to provide a consistent API to [`v8::Isolate#GetData()`](https://v8docs.nodesource.com/io.js-3.0/d5/dda/classv8_1_1_isolate.html#aabd223436bc1100a787dadaa024c6257).
Signature:
```c++
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;
};
```
console.log(require('path').relative('.', __dirname));
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2017 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/
#ifndef NAN_CALLBACKS_H_
#define NAN_CALLBACKS_H_
template<typename T> class FunctionCallbackInfo;
template<typename T> class PropertyCallbackInfo;
template<typename T> class Global;
typedef void(*FunctionCallback)(const FunctionCallbackInfo<v8::Value>&);
typedef void(*GetterCallback)
(v8::Local<v8::String>, const PropertyCallbackInfo<v8::Value>&);
typedef void(*SetterCallback)(
v8::Local<v8::String>,
v8::Local<v8::Value>,
const PropertyCallbackInfo<void>&);
typedef void(*PropertyGetterCallback)(
v8::Local<v8::String>,
const PropertyCallbackInfo<v8::Value>&);
typedef void(*PropertySetterCallback)(
v8::Local<v8::String>,
v8::Local<v8::Value>,
const PropertyCallbackInfo<v8::Value>&);
typedef void(*PropertyEnumeratorCallback)
(const PropertyCallbackInfo<v8::Array>&);
typedef void(*PropertyDeleterCallback)(
v8::Local<v8::String>,
const PropertyCallbackInfo<v8::Boolean>&);
typedef void(*PropertyQueryCallback)(
v8::Local<v8::String>,
const PropertyCallbackInfo<v8::Integer>&);
typedef void(*IndexGetterCallback)(
uint32_t,
const PropertyCallbackInfo<v8::Value>&);
typedef void(*IndexSetterCallback)(
uint32_t,
v8::Local<v8::Value>,
const PropertyCallbackInfo<v8::Value>&);
typedef void(*IndexEnumeratorCallback)
(const PropertyCallbackInfo<v8::Array>&);
typedef void(*IndexDeleterCallback)(
uint32_t,
const PropertyCallbackInfo<v8::Boolean>&);
typedef void(*IndexQueryCallback)(
uint32_t,
const PropertyCallbackInfo<v8::Integer>&);
namespace imp {
typedef v8::Local<v8::AccessorSignature> Sig;
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;
} // end of namespace imp
#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_
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2017 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/
#ifndef NAN_CONVERTERS_H_
#define NAN_CONVERTERS_H_
namespace imp {
template<typename T> struct ToFactoryBase {
typedef MaybeLocal<T> return_t;
};
template<typename T> struct ValueFactoryBase { typedef Maybe<T> return_t; };
template<typename T> struct ToFactory;
template<>
struct ToFactory<v8::Function> : ToFactoryBase<v8::Function> {
static inline return_t convert(v8::Local<v8::Value> val) {
if (val.IsEmpty() || !val->IsFunction()) return MaybeLocal<v8::Function>();
return MaybeLocal<v8::Function>(val.As<v8::Function>());
}
};
#define X(TYPE) \
template<> \
struct ToFactory<v8::TYPE> : ToFactoryBase<v8::TYPE> { \
static inline return_t convert(v8::Local<v8::Value> val); \
};
X(Boolean)
X(Number)
X(String)
X(Object)
X(Integer)
X(Uint32)
X(Int32)
#undef X
#define X(TYPE) \
template<> \
struct ToFactory<TYPE> : ValueFactoryBase<TYPE> { \
static inline return_t convert(v8::Local<v8::Value> val); \
};
X(bool)
X(double)
X(int64_t)
X(uint32_t)
X(int32_t)
#undef X
} // end of namespace imp
template<typename T>
inline
typename imp::ToFactory<T>::return_t To(v8::Local<v8::Value> val) {
return imp::ToFactory<T>::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_
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2017 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/
#ifndef NAN_CONVERTERS_43_INL_H_
#define NAN_CONVERTERS_43_INL_H_
#define X(TYPE) \
imp::ToFactory<v8::TYPE>::return_t \
imp::ToFactory<v8::TYPE>::convert(v8::Local<v8::Value> val) { \
v8::Isolate *isolate = v8::Isolate::GetCurrent(); \
v8::EscapableHandleScope scope(isolate); \
return scope.Escape( \
val->To ## TYPE(v8::Isolate::GetCurrent()->GetCurrentContext()) \
.FromMaybe(v8::Local<v8::TYPE>())); \
}
X(Boolean)
X(Number)
X(String)
X(Object)
X(Integer)
X(Uint32)
X(Int32)
#undef X
#define X(TYPE, NAME) \
imp::ToFactory<TYPE>::return_t \
imp::ToFactory<TYPE>::convert(v8::Local<v8::Value> val) { \
v8::Isolate *isolate = v8::Isolate::GetCurrent(); \
v8::HandleScope scope(isolate); \
return val->NAME ## Value(isolate->GetCurrentContext()); \
}
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_
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2017 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/
#ifndef NAN_CONVERTERS_PRE_43_INL_H_
#define NAN_CONVERTERS_PRE_43_INL_H_
#define X(TYPE) \
imp::ToFactory<v8::TYPE>::return_t \
imp::ToFactory<v8::TYPE>::convert(v8::Local<v8::Value> val) { \
return val->To ## TYPE(); \
}
X(Boolean)
X(Number)
X(String)
X(Object)
X(Integer)
X(Uint32)
X(Int32)
#undef X
#define X(TYPE, NAME) \
imp::ToFactory<TYPE>::return_t \
imp::ToFactory<TYPE>::convert(v8::Local<v8::Value> val) { \
return Just(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_
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2017 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/
#ifndef NAN_IMPLEMENTATION_PRE_12_INL_H_
#define NAN_IMPLEMENTATION_PRE_12_INL_H_
//==============================================================================
// node v0.10 implementation
//==============================================================================
namespace imp {
//=== Array ====================================================================
Factory<v8::Array>::return_t
Factory<v8::Array>::New() {
return v8::Array::New();
}
Factory<v8::Array>::return_t
Factory<v8::Array>::New(int length) {
return v8::Array::New(length);
}
//=== Boolean ==================================================================
Factory<v8::Boolean>::return_t
Factory<v8::Boolean>::New(bool value) {
return v8::Boolean::New(value)->ToBoolean();
}
//=== Boolean Object ===========================================================
Factory<v8::BooleanObject>::return_t
Factory<v8::BooleanObject>::New(bool value) {
return v8::BooleanObject::New(value).As<v8::BooleanObject>();
}
//=== Context ==================================================================
Factory<v8::Context>::return_t
Factory<v8::Context>::New( v8::ExtensionConfiguration* extensions
, v8::Local<v8::ObjectTemplate> tmpl
, v8::Local<v8::Value> obj) {
v8::Persistent<v8::Context> ctx = v8::Context::New(extensions, tmpl, obj);
v8::Local<v8::Context> lctx = v8::Local<v8::Context>::New(ctx);
ctx.Dispose();
return lctx;
}
//=== Date =====================================================================
Factory<v8::Date>::return_t
Factory<v8::Date>::New(double value) {
return v8::Date::New(value).As<v8::Date>();
}
//=== External =================================================================
Factory<v8::External>::return_t
Factory<v8::External>::New(void * value) {
return v8::External::New(value);
}
//=== Function =================================================================
Factory<v8::Function>::return_t
Factory<v8::Function>::New( FunctionCallback callback
, v8::Local<v8::Value> data) {
v8::HandleScope scope;
return scope.Close(Factory<v8::FunctionTemplate>::New(
callback, data, v8::Local<v8::Signature>())
->GetFunction());
}
//=== FunctionTemplate =========================================================
Factory<v8::FunctionTemplate>::return_t
Factory<v8::FunctionTemplate>::New( FunctionCallback callback
, v8::Local<v8::Value> data
, v8::Local<v8::Signature> signature) {
if (callback) {
v8::HandleScope scope;
v8::Local<v8::ObjectTemplate> tpl = v8::ObjectTemplate::New();
tpl->SetInternalFieldCount(imp::kFunctionFieldCount);
v8::Local<v8::Object> obj = tpl->NewInstance();
obj->SetInternalField(
imp::kFunctionIndex
, v8::External::New(reinterpret_cast<void *>(callback)));
v8::Local<v8::Value> val = v8::Local<v8::Value>::New(data);
if (!val.IsEmpty()) {
obj->SetInternalField(imp::kDataIndex, val);
}
// Note(agnat): Emulate length argument here. Unfortunately, I couldn't find
// a way. Have at it though...
return scope.Close(
v8::FunctionTemplate::New(imp::FunctionCallbackWrapper
, obj
, signature));
} else {
return v8::FunctionTemplate::New(0, data, signature);
}
}
//=== Number ===================================================================
Factory<v8::Number>::return_t
Factory<v8::Number>::New(double value) {
return v8::Number::New(value);
}
//=== Number Object ============================================================
Factory<v8::NumberObject>::return_t
Factory<v8::NumberObject>::New(double value) {
return v8::NumberObject::New(value).As<v8::NumberObject>();
}
//=== Integer, Int32 and Uint32 ================================================
template <typename T>
typename IntegerFactory<T>::return_t
IntegerFactory<T>::New(int32_t value) {
return To<T>(T::New(value));
}
template <typename T>
typename IntegerFactory<T>::return_t
IntegerFactory<T>::New(uint32_t value) {
return To<T>(T::NewFromUnsigned(value));
}
Factory<v8::Uint32>::return_t
Factory<v8::Uint32>::New(int32_t value) {
return To<v8::Uint32>(v8::Uint32::NewFromUnsigned(value));
}
Factory<v8::Uint32>::return_t
Factory<v8::Uint32>::New(uint32_t value) {
return To<v8::Uint32>(v8::Uint32::NewFromUnsigned(value));
}
//=== Object ===================================================================
Factory<v8::Object>::return_t
Factory<v8::Object>::New() {
return v8::Object::New();
}
//=== Object Template ==========================================================
Factory<v8::ObjectTemplate>::return_t
Factory<v8::ObjectTemplate>::New() {
return v8::ObjectTemplate::New();
}
//=== RegExp ===================================================================
Factory<v8::RegExp>::return_t
Factory<v8::RegExp>::New(
v8::Local<v8::String> pattern
, v8::RegExp::Flags flags) {
return v8::RegExp::New(pattern, flags);
}
//=== Script ===================================================================
Factory<v8::Script>::return_t
Factory<v8::Script>::New( v8::Local<v8::String> source) {
return v8::Script::New(source);
}
Factory<v8::Script>::return_t
Factory<v8::Script>::New( v8::Local<v8::String> source
, v8::ScriptOrigin const& origin) {
return v8::Script::New(source, const_cast<v8::ScriptOrigin*>(&origin));
}
//=== Signature ================================================================
Factory<v8::Signature>::return_t
Factory<v8::Signature>::New(Factory<v8::Signature>::FTH receiver) {
return v8::Signature::New(receiver);
}
//=== String ===================================================================
Factory<v8::String>::return_t
Factory<v8::String>::New() {
return v8::String::Empty();
}
Factory<v8::String>::return_t
Factory<v8::String>::New(const char * value, int length) {
return v8::String::New(value, length);
}
Factory<v8::String>::return_t
Factory<v8::String>::New(
std::string const& value) /* NOLINT(build/include_what_you_use) */ {
assert(value.size() <= INT_MAX && "string too long");
return v8::String::New(value.data(), static_cast<int>(value.size()));
}
Factory<v8::String>::return_t
Factory<v8::String>::New(const uint16_t * value, int length) {
return v8::String::New(value, length);
}
Factory<v8::String>::return_t
Factory<v8::String>::New(v8::String::ExternalStringResource * value) {
return v8::String::NewExternal(value);
}
Factory<v8::String>::return_t
Factory<v8::String>::New(v8::String::ExternalAsciiStringResource * value) {
return v8::String::NewExternal(value);
}
//=== String Object ============================================================
Factory<v8::StringObject>::return_t
Factory<v8::StringObject>::New(v8::Local<v8::String> value) {
return v8::StringObject::New(value).As<v8::StringObject>();
}
} // end of namespace imp
//=== Presistents and Handles ==================================================
template <typename T>
inline v8::Local<T> New(v8::Handle<T> h) {
return v8::Local<T>::New(h);
}
template <typename T>
inline v8::Local<T> New(v8::Persistent<T> const& p) {
return v8::Local<T>::New(p);
}
template <typename T, typename M>
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_
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2017 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/
#ifndef NAN_JSON_H_
#define NAN_JSON_H_
#if NODE_MODULE_VERSION < NODE_0_12_MODULE_VERSION
#define NAN_JSON_H_NEED_PARSE 1
#else
#define NAN_JSON_H_NEED_PARSE 0
#endif // NODE_MODULE_VERSION < NODE_0_12_MODULE_VERSION
#if NODE_MODULE_VERSION >= NODE_7_0_MODULE_VERSION
#define NAN_JSON_H_NEED_STRINGIFY 0
#else
#define NAN_JSON_H_NEED_STRINGIFY 1
#endif // NODE_MODULE_VERSION >= NODE_7_0_MODULE_VERSION
class JSON {
public:
JSON() {
#if NAN_JSON_H_NEED_PARSE + NAN_JSON_H_NEED_STRINGIFY
Nan::HandleScope scope;
Nan::MaybeLocal<v8::Value> maybe_global_json = Nan::Get(
Nan::GetCurrentContext()->Global(),
Nan::New("JSON").ToLocalChecked()
);
assert(!maybe_global_json.IsEmpty() && "global JSON is empty");
v8::Local<v8::Value> val_global_json = maybe_global_json.ToLocalChecked();
assert(val_global_json->IsObject() && "global JSON is not an object");
Nan::MaybeLocal<v8::Object> maybe_obj_global_json =
Nan::To<v8::Object>(val_global_json);
assert(!maybe_obj_global_json.IsEmpty() && "global JSON object is empty");
v8::Local<v8::Object> global_json = maybe_obj_global_json.ToLocalChecked();
#if NAN_JSON_H_NEED_PARSE
Nan::MaybeLocal<v8::Value> maybe_parse_method = Nan::Get(
global_json, Nan::New("parse").ToLocalChecked()
);
assert(!maybe_parse_method.IsEmpty() && "JSON.parse is empty");
v8::Local<v8::Value> parse_method = maybe_parse_method.ToLocalChecked();
assert(parse_method->IsFunction() && "JSON.parse is not a function");
parse_cb_.Reset(parse_method.As<v8::Function>());
#endif // NAN_JSON_H_NEED_PARSE
#if NAN_JSON_H_NEED_STRINGIFY
Nan::MaybeLocal<v8::Value> maybe_stringify_method = Nan::Get(
global_json, Nan::New("stringify").ToLocalChecked()
);
assert(!maybe_stringify_method.IsEmpty() && "JSON.stringify is empty");
v8::Local<v8::Value> stringify_method =
maybe_stringify_method.ToLocalChecked();
assert(
stringify_method->IsFunction() && "JSON.stringify is not a function"
);
stringify_cb_.Reset(stringify_method.As<v8::Function>());
#endif // NAN_JSON_H_NEED_STRINGIFY
#endif // NAN_JSON_H_NEED_PARSE + NAN_JSON_H_NEED_STRINGIFY
}
inline
Nan::MaybeLocal<v8::Value> Parse(v8::Local<v8::String> json_string) {
Nan::EscapableHandleScope scope;
#if NAN_JSON_H_NEED_PARSE
return scope.Escape(parse(json_string));
#else
Nan::MaybeLocal<v8::Value> result;
#if NODE_MODULE_VERSION == NODE_0_12_MODULE_VERSION
result = v8::JSON::Parse(json_string);
#else
#if NODE_MODULE_VERSION > NODE_6_0_MODULE_VERSION
v8::Local<v8::Context> context_or_isolate = Nan::GetCurrentContext();
#else
v8::Isolate* context_or_isolate = v8::Isolate::GetCurrent();
#endif // NODE_MODULE_VERSION > NODE_6_0_MODULE_VERSION
result = v8::JSON::Parse(context_or_isolate, json_string);
#endif // NODE_MODULE_VERSION == NODE_0_12_MODULE_VERSION
if (result.IsEmpty()) return v8::Local<v8::Value>();
return scope.Escape(result.ToLocalChecked());
#endif // NAN_JSON_H_NEED_PARSE
}
inline
Nan::MaybeLocal<v8::String> Stringify(v8::Local<v8::Object> json_object) {
Nan::EscapableHandleScope scope;
Nan::MaybeLocal<v8::String> result =
#if NAN_JSON_H_NEED_STRINGIFY
Nan::To<v8::String>(stringify(json_object));
#else
v8::JSON::Stringify(Nan::GetCurrentContext(), json_object);
#endif // NAN_JSON_H_NEED_STRINGIFY
if (result.IsEmpty()) return v8::Local<v8::String>();
return scope.Escape(result.ToLocalChecked());
}
inline
Nan::MaybeLocal<v8::String> Stringify(v8::Local<v8::Object> json_object,
v8::Local<v8::String> gap) {
Nan::EscapableHandleScope scope;
Nan::MaybeLocal<v8::String> result =
#if NAN_JSON_H_NEED_STRINGIFY
Nan::To<v8::String>(stringify(json_object, gap));
#else
v8::JSON::Stringify(Nan::GetCurrentContext(), json_object, gap);
#endif // NAN_JSON_H_NEED_STRINGIFY
if (result.IsEmpty()) return v8::Local<v8::String>();
return scope.Escape(result.ToLocalChecked());
}
private:
NAN_DISALLOW_ASSIGN_COPY_MOVE(JSON)
#if NAN_JSON_H_NEED_PARSE
Nan::Callback parse_cb_;
#endif // NAN_JSON_H_NEED_PARSE
#if NAN_JSON_H_NEED_STRINGIFY
Nan::Callback stringify_cb_;
#endif // NAN_JSON_H_NEED_STRINGIFY
#if NAN_JSON_H_NEED_PARSE
inline v8::Local<v8::Value> parse(v8::Local<v8::Value> arg) {
assert(!parse_cb_.IsEmpty() && "parse_cb_ is empty");
return parse_cb_.Call(1, &arg);
}
#endif // NAN_JSON_H_NEED_PARSE
#if NAN_JSON_H_NEED_STRINGIFY
inline v8::Local<v8::Value> stringify(v8::Local<v8::Value> arg) {
assert(!stringify_cb_.IsEmpty() && "stringify_cb_ is empty");
return stringify_cb_.Call(1, &arg);
}
inline v8::Local<v8::Value> stringify(v8::Local<v8::Value> arg,
v8::Local<v8::String> gap) {
assert(!stringify_cb_.IsEmpty() && "stringify_cb_ is empty");
v8::Local<v8::Value> argv[] = {
arg,
Nan::Null(),
gap
};
return stringify_cb_.Call(3, argv);
}
#endif // NAN_JSON_H_NEED_STRINGIFY
};
#endif // NAN_JSON_H_
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2017 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/
#ifndef NAN_MAYBE_PRE_43_INL_H_
#define NAN_MAYBE_PRE_43_INL_H_
template<typename T>
class MaybeLocal {
public:
inline MaybeLocal() : val_(v8::Local<T>()) {}
template<typename S>
# if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION
inline
MaybeLocal(v8::Local<S> that) : val_(that) {} // NOLINT(runtime/explicit)
# else
inline
MaybeLocal(v8::Local<S> that) : // NOLINT(runtime/explicit)
val_(*reinterpret_cast<v8::Local<T>*>(&that)) {}
# endif
inline bool IsEmpty() const { return val_.IsEmpty(); }
template<typename S>
inline bool ToLocal(v8::Local<S> *out) const {
*out = val_;
return !IsEmpty();
}
inline v8::Local<T> ToLocalChecked() const {
#if defined(V8_ENABLE_CHECKS)
assert(!IsEmpty() && "ToLocalChecked is Empty");
#endif // V8_ENABLE_CHECKS
return val_;
}
template<typename S>
inline v8::Local<S> FromMaybe(v8::Local<S> default_value) const {
return IsEmpty() ? default_value : v8::Local<S>(val_);
}
private:
v8::Local<T> val_;
};
template<typename T>
class Maybe {
public:
inline bool IsNothing() const { return !has_value_; }
inline bool IsJust() const { return has_value_; }
inline T FromJust() const {
#if defined(V8_ENABLE_CHECKS)
assert(IsJust() && "FromJust is Nothing");
#endif // V8_ENABLE_CHECKS
return value_;
}
inline T FromMaybe(const T& default_value) const {
return has_value_ ? value_ : default_value;
}
inline bool operator==(const Maybe &other) const {
return (IsJust() == other.IsJust()) &&
(!IsJust() || FromJust() == other.FromJust());
}
inline bool operator!=(const Maybe &other) const {
return !operator==(other);
}
private:
Maybe() : has_value_(false) {}
explicit Maybe(const T& t) : has_value_(true), value_(t) {}
bool has_value_;
T value_;
template<typename U>
friend Maybe<U> Nothing();
template<typename U>
friend Maybe<U> Just(const U& u);
};
template<typename T>
inline Maybe<T> Nothing() {
return Maybe<T>();
}
template<typename T>
inline Maybe<T> Just(const T& t) {
return Maybe<T>(t);
}
inline
MaybeLocal<v8::String> ToDetailString(v8::Handle<v8::Value> val) {
return MaybeLocal<v8::String>(val->ToDetailString());
}
inline
MaybeLocal<v8::Uint32> ToArrayIndex(v8::Handle<v8::Value> val) {
return MaybeLocal<v8::Uint32>(val->ToArrayIndex());
}
inline
Maybe<bool> Equals(v8::Handle<v8::Value> a, v8::Handle<v8::Value>(b)) {
return Just<bool>(a->Equals(b));
}
inline
MaybeLocal<v8::Object> NewInstance(v8::Handle<v8::Function> h) {
return MaybeLocal<v8::Object>(h->NewInstance());
}
inline
MaybeLocal<v8::Object> NewInstance(
v8::Local<v8::Function> h
, int argc
, v8::Local<v8::Value> argv[]) {
return MaybeLocal<v8::Object>(h->NewInstance(argc, argv));
}
inline
MaybeLocal<v8::Object> NewInstance(v8::Handle<v8::ObjectTemplate> h) {
return MaybeLocal<v8::Object>(h->NewInstance());
}
inline
MaybeLocal<v8::Function> GetFunction(v8::Handle<v8::FunctionTemplate> t) {
return MaybeLocal<v8::Function>(t->GetFunction());
}
inline Maybe<bool> Set(
v8::Handle<v8::Object> obj
, v8::Handle<v8::Value> key
, v8::Handle<v8::Value> value) {
return Just<bool>(obj->Set(key, value));
}
inline Maybe<bool> Set(
v8::Handle<v8::Object> obj
, uint32_t index
, v8::Handle<v8::Value> value) {
return Just<bool>(obj->Set(index, value));
}
inline Maybe<bool> ForceSet(
v8::Handle<v8::Object> obj
, v8::Handle<v8::Value> key
, v8::Handle<v8::Value> value
, v8::PropertyAttribute attribs = v8::None) {
return Just<bool>(obj->ForceSet(key, value, attribs));
}
inline MaybeLocal<v8::Value> Get(
v8::Handle<v8::Object> obj
, v8::Handle<v8::Value> key) {
return MaybeLocal<v8::Value>(obj->Get(key));
}
inline MaybeLocal<v8::Value> Get(
v8::Handle<v8::Object> obj
, uint32_t index) {
return MaybeLocal<v8::Value>(obj->Get(index));
}
inline Maybe<v8::PropertyAttribute> GetPropertyAttributes(
v8::Handle<v8::Object> obj
, v8::Handle<v8::Value> key) {
return Just<v8::PropertyAttribute>(obj->GetPropertyAttributes(key));
}
inline Maybe<bool> Has(
v8::Handle<v8::Object> obj
, v8::Handle<v8::String> key) {
return Just<bool>(obj->Has(key));
}
inline Maybe<bool> Has(
v8::Handle<v8::Object> obj
, uint32_t index) {
return Just<bool>(obj->Has(index));
}
inline Maybe<bool> Delete(
v8::Handle<v8::Object> obj
, v8::Handle<v8::String> key) {
return Just<bool>(obj->Delete(key));
}
inline Maybe<bool> Delete(
v8::Handle<v8::Object> obj
, uint32_t index) {
return Just<bool>(obj->Delete(index));
}
inline
MaybeLocal<v8::Array> GetPropertyNames(v8::Handle<v8::Object> obj) {
return MaybeLocal<v8::Array>(obj->GetPropertyNames());
}
inline
MaybeLocal<v8::Array> GetOwnPropertyNames(v8::Handle<v8::Object> obj) {
return MaybeLocal<v8::Array>(obj->GetOwnPropertyNames());
}
inline Maybe<bool> SetPrototype(
v8::Handle<v8::Object> obj
, v8::Handle<v8::Value> prototype) {
return Just<bool>(obj->SetPrototype(prototype));
}
inline MaybeLocal<v8::String> ObjectProtoToString(
v8::Handle<v8::Object> obj) {
return MaybeLocal<v8::String>(obj->ObjectProtoToString());
}
inline Maybe<bool> HasOwnProperty(
v8::Handle<v8::Object> obj
, v8::Handle<v8::String> key) {
return Just<bool>(obj->HasOwnProperty(key));
}
inline Maybe<bool> HasRealNamedProperty(
v8::Handle<v8::Object> obj
, v8::Handle<v8::String> key) {
return Just<bool>(obj->HasRealNamedProperty(key));
}
inline Maybe<bool> HasRealIndexedProperty(
v8::Handle<v8::Object> obj
, uint32_t index) {
return Just<bool>(obj->HasRealIndexedProperty(index));
}
inline Maybe<bool> HasRealNamedCallbackProperty(
v8::Handle<v8::Object> obj
, v8::Handle<v8::String> key) {
return Just<bool>(obj->HasRealNamedCallbackProperty(key));
}
inline MaybeLocal<v8::Value> GetRealNamedPropertyInPrototypeChain(
v8::Handle<v8::Object> obj
, v8::Handle<v8::String> key) {
return MaybeLocal<v8::Value>(
obj->GetRealNamedPropertyInPrototypeChain(key));
}
inline MaybeLocal<v8::Value> GetRealNamedProperty(
v8::Handle<v8::Object> obj
, v8::Handle<v8::String> key) {
return MaybeLocal<v8::Value>(obj->GetRealNamedProperty(key));
}
inline MaybeLocal<v8::Value> CallAsFunction(
v8::Handle<v8::Object> obj
, v8::Handle<v8::Object> recv
, int argc
, v8::Handle<v8::Value> argv[]) {
return MaybeLocal<v8::Value>(obj->CallAsFunction(recv, argc, argv));
}
inline MaybeLocal<v8::Value> CallAsConstructor(
v8::Handle<v8::Object> obj
, int argc
, v8::Local<v8::Value> argv[]) {
return MaybeLocal<v8::Value>(obj->CallAsConstructor(argc, argv));
}
inline
MaybeLocal<v8::String> GetSourceLine(v8::Handle<v8::Message> msg) {
return MaybeLocal<v8::String>(msg->GetSourceLine());
}
inline Maybe<int> GetLineNumber(v8::Handle<v8::Message> msg) {
return Just<int>(msg->GetLineNumber());
}
inline Maybe<int> GetStartColumn(v8::Handle<v8::Message> msg) {
return Just<int>(msg->GetStartColumn());
}
inline Maybe<int> GetEndColumn(v8::Handle<v8::Message> msg) {
return Just<int>(msg->GetEndColumn());
}
inline MaybeLocal<v8::Object> CloneElementAt(
v8::Handle<v8::Array> array
, uint32_t index) {
return MaybeLocal<v8::Object>(array->CloneElementAt(index));
}
inline MaybeLocal<v8::Value> Call(
v8::Local<v8::Function> fun
, v8::Local<v8::Object> recv
, int argc
, v8::Local<v8::Value> argv[]) {
return MaybeLocal<v8::Value>(fun->Call(recv, argc, argv));
}
#endif // NAN_MAYBE_PRE_43_INL_H_
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2017 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/
#ifndef NAN_NEW_H_
#define NAN_NEW_H_
namespace imp { // scnr
// TODO(agnat): Generalize
template <typename T> v8::Local<T> To(v8::Local<v8::Integer> i);
template <>
inline
v8::Local<v8::Integer>
To<v8::Integer>(v8::Local<v8::Integer> i) {
return Nan::To<v8::Integer>(i).ToLocalChecked();
}
template <>
inline
v8::Local<v8::Int32>
To<v8::Int32>(v8::Local<v8::Integer> i) {
return Nan::To<v8::Int32>(i).ToLocalChecked();
}
template <>
inline
v8::Local<v8::Uint32>
To<v8::Uint32>(v8::Local<v8::Integer> i) {
return Nan::To<v8::Uint32>(i).ToLocalChecked();
}
template <typename T> struct FactoryBase {
typedef v8::Local<T> return_t;
};
template <typename T> struct MaybeFactoryBase {
typedef MaybeLocal<T> return_t;
};
template <typename T> struct Factory;
template <>
struct Factory<v8::Array> : FactoryBase<v8::Array> {
static inline return_t New();
static inline return_t New(int length);
};
template <>
struct Factory<v8::Boolean> : FactoryBase<v8::Boolean> {
static inline return_t New(bool value);
};
template <>
struct Factory<v8::BooleanObject> : FactoryBase<v8::BooleanObject> {
static inline return_t New(bool value);
};
template <>
struct Factory<v8::Context> : FactoryBase<v8::Context> {
static inline
return_t
New( v8::ExtensionConfiguration* extensions = NULL
, v8::Local<v8::ObjectTemplate> tmpl = v8::Local<v8::ObjectTemplate>()
, v8::Local<v8::Value> obj = v8::Local<v8::Value>());
};
template <>
struct Factory<v8::Date> : MaybeFactoryBase<v8::Date> {
static inline return_t New(double value);
};
template <>
struct Factory<v8::External> : FactoryBase<v8::External> {
static inline return_t New(void *value);
};
template <>
struct Factory<v8::Function> : FactoryBase<v8::Function> {
static inline
return_t
New( FunctionCallback callback
, v8::Local<v8::Value> data = v8::Local<v8::Value>());
};
template <>
struct Factory<v8::FunctionTemplate> : FactoryBase<v8::FunctionTemplate> {
static inline
return_t
New( FunctionCallback callback = NULL
, v8::Local<v8::Value> data = v8::Local<v8::Value>()
, v8::Local<v8::Signature> signature = v8::Local<v8::Signature>());
};
template <>
struct Factory<v8::Number> : FactoryBase<v8::Number> {
static inline return_t New(double value);
};
template <>
struct Factory<v8::NumberObject> : FactoryBase<v8::NumberObject> {
static inline return_t New(double value);
};
template <typename T>
struct IntegerFactory : FactoryBase<T> {
typedef typename FactoryBase<T>::return_t return_t;
static inline return_t New(int32_t value);
static inline return_t New(uint32_t value);
};
template <>
struct Factory<v8::Integer> : IntegerFactory<v8::Integer> {};
template <>
struct Factory<v8::Int32> : IntegerFactory<v8::Int32> {};
template <>
struct Factory<v8::Uint32> : FactoryBase<v8::Uint32> {
static inline return_t New(int32_t value);
static inline return_t New(uint32_t value);
};
template <>
struct Factory<v8::Object> : FactoryBase<v8::Object> {
static inline return_t New();
};
template <>
struct Factory<v8::ObjectTemplate> : FactoryBase<v8::ObjectTemplate> {
static inline return_t New();
};
template <>
struct Factory<v8::RegExp> : MaybeFactoryBase<v8::RegExp> {
static inline return_t New(
v8::Local<v8::String> pattern, v8::RegExp::Flags flags);
};
template <>
struct Factory<v8::Script> : MaybeFactoryBase<v8::Script> {
static inline return_t New( v8::Local<v8::String> source);
static inline return_t New( v8::Local<v8::String> source
, v8::ScriptOrigin const& origin);
};
template <>
struct Factory<v8::Signature> : FactoryBase<v8::Signature> {
typedef v8::Local<v8::FunctionTemplate> FTH;
static inline return_t New(FTH receiver = FTH());
};
template <>
struct Factory<v8::String> : MaybeFactoryBase<v8::String> {
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);
static inline return_t New(std::string const& value);
static inline return_t New(v8::String::ExternalStringResource * value);
static inline return_t New(ExternalOneByteStringResource * value);
};
template <>
struct Factory<v8::StringObject> : FactoryBase<v8::StringObject> {
static inline return_t New(v8::Local<v8::String> value);
};
} // end of namespace imp
#if (NODE_MODULE_VERSION >= 12)
namespace imp {
template <>
struct Factory<v8::UnboundScript> : MaybeFactoryBase<v8::UnboundScript> {
static inline return_t New( v8::Local<v8::String> source);
static inline return_t New( v8::Local<v8::String> source
, v8::ScriptOrigin const& origin);
};
} // end of namespace imp
# include "nan_implementation_12_inl.h"
#else // NODE_MODULE_VERSION >= 12
# include "nan_implementation_pre_12_inl.h"
#endif
//=== API ======================================================================
template <typename T>
typename imp::Factory<T>::return_t
New() {
return imp::Factory<T>::New();
}
template <typename T, typename A0>
typename imp::Factory<T>::return_t
New(A0 arg0) {
return imp::Factory<T>::New(arg0);
}
template <typename T, typename A0, typename A1>
typename imp::Factory<T>::return_t
New(A0 arg0, A1 arg1) {
return imp::Factory<T>::New(arg0, arg1);
}
template <typename T, typename A0, typename A1, typename A2>
typename imp::Factory<T>::return_t
New(A0 arg0, A1 arg1, A2 arg2) {
return imp::Factory<T>::New(arg0, arg1, arg2);
}
template <typename T, typename A0, typename A1, typename A2, typename A3>
typename imp::Factory<T>::return_t
New(A0 arg0, A1 arg1, A2 arg2, A3 arg3) {
return imp::Factory<T>::New(arg0, arg1, arg2, arg3);
}
// Note(agnat): When passing overloaded function pointers to template functions
// as generic arguments the compiler needs help in picking the right overload.
// These two functions handle New<Function> and New<FunctionTemplate> with
// all argument variations.
// v8::Function and v8::FunctionTemplate with one or two arguments
template <typename T>
typename imp::Factory<T>::return_t
New( FunctionCallback callback
, v8::Local<v8::Value> data = v8::Local<v8::Value>()) {
return imp::Factory<T>::New(callback, data);
}
// v8::Function and v8::FunctionTemplate with three arguments
template <typename T, typename A2>
typename imp::Factory<T>::return_t
New( FunctionCallback callback
, v8::Local<v8::Value> data = v8::Local<v8::Value>()
, A2 a2 = A2()) {
return imp::Factory<T>::New(callback, data, a2);
}
// Convenience
#if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
template <typename T> inline v8::Local<T> New(v8::Handle<T> h);
#endif
#if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
template <typename T, typename M>
inline v8::Local<T> New(v8::Persistent<T, M> const& p);
#else
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
New(bool value) {
return New<v8::Boolean>(value);
}
inline
imp::Factory<v8::Int32>::return_t
New(int32_t value) {
return New<v8::Int32>(value);
}
inline
imp::Factory<v8::Uint32>::return_t
New(uint32_t value) {
return New<v8::Uint32>(value);
}
inline
imp::Factory<v8::Number>::return_t
New(double value) {
return New<v8::Number>(value);
}
inline
imp::Factory<v8::String>::return_t
New(std::string const& value) { // NOLINT(build/include_what_you_use)
return New<v8::String>(value);
}
inline
imp::Factory<v8::String>::return_t
New(const char * value, int length) {
return New<v8::String>(value, 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);
}
inline
imp::Factory<v8::String>::return_t
New(const uint16_t * value) {
return New<v8::String>(value);
}
inline
imp::Factory<v8::String>::return_t
New(v8::String::ExternalStringResource * value) {
return New<v8::String>(value);
}
inline
imp::Factory<v8::String>::return_t
New(ExternalOneByteStringResource * value) {
return New<v8::String>(value);
}
inline
imp::Factory<v8::RegExp>::return_t
New(v8::Local<v8::String> pattern, v8::RegExp::Flags flags) {
return New<v8::RegExp>(pattern, flags);
}
#endif // NAN_NEW_H_
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2017 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/
#ifndef NAN_OBJECT_WRAP_H_
#define NAN_OBJECT_WRAP_H_
class ObjectWrap {
public:
ObjectWrap() {
refs_ = 0;
}
virtual ~ObjectWrap() {
if (persistent().IsEmpty()) {
return;
}
assert(persistent().IsNearDeath());
persistent().ClearWeak();
persistent().Reset();
}
template <class T>
static inline T* Unwrap(v8::Local<v8::Object> object) {
assert(!object.IsEmpty());
assert(object->InternalFieldCount() > 0);
// Cast to ObjectWrap before casting to T. A direct cast from void
// to T won't work right when T has more than one base class.
void* ptr = GetInternalFieldPointer(object, 0);
ObjectWrap* wrap = static_cast<ObjectWrap*>(ptr);
return static_cast<T*>(wrap);
}
inline v8::Local<v8::Object> handle() const {
return New(handle_);
}
inline Persistent<v8::Object>& persistent() {
return handle_;
}
protected:
inline void Wrap(v8::Local<v8::Object> object) {
assert(persistent().IsEmpty());
assert(object->InternalFieldCount() > 0);
SetInternalFieldPointer(object, 0, this);
persistent().Reset(object);
MakeWeak();
}
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
inline void MakeWeak() {
persistent().v8::PersistentBase<v8::Object>::SetWeak(
this, WeakCallback, v8::WeakCallbackType::kParameter);
persistent().MarkIndependent();
}
#elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
inline void MakeWeak() {
persistent().v8::PersistentBase<v8::Object>::SetWeak(this, WeakCallback);
persistent().MarkIndependent();
}
#else
inline void MakeWeak() {
persistent().persistent.MakeWeak(this, WeakCallback);
persistent().MarkIndependent();
}
#endif
/* Ref() marks the object as being attached to an event loop.
* Refed objects will not be garbage collected, even if
* all references are lost.
*/
virtual void Ref() {
assert(!persistent().IsEmpty());
persistent().ClearWeak();
refs_++;
}
/* Unref() marks an object as detached from the event loop. This is its
* default state. When an object with a "weak" reference changes from
* attached to detached state it will be freed. Be careful not to access
* the object after making this call as it might be gone!
* (A "weak reference" means an object that only has a
* persistant handle.)
*
* DO NOT CALL THIS FROM DESTRUCTOR
*/
virtual void Unref() {
assert(!persistent().IsEmpty());
assert(!persistent().IsWeak());
assert(refs_ > 0);
if (--refs_ == 0)
MakeWeak();
}
int refs_; // ro
private:
NAN_DISALLOW_ASSIGN_COPY_MOVE(ObjectWrap)
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
static void
WeakCallback(v8::WeakCallbackInfo<ObjectWrap> const& info) {
ObjectWrap* wrap = info.GetParameter();
assert(wrap->refs_ == 0);
assert(wrap->handle_.IsNearDeath());
wrap->handle_.Reset();
delete wrap;
}
#elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
static void
WeakCallback(v8::WeakCallbackData<v8::Object, ObjectWrap> const& data) {
ObjectWrap* wrap = data.GetParameter();
assert(wrap->refs_ == 0);
assert(wrap->handle_.IsNearDeath());
wrap->handle_.Reset();
delete wrap;
}
#else
static void WeakCallback(v8::Persistent<v8::Value> value, void *data) {
ObjectWrap *wrap = static_cast<ObjectWrap*>(data);
assert(wrap->refs_ == 0);
assert(wrap->handle_.IsNearDeath());
wrap->handle_.Reset();
delete wrap;
}
#endif
Persistent<v8::Object> handle_;
};
#endif // NAN_OBJECT_WRAP_H_
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2017 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/
#ifndef NAN_PERSISTENT_12_INL_H_
#define NAN_PERSISTENT_12_INL_H_
template<typename T, typename M> class Persistent :
public v8::Persistent<T, M> {
public:
inline Persistent() : v8::Persistent<T, M>() {}
template<typename S> inline Persistent(v8::Local<S> that) :
v8::Persistent<T, M>(v8::Isolate::GetCurrent(), that) {}
template<typename S, typename M2>
inline
Persistent(const v8::Persistent<S, M2> &that) : // NOLINT(runtime/explicit)
v8::Persistent<T, M2>(v8::Isolate::GetCurrent(), that) {}
inline void Reset() { v8::PersistentBase<T>::Reset(); }
template <typename S>
inline void Reset(const v8::Local<S> &other) {
v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
}
template <typename S>
inline void Reset(const v8::PersistentBase<S> &other) {
v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
}
template<typename P>
inline void SetWeak(
P *parameter
, typename WeakCallbackInfo<P>::Callback callback
, WeakCallbackType type);
private:
inline T *operator*() const { return *PersistentBase<T>::persistent; }
template<typename S, typename M2>
inline void Copy(const Persistent<S, M2> &that) {
TYPE_CHECK(T, S);
this->Reset();
if (!that.IsEmpty()) {
this->Reset(that);
M::Copy(that, this);
}
}
};
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
template<typename T>
class Global : public v8::Global<T> {
public:
inline Global() : v8::Global<T>() {}
template<typename S> inline Global(v8::Local<S> that) :
v8::Global<T>(v8::Isolate::GetCurrent(), that) {}
template<typename S>
inline
Global(const v8::PersistentBase<S> &that) : // NOLINT(runtime/explicit)
v8::Global<S>(v8::Isolate::GetCurrent(), that) {}
inline void Reset() { v8::PersistentBase<T>::Reset(); }
template <typename S>
inline void Reset(const v8::Local<S> &other) {
v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
}
template <typename S>
inline void Reset(const v8::PersistentBase<S> &other) {
v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
}
template<typename P>
inline void SetWeak(
P *parameter
, typename WeakCallbackInfo<P>::Callback callback
, WeakCallbackType type) {
reinterpret_cast<Persistent<T>*>(this)->SetWeak(
parameter, callback, type);
}
};
#else
template<typename T>
class Global : public v8::UniquePersistent<T> {
public:
inline Global() : v8::UniquePersistent<T>() {}
template<typename S> inline Global(v8::Local<S> that) :
v8::UniquePersistent<T>(v8::Isolate::GetCurrent(), that) {}
template<typename S>
inline
Global(const v8::PersistentBase<S> &that) : // NOLINT(runtime/explicit)
v8::UniquePersistent<S>(v8::Isolate::GetCurrent(), that) {}
inline void Reset() { v8::PersistentBase<T>::Reset(); }
template <typename S>
inline void Reset(const v8::Local<S> &other) {
v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
}
template <typename S>
inline void Reset(const v8::PersistentBase<S> &other) {
v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
}
template<typename P>
inline void SetWeak(
P *parameter
, typename WeakCallbackInfo<P>::Callback callback
, WeakCallbackType type) {
reinterpret_cast<Persistent<T>*>(this)->SetWeak(
parameter, callback, type);
}
};
#endif
#endif // NAN_PERSISTENT_12_INL_H_
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2017 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/
#ifndef NAN_PERSISTENT_PRE_12_INL_H_
#define NAN_PERSISTENT_PRE_12_INL_H_
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:
inline PersistentBase() :
persistent() {}
inline void Reset() {
persistent.Dispose();
persistent.Clear();
}
template<typename S>
inline void Reset(const v8::Local<S> &other) {
TYPE_CHECK(T, S);
if (!persistent.IsEmpty()) {
persistent.Dispose();
}
if (other.IsEmpty()) {
persistent.Clear();
} else {
persistent = v8::Persistent<T>::New(other);
}
}
template<typename S>
inline void Reset(const PersistentBase<S> &other) {
TYPE_CHECK(T, S);
if (!persistent.IsEmpty()) {
persistent.Dispose();
}
if (other.IsEmpty()) {
persistent.Clear();
} else {
persistent = v8::Persistent<T>::New(other.persistent);
}
}
inline bool IsEmpty() const { return persistent.IsEmpty(); }
inline void Empty() { persistent.Clear(); }
template<typename S>
inline bool operator==(const PersistentBase<S> &that) const {
return this->persistent == that.persistent;
}
template<typename S>
inline bool operator==(const v8::Local<S> &that) const {
return this->persistent == that;
}
template<typename S>
inline bool operator!=(const PersistentBase<S> &that) const {
return !operator==(that);
}
template<typename S>
inline bool operator!=(const v8::Local<S> &that) const {
return !operator==(that);
}
template<typename P>
inline void SetWeak(
P *parameter
, typename WeakCallbackInfo<P>::Callback callback
, WeakCallbackType type);
inline void ClearWeak() { persistent.ClearWeak(); }
inline void MarkIndependent() { persistent.MarkIndependent(); }
inline bool IsIndependent() const { return persistent.IsIndependent(); }
inline bool IsNearDeath() const { return persistent.IsNearDeath(); }
inline bool IsWeak() const { return persistent.IsWeak(); }
private:
inline explicit PersistentBase(v8::Persistent<T> that) :
persistent(that) { }
inline explicit PersistentBase(T *val) : persistent(val) {}
template<typename S, typename M> friend class Persistent;
template<typename S> friend class Global;
friend class ObjectWrap;
};
template<typename T>
class NonCopyablePersistentTraits {
public:
typedef Persistent<T, NonCopyablePersistentTraits<T> >
NonCopyablePersistent;
static const bool kResetInDestructor = false;
template<typename S, typename M>
inline static void Copy(const Persistent<S, M> &source,
NonCopyablePersistent *dest) {
Uncompilable<v8::Object>();
}
template<typename O> inline static void Uncompilable() {
TYPE_CHECK(O, v8::Primitive);
}
};
template<typename T>
struct CopyablePersistentTraits {
typedef Persistent<T, CopyablePersistentTraits<T> > CopyablePersistent;
static const bool kResetInDestructor = true;
template<typename S, typename M>
static inline void Copy(const Persistent<S, M> &source,
CopyablePersistent *dest) {}
};
template<typename T, typename M> class Persistent :
public PersistentBase<T> {
public:
inline Persistent() {}
template<typename S> inline Persistent(v8::Handle<S> that)
: PersistentBase<T>(v8::Persistent<T>::New(that)) {
TYPE_CHECK(T, S);
}
inline Persistent(const Persistent &that) : PersistentBase<T>() {
Copy(that);
}
template<typename S, typename M2>
inline Persistent(const Persistent<S, M2> &that) :
PersistentBase<T>() {
Copy(that);
}
inline Persistent &operator=(const Persistent &that) {
Copy(that);
return *this;
}
template <class S, class M2>
inline Persistent &operator=(const Persistent<S, M2> &that) {
Copy(that);
return *this;
}
inline ~Persistent() {
if (M::kResetInDestructor) this->Reset();
}
private:
inline T *operator*() const { return *PersistentBase<T>::persistent; }
template<typename S, typename M2>
inline void Copy(const Persistent<S, M2> &that) {
TYPE_CHECK(T, S);
this->Reset();
if (!that.IsEmpty()) {
this->persistent = v8::Persistent<T>::New(that.persistent);
M::Copy(that, this);
}
}
};
template<typename T>
class Global : public PersistentBase<T> {
struct RValue {
inline explicit RValue(Global* obj) : object(obj) {}
Global* object;
};
public:
inline Global() : PersistentBase<T>(0) { }
template <typename S>
inline Global(v8::Local<S> that) // NOLINT(runtime/explicit)
: PersistentBase<T>(v8::Persistent<T>::New(that)) {
TYPE_CHECK(T, S);
}
template <typename S>
inline Global(const PersistentBase<S> &that) // NOLINT(runtime/explicit)
: PersistentBase<T>(that) {
TYPE_CHECK(T, S);
}
/**
* Move constructor.
*/
inline Global(RValue rvalue) // NOLINT(runtime/explicit)
: PersistentBase<T>(rvalue.object->persistent) {
rvalue.object->Reset();
}
inline ~Global() { this->Reset(); }
/**
* Move via assignment.
*/
template<typename S>
inline Global &operator=(Global<S> rhs) {
TYPE_CHECK(T, S);
this->Reset(rhs.persistent);
rhs.Reset();
return *this;
}
/**
* Cast operator for moves.
*/
inline operator RValue() { return RValue(this); }
/**
* Pass allows returning uniques from functions, etc.
*/
Global Pass() { return Global(RValue(this)); }
private:
Global(Global &);
void operator=(Global &);
template<typename S> friend class ReturnValue;
};
#endif // NAN_PERSISTENT_PRE_12_INL_H_
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2017 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/
#ifndef NAN_PRIVATE_H_
#define NAN_PRIVATE_H_
inline Maybe<bool>
HasPrivate(v8::Local<v8::Object> object, v8::Local<v8::String> key) {
HandleScope scope;
#if NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Private> private_key = v8::Private::ForApi(isolate, key);
return object->HasPrivate(context, private_key);
#else
return Just(!object->GetHiddenValue(key).IsEmpty());
#endif
}
inline MaybeLocal<v8::Value>
GetPrivate(v8::Local<v8::Object> object, v8::Local<v8::String> key) {
#if NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::EscapableHandleScope scope(isolate);
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Private> private_key = v8::Private::ForApi(isolate, key);
v8::MaybeLocal<v8::Value> v = object->GetPrivate(context, private_key);
return scope.Escape(v.ToLocalChecked());
#else
EscapableHandleScope scope;
v8::Local<v8::Value> v = object->GetHiddenValue(key);
if (v.IsEmpty()) {
v = Undefined();
}
return scope.Escape(v);
#endif
}
inline Maybe<bool> SetPrivate(
v8::Local<v8::Object> object,
v8::Local<v8::String> key,
v8::Local<v8::Value> value) {
#if NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION
HandleScope scope;
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Private> private_key = v8::Private::ForApi(isolate, key);
return object->SetPrivate(context, private_key, value);
#else
return Just(object->SetHiddenValue(key, value));
#endif
}
inline Maybe<bool> DeletePrivate(
v8::Local<v8::Object> object,
v8::Local<v8::String> key) {
#if NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION
HandleScope scope;
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::Local<v8::Private> private_key = v8::Private::ForApi(isolate, key);
return object->DeletePrivate(isolate->GetCurrentContext(), private_key);
#else
return Just(object->DeleteHiddenValue(key));
#endif
}
#endif // NAN_PRIVATE_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment