@@ -280,6 +280,8 @@ such as any libuv handles registered by the addon.
280280
281281The following ` addon.cc ` uses ` AddEnvironmentCleanupHook ` :
282282
283+ <!-- addon-verify-file worker_support/addon.cc -->
284+
283285``` cpp
284286// addon.cc
285287#include < node.h>
@@ -328,6 +330,8 @@ NODE_MODULE_INIT(/* exports, module, context */) {
328330
329331Test in JavaScript by running:
330332
333+ <!-- addon-verify-file worker_support/test.js -->
334+
331335```js
332336// test.js
333337require('./build/Release/addon');
@@ -526,6 +530,8 @@ code.
526530The following example illustrates how to read function arguments passed from
527531JavaScript and how to return a result:
528532
533+ <!-- addon-verify-file function_arguments/addon.cc -->
534+
529535``` cpp
530536// addon.cc
531537#include < node.h>
@@ -585,6 +591,8 @@ NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
585591
586592Once compiled, the example addon can be required and used from within Node.js:
587593
594+ <!-- addon-verify-file function_arguments/test.js -->
595+
588596```js
589597// test.js
590598const addon = require('./build/Release/addon');
@@ -598,6 +606,8 @@ It is common practice within addons to pass JavaScript functions to a C++
598606function and execute them from there. The following example illustrates how
599607to invoke such callbacks:
600608
609+ <!-- addon-verify-file callbacks/addon.cc -->
610+
601611``` cpp
602612// addon.cc
603613#include < node.h>
@@ -641,6 +651,8 @@ property of `exports`.
641651
642652To test it, run the following JavaScript:
643653
654+ <!-- addon-verify-file callbacks/test.js -->
655+
644656```js
645657// test.js
646658const addon = require('./build/Release/addon');
@@ -659,6 +671,8 @@ Addons can create and return new objects from within a C++ function as
659671illustrated in the following example. An object is created and returned with a
660672property ` msg ` that echoes the string passed to ` createObject() ` :
661673
674+ <!-- addon-verify-file object_factory/addon.cc -->
675+
662676``` cpp
663677// addon.cc
664678#include < node.h>
@@ -698,6 +712,8 @@ NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
698712
699713To test it in JavaScript:
700714
715+ <!-- addon-verify-file object_factory/test.js -->
716+
701717```js
702718// test.js
703719const addon = require('./build/Release/addon');
@@ -713,6 +729,8 @@ console.log(obj1.msg, obj2.msg);
713729Another common scenario is creating JavaScript functions that wrap C++
714730functions and returning those back to JavaScript:
715731
732+ <!-- addon-verify-file function_factory/addon.cc -->
733+
716734``` cpp
717735// addon.cc
718736#include < node.h>
@@ -760,6 +778,8 @@ NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
760778
761779To test:
762780
781+ <!-- addon-verify-file function_factory/test.js -->
782+
763783```js
764784// test.js
765785const addon = require('./build/Release/addon');
@@ -774,6 +794,8 @@ console.log(fn());
774794It is also possible to wrap C++ objects/classes in a way that allows new
775795instances to be created using the JavaScript ` new ` operator:
776796
797+ <!-- addon-verify-file wrapping_c_objects/addon.cc -->
798+
777799``` cpp
778800// addon.cc
779801#include < node.h>
@@ -795,6 +817,8 @@ NODE_MODULE(NODE_GYP_MODULE_NAME, InitAll)
795817
796818Then, in `myobject.h`, the wrapper class inherits from `node::ObjectWrap`:
797819
820+ <!-- addon-verify-file wrapping_c_objects/myobject.h -->
821+
798822```cpp
799823// myobject.h
800824#ifndef MYOBJECT_H
@@ -828,6 +852,8 @@ In `myobject.cc`, implement the various methods that are to be exposed.
828852In the following code, the method ` plusOne() ` is exposed by adding it to the
829853constructor's prototype:
830854
855+ <!-- addon-verify-file wrapping_c_objects/myobject.cc -->
856+
831857``` cpp
832858// myobject.cc
833859#include " myobject.h"
@@ -931,6 +957,8 @@ To build this example, the `myobject.cc` file must be added to the
931957
932958Test it with:
933959
960+ <!-- addon-verify-file wrapping_c_objects/test.js -->
961+
934962``` js
935963// test.js
936964const addon = require (' ./build/Release/addon' );
@@ -968,6 +996,8 @@ const obj = addon.createObject();
968996
969997First, the ` createObject() ` method is implemented in ` addon.cc ` :
970998
999+ <!-- addon-verify-file factory_of_wrapped_objects/addon.cc -->
1000+
9711001``` cpp
9721002// addon.cc
9731003#include < node.h>
@@ -1001,6 +1031,8 @@ In `myobject.h`, the static method `NewInstance()` is added to handle
10011031instantiating the object. This method takes the place of using `new` in
10021032JavaScript:
10031033
1034+ <!-- addon-verify-file factory_of_wrapped_objects/myobject.h -->
1035+
10041036```cpp
10051037// myobject.h
10061038#ifndef MYOBJECT_H
@@ -1033,6 +1065,8 @@ class MyObject : public node::ObjectWrap {
10331065
10341066The implementation in ` myobject.cc ` is similar to the previous example:
10351067
1068+ <!-- addon-verify-file factory_of_wrapped_objects/myobject.cc -->
1069+
10361070``` cpp
10371071// myobject.cc
10381072#include < node.h>
@@ -1147,6 +1181,8 @@ Once again, to build this example, the `myobject.cc` file must be added to the
11471181
11481182Test it with:
11491183
1184+ <!-- addon-verify-file factory_of_wrapped_objects/test.js -->
1185+
11501186``` js
11511187// test.js
11521188const createObject = require (' ./build/Release/addon' );
@@ -1175,6 +1211,8 @@ wrapped objects around by unwrapping them with the Node.js helper function
11751211` node::ObjectWrap::Unwrap ` . The following examples shows a function ` add() `
11761212that can take two ` MyObject ` objects as input arguments:
11771213
1214+ <!-- addon-verify-file passing_wrapped_objects_around/addon.cc -->
1215+
11781216``` cpp
11791217// addon.cc
11801218#include < node.h>
@@ -1224,6 +1262,8 @@ NODE_MODULE(NODE_GYP_MODULE_NAME, InitAll)
12241262In `myobject.h`, a new public method is added to allow access to private values
12251263after unwrapping the object.
12261264
1265+ <!-- addon-verify-file passing_wrapped_objects_around/myobject.h -->
1266+
12271267```cpp
12281268// myobject.h
12291269#ifndef MYOBJECT_H
@@ -1256,6 +1296,8 @@ class MyObject : public node::ObjectWrap {
12561296
12571297The implementation of ` myobject.cc ` remains similar to the previous version:
12581298
1299+ <!-- addon-verify-file passing_wrapped_objects_around/myobject.cc -->
1300+
12591301``` cpp
12601302// myobject.cc
12611303#include < node.h>
@@ -1340,6 +1382,8 @@ void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) {
13401382
13411383Test it with:
13421384
1385+ <!-- addon-verify-file passing_wrapped_objects_around/test.js -->
1386+
13431387```js
13441388// test.js
13451389const addon = require('./build/Release/addon');
0 commit comments