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
bbe239e1-d3ef-5c6a-9da3-d9b3045714d0
docs
nodejs
nodejs_addons
012x
12
Let's register our `createObject` method in `addon.cc`: ```None // addon.cc #include <node.h> #include "myobject.h" using namespace v8; void CreateObject(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); MyObject::NewInstance(args); } void InitAll...
large_dataset/raw/docs/nodejs/addons/addons_012x.md
fcdb17c8-fa4c-598b-bc3c-ea4c1dee2615
docs
nodejs
nodejs_addons
012x
13
static void New(const v8::FunctionCallbackInfo<v8::Value>& args); static void PlusOne(const v8::FunctionCallbackInfo<v8::Value>& args); static v8::Persistent<v8::Function> constructor; double value_; }; #endif ``` The implementation is similar to the above in `myobject.cc`: ```None // myobject.cc #include <n...
large_dataset/raw/docs/nodejs/addons/addons_012x.md
472c955c-ccd4-5cd2-88bb-7b4a69ba9b9c
docs
nodejs
nodejs_addons
012x
14
HandleScope scope(isolate); 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.This()); args.GetReturnValue().Set(args.This()); } else { // Inv...
large_dataset/raw/docs/nodejs/addons/addons_012x.md
14322085-ddab-5ea0-99f9-0539c24e02a6
docs
nodejs
nodejs_addons
012x
15
void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder()); obj->value_ += 1; args.GetReturnValue().Set(Number::New(isolate, obj->value_)); } ``` Test it with: ```None /...
large_dataset/raw/docs/nodejs/addons/addons_012x.md
62b38673-7a6e-5be7-bbc7-a7f63406b719
docs
nodejs
nodejs_addons
012x
16
#include <node.h> #include <node_object_wrap.h> #include "myobject.h" using namespace v8; void CreateObject(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); MyObject::NewInstance(args); } void Add(const FunctionCallbackInfo<Value>& args) { Isola...
large_dataset/raw/docs/nodejs/addons/addons_012x.md
2605096f-f88e-5e1f-bd72-acaf8723565a
docs
nodejs
nodejs_addons
012x
17
#ifndef MYOBJECT_H #define MYOBJECT_H #include <node.h> #include <node_object_wrap.h> class MyObject : public node::ObjectWrap { public: static void Init(); static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args); inline double value() const { return value_; } private: explicit MyObject(do...
large_dataset/raw/docs/nodejs/addons/addons_012x.md
843a4038-bff4-5f0f-bbd7-95c83cec8ae9
docs
nodejs
nodejs_addons
012x
18
tpl->InstanceTemplate()->SetInternalFieldCount(1); constructor.Reset(isolate, tpl->GetFunction()); } void MyObject::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); if (args.IsConstructCall()) { // Invoked as constructor: `new MyObject...
large_dataset/raw/docs/nodejs/addons/addons_012x.md
f7ea9703-32be-506c-8e0f-53dc36893943
docs
nodejs
nodejs_addons
012x
19
Local<Function> cons = Local<Function>::New(isolate, constructor); Local<Object> instance = cons->NewInstance(argc, argv); args.GetReturnValue().Set(instance); } ``` Test it with: ```None // test.js var addon = require('./build/Release/addon'); var obj1 = addon.createObject(10); var obj2 = addon.createObject(...
large_dataset/raw/docs/nodejs/addons/addons_012x.md
981e1dab-eca2-5028-a731-608d9ddd2830
docs
nodejs
nodejs_addons
10x
0
# C++ Addons# Source: https://nodejs.org/docs/latest-v10.x/api/addons.html Version: 10.x Node.js Addons are dynamically-linked shared objects, written in C++, that can be loaded into Node.js using the [`require()`](modules.html#modules_require) function, and used just as if they were an ordinary Node.js module. They ...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
96d3bc54-3543-560d-a140-3810ab3fc9be
docs
nodejs
nodejs_addons
10x
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_10x.md
8436f92f-8e25-51d6-8087-0a56bd07bb5f
docs
nodejs
nodejs_addons
10x
2
OpenSSL. These other libraries are located in the `deps/` directory in the Node.js source tree. Only the libuv, OpenSSL, V8 and zlib 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_dependenc...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
f77e94dd-8818-50d9-aa7b-d951326786ba
docs
nodejs
nodejs_addons
10x
3
Isolate* isolate = args.GetIsolate(); args.GetReturnValue().Set(String::NewFromUtf8( isolate, "world", NewStringType::kNormal).ToLocalChecked()); } void Initialize(Local<Object> exports) { NODE_SET_METHOD(exports, "hello", Method); } NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize) } // namespace demo ``` ...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
878afc3d-a919-5a18-97cd-c4dd6966f787
docs
nodejs
nodejs_addons
10x
4
### Context-aware addons[#](#addons_context_aware_addons) There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the [Electron](https://electronjs.org/) runtime runs multiple instances of Node.js in a single process. Each instance will have its own `requi...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
34d72088-4b90-521e-bfe9-7c7d06897099
docs
nodejs
nodejs_addons
10x
5
``` Another option is to use the macro `NODE_MODULE_INIT()`, which will also construct a context-aware addon. Unlike `NODE_MODULE()`, which is used to construct an addon around a given addon initializer function, `NODE_MODULE_INIT()` serves as the declaration of such an initializer to be followed by a function body. ...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
a0bbae98-22f7-5487-b754-d0d63f590463
docs
nodejs
nodejs_addons
10x
6
accessed from the wrong context or from a different thread than the one on which they were created. The context-aware addon can be structured to avoid global static data by performing the following steps: * defining a class which will hold per-addon-instance data. Such a class should include a `v8::Persistent<v8::O...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
cd765250-3440-5e0b-8352-92739be2db0c
docs
nodejs
nodejs_addons
10x
7
This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create. The following example illustrates the implementation of a context-aware addon: ```None #include <node.h> usi...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
0f4f7b4d-f853-5ab0-8441-5ab2589eba60
docs
nodejs
nodejs_addons
10x
8
// Weak handle to the "exports" object. An instance of this class will be // destroyed along with the exports object to which it is weakly bound. v8::Persistent<v8::Object> exports_; }; static void Method(const v8::FunctionCallbackInfo<v8::Value>& info) { // Retrieve the per-addon-instance data. AddonData* d...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
ddbf5927-9e6b-5f33-b3d0-6a14675386e9
docs
nodejs
nodejs_addons
10x
9
// per-addon-instance data we created above by passing `external` as the // third parameter to the FunctionTemplate constructor. exports->Set(context, String::NewFromUtf8(isolate, "method", NewStringType::kNormal) .ToLocalChecked(), FunctionTemplate::New(isolate, Me...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
a95e71ef-8e36-5eea-8872-78bcdac26da9
docs
nodejs
nodejs_addons
10x
10
Node.js as part of `npm`. This version is not made directly available for developers to use and is intended only to support the ability to use the `npm install` command to compile and install Addons. Developers who wish to use `node-gyp` directly can install it using the command `npm install -g node-gyp`. See the `node...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
e3ce0af4-0ba2-5c26-a047-06ae64c90d70
docs
nodejs
nodejs_addons
10x
11
compiled version of the Addon for the user's platform on demand. Once built, the binary Addon can be used from within Node.js by pointing [`require()`](modules.html#modules_require) to the built `addon.node` module: ```None // hello.js const addon = require('./build/Release/addon'); console.log(addon.hello()); // Pr...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
bebd7b73-a334-5d39-bf25-7d63267e37a7
docs
nodejs
nodejs_addons
10x
12
### Linking to Node.js' own dependencies[#](#addons_linking_to_node_js_own_dependencies) Node.js uses a number of statically linked libraries such as V8, libuv and OpenSSL. All Addons are required to link to V8 and may link to any of the other dependencies as well. Typically, this is as simple as including the appropr...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
2ed0bcf5-db06-5141-b56d-5a2f13b07e70
docs
nodejs
nodejs_addons
10x
13
source image. Using this option, the Addon will have access to the full set of dependencies. ### Loading Addons using require()[#](#addons_loading_addons_using_require) The filename extension of the compiled Addon binary is `.node` (as opposed to `.dll` or `.so`). The [`require()`](modules.html#modules_require) f...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
d364131a-094e-5b36-bf31-2648b3029004
docs
nodejs
nodejs_addons
10x
14
## Native Abstractions for Node.js[#](#addons_native_abstractions_for_node_js) Each of the examples illustrated in this document make direct use of the Node.js and V8 APIs for implementing Addons. It is important to understand that the V8 API can, and has, changed dramatically from one V8 release to the next (and one ...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
b03442c1-19f5-5ae6-ad5f-d2267ce80f56
docs
nodejs
nodejs_addons
10x
15
## N-API[#](#addons_n_api) [Stability: 2](documentation.html#documentation_stability_index) - Stable N-API is an API for building native Addons. It is independent from the underlying JavaScript runtime (e.g. V8) and is maintained as part of Node.js itself. This API will be Application Binary Interface (ABI) stable ac...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
da0beb15-8be1-5fe2-acc9-8ebd9119e5ec
docs
nodejs
nodejs_addons
10x
16
[implementation considerations](n-api.html#n_api_implications_of_abi_stability). To use N-API in the above "Hello world" example, replace the content of `hello.cc` with the following. All other instructions remain the same. ```None // hello.cc using N-API #include <node_api.h> namespace demo { napi_value Method(nap...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
eb4e01ae-2213-5a20-8218-d57f976af0b4
docs
nodejs
nodejs_addons
10x
17
section titled [C/C++ Addons - N-API](n-api.html). ## Addon examples[#](#addons_addon_examples) Following are some example Addons intended to help developers get started. The examples make use of the V8 APIs. Refer to the online [V8 reference](https://v8docs.nodesource.com/) for help with the various V8 calls, and V8...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
8bc1fadf-fa30-5b3c-83c6-f416b289c9c7
docs
nodejs
nodejs_addons
10x
18
### Function arguments[#](#addons_function_arguments) Addons will typically expose objects and functions that can be accessed from JavaScript running within Node.js. When functions are invoked from JavaScript, the input arguments and return value must be mapped to and from the C/C++ code. The following example illust...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
20350ed6-5862-5262-838e-bb0bca926b29
docs
nodejs
nodejs_addons
10x
19
isolate->ThrowException(Exception::TypeError( String::NewFromUtf8(isolate, "Wrong number of arguments", NewStringType::kNormal).ToLocalChecked())); return; } // Check the argument types if (!args[0]->IsNumber() || !args[1]->IsNumber()) { ...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
47c64b96-6ee3-5292-88ba-0e7d27876fe9
docs
nodejs
nodejs_addons
10x
20
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_10x.md
f6fe20da-69de-5d9b-8663-3418976494a9
docs
nodejs
nodejs_addons
10x
21
"hello world", NewStringType::kNormal).ToLocalChecked() }; cb->Call(context, Null(isolate), argc, argv).ToLocalChecked(); } void Init(Local<Object> exports, Local<Object> module) { NODE_SET_METHOD(module, "exports", RunCallback); } NODE_MODULE(NODE_GYP_MODULE_NA...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
8a2766d0-73b8-5188-9d2c-2fb55c1190d9
docs
nodejs
nodejs_addons
10x
22
### Object factory[#](#addons_object_factory) Addons can create and return new objects from within a C++ function as illustrated in the following example. An object is created and returned with a property `msg` that echoes the string passed to `createObject()`: ```None // addon.cc #include <node.h> namespace demo { ...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
74aa0526-1c3d-5150-b28d-f14eb17e8e7f
docs
nodejs
nodejs_addons
10x
23
void Init(Local<Object> exports, Local<Object> module) { NODE_SET_METHOD(module, "exports", CreateObject); } NODE_MODULE(NODE_GYP_MODULE_NAME, Init) } // namespace demo ``` To test it in JavaScript: ```None // test.js const addon = require('./build/Release/addon'); const obj1 = addon('hello'); const obj2 = addo...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
4dbe6c13-eda8-5675-92e9-375892f9c2cd
docs
nodejs
nodejs_addons
10x
24
isolate, "hello world", NewStringType::kNormal).ToLocalChecked()); } void CreateFunction(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); Local<Context> context = isolate->GetCurrentContext(); Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, MyFunction); Local...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
71a13d35-4f51-5fb9-9fd6-bf075ab4225f
docs
nodejs
nodejs_addons
10x
25
### 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_10x.md
edbb83b3-d8c6-5506-9cfc-ad69ef913e9f
docs
nodejs
nodejs_addons
10x
26
double value_; }; } // 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; usi...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
247178cc-9831-52ed-9c83-c9018b6180af
docs
nodejs
nodejs_addons
10x
27
// Prototype NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne); Local<Context> context = isolate->GetCurrentContext(); constructor.Reset(isolate, tpl->GetFunction(context).ToLocalChecked()); exports->Set(context, String::NewFromUtf8( isolate, "MyObject", NewStringType::kNormal).ToLocalChecked(), ...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
b4c9ceac-adb6-5791-937c-c70ac5a576e0
docs
nodejs
nodejs_addons
10x
28
Local<Function> cons = Local<Function>::New(isolate, constructor); Local<Object> result = cons->NewInstance(context, argc, argv).ToLocalChecked(); args.GetReturnValue().Set(result); } } void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); My...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
2bd5a550-6792-5b4a-b0b0-8501d872e0f8
docs
nodejs
nodejs_addons
10x
29
The destructor for a wrapper object will run when the object is garbage-collected. For destructor testing, there are command-line flags that can be used to make it possible to force garbage collection. These flags are provided by the underlying V8 JavaScript engine. They are subject to change or removal at any time. Th...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
28101c00-3ca9-5ca3-aead-16417f21b6b8
docs
nodejs
nodejs_addons
10x
30
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(NODE_GYP_MODULE_NAME, InitAll) } // namespace demo ```...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
e9ed13e4-88a7-594b-8c76-cb47a68b5d64
docs
nodejs
nodejs_addons
10x
31
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::Context; using v8::Function; using v8::FunctionCa...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
df01a26e-4c61-5b3b-ad1c-d3acb3d0fe16
docs
nodejs
nodejs_addons
10x
32
Local<Context> context = isolate->GetCurrentContext(); constructor.Reset(isolate, tpl->GetFunction(context).ToLocalChecked()); } void MyObject::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); Local<Context> context = isolate->GetCurrentContext(); if (args.IsConstructCall...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
8983d068-6867-5617-91d6-bf8f68844356
docs
nodejs
nodejs_addons
10x
33
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 = isolate->GetCurrentContext(); Local<Object>...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
5cbc76c2-76f4-5fe2-9c0f-ceb478fb9d87
docs
nodejs
nodejs_addons
10x
34
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 obj2 = createObject(20); console.log(obj2.plusOne()); // Prints: 21 console.log(obj2.plusOne()); // P...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
cf5f1090-f1e6-528b-8ad6-95c989a258f4
docs
nodejs
nodejs_addons
10x
35
void CreateObject(const FunctionCallbackInfo<Value>& args) { MyObject::NewInstance(args); } void Add(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); Local<Context> context = isolate->GetCurrentContext(); MyObject* obj1 = node::ObjectWrap::Unwrap<MyObject>( args[0]->ToObj...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
c035e1e8-44aa-5b60-8ef5-1c09d8adff66
docs
nodejs
nodejs_addons
10x
36
#include <node_object_wrap.h> namespace demo { 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); ...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
f1541535-e86e-5c4a-867b-0362278c5d29
docs
nodejs
nodejs_addons
10x
37
void MyObject::Init(Isolate* isolate) { // Prepare constructor template Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New); tpl->SetClassName(String::NewFromUtf8( isolate, "MyObject", NewStringType::kNormal).ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); Local<Cont...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
46fba29f-c177-5b11-a345-20a7913e327f
docs
nodejs
nodejs_addons
10x
38
Local<Value> argv[argc] = { args[0] }; Local<Function> cons = Local<Function>::New(isolate, constructor); Local<Object> instance = cons->NewInstance(context, argc, argv).ToLocalChecked(); args.GetReturnValue().Set(instance); } } void MyObject::NewInstance(const FunctionCallbackInfo<Value>& ar...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
1f98f5b7-64c0-5e98-a191-2712f24f6d4f
docs
nodejs
nodejs_addons
10x
39
### AtExit hooks[#](#addons_atexit_hooks) An `AtExit` hook is a function that is invoked after the Node.js event loop has ended but before the JavaScript VM is terminated and Node.js shuts down. `AtExit` hooks are registered using the `node::AtExit` API. #### void AtExit(callback, args)[#](#addons_void_atexit_callbac...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
04079026-d111-57d8-979a-a27941057d5f
docs
nodejs
nodejs_addons
10x
40
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); assert(!obj.IsEmpty()); // assert VM is still alive ...
large_dataset/raw/docs/nodejs/addons/addons_10x.md
dccf50c2-a330-536d-8097-b0d2530fc5bf
docs
nodejs
nodejs_addons
11x
0
# C++ Addons# Source: https://nodejs.org/docs/latest-v11.x/api/addons.html Version: 11.x Node.js Addons are dynamically-linked shared objects, written in C++, that can be loaded into Node.js using the [`require()`](modules.html#modules_require_id) function, and used just as if they were an ordinary Node.js module. Th...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
bf53ae1e-056d-51c7-8887-8133450552ab
docs
nodejs
nodejs_addons
11x
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_11x.md
836399dd-fc9a-51ae-b87c-ae4f9af5cd74
docs
nodejs
nodejs_addons
11x
2
OpenSSL. These other libraries are located in the `deps/` directory in the Node.js source tree. Only the libuv, OpenSSL, V8 and zlib 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_dependenc...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
fcc45c32-5419-59c9-896c-fae831ba4801
docs
nodejs
nodejs_addons
11x
3
Isolate* isolate = args.GetIsolate(); args.GetReturnValue().Set(String::NewFromUtf8( isolate, "world", NewStringType::kNormal).ToLocalChecked()); } void Initialize(Local<Object> exports) { NODE_SET_METHOD(exports, "hello", Method); } NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize) } // namespace demo ``` ...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
064d7b62-cf9c-5b38-aa64-32946ac58f21
docs
nodejs
nodejs_addons
11x
4
### Context-aware addons[#](#addons_context_aware_addons) There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the [Electron](https://electronjs.org/) runtime runs multiple instances of Node.js in a single process. Each instance will have its own `requi...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
e0ff8d6a-50e6-5d79-9cca-b62545f201f2
docs
nodejs
nodejs_addons
11x
5
``` Another option is to use the macro `NODE_MODULE_INIT()`, which will also construct a context-aware addon. Unlike `NODE_MODULE()`, which is used to construct an addon around a given addon initializer function, `NODE_MODULE_INIT()` serves as the declaration of such an initializer to be followed by a function body. ...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
2e5126f0-01b2-52df-80a1-02765c582c9a
docs
nodejs
nodejs_addons
11x
6
accessed from the wrong context or from a different thread than the one on which they were created. The context-aware addon can be structured to avoid global static data by performing the following steps: * defining a class which will hold per-addon-instance data. Such a class should include a `v8::Persistent<v8::O...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
2753f5c8-d96f-5fe1-a153-2f5dc242c062
docs
nodejs
nodejs_addons
11x
7
This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create. The following example illustrates the implementation of a context-aware addon: ```None #include <node.h> usi...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
53e39fe4-cc08-5204-af47-eaae82e73ea1
docs
nodejs
nodejs_addons
11x
8
// Weak handle to the "exports" object. An instance of this class will be // destroyed along with the exports object to which it is weakly bound. v8::Persistent<v8::Object> exports_; }; static void Method(const v8::FunctionCallbackInfo<v8::Value>& info) { // Retrieve the per-addon-instance data. AddonData* d...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
d6da4c20-ca41-588b-95db-6b5afa8afda8
docs
nodejs
nodejs_addons
11x
9
// per-addon-instance data we created above by passing `external` as the // third parameter to the FunctionTemplate constructor. exports->Set(context, String::NewFromUtf8(isolate, "method", NewStringType::kNormal) .ToLocalChecked(), FunctionTemplate::New(isolate, Me...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
9503ed10-33ca-5aeb-9ae4-69f5103d4614
docs
nodejs
nodejs_addons
11x
10
`RemoveEnvironmentCleanupHook()` before they are run, which has the same signature. In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either: * Be an N-API addon, or * Be declared as context-aware using `NODE_MODULE_INIT()` as described above ### ...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
72ac60e0-5cb2-5650-a2dc-553f52c3c0ed
docs
nodejs
nodejs_addons
11x
11
developers to use and is intended only to support the ability to use the `npm install` command to compile and install Addons. Developers who wish to use `node-gyp` directly can install it using the command `npm install -g node-gyp`. See the `node-gyp` [installation instructions](https://github.com/nodejs/node-gyp#insta...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
26330b65-cb36-5d0f-8b2b-ee0be07e3c8b
docs
nodejs
nodejs_addons
11x
12
Once built, the binary Addon can be used from within Node.js by pointing [`require()`](modules.html#modules_require_id) to the built `addon.node` module: ```None // hello.js const addon = require('./build/Release/addon'); console.log(addon.hello()); // Prints: 'world' ``` Please see the examples below for further in...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
2f58992a-54bf-53f1-98d1-5569830a330c
docs
nodejs
nodejs_addons
11x
13
### Linking to Node.js' own dependencies[#](#addons_linking_to_node_js_own_dependencies) Node.js uses a number of statically linked libraries such as V8, libuv and OpenSSL. All Addons are required to link to V8 and may link to any of the other dependencies as well. Typically, this is as simple as including the appropr...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
6b715a46-3d06-534a-8ed9-dc0598f17003
docs
nodejs
nodejs_addons
11x
14
source image. Using this option, the Addon will have access to the full set of dependencies. ### Loading Addons using require()[#](#addons_loading_addons_using_require) The filename extension of the compiled Addon binary is `.node` (as opposed to `.dll` or `.so`). The [`require()`](modules.html#modules_require_id...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
5b8c5f64-2f8a-5048-b0aa-365abdd1d543
docs
nodejs
nodejs_addons
11x
15
## Native Abstractions for Node.js[#](#addons_native_abstractions_for_node_js) Each of the examples illustrated in this document make direct use of the Node.js and V8 APIs for implementing Addons. It is important to understand that the V8 API can, and has, changed dramatically from one V8 release to the next (and one ...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
532b4097-e746-5e24-aa4c-7444b3480333
docs
nodejs
nodejs_addons
11x
16
## N-API[#](#addons_n_api) [Stability: 2](documentation.html#documentation_stability_index) - Stable N-API is an API for building native Addons. It is independent from the underlying JavaScript runtime (e.g. V8) and is maintained as part of Node.js itself. This API will be Application Binary Interface (ABI) stable ac...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
1882ad28-39b0-5334-868b-aa27f19973e9
docs
nodejs
nodejs_addons
11x
17
[implementation considerations](n-api.html#n_api_implications_of_abi_stability). To use N-API in the above "Hello world" example, replace the content of `hello.cc` with the following. All other instructions remain the same. ```None // hello.cc using N-API #include <node_api.h> namespace demo { napi_value Method(nap...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
c3c3253f-606c-5f8d-ae61-06d8de494fbc
docs
nodejs
nodejs_addons
11x
18
section titled [C/C++ Addons - N-API](n-api.html). ## Addon examples[#](#addons_addon_examples) Following are some example Addons intended to help developers get started. The examples make use of the V8 APIs. Refer to the online [V8 reference](https://v8docs.nodesource.com/) for help with the various V8 calls, and V8...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
aba668b2-c8a9-56ba-90ca-06cb02849fed
docs
nodejs
nodejs_addons
11x
19
### Function arguments[#](#addons_function_arguments) Addons will typically expose objects and functions that can be accessed from JavaScript running within Node.js. When functions are invoked from JavaScript, the input arguments and return value must be mapped to and from the C/C++ code. The following example illust...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
c3d5631a-8c69-546b-8df9-b94d1df2966a
docs
nodejs
nodejs_addons
11x
20
isolate->ThrowException(Exception::TypeError( String::NewFromUtf8(isolate, "Wrong number of arguments", NewStringType::kNormal).ToLocalChecked())); return; } // Check the argument types if (!args[0]->IsNumber() || !args[1]->IsNumber()) { ...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
7e51f2c4-8884-52b7-b7f6-0608f1e17701
docs
nodejs
nodejs_addons
11x
21
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_11x.md
0a91aee2-e28e-52e0-8bd6-1faff80248ff
docs
nodejs
nodejs_addons
11x
22
"hello world", NewStringType::kNormal).ToLocalChecked() }; cb->Call(context, Null(isolate), argc, argv).ToLocalChecked(); } void Init(Local<Object> exports, Local<Object> module) { NODE_SET_METHOD(module, "exports", RunCallback); } NODE_MODULE(NODE_GYP_MODULE_NA...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
0814358b-ddf3-51ef-9bdb-1435a43810d6
docs
nodejs
nodejs_addons
11x
23
### Object factory[#](#addons_object_factory) Addons can create and return new objects from within a C++ function as illustrated in the following example. An object is created and returned with a property `msg` that echoes the string passed to `createObject()`: ```None // addon.cc #include <node.h> namespace demo { ...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
75e883ab-ec60-506f-adb3-a2f9ca4d9cb5
docs
nodejs
nodejs_addons
11x
24
void Init(Local<Object> exports, Local<Object> module) { NODE_SET_METHOD(module, "exports", CreateObject); } NODE_MODULE(NODE_GYP_MODULE_NAME, Init) } // namespace demo ``` To test it in JavaScript: ```None // test.js const addon = require('./build/Release/addon'); const obj1 = addon('hello'); const obj2 = addo...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
cb18ebca-36a6-57eb-9730-b3ababfd344b
docs
nodejs
nodejs_addons
11x
25
isolate, "hello world", NewStringType::kNormal).ToLocalChecked()); } void CreateFunction(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); Local<Context> context = isolate->GetCurrentContext(); Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, MyFunction); Local...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
96140767-2c74-568a-bb24-d1e93f153cea
docs
nodejs
nodejs_addons
11x
26
### 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_11x.md
4c9926bd-764e-5f2d-adb7-3a6b123748ed
docs
nodejs
nodejs_addons
11x
27
double value_; }; } // 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; usi...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
32bc8ef1-05ce-5005-a780-2a6d39693216
docs
nodejs
nodejs_addons
11x
28
// Prototype NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne); Local<Context> context = isolate->GetCurrentContext(); constructor.Reset(isolate, tpl->GetFunction(context).ToLocalChecked()); exports->Set(context, String::NewFromUtf8( isolate, "MyObject", NewStringType::kNormal).ToLocalChecked(), ...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
0b0ab259-4d0b-58f2-ab63-88b47c54ac69
docs
nodejs
nodejs_addons
11x
29
Local<Function> cons = Local<Function>::New(isolate, constructor); Local<Object> result = cons->NewInstance(context, argc, argv).ToLocalChecked(); args.GetReturnValue().Set(result); } } void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); My...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
0e03ab65-a1b8-59d2-bba7-cda46c242295
docs
nodejs
nodejs_addons
11x
30
The destructor for a wrapper object will run when the object is garbage-collected. For destructor testing, there are command-line flags that can be used to make it possible to force garbage collection. These flags are provided by the underlying V8 JavaScript engine. They are subject to change or removal at any time. Th...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
d0a471eb-5e5b-54a3-9a5c-401fb6b88e81
docs
nodejs
nodejs_addons
11x
31
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(NODE_GYP_MODULE_NAME, InitAll) } // namespace demo ```...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
89ccbbbd-d343-512f-b506-602803adb572
docs
nodejs
nodejs_addons
11x
32
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::Context; using v8::Function; using v8::FunctionCa...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
87a0e75c-bd2f-5776-8564-28925d3c74ab
docs
nodejs
nodejs_addons
11x
33
Local<Context> context = isolate->GetCurrentContext(); constructor.Reset(isolate, tpl->GetFunction(context).ToLocalChecked()); } void MyObject::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); Local<Context> context = isolate->GetCurrentContext(); if (args.IsConstructCall...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
fe8d48e4-cf11-5dec-950c-09a3897dbf37
docs
nodejs
nodejs_addons
11x
34
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 = isolate->GetCurrentContext(); Local<Object>...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
e8cd05b3-bdc0-53b8-929a-85301793ed97
docs
nodejs
nodejs_addons
11x
35
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 obj2 = createObject(20); console.log(obj2.plusOne()); // Prints: 21 console.log(obj2.plusOne()); // P...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
e6e818aa-b2d4-52ab-8ece-759680e55d42
docs
nodejs
nodejs_addons
11x
36
void CreateObject(const FunctionCallbackInfo<Value>& args) { MyObject::NewInstance(args); } void Add(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); Local<Context> context = isolate->GetCurrentContext(); MyObject* obj1 = node::ObjectWrap::Unwrap<MyObject>( args[0]->ToObj...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
8d64e4f0-6c55-5293-84ab-eddb01fff137
docs
nodejs
nodejs_addons
11x
37
#include <node_object_wrap.h> namespace demo { 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); ...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
fde21f7e-3f9b-501e-8da2-ba0f09105dfe
docs
nodejs
nodejs_addons
11x
38
void MyObject::Init(Isolate* isolate) { // Prepare constructor template Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New); tpl->SetClassName(String::NewFromUtf8( isolate, "MyObject", NewStringType::kNormal).ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); Local<Cont...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
bf9d4981-a9c3-5eba-b3a9-53a6078ad2b6
docs
nodejs
nodejs_addons
11x
39
Local<Value> argv[argc] = { args[0] }; Local<Function> cons = Local<Function>::New(isolate, constructor); Local<Object> instance = cons->NewInstance(context, argc, argv).ToLocalChecked(); args.GetReturnValue().Set(instance); } } void MyObject::NewInstance(const FunctionCallbackInfo<Value>& ar...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
8d4ffad4-3abb-5dbf-89ca-35b2f0c6a648
docs
nodejs
nodejs_addons
11x
40
### AtExit hooks[#](#addons_atexit_hooks) An `AtExit` hook is a function that is invoked after the Node.js event loop has ended but before the JavaScript VM is terminated and Node.js shuts down. `AtExit` hooks are registered using the `node::AtExit` API. #### void AtExit(callback, args)[#](#addons_void_atexit_callbac...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
c5f53e3d-bd00-5abd-a7ae-2aef1563e078
docs
nodejs
nodejs_addons
11x
41
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); assert(!obj.IsEmpty()); // assert VM is still alive ...
large_dataset/raw/docs/nodejs/addons/addons_11x.md
c3412ec7-6ddc-5968-9df4-f71245360e16
docs
nodejs
nodejs_addons
4x
0
# C/C++ Addons# Source: https://nodejs.org/docs/latest-v4.x/api/addons.html Version: 4.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_4x.md
280f1b74-9680-5592-b162-549b3939b240
docs
nodejs
nodejs_addons
4x
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_4x.md
ce342989-46ef-5b60-a145-64eec69e3997
docs
nodejs
nodejs_addons
4x
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](#linking-to-nodejs-own-dependencies) for additional inf...
large_dataset/raw/docs/nodejs/addons/addons_4x.md
b181f2c5-a79f-53c2-b51d-ad01fd358860
docs
nodejs
nodejs_addons
4x
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_4x.md
aa6f42c2-c9fb-5c8d-b605-6334dbf77325
docs
nodejs
nodejs_addons
4x
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_4x.md
fa3047dd-1129-5b1b-b5ab-c1d7a4c81550
docs
nodejs
nodejs_addons
4x
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_4x.md
53704236-28fb-5f02-b47a-ce0f17e38c54
docs
nodejs
nodejs_addons
4x
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_4x.md
e996c1ca-e923-50d8-a76e-3a55f50fb209
docs
nodejs
nodejs_addons
4x
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_4x.md
130b8de8-aecd-55e0-a461-18cced3dd9da
docs
nodejs
nodejs_addons
4x
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_4x.md