@@ -280,6 +280,8 @@ such as any libuv handles registered by the addon. |
280 | 280 |
|
281 | 281 | The following `addon.cc` uses `AddEnvironmentCleanupHook`: |
282 | 282 |
|
| 283 | +<!-- addon-verify-file worker_support/addon.cc --> |
| 284 | +
|
283 | 285 | ```cpp |
284 | 286 | // addon.cc |
285 | 287 | #include <node.h> |
@@ -328,6 +330,8 @@ NODE_MODULE_INIT(/* exports, module, context */) { |
328 | 330 |
|
329 | 331 | Test in JavaScript by running: |
330 | 332 |
|
| 333 | +<!-- addon-verify-file worker_support/test.js --> |
| 334 | + |
331 | 335 | ```js |
332 | 336 | // test.js |
333 | 337 | require('./build/Release/addon'); |
@@ -526,6 +530,8 @@ code. |
526 | 530 | The following example illustrates how to read function arguments passed from |
527 | 531 | JavaScript and how to return a result: |
528 | 532 |
|
| 533 | +<!-- addon-verify-file function_arguments/addon.cc --> |
| 534 | + |
529 | 535 | ```cpp |
530 | 536 | // addon.cc |
531 | 537 | #include <node.h> |
@@ -585,6 +591,8 @@ NODE_MODULE(NODE_GYP_MODULE_NAME, Init) |
585 | 591 |
|
586 | 592 | Once compiled, the example addon can be required and used from within Node.js: |
587 | 593 |
|
| 594 | +<!-- addon-verify-file function_arguments/test.js --> |
| 595 | +
|
588 | 596 | ```js |
589 | 597 | // test.js |
590 | 598 | const addon = require('./build/Release/addon'); |
@@ -598,6 +606,8 @@ It is common practice within addons to pass JavaScript functions to a C++ |
598 | 606 | function and execute them from there. The following example illustrates how |
599 | 607 | to invoke such callbacks: |
600 | 608 |
|
| 609 | +<!-- addon-verify-file callbacks/addon.cc --> |
| 610 | + |
601 | 611 | ```cpp |
602 | 612 | // addon.cc |
603 | 613 | #include <node.h> |
@@ -641,6 +651,8 @@ property of `exports`. |
641 | 651 |
|
642 | 652 | To test it, run the following JavaScript: |
643 | 653 |
|
| 654 | +<!-- addon-verify-file callbacks/test.js --> |
| 655 | +
|
644 | 656 | ```js |
645 | 657 | // test.js |
646 | 658 | const addon = require('./build/Release/addon'); |
@@ -659,6 +671,8 @@ Addons can create and return new objects from within a C++ function as |
659 | 671 | illustrated in the following example. An object is created and returned with a |
660 | 672 | property `msg` that echoes the string passed to `createObject()`: |
661 | 673 |
|
| 674 | +<!-- addon-verify-file object_factory/addon.cc --> |
| 675 | + |
662 | 676 | ```cpp |
663 | 677 | // addon.cc |
664 | 678 | #include <node.h> |
@@ -698,6 +712,8 @@ NODE_MODULE(NODE_GYP_MODULE_NAME, Init) |
698 | 712 |
|
699 | 713 | To test it in JavaScript: |
700 | 714 |
|
| 715 | +<!-- addon-verify-file object_factory/test.js --> |
| 716 | +
|
701 | 717 | ```js |
702 | 718 | // test.js |
703 | 719 | const addon = require('./build/Release/addon'); |
@@ -713,6 +729,8 @@ console.log(obj1.msg, obj2.msg); |
713 | 729 | Another common scenario is creating JavaScript functions that wrap C++ |
714 | 730 | functions and returning those back to JavaScript: |
715 | 731 |
|
| 732 | +<!-- addon-verify-file function_factory/addon.cc --> |
| 733 | + |
716 | 734 | ```cpp |
717 | 735 | // addon.cc |
718 | 736 | #include <node.h> |
@@ -760,6 +778,8 @@ NODE_MODULE(NODE_GYP_MODULE_NAME, Init) |
760 | 778 |
|
761 | 779 | To test: |
762 | 780 |
|
| 781 | +<!-- addon-verify-file function_factory/test.js --> |
| 782 | +
|
763 | 783 | ```js |
764 | 784 | // test.js |
765 | 785 | const addon = require('./build/Release/addon'); |
@@ -774,6 +794,8 @@ console.log(fn()); |
774 | 794 | It is also possible to wrap C++ objects/classes in a way that allows new |
775 | 795 | instances to be created using the JavaScript `new` operator: |
776 | 796 |
|
| 797 | +<!-- addon-verify-file wrapping_c_objects/addon.cc --> |
| 798 | + |
777 | 799 | ```cpp |
778 | 800 | // addon.cc |
779 | 801 | #include <node.h> |
@@ -795,6 +817,8 @@ NODE_MODULE(NODE_GYP_MODULE_NAME, InitAll) |
795 | 817 |
|
796 | 818 | Then, in `myobject.h`, the wrapper class inherits from `node::ObjectWrap`: |
797 | 819 |
|
| 820 | +<!-- addon-verify-file wrapping_c_objects/myobject.h --> |
| 821 | +
|
798 | 822 | ```cpp |
799 | 823 | // myobject.h |
800 | 824 | #ifndef MYOBJECT_H |
@@ -828,6 +852,8 @@ In `myobject.cc`, implement the various methods that are to be exposed. |
828 | 852 | In the following code, the method `plusOne()` is exposed by adding it to the |
829 | 853 | constructor's prototype: |
830 | 854 |
|
| 855 | +<!-- addon-verify-file wrapping_c_objects/myobject.cc --> |
| 856 | + |
831 | 857 | ```cpp |
832 | 858 | // myobject.cc |
833 | 859 | #include "myobject.h" |
@@ -931,6 +957,8 @@ To build this example, the `myobject.cc` file must be added to the |
931 | 957 |
|
932 | 958 | Test it with: |
933 | 959 |
|
| 960 | +<!-- addon-verify-file wrapping_c_objects/test.js --> |
| 961 | + |
934 | 962 | ```js |
935 | 963 | // test.js |
936 | 964 | const addon = require('./build/Release/addon'); |
@@ -968,6 +996,8 @@ const obj = addon.createObject(); |
968 | 996 |
|
969 | 997 | First, the `createObject()` method is implemented in `addon.cc`: |
970 | 998 |
|
| 999 | +<!-- addon-verify-file factory_of_wrapped_objects/addon.cc --> |
| 1000 | + |
971 | 1001 | ```cpp |
972 | 1002 | // addon.cc |
973 | 1003 | #include <node.h> |
@@ -1001,6 +1031,8 @@ In `myobject.h`, the static method `NewInstance()` is added to handle |
1001 | 1031 | instantiating the object. This method takes the place of using `new` in |
1002 | 1032 | JavaScript: |
1003 | 1033 |
|
| 1034 | +<!-- addon-verify-file factory_of_wrapped_objects/myobject.h --> |
| 1035 | +
|
1004 | 1036 | ```cpp |
1005 | 1037 | // myobject.h |
1006 | 1038 | #ifndef MYOBJECT_H |
@@ -1033,6 +1065,8 @@ class MyObject : public node::ObjectWrap { |
1033 | 1065 |
|
1034 | 1066 | The implementation in `myobject.cc` is similar to the previous example: |
1035 | 1067 |
|
| 1068 | +<!-- addon-verify-file factory_of_wrapped_objects/myobject.cc --> |
| 1069 | + |
1036 | 1070 | ```cpp |
1037 | 1071 | // myobject.cc |
1038 | 1072 | #include <node.h> |
@@ -1147,6 +1181,8 @@ Once again, to build this example, the `myobject.cc` file must be added to the |
1147 | 1181 |
|
1148 | 1182 | Test it with: |
1149 | 1183 |
|
| 1184 | +<!-- addon-verify-file factory_of_wrapped_objects/test.js --> |
| 1185 | + |
1150 | 1186 | ```js |
1151 | 1187 | // test.js |
1152 | 1188 | const createObject = require('./build/Release/addon'); |
@@ -1175,6 +1211,8 @@ wrapped objects around by unwrapping them with the Node.js helper function |
1175 | 1211 | `node::ObjectWrap::Unwrap`. The following examples shows a function `add()` |
1176 | 1212 | that can take two `MyObject` objects as input arguments: |
1177 | 1213 |
|
| 1214 | +<!-- addon-verify-file passing_wrapped_objects_around/addon.cc --> |
| 1215 | + |
1178 | 1216 | ```cpp |
1179 | 1217 | // addon.cc |
1180 | 1218 | #include <node.h> |
@@ -1224,6 +1262,8 @@ NODE_MODULE(NODE_GYP_MODULE_NAME, InitAll) |
1224 | 1262 | In `myobject.h`, a new public method is added to allow access to private values |
1225 | 1263 | after unwrapping the object. |
1226 | 1264 |
|
| 1265 | +<!-- addon-verify-file passing_wrapped_objects_around/myobject.h --> |
| 1266 | +
|
1227 | 1267 | ```cpp |
1228 | 1268 | // myobject.h |
1229 | 1269 | #ifndef MYOBJECT_H |
@@ -1256,6 +1296,8 @@ class MyObject : public node::ObjectWrap { |
1256 | 1296 |
|
1257 | 1297 | The implementation of `myobject.cc` remains similar to the previous version: |
1258 | 1298 |
|
| 1299 | +<!-- addon-verify-file passing_wrapped_objects_around/myobject.cc --> |
| 1300 | + |
1259 | 1301 | ```cpp |
1260 | 1302 | // myobject.cc |
1261 | 1303 | #include <node.h> |
@@ -1340,6 +1382,8 @@ void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) { |
1340 | 1382 |
|
1341 | 1383 | Test it with: |
1342 | 1384 |
|
| 1385 | +<!-- addon-verify-file passing_wrapped_objects_around/test.js --> |
| 1386 | +
|
1343 | 1387 | ```js |
1344 | 1388 | // test.js |
1345 | 1389 | const addon = require('./build/Release/addon'); |
|