-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddons.html
More file actions
1323 lines (1150 loc) · 79.1 KB
/
addons.html
File metadata and controls
1323 lines (1150 loc) · 79.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>C++ Addons | Node.js v12.0.0 Documentation</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic">
<link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="assets/hljs.css">
<link rel="canonical" href="https://nodejs.org/api/addons.html">
</head>
<body class="alt apidoc" id="api-section-addons">
<div id="content" class="clearfix">
<div id="column2" class="interior">
<div id="intro" class="interior">
<a href="/" title="Go back to the home page">
Node.js
</a>
</div>
<ul>
<li><a href="documentation.html" class="nav-documentation">About these Docs</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage & Example</a></li>
</ul>
<div class="line"></div>
<ul>
<li><a href="assert.html" class="nav-assert">Assertion Testing</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async Hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons active">C++ Addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ Addons - N-API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child Processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command Line Options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="esm.html" class="nav-esm">ECMAScript Modules</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs">File System</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance Hooks</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query Strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String Decoder</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace Events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/Datagram</a></li>
<li><a href="url.html" class="nav-url">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker Threads</a></li>
<li><a href="zlib.html" class="nav-zlib">ZLIB</a></li>
</ul>
<div class="line"></div>
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">GitHub Repo & Issue Tracker</a></li>
</ul>
</div>
<div id="column1" data-id="addons" class="interior">
<header>
<h1>Node.js v12.0.0 Documentation</h1>
<div id="gtoc">
<ul>
<li>
<a href="index.html" name="toc">Index</a>
</li>
<li>
<a href="all.html">View on single page</a>
</li>
<li>
<a href="addons.json">View as JSON</a>
</li>
<li class="version-picker">
<a href="#">View another version <span>▼</span></a>
<ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v11.x/api/addons.html">11.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v10.x/api/addons.html">10.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v9.x/api/addons.html">9.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v8.x/api/addons.html">8.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v7.x/api/addons.html">7.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v6.x/api/addons.html">6.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v5.x/api/addons.html">5.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v4.x/api/addons.html">4.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/addons.html">0.12.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/addons.html">0.10.x</a></li></ol>
</li>
<li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/addons.md"><span class="github_icon"><svg height="16" width="16" viewBox="0 0 16.1 16.1" fill="currentColor"><path d="M8 0a8 8 0 0 0-2.5 15.6c.4 0 .5-.2.5-.4v-1.5c-2 .4-2.5-.5-2.7-1 0-.1-.5-.9-.8-1-.3-.2-.7-.6 0-.6.6 0 1 .6 1.2.8.7 1.2 1.9 1 2.4.7 0-.5.2-.9.5-1-1.8-.3-3.7-1-3.7-4 0-.9.3-1.6.8-2.2 0-.2-.3-1 .1-2 0 0 .7-.3 2.2.7a7.4 7.4 0 0 1 4 0c1.5-1 2.2-.8 2.2-.8.5 1.1.2 2 .1 2.1.5.6.8 1.3.8 2.2 0 3-1.9 3.7-3.6 4 .3.2.5.7.5 1.4v2.2c0 .2.1.5.5.4A8 8 0 0 0 16 8a8 8 0 0 0-8-8z"/></svg></span>Edit on GitHub</a></li>
</ul>
</div>
<hr>
</header>
<div id="toc">
<h2>Table of Contents</h2>
<ul>
<li>
<p><a href="#addons_c_addons">C++ Addons</a></p>
<ul>
<li>
<p><a href="#addons_hello_world">Hello world</a></p>
<ul>
<li><a href="#addons_context_aware_addons">Context-aware addons</a></li>
<li><a href="#addons_building">Building</a></li>
<li><a href="#addons_linking_to_node_js_own_dependencies">Linking to Node.js' own dependencies</a></li>
<li><a href="#addons_loading_addons_using_require">Loading Addons using require()</a></li>
</ul>
</li>
<li><a href="#addons_native_abstractions_for_node_js">Native Abstractions for Node.js</a></li>
<li><a href="#addons_n_api">N-API</a></li>
<li>
<p><a href="#addons_addon_examples">Addon examples</a></p>
<ul>
<li><a href="#addons_function_arguments">Function arguments</a></li>
<li><a href="#addons_callbacks">Callbacks</a></li>
<li><a href="#addons_object_factory">Object factory</a></li>
<li><a href="#addons_function_factory">Function factory</a></li>
<li><a href="#addons_wrapping_c_objects">Wrapping C++ objects</a></li>
<li><a href="#addons_factory_of_wrapped_objects">Factory of wrapped objects</a></li>
<li><a href="#addons_passing_wrapped_objects_around">Passing wrapped objects around</a></li>
<li>
<p><a href="#addons_atexit_hooks">AtExit hooks</a></p>
<ul>
<li><a href="#addons_void_atexit_callback_args">void AtExit(callback, args)</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div id="apicontent">
<h1>C++ Addons<span><a class="mark" href="#addons_c_addons" id="addons_c_addons">#</a></span></h1>
<p>Node.js Addons are dynamically-linked shared objects, written in C++, that
can be loaded into Node.js using the <a href="modules.html#modules_require"><code>require()</code></a> function, and used
just as if they were an ordinary Node.js module. They are used primarily to
provide an interface between JavaScript running in Node.js and C/C++ libraries.</p>
<p>At the moment, the method for implementing Addons is rather complicated,
involving knowledge of several components and APIs:</p>
<ul>
<li>
<p>V8: the C++ library Node.js currently uses to provide the
JavaScript implementation. V8 provides the mechanisms for creating objects,
calling functions, etc. V8's API is documented mostly in the
<code>v8.h</code> header file (<code>deps/v8/include/v8.h</code> in the Node.js source
tree), which is also available <a href="https://v8docs.nodesource.com/">online</a>.</p>
</li>
<li>
<p><a href="https://github.com/libuv/libuv">libuv</a>: The C library that implements the Node.js event loop, its worker
threads and all of the asynchronous behaviors of the platform. It also
serves as a cross-platform abstraction library, giving easy, POSIX-like
access across all major operating systems to many common system tasks, such
as interacting with the filesystem, sockets, timers, and system events. libuv
also provides a pthreads-like threading abstraction that may be used to
power more sophisticated asynchronous Addons that need to move beyond the
standard event loop. Addon authors are encouraged to think about how to
avoid blocking the event loop with I/O or other time-intensive tasks by
off-loading work via libuv to non-blocking system operations, worker threads
or a custom use of libuv's threads.</p>
</li>
<li>
<p>Internal Node.js libraries. Node.js itself exports a number of C++ APIs
that Addons can use — the most important of which is the
<code>node::ObjectWrap</code> class.</p>
</li>
<li>
<p>Node.js includes a number of other statically linked libraries including
OpenSSL. These other libraries are located in the <code>deps/</code> directory in the
Node.js source tree. Only the libuv, OpenSSL, V8 and zlib symbols are
purposefully re-exported by Node.js and may be used to various extents by
Addons.
See <a href="#addons_linking_to_node_js_own_dependencies">Linking to Node.js' own dependencies</a> for additional information.</p>
</li>
</ul>
<p>All of the following examples are available for <a href="https://github.com/nodejs/node-addon-examples">download</a> and may
be used as the starting-point for an Addon.</p>
<h2>Hello world<span><a class="mark" href="#addons_hello_world" id="addons_hello_world">#</a></span></h2>
<p>This "Hello world" example is a simple Addon, written in C++, that is the
equivalent of the following JavaScript code:</p>
<pre><code class="language-js"><span class="hljs-built_in">module</span>.exports.hello = <span class="hljs-function"><span class="hljs-params">()</span> =></span> <span class="hljs-string">'world'</span>;</code></pre>
<p>First, create the file <code>hello.cc</code>:</p>
<pre><code class="language-cpp"><span class="hljs-comment">// hello.cc</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><node.h></span></span>
<span class="hljs-keyword">namespace</span> demo {
<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
<span class="hljs-keyword">using</span> v8::Isolate;
<span class="hljs-keyword">using</span> v8::Local;
<span class="hljs-keyword">using</span> v8::NewStringType;
<span class="hljs-keyword">using</span> v8::Object;
<span class="hljs-keyword">using</span> v8::String;
<span class="hljs-keyword">using</span> v8::Value;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Method</span><span class="hljs-params">(<span class="hljs-keyword">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{
Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(String::NewFromUtf8(
isolate, <span class="hljs-string">"world"</span>, NewStringType::kNormal).ToLocalChecked());
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Initialize</span><span class="hljs-params">(Local<Object> exports)</span> </span>{
NODE_SET_METHOD(exports, <span class="hljs-string">"hello"</span>, Method);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
} <span class="hljs-comment">// namespace demo</span></code></pre>
<p>Note that all Node.js Addons must export an initialization function following
the pattern:</p>
<pre><code class="language-cpp"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Initialize</span><span class="hljs-params">(Local<Object> exports)</span></span>;
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)</code></pre>
<p>There is no semi-colon after <code>NODE_MODULE</code> as it's not a function (see
<code>node.h</code>).</p>
<p>The <code>module_name</code> must match the filename of the final binary (excluding
the <code>.node</code> suffix).</p>
<p>In the <code>hello.cc</code> example, then, the initialization function is <code>Initialize</code>
and the addon module name is <code>addon</code>.</p>
<p>When building addons with <code>node-gyp</code>, using the macro <code>NODE_GYP_MODULE_NAME</code> as
the first parameter of <code>NODE_MODULE()</code> will ensure that the name of the final
binary will be passed to <code>NODE_MODULE()</code>.</p>
<h3>Context-aware addons<span><a class="mark" href="#addons_context_aware_addons" id="addons_context_aware_addons">#</a></span></h3>
<p>There are environments in which Node.js addons may need to be loaded multiple
times in multiple contexts. For example, the <a href="https://electronjs.org/">Electron</a> runtime runs multiple
instances of Node.js in a single process. Each instance will have its own
<code>require()</code> cache, and thus each instance will need a native addon to behave
correctly when loaded via <code>require()</code>. From the addon's perspective, this means
that it must support multiple initializations.</p>
<p>A context-aware addon can be constructed by using the macro
<code>NODE_MODULE_INITIALIZER</code>, which expands to the name of a function which Node.js
will expect to find when it loads an addon. An addon can thus be initialized as
in the following example:</p>
<pre><code class="language-cpp"><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> v8;
<span class="hljs-keyword">extern</span> <span class="hljs-string">"C"</span> <span class="hljs-function">NODE_MODULE_EXPORT <span class="hljs-keyword">void</span>
<span class="hljs-title">NODE_MODULE_INITIALIZER</span><span class="hljs-params">(Local<Object> exports,
Local<Value> <span class="hljs-keyword">module</span>,
Local<Context> context)</span> </span>{
<span class="hljs-comment">/* Perform addon initialization steps here. */</span>
}</code></pre>
<p>Another option is to use the macro <code>NODE_MODULE_INIT()</code>, which will also
construct a context-aware addon. Unlike <code>NODE_MODULE()</code>, which is used to
construct an addon around a given addon initializer function,
<code>NODE_MODULE_INIT()</code> serves as the declaration of such an initializer to be
followed by a function body.</p>
<p>The following three variables may be used inside the function body following an
invocation of <code>NODE_MODULE_INIT()</code>:</p>
<ul>
<li><code>Local<Object> exports</code>,</li>
<li><code>Local<Value> module</code>, and</li>
<li><code>Local<Context> context</code></li>
</ul>
<p>The choice to build a context-aware addon carries with it the responsibility of
carefully managing global static data. Since the addon may be loaded multiple
times, potentially even from different threads, any global static data stored
in the addon must be properly protected, and must not contain any persistent
references to JavaScript objects. The reason for this is that JavaScript
objects are only valid in one context, and will likely cause a crash when
accessed from the wrong context or from a different thread than the one on which
they were created.</p>
<p>The context-aware addon can be structured to avoid global static data by
performing the following steps:</p>
<ul>
<li>defining a class which will hold per-addon-instance data. Such
a class should include a <code>v8::Persistent<v8::Object></code> which will hold a weak
reference to the addon's <code>exports</code> object. The callback associated with the weak
reference will then destroy the instance of the class.</li>
<li>constructing an instance of this class in the addon initializer such that the
<code>v8::Persistent<v8::Object></code> is set to the <code>exports</code> object.</li>
<li>storing the instance of the class in a <code>v8::External</code>, and</li>
<li>passing the <code>v8::External</code> to all methods exposed to JavaScript by passing it
to the <code>v8::FunctionTemplate</code> constructor which creates the native-backed
JavaScript functions. The <code>v8::FunctionTemplate</code> constructor's third parameter
accepts the <code>v8::External</code>.</li>
</ul>
<p>This will ensure that the per-addon-instance data reaches each binding that can
be called from JavaScript. The per-addon-instance data must also be passed into
any asynchronous callbacks the addon may create.</p>
<p>The following example illustrates the implementation of a context-aware addon:</p>
<pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><node.h></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> v8;
<span class="hljs-keyword">class</span> AddonData {
<span class="hljs-keyword">public</span>:
AddonData(Isolate* isolate, Local<Object> exports):
call_count(<span class="hljs-number">0</span>) {
<span class="hljs-comment">// Link the existence of this object instance to the existence of exports.</span>
exports_.Reset(isolate, exports);
exports_.SetWeak(<span class="hljs-keyword">this</span>, DeleteMe, WeakCallbackType::kParameter);
}
~AddonData() {
<span class="hljs-keyword">if</span> (!exports_.IsEmpty()) {
<span class="hljs-comment">// Reset the reference to avoid leaking data.</span>
exports_.ClearWeak();
exports_.Reset();
}
}
<span class="hljs-comment">// Per-addon data.</span>
<span class="hljs-keyword">int</span> call_count;
<span class="hljs-keyword">private</span>:
<span class="hljs-comment">// Method to call when "exports" is about to be garbage-collected.</span>
<span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">DeleteMe</span><span class="hljs-params">(<span class="hljs-keyword">const</span> WeakCallbackInfo<AddonData>& info)</span> </span>{
<span class="hljs-keyword">delete</span> info.GetParameter();
}
<span class="hljs-comment">// Weak handle to the "exports" object. An instance of this class will be</span>
<span class="hljs-comment">// destroyed along with the exports object to which it is weakly bound.</span>
v8::Persistent<v8::Object> exports_;
};
<span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Method</span><span class="hljs-params">(<span class="hljs-keyword">const</span> v8::FunctionCallbackInfo<v8::Value>& info)</span> </span>{
<span class="hljs-comment">// Retrieve the per-addon-instance data.</span>
AddonData* data =
<span class="hljs-keyword">reinterpret_cast</span><AddonData*>(info.Data().As<External>()->Value());
data->call_count++;
info.GetReturnValue().Set((<span class="hljs-keyword">double</span>)data->call_count);
}
<span class="hljs-comment">// Initialize this addon to be context-aware.</span>
NODE_MODULE_INIT(<span class="hljs-comment">/* exports, module, context */</span>) {
Isolate* isolate = context->GetIsolate();
<span class="hljs-comment">// Create a new instance of AddonData for this instance of the addon.</span>
AddonData* data = <span class="hljs-keyword">new</span> AddonData(isolate, exports);
<span class="hljs-comment">// Wrap the data in a v8::External so we can pass it to the method we expose.</span>
Local<External> external = External::New(isolate, data);
<span class="hljs-comment">// Expose the method "Method" to JavaScript, and make sure it receives the</span>
<span class="hljs-comment">// per-addon-instance data we created above by passing `external` as the</span>
<span class="hljs-comment">// third parameter to the FunctionTemplate constructor.</span>
exports->Set(context,
String::NewFromUtf8(isolate, <span class="hljs-string">"method"</span>, NewStringType::kNormal)
.ToLocalChecked(),
FunctionTemplate::New(isolate, Method, external)
->GetFunction(context).ToLocalChecked()).FromJust();
}</code></pre>
<h3>Building<span><a class="mark" href="#addons_building" id="addons_building">#</a></span></h3>
<p>Once the source code has been written, it must be compiled into the binary
<code>addon.node</code> file. To do so, create a file called <code>binding.gyp</code> in the
top-level of the project describing the build configuration of the module
using a JSON-like format. This file is used by <a href="https://github.com/nodejs/node-gyp">node-gyp</a> — a tool written
specifically to compile Node.js Addons.</p>
<pre><code class="language-json">{
<span class="hljs-attr">"targets"</span>: [
{
<span class="hljs-attr">"target_name"</span>: <span class="hljs-string">"addon"</span>,
<span class="hljs-attr">"sources"</span>: [ <span class="hljs-string">"hello.cc"</span> ]
}
]
}</code></pre>
<p>A version of the <code>node-gyp</code> utility is bundled and distributed with
Node.js as part of <code>npm</code>. This version is not made directly available for
developers to use and is intended only to support the ability to use the
<code>npm install</code> command to compile and install Addons. Developers who wish to
use <code>node-gyp</code> directly can install it using the command
<code>npm install -g node-gyp</code>. See the <code>node-gyp</code> <a href="https://github.com/nodejs/node-gyp#installation">installation instructions</a> for
more information, including platform-specific requirements.</p>
<p>Once the <code>binding.gyp</code> file has been created, use <code>node-gyp configure</code> to
generate the appropriate project build files for the current platform. This
will generate either a <code>Makefile</code> (on Unix platforms) or a <code>vcxproj</code> file
(on Windows) in the <code>build/</code> directory.</p>
<p>Next, invoke the <code>node-gyp build</code> command to generate the compiled <code>addon.node</code>
file. This will be put into the <code>build/Release/</code> directory.</p>
<p>When using <code>npm install</code> to install a Node.js Addon, npm uses its own bundled
version of <code>node-gyp</code> to perform this same set of actions, generating a
compiled version of the Addon for the user's platform on demand.</p>
<p>Once built, the binary Addon can be used from within Node.js by pointing
<a href="modules.html#modules_require"><code>require()</code></a> to the built <code>addon.node</code> module:</p>
<pre><code class="language-js"><span class="hljs-comment">// hello.js</span>
<span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>);
<span class="hljs-built_in">console</span>.log(addon.hello());
<span class="hljs-comment">// Prints: 'world'</span></code></pre>
<p>Please see the examples below for further information or
<a href="https://github.com/arturadib/node-qt">https://github.com/arturadib/node-qt</a> for an example in production.</p>
<p>Because the exact path to the compiled Addon binary can vary depending on how
it is compiled (i.e. sometimes it may be in <code>./build/Debug/</code>), Addons can use
the <a href="https://github.com/TooTallNate/node-bindings">bindings</a> package to load the compiled module.</p>
<p>Note that while the <code>bindings</code> package implementation is more sophisticated
in how it locates Addon modules, it is essentially using a try-catch pattern
similar to:</p>
<pre><code class="language-js"><span class="hljs-keyword">try</span> {
<span class="hljs-keyword">return</span> <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon.node'</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-keyword">return</span> <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Debug/addon.node'</span>);
}</code></pre>
<h3>Linking to Node.js' own dependencies<span><a class="mark" href="#addons_linking_to_node_js_own_dependencies" id="addons_linking_to_node_js_own_dependencies">#</a></span></h3>
<p>Node.js uses a number of statically linked libraries such as V8, libuv and
OpenSSL. All Addons are required to link to V8 and may link to any of the
other dependencies as well. Typically, this is as simple as including
the appropriate <code>#include <...></code> statements (e.g. <code>#include <v8.h></code>) and
<code>node-gyp</code> will locate the appropriate headers automatically. However, there
are a few caveats to be aware of:</p>
<ul>
<li>
<p>When <code>node-gyp</code> runs, it will detect the specific release version of Node.js
and download either the full source tarball or just the headers. If the full
source is downloaded, Addons will have complete access to the full set of
Node.js dependencies. However, if only the Node.js headers are downloaded, then
only the symbols exported by Node.js will be available.</p>
</li>
<li>
<p><code>node-gyp</code> can be run using the <code>--nodedir</code> flag pointing at a local Node.js
source image. Using this option, the Addon will have access to the full set of
dependencies.</p>
</li>
</ul>
<h3>Loading Addons using require()<span><a class="mark" href="#addons_loading_addons_using_require" id="addons_loading_addons_using_require">#</a></span></h3>
<p>The filename extension of the compiled Addon binary is <code>.node</code> (as opposed
to <code>.dll</code> or <code>.so</code>). The <a href="modules.html#modules_require"><code>require()</code></a> function is written to look for
files with the <code>.node</code> file extension and initialize those as dynamically-linked
libraries.</p>
<p>When calling <a href="modules.html#modules_require"><code>require()</code></a>, the <code>.node</code> 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 <code>addon.js</code> in the same directory as the binary <code>addon.node</code>,
then <a href="modules.html#modules_require"><code>require('addon')</code></a> will give precedence to the <code>addon.js</code> file
and load it instead.</p>
<h2>Native Abstractions for Node.js<span><a class="mark" href="#addons_native_abstractions_for_node_js" id="addons_native_abstractions_for_node_js">#</a></span></h2>
<p>Each of the examples illustrated in this document make direct use of the
Node.js and V8 APIs for implementing Addons. It is important to understand
that the V8 API can, and has, changed dramatically from one V8 release to the
next (and one major Node.js release to the next). With each change, Addons may
need to be updated and recompiled in order to continue functioning. The Node.js
release schedule is designed to minimize the frequency and impact of such
changes but there is little that Node.js can do currently to ensure stability
of the V8 APIs.</p>
<p>The <a href="https://github.com/nodejs/nan">Native Abstractions for Node.js</a> (or <code>nan</code>) provide a set of tools that
Addon developers are recommended to use to keep compatibility between past and
future releases of V8 and Node.js. See the <code>nan</code> <a href="https://github.com/nodejs/nan/tree/master/examples/">examples</a> for an
illustration of how it can be used.</p>
<h2>N-API<span><a class="mark" href="#addons_n_api" id="addons_n_api">#</a></span></h2>
<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
<p>N-API is an API for building native Addons. It is independent from
the underlying JavaScript runtime (e.g. V8) and is maintained as part of
Node.js itself. This API will be Application Binary Interface (ABI) stable
across versions of Node.js. It is intended to insulate Addons from
changes in the underlying JavaScript engine and allow modules
compiled for one version to run on later versions of Node.js without
recompilation. Addons are built/packaged with the same approach/tools
outlined in this document (node-gyp, etc.). The only difference is the
set of APIs that are used by the native code. Instead of using the V8
or <a href="https://github.com/nodejs/nan">Native Abstractions for Node.js</a> APIs, the functions available
in the N-API are used.</p>
<p>Creating and maintaining an addon that benefits from the ABI stability
provided by N-API carries with it certain
<a href="n-api.html#n_api_implications_of_abi_stability">implementation considerations</a>.</p>
<p>To use N-API in the above "Hello world" example, replace the content of
<code>hello.cc</code> with the following. All other instructions remain the same.</p>
<pre><code class="language-cpp"><span class="hljs-comment">// hello.cc using N-API</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><node_api.h></span></span>
<span class="hljs-keyword">namespace</span> demo {
<span class="hljs-function">napi_value <span class="hljs-title">Method</span><span class="hljs-params">(napi_env env, napi_callback_info args)</span> </span>{
napi_value greeting;
napi_status status;
status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &greeting);
<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">nullptr</span>;
<span class="hljs-keyword">return</span> greeting;
}
<span class="hljs-function">napi_value <span class="hljs-title">init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> </span>{
napi_status status;
napi_value fn;
status = napi_create_function(env, <span class="hljs-literal">nullptr</span>, <span class="hljs-number">0</span>, Method, <span class="hljs-literal">nullptr</span>, &fn);
<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">nullptr</span>;
status = napi_set_named_property(env, exports, <span class="hljs-string">"hello"</span>, fn);
<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">nullptr</span>;
<span class="hljs-keyword">return</span> exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, init)
} <span class="hljs-comment">// namespace demo</span></code></pre>
<p>The functions available and how to use them are documented in the
section titled <a href="n-api.html">C/C++ Addons - N-API</a>.</p>
<h2>Addon examples<span><a class="mark" href="#addons_addon_examples" id="addons_addon_examples">#</a></span></h2>
<p>Following are some example Addons intended to help developers get started. The
examples make use of the V8 APIs. Refer to the online <a href="https://v8docs.nodesource.com/">V8 reference</a>
for help with the various V8 calls, and V8's <a href="https://github.com/v8/v8/wiki/Embedder's%20Guide">Embedder's Guide</a> for an
explanation of several concepts used such as handles, scopes, function
templates, etc.</p>
<p>Each of these examples using the following <code>binding.gyp</code> file:</p>
<pre><code class="language-json">{
<span class="hljs-attr">"targets"</span>: [
{
<span class="hljs-attr">"target_name"</span>: <span class="hljs-string">"addon"</span>,
<span class="hljs-attr">"sources"</span>: [ <span class="hljs-string">"addon.cc"</span> ]
}
]
}</code></pre>
<p>In cases where there is more than one <code>.cc</code> file, simply add the additional
filename to the <code>sources</code> array:</p>
<pre><code class="language-json">"sources": ["addon.cc", "myexample.cc"]</code></pre>
<p>Once the <code>binding.gyp</code> file is ready, the example Addons can be configured and
built using <code>node-gyp</code>:</p>
<pre><code class="language-console">$ node-gyp configure build</code></pre>
<h3>Function arguments<span><a class="mark" href="#addons_function_arguments" id="addons_function_arguments">#</a></span></h3>
<p>Addons will typically expose objects and functions that can be accessed from
JavaScript running within Node.js. When functions are invoked from JavaScript,
the input arguments and return value must be mapped to and from the C/C++
code.</p>
<p>The following example illustrates how to read function arguments passed from
JavaScript and how to return a result:</p>
<pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><node.h></span></span>
<span class="hljs-keyword">namespace</span> demo {
<span class="hljs-keyword">using</span> v8::Exception;
<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
<span class="hljs-keyword">using</span> v8::Isolate;
<span class="hljs-keyword">using</span> v8::Local;
<span class="hljs-keyword">using</span> v8::NewStringType;
<span class="hljs-keyword">using</span> v8::Number;
<span class="hljs-keyword">using</span> v8::Object;
<span class="hljs-keyword">using</span> v8::String;
<span class="hljs-keyword">using</span> v8::Value;
<span class="hljs-comment">// This is the implementation of the "add" method</span>
<span class="hljs-comment">// Input arguments are passed using the</span>
<span class="hljs-comment">// const FunctionCallbackInfo<Value>& args struct</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Add</span><span class="hljs-params">(<span class="hljs-keyword">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{
Isolate* isolate = args.GetIsolate();
<span class="hljs-comment">// Check the number of arguments passed.</span>
<span class="hljs-keyword">if</span> (args.Length() < <span class="hljs-number">2</span>) {
<span class="hljs-comment">// Throw an Error that is passed back to JavaScript</span>
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate,
<span class="hljs-string">"Wrong number of arguments"</span>,
NewStringType::kNormal).ToLocalChecked()));
<span class="hljs-keyword">return</span>;
}
<span class="hljs-comment">// Check the argument types</span>
<span class="hljs-keyword">if</span> (!args[<span class="hljs-number">0</span>]->IsNumber() || !args[<span class="hljs-number">1</span>]->IsNumber()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate,
<span class="hljs-string">"Wrong arguments"</span>,
NewStringType::kNormal).ToLocalChecked()));
<span class="hljs-keyword">return</span>;
}
<span class="hljs-comment">// Perform the operation</span>
<span class="hljs-keyword">double</span> value =
args[<span class="hljs-number">0</span>].As<Number>()->Value() + args[<span class="hljs-number">1</span>].As<Number>()->Value();
Local<Number> num = Number::New(isolate, value);
<span class="hljs-comment">// Set the return value (using the passed in</span>
<span class="hljs-comment">// FunctionCallbackInfo<Value>&)</span>
args.GetReturnValue().Set(num);
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Init</span><span class="hljs-params">(Local<Object> exports)</span> </span>{
NODE_SET_METHOD(exports, <span class="hljs-string">"add"</span>, Add);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
} <span class="hljs-comment">// namespace demo</span></code></pre>
<p>Once compiled, the example Addon can be required and used from within Node.js:</p>
<pre><code class="language-js"><span class="hljs-comment">// test.js</span>
<span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'This should be eight:'</span>, addon.add(<span class="hljs-number">3</span>, <span class="hljs-number">5</span>));</code></pre>
<h3>Callbacks<span><a class="mark" href="#addons_callbacks" id="addons_callbacks">#</a></span></h3>
<p>It is common practice within Addons to pass JavaScript functions to a C++
function and execute them from there. The following example illustrates how
to invoke such callbacks:</p>
<pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><node.h></span></span>
<span class="hljs-keyword">namespace</span> demo {
<span class="hljs-keyword">using</span> v8::Context;
<span class="hljs-keyword">using</span> v8::Function;
<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
<span class="hljs-keyword">using</span> v8::Isolate;
<span class="hljs-keyword">using</span> v8::Local;
<span class="hljs-keyword">using</span> v8::NewStringType;
<span class="hljs-keyword">using</span> v8::Null;
<span class="hljs-keyword">using</span> v8::Object;
<span class="hljs-keyword">using</span> v8::String;
<span class="hljs-keyword">using</span> v8::Value;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">RunCallback</span><span class="hljs-params">(<span class="hljs-keyword">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Local<Function> cb = Local<Function>::Cast(args[<span class="hljs-number">0</span>]);
<span class="hljs-keyword">const</span> <span class="hljs-keyword">unsigned</span> argc = <span class="hljs-number">1</span>;
Local<Value> argv[argc] = {
String::NewFromUtf8(isolate,
<span class="hljs-string">"hello world"</span>,
NewStringType::kNormal).ToLocalChecked() };
cb->Call(context, Null(isolate), argc, argv).ToLocalChecked();
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Init</span><span class="hljs-params">(Local<Object> exports, Local<Object> <span class="hljs-keyword">module</span>)</span> </span>{
NODE_SET_METHOD(<span class="hljs-keyword">module</span>, <span class="hljs-string">"exports"</span>, RunCallback);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
} <span class="hljs-comment">// namespace demo</span></code></pre>
<p>Note that this example uses a two-argument form of <code>Init()</code> that receives
the full <code>module</code> object as the second argument. This allows the Addon
to completely overwrite <code>exports</code> with a single function instead of
adding the function as a property of <code>exports</code>.</p>
<p>To test it, run the following JavaScript:</p>
<pre><code class="language-js"><span class="hljs-comment">// test.js</span>
<span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>);
addon(<span class="hljs-function">(<span class="hljs-params">msg</span>) =></span> {
<span class="hljs-built_in">console</span>.log(msg);
<span class="hljs-comment">// Prints: 'hello world'</span>
});</code></pre>
<p>Note that, in this example, the callback function is invoked synchronously.</p>
<h3>Object factory<span><a class="mark" href="#addons_object_factory" id="addons_object_factory">#</a></span></h3>
<p>Addons can create and return new objects from within a C++ function as
illustrated in the following example. An object is created and returned with a
property <code>msg</code> that echoes the string passed to <code>createObject()</code>:</p>
<pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><node.h></span></span>
<span class="hljs-keyword">namespace</span> demo {
<span class="hljs-keyword">using</span> v8::Context;
<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
<span class="hljs-keyword">using</span> v8::Isolate;
<span class="hljs-keyword">using</span> v8::Local;
<span class="hljs-keyword">using</span> v8::NewStringType;
<span class="hljs-keyword">using</span> v8::Object;
<span class="hljs-keyword">using</span> v8::String;
<span class="hljs-keyword">using</span> v8::Value;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">CreateObject</span><span class="hljs-params">(<span class="hljs-keyword">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Local<Object> obj = Object::New(isolate);
obj->Set(context,
String::NewFromUtf8(isolate,
<span class="hljs-string">"msg"</span>,
NewStringType::kNormal).ToLocalChecked(),
args[<span class="hljs-number">0</span>]->ToString(context).ToLocalChecked())
.FromJust();
args.GetReturnValue().Set(obj);
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Init</span><span class="hljs-params">(Local<Object> exports, Local<Object> <span class="hljs-keyword">module</span>)</span> </span>{
NODE_SET_METHOD(<span class="hljs-keyword">module</span>, <span class="hljs-string">"exports"</span>, CreateObject);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
} <span class="hljs-comment">// namespace demo</span></code></pre>
<p>To test it in JavaScript:</p>
<pre><code class="language-js"><span class="hljs-comment">// test.js</span>
<span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>);
<span class="hljs-keyword">const</span> obj1 = addon(<span class="hljs-string">'hello'</span>);
<span class="hljs-keyword">const</span> obj2 = addon(<span class="hljs-string">'world'</span>);
<span class="hljs-built_in">console</span>.log(obj1.msg, obj2.msg);
<span class="hljs-comment">// Prints: 'hello world'</span></code></pre>
<h3>Function factory<span><a class="mark" href="#addons_function_factory" id="addons_function_factory">#</a></span></h3>
<p>Another common scenario is creating JavaScript functions that wrap C++
functions and returning those back to JavaScript:</p>
<pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><node.h></span></span>
<span class="hljs-keyword">namespace</span> demo {
<span class="hljs-keyword">using</span> v8::Context;
<span class="hljs-keyword">using</span> v8::Function;
<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
<span class="hljs-keyword">using</span> v8::FunctionTemplate;
<span class="hljs-keyword">using</span> v8::Isolate;
<span class="hljs-keyword">using</span> v8::Local;
<span class="hljs-keyword">using</span> v8::NewStringType;
<span class="hljs-keyword">using</span> v8::Object;
<span class="hljs-keyword">using</span> v8::String;
<span class="hljs-keyword">using</span> v8::Value;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">MyFunction</span><span class="hljs-params">(<span class="hljs-keyword">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{
Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(String::NewFromUtf8(
isolate, <span class="hljs-string">"hello world"</span>, NewStringType::kNormal).ToLocalChecked());
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">CreateFunction</span><span class="hljs-params">(<span class="hljs-keyword">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, MyFunction);
Local<Function> fn = tpl->GetFunction(context).ToLocalChecked();
<span class="hljs-comment">// omit this to make it anonymous</span>
fn->SetName(String::NewFromUtf8(
isolate, <span class="hljs-string">"theFunction"</span>, NewStringType::kNormal).ToLocalChecked());
args.GetReturnValue().Set(fn);
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Init</span><span class="hljs-params">(Local<Object> exports, Local<Object> <span class="hljs-keyword">module</span>)</span> </span>{
NODE_SET_METHOD(<span class="hljs-keyword">module</span>, <span class="hljs-string">"exports"</span>, CreateFunction);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
} <span class="hljs-comment">// namespace demo</span></code></pre>
<p>To test:</p>
<pre><code class="language-js"><span class="hljs-comment">// test.js</span>
<span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>);
<span class="hljs-keyword">const</span> fn = addon();
<span class="hljs-built_in">console</span>.log(fn());
<span class="hljs-comment">// Prints: 'hello world'</span></code></pre>
<h3>Wrapping C++ objects<span><a class="mark" href="#addons_wrapping_c_objects" id="addons_wrapping_c_objects">#</a></span></h3>
<p>It is also possible to wrap C++ objects/classes in a way that allows new
instances to be created using the JavaScript <code>new</code> operator:</p>
<pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><node.h></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"myobject.h"</span></span>
<span class="hljs-keyword">namespace</span> demo {
<span class="hljs-keyword">using</span> v8::Local;
<span class="hljs-keyword">using</span> v8::Object;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">InitAll</span><span class="hljs-params">(Local<Object> exports)</span> </span>{
MyObject::Init(exports);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, InitAll)
} <span class="hljs-comment">// namespace demo</span></code></pre>
<p>Then, in <code>myobject.h</code>, the wrapper class inherits from <code>node::ObjectWrap</code>:</p>
<pre><code class="language-cpp"><span class="hljs-comment">// myobject.h</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">ifndef</span> MYOBJECT_H</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MYOBJECT_H</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><node.h></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><node_object_wrap.h></span></span>
<span class="hljs-keyword">namespace</span> demo {
<span class="hljs-keyword">class</span> MyObject : <span class="hljs-keyword">public</span> node::ObjectWrap {
<span class="hljs-keyword">public</span>:
<span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Init</span><span class="hljs-params">(v8::Local<v8::Object> exports)</span></span>;
<span class="hljs-keyword">private</span>:
<span class="hljs-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">MyObject</span><span class="hljs-params">(<span class="hljs-keyword">double</span> value = <span class="hljs-number">0</span>)</span></span>;
~MyObject();
<span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">New</span><span class="hljs-params">(<span class="hljs-keyword">const</span> v8::FunctionCallbackInfo<v8::Value>& args)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">PlusOne</span><span class="hljs-params">(<span class="hljs-keyword">const</span> v8::FunctionCallbackInfo<v8::Value>& args)</span></span>;
<span class="hljs-keyword">static</span> v8::Persistent<v8::Function> constructor;
<span class="hljs-keyword">double</span> value_;
};
} <span class="hljs-comment">// namespace demo</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span></code></pre>
<p>In <code>myobject.cc</code>, implement the various methods that are to be exposed.
Below, the method <code>plusOne()</code> is exposed by adding it to the constructor's
prototype:</p>
<pre><code class="language-cpp"><span class="hljs-comment">// myobject.cc</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"myobject.h"</span></span>
<span class="hljs-keyword">namespace</span> demo {
<span class="hljs-keyword">using</span> v8::Context;
<span class="hljs-keyword">using</span> v8::Function;
<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
<span class="hljs-keyword">using</span> v8::FunctionTemplate;
<span class="hljs-keyword">using</span> v8::Isolate;
<span class="hljs-keyword">using</span> v8::Local;
<span class="hljs-keyword">using</span> v8::NewStringType;
<span class="hljs-keyword">using</span> v8::Number;
<span class="hljs-keyword">using</span> v8::Object;
<span class="hljs-keyword">using</span> v8::Persistent;
<span class="hljs-keyword">using</span> v8::String;
<span class="hljs-keyword">using</span> v8::Value;
Persistent<Function> MyObject::constructor;
MyObject::MyObject(<span class="hljs-keyword">double</span> value) : value_(value) {
}
MyObject::~MyObject() {
}
<span class="hljs-keyword">void</span> MyObject::Init(Local<Object> exports) {
Isolate* isolate = exports->GetIsolate();
<span class="hljs-comment">// Prepare constructor template</span>
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
tpl->SetClassName(String::NewFromUtf8(
isolate, <span class="hljs-string">"MyObject"</span>, NewStringType::kNormal).ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(<span class="hljs-number">1</span>);
<span class="hljs-comment">// Prototype</span>
NODE_SET_PROTOTYPE_METHOD(tpl, <span class="hljs-string">"plusOne"</span>, PlusOne);
Local<Context> context = isolate->GetCurrentContext();
constructor.Reset(isolate, tpl->GetFunction(context).ToLocalChecked());
exports->Set(context, String::NewFromUtf8(
isolate, <span class="hljs-string">"MyObject"</span>, NewStringType::kNormal).ToLocalChecked(),
tpl->GetFunction(context).ToLocalChecked()).FromJust();
}
<span class="hljs-keyword">void</span> MyObject::New(<span class="hljs-keyword">const</span> FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
<span class="hljs-keyword">if</span> (args.IsConstructCall()) {
<span class="hljs-comment">// Invoked as constructor: `new MyObject(...)`</span>
<span class="hljs-keyword">double</span> value = args[<span class="hljs-number">0</span>]->IsUndefined() ?
<span class="hljs-number">0</span> : args[<span class="hljs-number">0</span>]->NumberValue(context).FromMaybe(<span class="hljs-number">0</span>);
MyObject* obj = <span class="hljs-keyword">new</span> MyObject(value);
obj->Wrap(args.This());
args.GetReturnValue().Set(args.This());
} <span class="hljs-keyword">else</span> {
<span class="hljs-comment">// Invoked as plain function `MyObject(...)`, turn into construct call.</span>
<span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> argc = <span class="hljs-number">1</span>;
Local<Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
Local<Function> cons = Local<Function>::New(isolate, constructor);
Local<Object> result =
cons->NewInstance(context, argc, argv).ToLocalChecked();
args.GetReturnValue().Set(result);
}
}
<span class="hljs-keyword">void</span> MyObject::PlusOne(<span class="hljs-keyword">const</span> FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
obj->value_ += <span class="hljs-number">1</span>;
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
}
} <span class="hljs-comment">// namespace demo</span></code></pre>
<p>To build this example, the <code>myobject.cc</code> file must be added to the
<code>binding.gyp</code>:</p>
<pre><code class="language-json">{
<span class="hljs-attr">"targets"</span>: [
{
<span class="hljs-attr">"target_name"</span>: <span class="hljs-string">"addon"</span>,
<span class="hljs-attr">"sources"</span>: [
<span class="hljs-string">"addon.cc"</span>,
<span class="hljs-string">"myobject.cc"</span>
]
}
]
}</code></pre>
<p>Test it with:</p>
<pre><code class="language-js"><span class="hljs-comment">// test.js</span>
<span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>);
<span class="hljs-keyword">const</span> obj = <span class="hljs-keyword">new</span> addon.MyObject(<span class="hljs-number">10</span>);
<span class="hljs-built_in">console</span>.log(obj.plusOne());
<span class="hljs-comment">// Prints: 11</span>
<span class="hljs-built_in">console</span>.log(obj.plusOne());
<span class="hljs-comment">// Prints: 12</span>
<span class="hljs-built_in">console</span>.log(obj.plusOne());
<span class="hljs-comment">// Prints: 13</span></code></pre>
<p>The destructor for a wrapper object will run when the object is
garbage-collected. For destructor testing, there are command-line flags that
can be used to make it possible to force garbage collection. These flags are
provided by the underlying V8 JavaScript engine. They are subject to change
or removal at any time. They are not documented by Node.js or V8, and they
should never be used outside of testing.</p>
<h3>Factory of wrapped objects<span><a class="mark" href="#addons_factory_of_wrapped_objects" id="addons_factory_of_wrapped_objects">#</a></span></h3>
<p>Alternatively, it is possible to use a factory pattern to avoid explicitly
creating object instances using the JavaScript <code>new</code> operator:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> obj = addon.createObject();
<span class="hljs-comment">// instead of:</span>
<span class="hljs-comment">// const obj = new addon.Object();</span></code></pre>
<p>First, the <code>createObject()</code> method is implemented in <code>addon.cc</code>:</p>
<pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><node.h></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"myobject.h"</span></span>
<span class="hljs-keyword">namespace</span> demo {
<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
<span class="hljs-keyword">using</span> v8::Isolate;
<span class="hljs-keyword">using</span> v8::Local;
<span class="hljs-keyword">using</span> v8::Object;
<span class="hljs-keyword">using</span> v8::String;
<span class="hljs-keyword">using</span> v8::Value;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">CreateObject</span><span class="hljs-params">(<span class="hljs-keyword">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{
MyObject::NewInstance(args);
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">InitAll</span><span class="hljs-params">(Local<Object> exports, Local<Object> <span class="hljs-keyword">module</span>)</span> </span>{
MyObject::Init(exports->GetIsolate());
NODE_SET_METHOD(<span class="hljs-keyword">module</span>, <span class="hljs-string">"exports"</span>, CreateObject);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, InitAll)
} <span class="hljs-comment">// namespace demo</span></code></pre>
<p>In <code>myobject.h</code>, the static method <code>NewInstance()</code> is added to handle
instantiating the object. This method takes the place of using <code>new</code> in
JavaScript:</p>
<pre><code class="language-cpp"><span class="hljs-comment">// myobject.h</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">ifndef</span> MYOBJECT_H</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MYOBJECT_H</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><node.h></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><node_object_wrap.h></span></span>
<span class="hljs-keyword">namespace</span> demo {
<span class="hljs-keyword">class</span> MyObject : <span class="hljs-keyword">public</span> node::ObjectWrap {
<span class="hljs-keyword">public</span>:
<span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Init</span><span class="hljs-params">(v8::Isolate* isolate)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">NewInstance</span><span class="hljs-params">(<span class="hljs-keyword">const</span> v8::FunctionCallbackInfo<v8::Value>& args)</span></span>;
<span class="hljs-keyword">private</span>:
<span class="hljs-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">MyObject</span><span class="hljs-params">(<span class="hljs-keyword">double</span> value = <span class="hljs-number">0</span>)</span></span>;
~MyObject();
<span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">New</span><span class="hljs-params">(<span class="hljs-keyword">const</span> v8::FunctionCallbackInfo<v8::Value>& args)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">PlusOne</span><span class="hljs-params">(<span class="hljs-keyword">const</span> v8::FunctionCallbackInfo<v8::Value>& args)</span></span>;
<span class="hljs-keyword">static</span> v8::Persistent<v8::Function> constructor;
<span class="hljs-keyword">double</span> value_;
};
} <span class="hljs-comment">// namespace demo</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span></code></pre>
<p>The implementation in <code>myobject.cc</code> is similar to the previous example:</p>
<pre><code class="language-cpp"><span class="hljs-comment">// myobject.cc</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><node.h></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"myobject.h"</span></span>
<span class="hljs-keyword">namespace</span> demo {
<span class="hljs-keyword">using</span> v8::Context;
<span class="hljs-keyword">using</span> v8::Function;
<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
<span class="hljs-keyword">using</span> v8::FunctionTemplate;
<span class="hljs-keyword">using</span> v8::Isolate;
<span class="hljs-keyword">using</span> v8::Local;
<span class="hljs-keyword">using</span> v8::NewStringType;
<span class="hljs-keyword">using</span> v8::Number;
<span class="hljs-keyword">using</span> v8::Object;
<span class="hljs-keyword">using</span> v8::Persistent;
<span class="hljs-keyword">using</span> v8::String;
<span class="hljs-keyword">using</span> v8::Value;
Persistent<Function> MyObject::constructor;
MyObject::MyObject(<span class="hljs-keyword">double</span> value) : value_(value) {
}
MyObject::~MyObject() {
}
<span class="hljs-keyword">void</span> MyObject::Init(Isolate* isolate) {