summaryrefslogtreecommitdiff
path: root/doc/api/addons.md
diff options
context:
space:
mode:
authorRod Vagg <rod@vagg.org>2018-01-23 21:49:25 +1100
committerRod Vagg <rod@vagg.org>2018-01-25 08:18:24 +1100
commit57bd27eda8ae0f540c14c8480128bca42b387314 (patch)
tree5102c45e897172b28db20b9d3f2258af3878675d /doc/api/addons.md
parentd5d024d6ec4241bfb6449c314ee620cf6953ab25 (diff)
downloadandroid-node-v8-57bd27eda8ae0f540c14c8480128bca42b387314.tar.gz
android-node-v8-57bd27eda8ae0f540c14c8480128bca42b387314.tar.bz2
android-node-v8-57bd27eda8ae0f540c14c8480128bca42b387314.zip
Revert "build,test: make building addon tests less fragile"
This reverts commit d9b59def72c718aaad3eefb6bf43f409ccefe4d2. Breaks downloadable source tarball builds as we remove some files prior to creating a tarball but those files are included in the comprehensive list of dependencies listed in .deps. Ref: https://github.com/nodejs/node/pull/17407 PR-URL: https://github.com/nodejs/node/pull/18287 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'doc/api/addons.md')
-rw-r--r--doc/api/addons.md101
1 files changed, 49 insertions, 52 deletions
diff --git a/doc/api/addons.md b/doc/api/addons.md
index 6b84723791..e2df5f30e9 100644
--- a/doc/api/addons.md
+++ b/doc/api/addons.md
@@ -1,6 +1,3 @@
-<!--
-Run `node tools/doc/addon-verify.js` when you change examples in this document.
--->
# C++ Addons
<!--introduced_in=v0.10.0-->
@@ -97,12 +94,12 @@ The `module_name` must match the filename of the final binary (excluding
the .node suffix).
In the `hello.cc` example, then, the initialization function is `init` and the
-Addon module name is `binding`.
+Addon module name is `addon`.
### Building
Once the source code has been written, it must be compiled into the binary
-`binding.node` file. To do so, create a file called `binding.gyp` in the
+`addon.node` file. To do so, create a file called `binding.gyp` in the
top-level of the project describing the build configuration of the module
using a JSON-like format. This file is used by [node-gyp][] -- a tool written
specifically to compile Node.js Addons.
@@ -111,7 +108,7 @@ specifically to compile Node.js Addons.
{
"targets": [
{
- "target_name": "binding",
+ "target_name": "addon",
"sources": [ "hello.cc" ]
}
]
@@ -131,21 +128,21 @@ generate the appropriate project build files for the current platform. This
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
-`binding.node` file. This will be put into the `build/Release/` 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 its own bundled
version of `node-gyp` to perform this same set of actions, generating a
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()`][require] to the built `binding.node` module:
+[`require()`][require] to the built `addon.node` module:
```js
// hello.js
-const binding = require('./build/Release/binding');
+const addon = require('./build/Release/addon');
-console.log(binding.hello());
+console.log(addon.hello());
// Prints: 'world'
```
@@ -162,9 +159,9 @@ similar to:
```js
try {
- return require('./build/Release/binding.node');
+ return require('./build/Release/addon.node');
} catch (err) {
- return require('./build/Debug/binding.node');
+ return require('./build/Debug/addon.node');
}
```
@@ -198,9 +195,9 @@ When calling [`require()`][require], the `.node` extension can usually be
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 `binding.js` in the same directory as the binary `binding.node`,
-then [`require('binding')`][require] will give precedence to the `binding.js`
-file and load it instead.
+there is a file `addon.js` in the same directory as the binary `addon.node`,
+then [`require('addon')`][require] will give precedence to the `addon.js` file
+and load it instead.
## Native Abstractions for Node.js
@@ -287,8 +284,8 @@ Each of these examples using the following `binding.gyp` file:
{
"targets": [
{
- "target_name": "binding",
- "sources": [ "binding.cc" ]
+ "target_name": "addon",
+ "sources": [ "addon.cc" ]
}
]
}
@@ -298,7 +295,7 @@ In cases where there is more than one `.cc` file, simply add the additional
filename to the `sources` array. For example:
```json
-"sources": ["binding.cc", "myexample.cc"]
+"sources": ["addon.cc", "myexample.cc"]
```
Once the `binding.gyp` file is ready, the example Addons can be configured and
@@ -320,7 +317,7 @@ The following example illustrates how to read function arguments passed from
JavaScript and how to return a result:
```cpp
-// binding.cc
+// addon.cc
#include <node.h>
namespace demo {
@@ -377,9 +374,9 @@ Once compiled, the example Addon can be required and used from within Node.js:
```js
// test.js
-const binding = require('./build/Release/binding');
+const addon = require('./build/Release/addon');
-console.log('This should be eight:', binding.add(3, 5));
+console.log('This should be eight:', addon.add(3, 5));
```
@@ -390,7 +387,7 @@ function and execute them from there. The following example illustrates how
to invoke such callbacks:
```cpp
-// binding.cc
+// addon.cc
#include <node.h>
namespace demo {
@@ -430,9 +427,9 @@ To test it, run the following JavaScript:
```js
// test.js
-const binding = require('./build/Release/binding');
+const addon = require('./build/Release/addon');
-binding((msg) => {
+addon((msg) => {
console.log(msg);
// Prints: 'hello world'
});
@@ -447,7 +444,7 @@ illustrated in the following example. An object is created and returned with a
property `msg` that echoes the string passed to `createObject()`:
```cpp
-// binding.cc
+// addon.cc
#include <node.h>
namespace demo {
@@ -481,10 +478,10 @@ To test it in JavaScript:
```js
// test.js
-const binding = require('./build/Release/binding');
+const addon = require('./build/Release/addon');
-const obj1 = binding('hello');
-const obj2 = binding('world');
+const obj1 = addon('hello');
+const obj2 = addon('world');
console.log(obj1.msg, obj2.msg);
// Prints: 'hello world'
```
@@ -496,7 +493,7 @@ Another common scenario is creating JavaScript functions that wrap C++
functions and returning those back to JavaScript:
```cpp
-// binding.cc
+// addon.cc
#include <node.h>
namespace demo {
@@ -540,9 +537,9 @@ To test:
```js
// test.js
-const binding = require('./build/Release/binding');
+const addon = require('./build/Release/addon');
-const fn = binding();
+const fn = addon();
console.log(fn());
// Prints: 'hello world'
```
@@ -554,7 +551,7 @@ It is also possible to wrap C++ objects/classes in a way that allows new
instances to be created using the JavaScript `new` operator:
```cpp
-// binding.cc
+// addon.cc
#include <node.h>
#include "myobject.h"
@@ -689,9 +686,9 @@ To build this example, the `myobject.cc` file must be added to the
{
"targets": [
{
- "target_name": "binding",
+ "target_name": "addon",
"sources": [
- "binding.cc",
+ "addon.cc",
"myobject.cc"
]
}
@@ -703,9 +700,9 @@ Test it with:
```js
// test.js
-const binding = require('./build/Release/binding');
+const addon = require('./build/Release/addon');
-const obj = new binding.MyObject(10);
+const obj = new addon.MyObject(10);
console.log(obj.plusOne());
// Prints: 11
console.log(obj.plusOne());
@@ -720,15 +717,15 @@ Alternatively, it is possible to use a factory pattern to avoid explicitly
creating object instances using the JavaScript `new` operator:
```js
-const obj = binding.createObject();
+const obj = addon.createObject();
// instead of:
-// const obj = new binding.Object();
+// const obj = new addon.Object();
```
-First, the `createObject()` method is implemented in `binding.cc`:
+First, the `createObject()` method is implemented in `addon.cc`:
```cpp
-// binding.cc
+// addon.cc
#include <node.h>
#include "myobject.h"
@@ -884,9 +881,9 @@ Once again, to build this example, the `myobject.cc` file must be added to the
{
"targets": [
{
- "target_name": "binding",
+ "target_name": "addon",
"sources": [
- "binding.cc",
+ "addon.cc",
"myobject.cc"
]
}
@@ -898,7 +895,7 @@ Test it with:
```js
// test.js
-const createObject = require('./build/Release/binding');
+const createObject = require('./build/Release/addon');
const obj = createObject(10);
console.log(obj.plusOne());
@@ -926,7 +923,7 @@ wrapped objects around by unwrapping them with the Node.js helper function
that can take two `MyObject` objects as input arguments:
```cpp
-// binding.cc
+// addon.cc
#include <node.h>
#include <node_object_wrap.h>
#include "myobject.h"
@@ -1080,11 +1077,11 @@ Test it with:
```js
// test.js
-const binding = require('./build/Release/binding');
+const addon = require('./build/Release/addon');
-const obj1 = binding.createObject(10);
-const obj2 = binding.createObject(20);
-const result = binding.add(obj1, obj2);
+const obj1 = addon.createObject(10);
+const obj2 = addon.createObject(20);
+const result = addon.add(obj1, obj2);
console.log(result);
// Prints: 30
@@ -1109,10 +1106,10 @@ and a pointer to untyped context data to be passed to that callback.
Callbacks are run in last-in first-out order.
-The following `binding.cc` implements AtExit:
+The following `addon.cc` implements AtExit:
```cpp
-// binding.cc
+// addon.cc
#include <assert.h>
#include <stdlib.h>
#include <node.h>
@@ -1164,7 +1161,7 @@ Test in JavaScript by running:
```js
// test.js
-require('./build/Release/binding');
+require('./build/Release/addon');
```
[Embedder's Guide]: https://github.com/v8/v8/wiki/Embedder's%20Guide