| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent bb6293a commit b36e55a
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -391,17 +391,15 @@ DOC_KIT ?= tools/doc/node_modules/@node-core/doc-kit/bin/cli.mjs | |||
| 391 | 391 | ||
| 392 | 392 | node_use_openssl_and_icu = $(call available-node,"-p" \ | |
| 393 | 393 | "process.versions.openssl != undefined && process.versions.icu != undefined") | |
| 394 | - test/addons/.docbuildstamp: $(DOCBUILDSTAMP_PREREQS) tools/doc/node_modules | ||
| 394 | + test/addons/.docbuildstamp: $(DOCBUILDSTAMP_PREREQS) tools/doc/addon-verify.mjs | ||
| 395 | 395 | @if [ "$(shell $(node_use_openssl_and_icu))" != "true" ]; then \ | |
| 396 | 396 | echo "Skipping .docbuildstamp (no crypto and/or no ICU)"; \ | |
| 397 | 397 | else \ | |
| 398 | 398 | $(RM) -r test/addons/??_*/; \ | |
| 399 | 399 | $(call available-node, \ | |
| 400 | - $(DOC_KIT) generate \ | ||
| 401 | - -t addon-verify \ | ||
| 402 | - -i doc/api/addons.md \ | ||
| 403 | - -o test/addons/ \ | ||
| 404 | - --type-map doc/type-map.json \ | ||
| 400 | + tools/doc/addon-verify.mjs \ | ||
| 401 | + --input doc/api/addons.md \ | ||
| 402 | + --output test/addons/ \ | ||
| 405 | 403 | ) \ | |
| 406 | 404 | [ $$? -eq 0 ] && touch $@; \ | |
| 407 | 405 | fi | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -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'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,102 @@ | |||
| 1 | + #!/usr/bin/env node | ||
| 2 | + | ||
| 3 | + // Extracts C++ addon examples from doc/api/addons.md into numbered test | ||
| 4 | + // directories under test/addons/. | ||
| 5 | + // | ||
| 6 | + // Each code block to extract is preceded by a marker in the markdown: | ||
| 7 | + // | ||
| 8 | + // <!-- addon-verify-file worker_support/addon.cc --> | ||
| 9 | + // ```cpp | ||
| 10 | + // #include <node.h> | ||
| 11 | + // ... | ||
| 12 | + // ``` | ||
| 13 | + // | ||
| 14 | + // This produces test/addons/01_worker_support/addon.cc. | ||
| 15 | + // Sections are numbered in order of first appearance. | ||
| 16 | + | ||
| 17 | + import { mkdirSync, writeFileSync } from 'node:fs'; | ||
| 18 | + import { open } from 'node:fs/promises'; | ||
| 19 | + import { join } from 'node:path'; | ||
| 20 | + import { parseArgs } from 'node:util'; | ||
| 21 | + | ||
| 22 | + const { values } = parseArgs({ | ||
| 23 | + options: { | ||
| 24 | + input: { type: 'string' }, | ||
| 25 | + output: { type: 'string' }, | ||
| 26 | + }, | ||
| 27 | + }); | ||
| 28 | + | ||
| 29 | + if (!values.input || !values.output) { | ||
| 30 | + console.error('Usage: addon-verify.mjs --input <file> --output <dir>'); | ||
| 31 | + process.exit(1); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + const src = await open(values.input, 'r'); | ||
| 35 | + | ||
| 36 | + const MARKER_RE = /^<!--\s*addon-verify-file\s+(\S+?)\/(\S+)\s*-->$/; | ||
| 37 | + | ||
| 38 | + const entries = []; | ||
| 39 | + let nextBlockIsAddonVerifyFile = false; | ||
| 40 | + let expectedClosingFenceMarker; | ||
| 41 | + for await (const line of src.readLines()) { | ||
| 42 | + if (expectedClosingFenceMarker) { | ||
| 43 | + // We're inside a Addon snippet | ||
| 44 | + if (line === expectedClosingFenceMarker) { | ||
| 45 | + // End of the snippet | ||
| 46 | + expectedClosingFenceMarker = null; | ||
| 47 | + continue; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + entries.at(-1).content += `${line}\n`; | ||
| 51 | + } | ||
| 52 | + if (nextBlockIsAddonVerifyFile) { | ||
| 53 | + if (line) { | ||
| 54 | + expectedClosingFenceMarker = line.replace(/\w/g, ''); | ||
| 55 | + nextBlockIsAddonVerifyFile = false; | ||
| 56 | + } | ||
| 57 | + continue; | ||
| 58 | + } | ||
| 59 | + const match = MARKER_RE.exec(line); | ||
| 60 | + if (match) { | ||
| 61 | + nextBlockIsAddonVerifyFile = true; | ||
| 62 | + const [, dir, name] = match; | ||
| 63 | + entries.push({ dir, name, content: '' }); | ||
| 64 | + } | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + // Collect files grouped by section directory name. | ||
| 68 | + const sections = Map.groupBy(entries, (e) => e.dir); | ||
| 69 | + | ||
| 70 | + let idx = 0; | ||
| 71 | + for (const [name, files] of sections) { | ||
| 72 | + const dirName = `${String(++idx).padStart(2, '0')}_${name}`; | ||
| 73 | + const dir = join(values.output, dirName); | ||
| 74 | + mkdirSync(dir, { recursive: true }); | ||
| 75 | + | ||
| 76 | + for (const file of files) { | ||
| 77 | + let content = file.content; | ||
| 78 | + if (file.name === 'test.js') { | ||
| 79 | + content = | ||
| 80 | + "'use strict';\n" + | ||
| 81 | + "const common = require('../../common');\n" + | ||
| 82 | + content.replace( | ||
| 83 | + "'./build/Release/addon'", | ||
| 84 | + // eslint-disable-next-line no-template-curly-in-string | ||
| 85 | + '`./build/${common.buildType}/addon`', | ||
| 86 | + ); | ||
| 87 | + } | ||
| 88 | + writeFileSync(join(dir, file.name), content); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + // Generate binding.gyp | ||
| 92 | + const names = files.map((f) => f.name); | ||
| 93 | + writeFileSync(join(dir, 'binding.gyp'), JSON.stringify({ | ||
| 94 | + targets: [{ | ||
| 95 | + target_name: 'addon', | ||
| 96 | + sources: names, | ||
| 97 | + includes: ['../common.gypi'], | ||
| 98 | + }], | ||
| 99 | + })); | ||
| 100 | + | ||
| 101 | + console.log(`Generated ${dirName} with files: ${names.join(', ')}`); | ||
| 102 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -687,7 +687,7 @@ for /d %%F in (test\addons\??_*) do ( | |||
| 687 | 687 | rd /s /q %%F | |
| 688 | 688 | ) | |
| 689 | 689 | :: generate | |
| 690 | - %doc_kit_exe% generate -t addon-verify -i "file://%~dp0doc\api\addons.md" -o "file://%~dp0test\addons" --type-map "file://%~dp0doc\type-map.json" | ||
| 690 | + "%node_exe%" tools\doc\addon-verify.mjs --input "%~dp0doc\api\addons.md" --output "%~dp0test\addons" | ||
| 691 | 691 | if %errorlevel% neq 0 exit /b %errorlevel% | |
| 692 | 692 | :: building addons | |
| 693 | 693 | setlocal | |
| Back | FazBrowse Home | New Git URL |
0 commit comments