chunk_id
large_stringlengths
36
36
doc_type
large_stringclasses
2 values
category
large_stringclasses
17 values
document
large_stringclasses
50 values
version_raw
large_stringclasses
590 values
chunk_index
int64
0
262
text
large_stringlengths
11
5.69k
file_path
large_stringclasses
882 values
638dffc7-f7b3-558c-9eb4-7f86675d21a9
docs
nodejs
nodejs_addons
4x
9
The [Native Abstractions for Node.js](https://github.com/nodejs/nan) (or `nan`) provide a set of tools that Addon developers are recommended to use to keep compatibility between past and future releases of V8 and Node.js. See the `nan` [examples](https://github.com/nodejs/nan/tree/master/examples/) for an illustration ...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
b2e3ad70-4eff-57dc-80e1-309f9278117b
docs
nodejs
nodejs_addons
4x
10
filename to the `sources` array. For example: ```None "sources": ["addon.cc", "myexample.cc"] ``` Once the `binding.gyp` file is ready, the example Addons can be configured and built using `node-gyp`: ```None $ node-gyp configure build ``` ### Function arguments[#](#addons_function_arguments) Addons will typically...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
b90d4833-75c5-50e4-818a-a45461ae7296
docs
nodejs
nodejs_addons
4x
11
void Add(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); // Check the number of arguments passed. if (args.Length() < 2) { // Throw an Error that is passed back to JavaScript isolate->ThrowException(Exception::TypeError( String::NewFromUtf8(isolate, "Wrong number ...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
a9a1f664-45b1-50fe-99dd-68e78d574c2a
docs
nodejs
nodejs_addons
4x
12
Once compiled, the example Addon can be required and used from within Node.js: ```None // test.js const addon = require('./build/Release/addon'); console.log('This should be eight:', addon.add(3, 5)); ``` ### Callbacks[#](#addons_callbacks) It is common practice within Addons to pass JavaScript functions to a C++ f...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
4fcf5733-0e88-5c97-969b-8250b96400b7
docs
nodejs
nodejs_addons
4x
13
NODE_SET_METHOD(module, "exports", RunCallback); } NODE_MODULE(addon, Init) } // namespace demo ``` Note that this example uses a two-argument form of `Init()` that receives the full `module` object as the second argument. This allows the Addon to completely overwrite `exports` with a single function instead of a...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
4bfe3110-8f02-5e69-9a7f-dd61e57c6833
docs
nodejs
nodejs_addons
4x
14
using v8::Object; using v8::String; using v8::Value; void CreateObject(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); Local<Object> obj = Object::New(isolate); obj->Set(String::NewFromUtf8(isolate, "msg"), args[0]->ToString()); args.GetReturnValue().Set(obj); } void Init(Lo...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
bb6f4d6e-4221-5b87-a216-4a0906a039e5
docs
nodejs
nodejs_addons
4x
15
using v8::Object; using v8::String; using v8::Value; void MyFunction(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello world")); } void CreateFunction(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.G...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
e196df53-6a7c-59d2-a777-659b00da4ec5
docs
nodejs
nodejs_addons
4x
16
### Wrapping C++ objects[#](#addons_wrapping_c_objects) It is also possible to wrap C++ objects/classes in a way that allows new instances to be created using the JavaScript `new` operator: ```None // addon.cc #include <node.h> #include "myobject.h" namespace demo { using v8::Local; using v8::Object; void InitAll(...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
71f3c350-04fc-53f0-b289-62cf668e39bf
docs
nodejs
nodejs_addons
4x
17
} // namespace demo #endif ``` In `myobject.cc`, implement the various methods that are to be exposed. Below, the method `plusOne()` is exposed by adding it to the constructor's prototype: ```None // myobject.cc #include "myobject.h" namespace demo { using v8::Context; using v8::Function; using v8::FunctionCallba...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
637dee03-21cf-54ec-a649-ba2504737a9f
docs
nodejs
nodejs_addons
4x
18
constructor.Reset(isolate, tpl->GetFunction()); exports->Set(String::NewFromUtf8(isolate, "MyObject"), tpl->GetFunction()); } void MyObject::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); if (args.IsConstructCall()) { // Invoked as constructor: `new MyO...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
f9030796-0bd7-57b3-a8ca-ab3de5979312
docs
nodejs
nodejs_addons
4x
19
Isolate* isolate = args.GetIsolate(); MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder()); obj->value_ += 1; args.GetReturnValue().Set(Number::New(isolate, obj->value_)); } } // namespace demo ``` To build this example, the `myobject.cc` file must be added to the `binding.gyp`: ```None { "target...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
557288bd-8d25-532c-9839-6a652026bd13
docs
nodejs
nodejs_addons
4x
20
First, the `createObject()` method is implemented in `addon.cc`: ```None // addon.cc #include <node.h> #include "myobject.h" namespace demo { using v8::FunctionCallbackInfo; using v8::Isolate; using v8::Local; using v8::Object; using v8::String; using v8::Value; void CreateObject(const FunctionCallbackInfo<Value>& ...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
78d3618a-f1c1-5742-93e8-029f00a148e7
docs
nodejs
nodejs_addons
4x
21
static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args); private: explicit MyObject(double value = 0); ~MyObject(); static void New(const v8::FunctionCallbackInfo<v8::Value>& args); static void PlusOne(const v8::FunctionCallbackInfo<v8::Value>& args); static v8::Persistent<v8::Function> ...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
499e0d80-989e-520e-b0e2-6e88bfb5ed68
docs
nodejs
nodejs_addons
4x
22
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New); tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject")); tpl->InstanceTemplate()->SetInternalFieldCount(1); // Prototype NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne); constructor.Reset(isolate, tpl->GetFunction()); } void MyObje...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
7119d4a8-ac25-5fd6-8aff-5359635a9dda
docs
nodejs
nodejs_addons
4x
23
cons->NewInstance(context, argc, argv).ToLocalChecked(); args.GetReturnValue().Set(instance); } } void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); const unsigned argc = 1; Local<Value> argv[argc] = { args[0] }; Local<Function> cons = Loc...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
548af10a-593e-5947-8d12-20d0bc779534
docs
nodejs
nodejs_addons
4x
24
"addon.cc", "myobject.cc" ] } ] } ``` Test it with: ```None // test.js const createObject = require('./build/Release/addon'); var obj = createObject(10); console.log(obj.plusOne()); // 11 console.log(obj.plusOne()); // 12 console.log(obj.plusOne()); // 13 var obj2 = createObject(20); con...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
468b2fbc-f2fc-5cb8-a572-f637d0600345
docs
nodejs
nodejs_addons
4x
25
using v8::Value; void CreateObject(const FunctionCallbackInfo<Value>& args) { MyObject::NewInstance(args); } void Add(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); MyObject* obj1 = node::ObjectWrap::Unwrap<MyObject>( args[0]->ToObject()); MyObject* obj2 = node::Object...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
0dd60da2-db75-5381-812a-c614cc52c60c
docs
nodejs
nodejs_addons
4x
26
static void Init(v8::Isolate* isolate); static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args); inline double value() const { return value_; } private: explicit MyObject(double value = 0); ~MyObject(); static void New(const v8::FunctionCallbackInfo<v8::Value>& args); static v8::Persis...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
d40f9814-0cbe-5934-a335-e013f7b3f3cf
docs
nodejs
nodejs_addons
4x
27
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New); tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject")); tpl->InstanceTemplate()->SetInternalFieldCount(1); constructor.Reset(isolate, tpl->GetFunction()); } void MyObject::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate ...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
f8e03c10-acb1-54bc-8fa5-129b10f1f54f
docs
nodejs
nodejs_addons
4x
28
args.GetReturnValue().Set(instance); } } void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); const unsigned argc = 1; Local<Value> argv[argc] = { args[0] }; Local<Function> cons = Local<Function>::New(isolate, constructor); Local<Context> context...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
f39975ce-4576-5035-89f4-b3e6e47e3546
docs
nodejs
nodejs_addons
4x
29
#### void AtExit(callback, args)[#](#addons_void_atexit_callback_args) * `callback`: `void (*)(void*)` - A pointer to the function to call at exit. * `args`: `void*` - A pointer to pass to the callback at exit. Registers exit hooks that run after the event loop has ended but before the VM is killed. AtExit takes two...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
68ec2497-04e3-5178-84bf-7aa558e620d6
docs
nodejs
nodejs_addons
4x
30
assert(!obj.IsEmpty()); // assert VM is still alive assert(obj->IsObject()); at_exit_cb1_called++; } static void at_exit_cb2(void* arg) { assert(arg == static_cast<void*>(cookie)); at_exit_cb2_called++; } static void sanity_check(void*) { assert(at_exit_cb1_called == 1); assert(at_exit_cb2_called == 2);...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
bbb45ca7-a58a-596d-a610-e02b5b584de8
docs
nodejs
nodejs_addons
5x
0
# Addons# Source: https://nodejs.org/docs/latest-v5.x/api/addons.html Version: 5.x Node.js Addons are dynamically-linked shared objects, written in C or C++, that can be loaded into Node.js using the [`require()`](globals.html#globals_require) function, and used just as if they were an ordinary Node.js module. They a...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
9bcabe32-7a81-57f3-a3d4-b246dbe06c68
docs
nodejs
nodejs_addons
5x
1
threads and all of the asynchronous behaviors of the platform. It also serves as a cross-platform abstraction library, giving easy, POSIX-like access across all major operating systems to many common system tasks, such as interacting with the filesystem, sockets, timers and system events. libuv also provides ...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
a08ec40c-1002-583d-8295-0e19e0d69370
docs
nodejs
nodejs_addons
5x
2
OpenSSL. These other libraries are located in the `deps/` directory in the Node.js source tree. Only the V8 and OpenSSL symbols are purposefully re-exported by Node.js and may be used to various extents by Addons. See [Linking to Node.js' own dependencies](#addons_linking_to_node_js_own_dependencies) for additi...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
c56ca4cc-04ec-5359-96b9-f8cbe444d571
docs
nodejs
nodejs_addons
5x
3
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world")); } void init(Local<Object> exports) { NODE_SET_METHOD(exports, "hello", Method); } NODE_MODULE(addon, init) } // namespace demo ``` Note that all Node.js Addons must export an initialization function following the pattern: ```None void Initializ...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
249179ff-82d9-5538-ad10-dd3de6a6b26f
docs
nodejs
nodejs_addons
5x
4
using a JSON-like format. This file is used by [node-gyp](https://github.com/nodejs/node-gyp) -- a tool written specifically to compile Node.js Addons. ```None { "targets": [ { "target_name": "addon", "sources": [ "hello.cc" ] } ] } ``` *Note: A version of the `node-gyp` utility is bundled and...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
7ce85c09-f78b-53d3-9cb5-9b50437aaeff
docs
nodejs
nodejs_addons
5x
5
will generate either a `Makefile` (on Unix platforms) or a `vcxproj` file (on Windows) in the `build/` directory. Next, invoke the `node-gyp build` command to generate the compiled `addon.node` file. This will be put into the `build/Release/` directory. When using `npm install` to install a Node.js Addon, npm uses it...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
4e27a5b6-2fc1-55d5-b0f3-4ef14588ab03
docs
nodejs
nodejs_addons
5x
6
the [bindings](https://github.com/TooTallNate/node-bindings) package to load the compiled module. Note that while the `bindings` package implementation is more sophisticated in how it locates Addon modules, it is essentially using a try-catch pattern similar to: ```None try { return require('./build/Release/addon.n...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
496b5b80-5f5e-57de-954b-9616828ea307
docs
nodejs
nodejs_addons
5x
7
and download either the full source tarball or just the headers. If the full source is downloaded, Addons will have complete access to the full set of Node.js dependencies. However, if only the Node.js headers are downloaded, then only the symbols exported by Node.js will be available. * `node-gyp` can be run u...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
2d3859c1-488c-523f-ac98-23d13427caa7
docs
nodejs
nodejs_addons
5x
8
however, is that Node.js will first attempt to locate and load modules or JavaScript files that happen to share the same base name. For instance, if there is a file `addon.js` in the same directory as the binary `addon.node`, then [`require('addon')`](globals.html#globals_require) will give precedence to the `addon.js`...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
50905380-6dce-56aa-945d-2bcdd80d4c47
docs
nodejs
nodejs_addons
5x
9
The [Native Abstractions for Node.js](https://github.com/nodejs/nan) (or `nan`) provide a set of tools that Addon developers are recommended to use to keep compatibility between past and future releases of V8 and Node.js. See the `nan` [examples](https://github.com/nodejs/nan/tree/master/examples/) for an illustration ...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
25f38929-faac-50a1-932b-fdabd6e41494
docs
nodejs
nodejs_addons
5x
10
filename to the `sources` array. For example: ```None "sources": ["addon.cc", "myexample.cc"] ``` Once the `binding.gyp` file is ready, the example Addons can be configured and built using `node-gyp`: ```None $ node-gyp configure build ``` ### Function arguments[#](#addons_function_arguments) Addons will typically...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
7c326913-d476-52d8-93e0-5e72cb2609f9
docs
nodejs
nodejs_addons
5x
11
void Add(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); // Check the number of arguments passed. if (args.Length() < 2) { // Throw an Error that is passed back to JavaScript isolate->ThrowException(Exception::TypeError( String::NewFromUtf8(isolate, "Wrong number ...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
88152355-a5d8-521a-b4db-c3f8ca90c78e
docs
nodejs
nodejs_addons
5x
12
Once compiled, the example Addon can be required and used from within Node.js: ```None // test.js const addon = require('./build/Release/addon'); console.log('This should be eight:', addon.add(3, 5)); ``` ### Callbacks[#](#addons_callbacks) It is common practice within Addons to pass JavaScript functions to a C++ f...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
a39d3fcd-5976-579c-a082-1d5b9f0092a2
docs
nodejs
nodejs_addons
5x
13
NODE_SET_METHOD(module, "exports", RunCallback); } NODE_MODULE(addon, Init) } // namespace demo ``` Note that this example uses a two-argument form of `Init()` that receives the full `module` object as the second argument. This allows the Addon to completely overwrite `exports` with a single function instead of a...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
92b2fad9-2139-55be-8977-05c00d8cbd8a
docs
nodejs
nodejs_addons
5x
14
using v8::Object; using v8::String; using v8::Value; void CreateObject(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); Local<Object> obj = Object::New(isolate); obj->Set(String::NewFromUtf8(isolate, "msg"), args[0]->ToString()); args.GetReturnValue().Set(obj); } void Init(Lo...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
13fab351-760c-5fa8-b959-5bd86ac301b7
docs
nodejs
nodejs_addons
5x
15
using v8::Local; using v8::Object; using v8::String; using v8::Value; void MyFunction(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello world")); } void CreateFunction(const FunctionCallbackInfo<Value>& args) { Isolate*...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
fb63f519-7008-51f3-9b0b-785200de9a5c
docs
nodejs
nodejs_addons
5x
16
### Wrapping C++ objects[#](#addons_wrapping_c_objects) It is also possible to wrap C++ objects/classes in a way that allows new instances to be created using the JavaScript `new` operator: ```None // addon.cc #include <node.h> #include "myobject.h" namespace demo { using v8::Local; using v8::Object; void InitAll(...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
b9eacef3-511c-5b1c-87f6-94cc680810ac
docs
nodejs
nodejs_addons
5x
17
} // namespace demo #endif ``` In `myobject.cc`, implement the various methods that are to be exposed. Below, the method `plusOne()` is exposed by adding it to the constructor's prototype: ```None // myobject.cc #include "myobject.h" namespace demo { using v8::Function; using v8::FunctionCallbackInfo; using v8::F...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
df11edf6-b2d8-542b-9a0f-7f7efb80f5f7
docs
nodejs
nodejs_addons
5x
18
exports->Set(String::NewFromUtf8(isolate, "MyObject"), tpl->GetFunction()); } void MyObject::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); if (args.IsConstructCall()) { // Invoked as constructor: `new MyObject(...)` double value = args[0]->IsUndefine...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
a086a868-64d1-5aed-86c3-d25cf41554b6
docs
nodejs
nodejs_addons
5x
19
} // namespace demo ``` To build this example, the `myobject.cc` file must be added to the `binding.gyp`: ```None { "targets": [ { "target_name": "addon", "sources": [ "addon.cc", "myobject.cc" ] } ] } ``` Test it with: ```None // test.js const addon = require('./build...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
a57f12ff-15bb-5d92-924e-49c3ea15c4bc
docs
nodejs
nodejs_addons
5x
20
using v8::String; using v8::Value; void CreateObject(const FunctionCallbackInfo<Value>& args) { MyObject::NewInstance(args); } void InitAll(Local<Object> exports, Local<Object> module) { MyObject::Init(exports->GetIsolate()); NODE_SET_METHOD(module, "exports", CreateObject); } NODE_MODULE(addon, InitAll) } ...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
3cf65379-031c-5c0f-ae77-3fb04556e0f8
docs
nodejs
nodejs_addons
5x
21
static v8::Persistent<v8::Function> constructor; double value_; }; } // namespace demo #endif ``` The implementation in `myobject.cc` is similar to the previous example: ```None // myobject.cc #include <node.h> #include "myobject.h" namespace demo { using v8::Function; using v8::FunctionCallbackInfo; using v...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
95b364c9-d0af-5c24-b119-d59475d197c8
docs
nodejs
nodejs_addons
5x
22
void MyObject::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); if (args.IsConstructCall()) { // Invoked as constructor: `new MyObject(...)` double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue(); MyObject* obj = new MyObject(value); obj->Wrap(args.Th...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
e1856626-8df8-529a-9ebb-ce91eaade41b
docs
nodejs
nodejs_addons
5x
23
void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder()); obj->value_ += 1; args.GetReturnValue().Set(Number::New(isolate, obj->value_)); } } // namespace demo ``` Once again, to build this example, t...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
684d2cae-a11f-53cb-bd5b-59af3c96e309
docs
nodejs
nodejs_addons
5x
24
### Passing wrapped objects around[#](#addons_passing_wrapped_objects_around) In addition to wrapping and returning C++ objects, it is possible to pass wrapped objects around by unwrapping them with the Node.js helper function `node::ObjectWrap::Unwrap`. The following examples shows a function `add()` that can take tw...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
51115540-f7ac-5282-ba45-74785b942eb5
docs
nodejs
nodejs_addons
5x
25
args.GetReturnValue().Set(Number::New(isolate, sum)); } void InitAll(Local<Object> exports) { MyObject::Init(exports->GetIsolate()); NODE_SET_METHOD(exports, "createObject", CreateObject); NODE_SET_METHOD(exports, "add", Add); } NODE_MODULE(addon, InitAll) } // namespace demo ``` In `myobject.h`, a new pu...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
ec508fe9-7285-5f9a-bc7d-24fe202992c2
docs
nodejs
nodejs_addons
5x
26
The implementation of `myobject.cc` is similar to before: ```None // myobject.cc #include <node.h> #include "myobject.h" namespace demo { using v8::Function; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Isolate; using v8::Local; using v8::Object; using v8::Persistent; using v8::String; using...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
855b42c9-f0aa-53bf-892b-b4fb58dde060
docs
nodejs
nodejs_addons
5x
27
double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue(); MyObject* obj = new MyObject(value); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); } else { // Invoked as plain function `MyObject(...)`, turn into construct call. const int argc = 1; Local<Value> argv[ar...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
8a8fc124-ae12-5b4f-83cc-73a76b89f659
docs
nodejs
nodejs_addons
5x
28
var result = addon.add(obj1, obj2); console.log(result); // 30 ``` ### AtExit hooks[#](#addons_atexit_hooks) An "AtExit" hook is a function that is invoked after the Node.js event loop has ended by before the JavaScript VM is terminated and Node.js shuts down. "AtExit" hooks are registered using the `node::AtExit` A...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
757a8473-990a-5f6d-bdb7-e8a191bffb0d
docs
nodejs
nodejs_addons
5x
29
using v8::Isolate; using v8::Local; using v8::Object; static char cookie[] = "yum yum"; static int at_exit_cb1_called = 0; static int at_exit_cb2_called = 0; static void at_exit_cb1(void* arg) { Isolate* isolate = static_cast<Isolate*>(arg); HandleScope scope(isolate); Local<Object> obj = Object::New(isolate); ...
large_dataset/raw/docs/nodejs/addons/addons_5x.md
7a088261-4d23-557d-b9e5-d4086f00235a
docs
nodejs
nodejs_addons
6x
0
# C++ Addons# Source: https://nodejs.org/docs/latest-v6.x/api/addons.html Version: 6.x Node.js Addons are dynamically-linked shared objects, written in C++, that can be loaded into Node.js using the [`require()`](globals.html#globals_require) function, and used just as if they were an ordinary Node.js module. They ar...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
c8c9c3c8-6ba9-54a3-af67-87bb67c24e42
docs
nodejs
nodejs_addons
6x
1
threads and all of the asynchronous behaviors of the platform. It also serves as a cross-platform abstraction library, giving easy, POSIX-like access across all major operating systems to many common system tasks, such as interacting with the filesystem, sockets, timers and system events. libuv also provides ...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
47b5f827-04ab-582b-9900-aad18f23dd89
docs
nodejs
nodejs_addons
6x
2
OpenSSL. These other libraries are located in the `deps/` directory in the Node.js source tree. Only the V8 and OpenSSL symbols are purposefully re-exported by Node.js and may be used to various extents by Addons. See [Linking to Node.js' own dependencies](#addons_linking_to_node_js_own_dependencies) for additi...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
c0ebd1f9-fcff-5e92-a8fe-183c4881d44a
docs
nodejs
nodejs_addons
6x
3
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world")); } void init(Local<Object> exports) { NODE_SET_METHOD(exports, "hello", Method); } NODE_MODULE(addon, init) } // namespace demo ``` Note that all Node.js Addons must export an initialization function following the pattern: ```None void Initializ...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
853f6c61-ff88-5384-bbc7-2bc5e0719fd7
docs
nodejs
nodejs_addons
6x
4
using a JSON-like format. This file is used by [node-gyp](https://github.com/nodejs/node-gyp) — a tool written specifically to compile Node.js Addons. ```None { "targets": [ { "target_name": "addon", "sources": [ "hello.cc" ] } ] } ``` *Note: A version of the `node-gyp` utility is bundled and ...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
d4a4daa6-f741-5d01-a8a1-d8cc9f3f7d69
docs
nodejs
nodejs_addons
6x
5
will generate either a `Makefile` (on Unix platforms) or a `vcxproj` file (on Windows) in the `build/` directory. Next, invoke the `node-gyp build` command to generate the compiled `addon.node` file. This will be put into the `build/Release/` directory. When using `npm install` to install a Node.js Addon, npm uses it...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
7ee0d9c5-46ec-5808-ab69-d89cfe74c2cd
docs
nodejs
nodejs_addons
6x
6
it is compiled (i.e. sometimes it may be in `./build/Debug/`), Addons can use the [bindings](https://github.com/TooTallNate/node-bindings) package to load the compiled module. Note that while the `bindings` package implementation is more sophisticated in how it locates Addon modules, it is essentially using a try-catc...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
329ab7ba-5454-5bba-9cf0-3dfb94e4dfd3
docs
nodejs
nodejs_addons
6x
7
* When `node-gyp` runs, it will detect the specific release version of Node.js and download either the full source tarball or just the headers. If the full source is downloaded, Addons will have complete access to the full set of Node.js dependencies. However, if only the Node.js headers are downloaded, then on...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
112c5e4e-24e5-5b2f-a1ce-6d84bd95fdb8
docs
nodejs
nodejs_addons
6x
8
omitted and Node.js will still find and initialize the Addon. One caveat, however, is that Node.js will first attempt to locate and load modules or JavaScript files that happen to share the same base name. For instance, if there is a file `addon.js` in the same directory as the binary `addon.node`, then [`require('addo...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
b71a064d-d7be-5100-a4c8-1489b95f36fa
docs
nodejs
nodejs_addons
6x
9
changes but there is little that Node.js can do currently to ensure stability of the V8 APIs. The [Native Abstractions for Node.js](https://github.com/nodejs/nan) (or `nan`) provide a set of tools that Addon developers are recommended to use to keep compatibility between past and future releases of V8 and Node.js. See...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
ace68103-7d84-53f1-9664-f49b1a7576bf
docs
nodejs
nodejs_addons
6x
10
recompilation. Addons are built/packaged with the same approach/tools outlined in this document (node-gyp, etc.). The only difference is the set of APIs that are used by the native code. Instead of using the V8 or [Native Abstractions for Node.js](https://github.com/nodejs/nan) APIs, the functions available in the N-AP...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
eb9da10b-b6ac-5ac2-bfbf-45fce7abf579
docs
nodejs
nodejs_addons
6x
11
"sources": [ "addon.cc" ] } ] } ``` In cases where there is more than one `.cc` file, simply add the additional filename to the `sources` array. For example: ```None "sources": ["addon.cc", "myexample.cc"] ``` Once the `binding.gyp` file is ready, the example Addons can be configured and built using `nod...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
a681e588-8fd5-5ba6-8f18-201df3af813b
docs
nodejs
nodejs_addons
6x
12
// This is the implementation of the "add" method // Input arguments are passed using the // const FunctionCallbackInfo<Value>& args struct void Add(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); // Check the number of arguments passed. if (args.Length() < 2) { // Throw an E...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
55b79a67-5110-560c-8502-952dc9338d42
docs
nodejs
nodejs_addons
6x
13
NODE_SET_METHOD(exports, "add", Add); } NODE_MODULE(addon, Init) } // namespace demo ``` Once compiled, the example Addon can be required and used from within Node.js: ```None // test.js const addon = require('./build/Release/addon'); console.log('This should be eight:', addon.add(3, 5)); ``` ### Callbacks[#](...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
954a355d-f403-53d6-b3fe-f7b9b201ba45
docs
nodejs
nodejs_addons
6x
14
cb->Call(Null(isolate), argc, argv); } void Init(Local<Object> exports, Local<Object> module) { NODE_SET_METHOD(module, "exports", RunCallback); } NODE_MODULE(addon, Init) } // namespace demo ``` Note that this example uses a two-argument form of `Init()` that receives the full `module` object as the second ar...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
284ad3ee-f32b-5100-be99-ed78b93b39c2
docs
nodejs
nodejs_addons
6x
15
#include <node.h> namespace demo { using v8::FunctionCallbackInfo; using v8::Isolate; using v8::Local; using v8::Object; using v8::String; using v8::Value; void CreateObject(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); Local<Object> obj = Object::New(isolate); obj->Set(Stri...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
6f86d72d-dc43-536c-8a96-7bd572db6745
docs
nodejs
nodejs_addons
6x
16
using v8::Function; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Isolate; using v8::Local; using v8::Object; using v8::String; using v8::Value; void MyFunction(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); args.GetReturnValue().Set(String::NewFromUtf8(iso...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
f008414f-6bd8-5698-a213-5a515ccc43b4
docs
nodejs
nodejs_addons
6x
17
### Wrapping C++ objects[#](#addons_wrapping_c_objects) It is also possible to wrap C++ objects/classes in a way that allows new instances to be created using the JavaScript `new` operator: ```None // addon.cc #include <node.h> #include "myobject.h" namespace demo { using v8::Local; using v8::Object; void InitAll(...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
f630e507-45e1-5f6d-b3d8-cec49a41af66
docs
nodejs
nodejs_addons
6x
18
} // namespace demo #endif ``` In `myobject.cc`, implement the various methods that are to be exposed. Below, the method `plusOne()` is exposed by adding it to the constructor's prototype: ```None // myobject.cc #include "myobject.h" namespace demo { using v8::Context; using v8::Function; using v8::FunctionCallba...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
ce10f1d0-f25e-523d-9fad-513f7cc108f2
docs
nodejs
nodejs_addons
6x
19
constructor.Reset(isolate, tpl->GetFunction()); exports->Set(String::NewFromUtf8(isolate, "MyObject"), tpl->GetFunction()); } void MyObject::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); if (args.IsConstructCall()) { // Invoked as constructor: `new MyO...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
ed34350d-1a9a-5e37-8cc0-f825d838f3fc
docs
nodejs
nodejs_addons
6x
20
Isolate* isolate = args.GetIsolate(); MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder()); obj->value_ += 1; args.GetReturnValue().Set(Number::New(isolate, obj->value_)); } } // namespace demo ``` To build this example, the `myobject.cc` file must be added to the `binding.gyp`: ```None { "target...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
bdf22da8-84b3-577a-af5f-0d040ef82f06
docs
nodejs
nodejs_addons
6x
21
First, the `createObject()` method is implemented in `addon.cc`: ```None // addon.cc #include <node.h> #include "myobject.h" namespace demo { using v8::FunctionCallbackInfo; using v8::Isolate; using v8::Local; using v8::Object; using v8::String; using v8::Value; void CreateObject(const FunctionCallbackInfo<Value>& ...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
7649619d-e840-54e7-b2c2-72501938ec6d
docs
nodejs
nodejs_addons
6x
22
static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args); private: explicit MyObject(double value = 0); ~MyObject(); static void New(const v8::FunctionCallbackInfo<v8::Value>& args); static void PlusOne(const v8::FunctionCallbackInfo<v8::Value>& args); static v8::Persistent<v8::Function> ...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
1bf62471-5618-5e90-9fd4-5f5cc71c7a44
docs
nodejs
nodejs_addons
6x
23
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New); tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject")); tpl->InstanceTemplate()->SetInternalFieldCount(1); // Prototype NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne); constructor.Reset(isolate, tpl->GetFunction()); } void MyObje...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
af51d38a-0891-5c36-bf0a-16dfe978d5ef
docs
nodejs
nodejs_addons
6x
24
cons->NewInstance(context, argc, argv).ToLocalChecked(); args.GetReturnValue().Set(instance); } } void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); const unsigned argc = 1; Local<Value> argv[argc] = { args[0] }; Local<Function> cons = Loc...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
a19a4d97-71a7-53f6-bf2c-b8a3e86bb5ad
docs
nodejs
nodejs_addons
6x
25
"addon.cc", "myobject.cc" ] } ] } ``` Test it with: ```None // test.js const createObject = require('./build/Release/addon'); const obj = createObject(10); console.log(obj.plusOne()); // Prints: 11 console.log(obj.plusOne()); // Prints: 12 console.log(obj.plusOne()); // Prints: 13 const ...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
7d7ef7e9-651d-5b55-808c-89596b721975
docs
nodejs
nodejs_addons
6x
26
using v8::Number; using v8::Object; using v8::String; using v8::Value; void CreateObject(const FunctionCallbackInfo<Value>& args) { MyObject::NewInstance(args); } void Add(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); MyObject* obj1 = node::ObjectWrap::Unwrap<MyObject>( ...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
8e163a52-0559-592e-b119-e7eba04553c4
docs
nodejs
nodejs_addons
6x
27
class MyObject : public node::ObjectWrap { public: static void Init(v8::Isolate* isolate); static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args); inline double value() const { return value_; } private: explicit MyObject(double value = 0); ~MyObject(); static void New(const v8::Functio...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
49faf07e-d2e9-5aa7-af51-577399b9ce31
docs
nodejs
nodejs_addons
6x
28
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New); tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject")); tpl->InstanceTemplate()->SetInternalFieldCount(1); constructor.Reset(isolate, tpl->GetFunction()); } void MyObject::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate ...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
01ab58d5-1b55-5ec5-84f6-448bf28d4592
docs
nodejs
nodejs_addons
6x
29
args.GetReturnValue().Set(instance); } } void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); const unsigned argc = 1; Local<Value> argv[argc] = { args[0] }; Local<Function> cons = Local<Function>::New(isolate, constructor); Local<Context> context...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
4b7178eb-36c8-571e-ae63-98a6810f3acc
docs
nodejs
nodejs_addons
6x
30
#### void AtExit(callback, args)[#](#addons_void_atexit_callback_args) * `callback` <void (\*)(void\*)> A pointer to the function to call at exit. * `args` <void\*> A pointer to pass to the callback at exit. Registers exit hooks that run after the event loop has ended but before the VM is killed. AtExit takes two pa...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
3560871e-4bfd-5ae2-be3a-8a1d54176e01
docs
nodejs
nodejs_addons
6x
31
assert(!obj.IsEmpty()); // assert VM is still alive assert(obj->IsObject()); at_exit_cb1_called++; } static void at_exit_cb2(void* arg) { assert(arg == static_cast<void*>(cookie)); at_exit_cb2_called++; } static void sanity_check(void*) { assert(at_exit_cb1_called == 1); assert(at_exit_cb2_called == 2);...
large_dataset/raw/docs/nodejs/addons/addons_6x.md
9e1439a8-504c-5663-b273-e55e4e979fdf
docs
nodejs
nodejs_addons
7x
0
# C/C++ Addons# Source: https://nodejs.org/docs/latest-v7.x/api/addons.html Version: 7.x Node.js Addons are dynamically-linked shared objects, written in C or C++, that can be loaded into Node.js using the [`require()`](globals.html#globals_require) function, and used just as if they were an ordinary Node.js module. ...
large_dataset/raw/docs/nodejs/addons/addons_7x.md
78c9cfa1-574e-5f80-bd6a-bb0cda234ee8
docs
nodejs
nodejs_addons
7x
1
threads and all of the asynchronous behaviors of the platform. It also serves as a cross-platform abstraction library, giving easy, POSIX-like access across all major operating systems to many common system tasks, such as interacting with the filesystem, sockets, timers and system events. libuv also provides ...
large_dataset/raw/docs/nodejs/addons/addons_7x.md
6442419d-2aac-5510-82fa-9155ddd3f902
docs
nodejs
nodejs_addons
7x
2
OpenSSL. These other libraries are located in the `deps/` directory in the Node.js source tree. Only the V8 and OpenSSL symbols are purposefully re-exported by Node.js and may be used to various extents by Addons. See [Linking to Node.js' own dependencies](#addons_linking_to_node_js_own_dependencies) for additi...
large_dataset/raw/docs/nodejs/addons/addons_7x.md
cda7aafe-9a0c-5949-99cc-d2ce7e4325fa
docs
nodejs
nodejs_addons
7x
3
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world")); } void init(Local<Object> exports) { NODE_SET_METHOD(exports, "hello", Method); } NODE_MODULE(addon, init) } // namespace demo ``` Note that all Node.js Addons must export an initialization function following the pattern: ```None void Initializ...
large_dataset/raw/docs/nodejs/addons/addons_7x.md
5faff948-c214-5ed0-8985-0dcf2a8ab298
docs
nodejs
nodejs_addons
7x
4
using a JSON-like format. This file is used by [node-gyp](https://github.com/nodejs/node-gyp) -- a tool written specifically to compile Node.js Addons. ```None { "targets": [ { "target_name": "addon", "sources": [ "hello.cc" ] } ] } ``` *Note: A version of the `node-gyp` utility is bundled and...
large_dataset/raw/docs/nodejs/addons/addons_7x.md
e72289a9-22ca-5fb2-889e-8f24d4b80f8f
docs
nodejs
nodejs_addons
7x
5
will generate either a `Makefile` (on Unix platforms) or a `vcxproj` file (on Windows) in the `build/` directory. Next, invoke the `node-gyp build` command to generate the compiled `addon.node` file. This will be put into the `build/Release/` directory. When using `npm install` to install a Node.js Addon, npm uses it...
large_dataset/raw/docs/nodejs/addons/addons_7x.md
609954b0-9013-5133-a05c-0ef2dfde2c1e
docs
nodejs
nodejs_addons
7x
6
it is compiled (i.e. sometimes it may be in `./build/Debug/`), Addons can use the [bindings](https://github.com/TooTallNate/node-bindings) package to load the compiled module. Note that while the `bindings` package implementation is more sophisticated in how it locates Addon modules, it is essentially using a try-catc...
large_dataset/raw/docs/nodejs/addons/addons_7x.md
399797b8-bcc6-5935-9243-a1e88f69c80e
docs
nodejs
nodejs_addons
7x
7
* When `node-gyp` runs, it will detect the specific release version of Node.js and download either the full source tarball or just the headers. If the full source is downloaded, Addons will have complete access to the full set of Node.js dependencies. However, if only the Node.js headers are downloaded, then on...
large_dataset/raw/docs/nodejs/addons/addons_7x.md
146930f2-40de-5dfb-bf96-30b080d77c94
docs
nodejs
nodejs_addons
7x
8
omitted and Node.js will still find and initialize the Addon. One caveat, however, is that Node.js will first attempt to locate and load modules or JavaScript files that happen to share the same base name. For instance, if there is a file `addon.js` in the same directory as the binary `addon.node`, then [`require('addo...
large_dataset/raw/docs/nodejs/addons/addons_7x.md
79112a8f-939a-5e91-81bf-b7faf79cfb5d
docs
nodejs
nodejs_addons
7x
9
changes but there is little that Node.js can do currently to ensure stability of the V8 APIs. The [Native Abstractions for Node.js](https://github.com/nodejs/nan) (or `nan`) provide a set of tools that Addon developers are recommended to use to keep compatibility between past and future releases of V8 and Node.js. See...
large_dataset/raw/docs/nodejs/addons/addons_7x.md
c94a6863-f00f-5465-aa43-b1a29c34fc39
docs
nodejs
nodejs_addons
7x
10
"sources": [ "addon.cc" ] } ] } ``` In cases where there is more than one `.cc` file, simply add the additional filename to the `sources` array. For example: ```None "sources": ["addon.cc", "myexample.cc"] ``` Once the `binding.gyp` file is ready, the example Addons can be configured and built using `nod...
large_dataset/raw/docs/nodejs/addons/addons_7x.md
540e7b98-1de5-58ac-9870-5c24eeb2cb8c
docs
nodejs
nodejs_addons
7x
11
// This is the implementation of the "add" method // Input arguments are passed using the // const FunctionCallbackInfo<Value>& args struct void Add(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); // Check the number of arguments passed. if (args.Length() < 2) { // Throw an E...
large_dataset/raw/docs/nodejs/addons/addons_7x.md
6b0bf826-174c-5089-856a-0cf6a4fb5a87
docs
nodejs
nodejs_addons
7x
12
NODE_SET_METHOD(exports, "add", Add); } NODE_MODULE(addon, Init) } // namespace demo ``` Once compiled, the example Addon can be required and used from within Node.js: ```None // test.js const addon = require('./build/Release/addon'); console.log('This should be eight:', addon.add(3, 5)); ``` ### Callbacks[#](...
large_dataset/raw/docs/nodejs/addons/addons_7x.md
a056df9a-1c32-5215-a524-3526e04ab6bc
docs
nodejs
nodejs_addons
7x
13
cb->Call(Null(isolate), argc, argv); } void Init(Local<Object> exports, Local<Object> module) { NODE_SET_METHOD(module, "exports", RunCallback); } NODE_MODULE(addon, Init) } // namespace demo ``` Note that this example uses a two-argument form of `Init()` that receives the full `module` object as the second ar...
large_dataset/raw/docs/nodejs/addons/addons_7x.md
77323e7e-b4ae-57dd-8dd0-466fad3a17c5
docs
nodejs
nodejs_addons
7x
14
#include <node.h> namespace demo { using v8::FunctionCallbackInfo; using v8::Isolate; using v8::Local; using v8::Object; using v8::String; using v8::Value; void CreateObject(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); Local<Object> obj = Object::New(isolate); obj->Set(Stri...
large_dataset/raw/docs/nodejs/addons/addons_7x.md
c14a010e-c716-52f8-88b2-5aaabfa7237c
docs
nodejs
nodejs_addons
7x
15
using v8::Function; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Isolate; using v8::Local; using v8::Object; using v8::String; using v8::Value; void MyFunction(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); args.GetReturnValue().Set(String::NewFromUtf8(iso...
large_dataset/raw/docs/nodejs/addons/addons_7x.md