1
1
# Addons
2
2
3
- Addons are dynamically linked shared objects. They can provide glue to C and
3
+ Addons are dynamically- linked shared objects. They can provide glue to C and
4
4
C++ libraries. The API (at the moment) is rather complex, involving
5
5
knowledge of several libraries:
6
6
@@ -11,11 +11,11 @@ knowledge of several libraries:
11
11
12
12
- [ libuv] [ ] , C event loop library. Anytime one needs to wait for a file
13
13
descriptor to become readable, wait for a timer, or wait for a signal
14
- to be received one will need to interface with libuv. That is, if you
14
+ to be received, one will need to interface with libuv. That is, if you
15
15
perform any I/O, libuv will need to be used.
16
16
17
- - Internal Node.js libraries. Most importantly is the ` node::ObjectWrap `
18
- class which you will likely want to derive from.
17
+ - Internal Node.js libraries. The most important class is ` node::ObjectWrap `
18
+ which you will likely want to derive from.
19
19
20
20
- Others. Look in ` deps/ ` for what else is available.
21
21
@@ -28,7 +28,7 @@ be used as a starting-point for your own Addon.
28
28
29
29
## Hello world
30
30
31
- To get started let's make a small Addon which is the C++ equivalent of
31
+ To get started, let's make a small Addon which is the C++ equivalent of
32
32
the following JavaScript code:
33
33
34
34
module.exports.hello = function() { return 'world'; };
@@ -68,11 +68,11 @@ Note that all Node.js addons must export an initialization function:
68
68
There is no semi-colon after ` NODE_MODULE ` as it's not a function (see
69
69
` node.h ` ).
70
70
71
- The ` module_name ` needs to match the filename of the final binary (minus the
72
- .node suffix).
71
+ The ` module_name ` needs to match the filename of the final binary (excluding
72
+ the .node suffix).
73
73
74
74
The source code needs to be built into ` addon.node ` , the binary Addon. To
75
- do this we create a file called ` binding.gyp ` which describes the configuration
75
+ do this, we create a file called ` binding.gyp ` which describes the configuration
76
76
to build your module in a JSON-like format. This file gets compiled by
77
77
[ node-gyp] [ ] .
78
78
@@ -89,13 +89,13 @@ The next step is to generate the appropriate project build files for the
89
89
current platform. Use ` node-gyp configure ` for that.
90
90
91
91
Now you will have either a ` Makefile ` (on Unix platforms) or a ` vcxproj ` file
92
- (on Windows) in the ` build/ ` directory. Next invoke the ` node-gyp build `
92
+ (on Windows) in the ` build/ ` directory. Next, invoke the ` node-gyp build `
93
93
command.
94
94
95
95
Now you have your compiled ` .node ` bindings file! The compiled bindings end up
96
96
in ` build/Release/ ` .
97
97
98
- You can now use the binary addon in an Node.js project ` hello.js ` by pointing
98
+ You can now use the binary addon in a Node.js project ` hello.js ` by pointing
99
99
` require ` to the recently built ` hello.node ` module:
100
100
101
101
// hello.js
@@ -114,7 +114,7 @@ Below are some addon patterns to help you get started. Consult the online
114
114
[ Embedder's Guide] [ ] for an explanation of several concepts used such as
115
115
handles, scopes, function templates, etc.
116
116
117
- In order to use these examples you need to compile them using ` node-gyp ` .
117
+ In order to use these examples, you need to compile them using ` node-gyp ` .
118
118
Create the following ` binding.gyp ` file:
119
119
120
120
{
@@ -127,7 +127,7 @@ Create the following `binding.gyp` file:
127
127
}
128
128
129
129
In cases where there is more than one ` .cc ` file, simply add the file name to
130
- the ` sources ` array, e.g. :
130
+ the ` sources ` array. For example :
131
131
132
132
"sources": ["addon.cc", "myexample.cc"]
133
133
@@ -234,7 +234,7 @@ the full `module` object as the second argument. This allows the addon
234
234
to completely overwrite ` exports ` with a single function instead of
235
235
adding the function as a property of ` exports ` .
236
236
237
- To test it run the following JavaScript snippet:
237
+ To test it, run the following JavaScript snippet:
238
238
239
239
// test.js
240
240
var addon = require('./build/Release/addon');
@@ -344,8 +344,8 @@ To test:
344
344
345
345
### Wrapping C++ objects
346
346
347
- Here we will create a wrapper for a C++ object/class ` MyObject ` that can be
348
- instantiated in JavaScript through the ` new ` operator. First prepare the main
347
+ Here, we will create a wrapper for a C++ object/class ` MyObject ` that can be
348
+ instantiated in JavaScript through the ` new ` operator. First, prepare the main
349
349
module ` addon.cc ` :
350
350
351
351
// addon.cc
@@ -365,7 +365,7 @@ module `addon.cc`:
365
365
366
366
} // namespace demo
367
367
368
- Then in ` myobject.h ` make your wrapper inherit from ` node::ObjectWrap ` :
368
+ Then, in ` myobject.h ` , make your wrapper inherit from ` node::ObjectWrap ` :
369
369
370
370
// myobject.h
371
371
#ifndef MYOBJECT_H
@@ -394,7 +394,7 @@ Then in `myobject.h` make your wrapper inherit from `node::ObjectWrap`:
394
394
395
395
#endif
396
396
397
- And in ` myobject.cc ` implement the various methods that you want to expose.
397
+ And in ` myobject.cc ` , implement the various methods that you want to expose.
398
398
Here we expose the method ` plusOne ` by adding it to the constructor's
399
399
prototype:
400
400
@@ -480,7 +480,8 @@ Test it with:
480
480
### Factory of wrapped objects
481
481
482
482
This is useful when you want to be able to create native objects without
483
- explicitly instantiating them with the ` new ` operator in JavaScript, e.g.
483
+ explicitly instantiating them with the ` new ` operator in JavaScript. For
484
+ example:
484
485
485
486
var obj = addon.createObject();
486
487
// instead of:
@@ -515,8 +516,9 @@ Let's register our `createObject` method in `addon.cc`:
515
516
516
517
} // namespace demo
517
518
518
- In ` myobject.h ` we now introduce the static method ` NewInstance ` that takes
519
- care of instantiating the object (i.e. it does the job of ` new ` in JavaScript):
519
+ In ` myobject.h ` , we now introduce the static method ` NewInstance ` that takes
520
+ care of instantiating the object. In other words, it does the job of ` new ` in
521
+ JavaScript:
520
522
521
523
// myobject.h
522
524
#ifndef MYOBJECT_H
@@ -644,9 +646,9 @@ Test it with:
644
646
### Passing wrapped objects around
645
647
646
648
In addition to wrapping and returning C++ objects, you can pass them around
647
- by unwrapping them with Node.js's ` node::ObjectWrap::Unwrap ` helper function .
648
- In the following ` addon.cc ` we introduce a function ` add() ` that can take on two
649
- ` MyObject ` objects:
649
+ by unwrapping them with the Node.js helper function ` node::ObjectWrap::Unwrap ` .
650
+ In the following ` addon.cc ` , we introduce a function ` add() ` that can take on
651
+ two ` MyObject ` objects:
650
652
651
653
// addon.cc
652
654
#include <node.h>
@@ -690,7 +692,7 @@ In the following `addon.cc` we introduce a function `add()` that can take on two
690
692
691
693
} // namespace demo
692
694
693
- To make things interesting we introduce a public method in ` myobject.h ` so we
695
+ To make things interesting, we introduce a public method in ` myobject.h ` so we
694
696
can probe private values after unwrapping the object:
695
697
696
698
// myobject.h
@@ -721,7 +723,7 @@ can probe private values after unwrapping the object:
721
723
722
724
#endif
723
725
724
- The implementation of ` myobject.cc ` is similar as before:
726
+ The implementation of ` myobject.cc ` is similar to before:
725
727
726
728
// myobject.cc
727
729
#include <node.h>
@@ -804,10 +806,10 @@ Test it with:
804
806
* ` callback ` : ` void (*)(void*) ` - A pointer to the function to call at exit.
805
807
* ` args ` : ` void* ` - A pointer to pass to the callback at exit.
806
808
807
- Registers exit hooks that run after the event loop has ended, but before the VM
809
+ Registers exit hooks that run after the event loop has ended but before the VM
808
810
is killed.
809
811
810
- Callbacks are run in last-in, first-out order. AtExit takes two parameters:
812
+ Callbacks are run in last-in first-out order. AtExit takes two parameters:
811
813
a pointer to a callback function to run at exit, and a pointer to untyped
812
814
context data to be passed to that callback.
813
815
0 commit comments