@@ -41,7 +41,15 @@ First we create a file `hello.cc`:
41
41
// hello.cc
42
42
#include <node.h>
43
43
44
- using namespace v8;
44
+ namespace demo {
45
+
46
+ using v8::FunctionCallbackInfo;
47
+ using v8::HandleScope;
48
+ using v8::Isolate;
49
+ using v8::Local;
50
+ using v8::Object;
51
+ using v8::String;
52
+ using v8::Value;
45
53
46
54
void Method(const FunctionCallbackInfo<Value>& args) {
47
55
Isolate* isolate = args.GetIsolate();
@@ -54,6 +62,8 @@ First we create a file `hello.cc`:
54
62
55
63
NODE_MODULE(addon, init)
56
64
65
+ } // namespace demo
66
+
57
67
Note that all io.js addons must export an initialization function:
58
68
59
69
void Initialize(Local<Object> exports);
@@ -141,7 +151,17 @@ function calls and return a result. This is the main and only needed source
141
151
// addon.cc
142
152
#include <node.h>
143
153
144
- using namespace v8;
154
+ namespace demo {
155
+
156
+ using v8::Exception;
157
+ using v8::FunctionCallbackInfo;
158
+ using v8::HandleScope;
159
+ using v8::Isolate;
160
+ using v8::Local;
161
+ using v8::Number;
162
+ using v8::Object;
163
+ using v8::String;
164
+ using v8::Value;
145
165
146
166
void Add(const FunctionCallbackInfo<Value>& args) {
147
167
Isolate* isolate = args.GetIsolate();
@@ -170,6 +190,8 @@ function calls and return a result. This is the main and only needed source
170
190
171
191
NODE_MODULE(addon, Init)
172
192
193
+ } // namespace demo
194
+
173
195
You can test it with the following JavaScript snippet:
174
196
175
197
// test.js
@@ -186,7 +208,16 @@ there. Here's `addon.cc`:
186
208
// addon.cc
187
209
#include <node.h>
188
210
189
- using namespace v8;
211
+ namespace demo {
212
+
213
+ using v8::Function;
214
+ using v8::FunctionCallbackInfo;
215
+ using v8::HandleScope;
216
+ using v8::Isolate;
217
+ using v8::Local;
218
+ using v8::Object;
219
+ using v8::String;
220
+ using v8::Value;
190
221
191
222
void RunCallback(const FunctionCallbackInfo<Value>& args) {
192
223
Isolate* isolate = args.GetIsolate();
@@ -202,6 +233,8 @@ there. Here's `addon.cc`:
202
233
203
234
NODE_MODULE(addon, Init)
204
235
236
+ } // namespace demo
237
+
205
238
Note that this example uses a two-argument form of ` Init() ` that receives
206
239
the full ` module ` object as the second argument. This allows the addon
207
240
to completely overwrite ` exports ` with a single function instead of
@@ -226,7 +259,15 @@ the string passed to `createObject()`:
226
259
// addon.cc
227
260
#include <node.h>
228
261
229
- using namespace v8;
262
+ namespace demo {
263
+
264
+ using v8::FunctionCallbackInfo;
265
+ using v8::HandleScope;
266
+ using v8::Isolate;
267
+ using v8::Local;
268
+ using v8::Object;
269
+ using v8::String;
270
+ using v8::Value;
230
271
231
272
void CreateObject(const FunctionCallbackInfo<Value>& args) {
232
273
Isolate* isolate = args.GetIsolate();
@@ -243,6 +284,8 @@ the string passed to `createObject()`:
243
284
244
285
NODE_MODULE(addon, Init)
245
286
287
+ } // namespace demo
288
+
246
289
To test it in JavaScript:
247
290
248
291
// test.js
@@ -261,7 +304,17 @@ wraps a C++ function:
261
304
// addon.cc
262
305
#include <node.h>
263
306
264
- using namespace v8;
307
+ namespace demo {
308
+
309
+ using v8::Function;
310
+ using v8::FunctionCallbackInfo;
311
+ using v8::FunctionTemplate;
312
+ using v8::HandleScope;
313
+ using v8::Isolate;
314
+ using v8::Local;
315
+ using v8::Object;
316
+ using v8::String;
317
+ using v8::Value;
265
318
266
319
void MyFunction(const FunctionCallbackInfo<Value>& args) {
267
320
Isolate* isolate = args.GetIsolate();
@@ -286,6 +339,8 @@ wraps a C++ function:
286
339
287
340
NODE_MODULE(addon, Init)
288
341
342
+ } // namespace demo
343
+
289
344
To test:
290
345
291
346
// test.js
@@ -305,14 +360,19 @@ module `addon.cc`:
305
360
#include <node.h>
306
361
#include "myobject.h"
307
362
308
- using namespace v8;
363
+ namespace demo {
364
+
365
+ using v8::Local;
366
+ using v8::Object;
309
367
310
368
void InitAll(Local<Object> exports) {
311
369
MyObject::Init(exports);
312
370
}
313
371
314
372
NODE_MODULE(addon, InitAll)
315
373
374
+ } // namespace demo
375
+
316
376
Then in ` myobject.h ` make your wrapper inherit from ` node::ObjectWrap ` :
317
377
318
378
// myobject.h
@@ -322,6 +382,8 @@ Then in `myobject.h` make your wrapper inherit from `node::ObjectWrap`:
322
382
#include <node.h>
323
383
#include <node_object_wrap.h>
324
384
385
+ namespace demo {
386
+
325
387
class MyObject : public node::ObjectWrap {
326
388
public:
327
389
static void Init(v8::Local<v8::Object> exports);
@@ -336,6 +398,8 @@ Then in `myobject.h` make your wrapper inherit from `node::ObjectWrap`:
336
398
double value_;
337
399
};
338
400
401
+ } // namespace demo
402
+
339
403
#endif
340
404
341
405
And in ` myobject.cc ` implement the various methods that you want to expose.
@@ -345,7 +409,19 @@ prototype:
345
409
// myobject.cc
346
410
#include "myobject.h"
347
411
348
- using namespace v8;
412
+ namespace demo {
413
+
414
+ using v8::Function;
415
+ using v8::FunctionCallbackInfo;
416
+ using v8::FunctionTemplate;
417
+ using v8::HandleScope;
418
+ using v8::Isolate;
419
+ using v8::Local;
420
+ using v8::Number;
421
+ using v8::Object;
422
+ using v8::Persistent;
423
+ using v8::String;
424
+ using v8::Value;
349
425
350
426
Persistent<Function> MyObject::constructor;
351
427
@@ -398,6 +474,8 @@ prototype:
398
474
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
399
475
}
400
476
477
+ } // namespace demo
478
+
401
479
Test it with:
402
480
403
481
// test.js
@@ -423,7 +501,15 @@ Let's register our `createObject` method in `addon.cc`:
423
501
#include <node.h>
424
502
#include "myobject.h"
425
503
426
- using namespace v8;
504
+ namespace demo {
505
+
506
+ using v8::FunctionCallbackInfo;
507
+ using v8::HandleScope;
508
+ using v8::Isolate;
509
+ using v8::Local;
510
+ using v8::Object;
511
+ using v8::String;
512
+ using v8::Value;
427
513
428
514
void CreateObject(const FunctionCallbackInfo<Value>& args) {
429
515
MyObject::NewInstance(args);
@@ -437,6 +523,8 @@ Let's register our `createObject` method in `addon.cc`:
437
523
438
524
NODE_MODULE(addon, InitAll)
439
525
526
+ } // namespace demo
527
+
440
528
In ` myobject.h ` we now introduce the static method ` NewInstance ` that takes
441
529
care of instantiating the object (i.e. it does the job of ` new ` in JavaScript):
442
530
@@ -447,6 +535,8 @@ care of instantiating the object (i.e. it does the job of `new` in JavaScript):
447
535
#include <node.h>
448
536
#include <node_object_wrap.h>
449
537
538
+ namespace demo {
539
+
450
540
class MyObject : public node::ObjectWrap {
451
541
public:
452
542
static void Init(v8::Isolate* isolate);
@@ -462,6 +552,8 @@ care of instantiating the object (i.e. it does the job of `new` in JavaScript):
462
552
double value_;
463
553
};
464
554
555
+ } // namespace demo
556
+
465
557
#endif
466
558
467
559
The implementation is similar to the above in ` myobject.cc ` :
@@ -470,7 +562,19 @@ The implementation is similar to the above in `myobject.cc`:
470
562
#include <node.h>
471
563
#include "myobject.h"
472
564
473
- using namespace v8;
565
+ namespace demo {
566
+
567
+ using v8::Function;
568
+ using v8::FunctionCallbackInfo;
569
+ using v8::FunctionTemplate;
570
+ using v8::HandleScope;
571
+ using v8::Isolate;
572
+ using v8::Local;
573
+ using v8::Number;
574
+ using v8::Object;
575
+ using v8::Persistent;
576
+ using v8::String;
577
+ using v8::Value;
474
578
475
579
Persistent<Function> MyObject::constructor;
476
580
@@ -530,6 +634,8 @@ The implementation is similar to the above in `myobject.cc`:
530
634
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
531
635
}
532
636
637
+ } // namespace demo
638
+
533
639
Test it with:
534
640
535
641
// test.js
@@ -558,7 +664,16 @@ In the following `addon.cc` we introduce a function `add()` that can take on two
558
664
#include <node_object_wrap.h>
559
665
#include "myobject.h"
560
666
561
- using namespace v8;
667
+ namespace demo {
668
+
669
+ using v8::FunctionCallbackInfo;
670
+ using v8::HandleScope;
671
+ using v8::Isolate;
672
+ using v8::Local;
673
+ using v8::Number;
674
+ using v8::Object;
675
+ using v8::String;
676
+ using v8::Value;
562
677
563
678
void CreateObject(const FunctionCallbackInfo<Value>& args) {
564
679
MyObject::NewInstance(args);
@@ -585,6 +700,8 @@ In the following `addon.cc` we introduce a function `add()` that can take on two
585
700
586
701
NODE_MODULE(addon, InitAll)
587
702
703
+ } // namespace demo
704
+
588
705
To make things interesting we introduce a public method in ` myobject.h ` so we
589
706
can probe private values after unwrapping the object:
590
707
@@ -595,6 +712,8 @@ can probe private values after unwrapping the object:
595
712
#include <node.h>
596
713
#include <node_object_wrap.h>
597
714
715
+ namespace demo {
716
+
598
717
class MyObject : public node::ObjectWrap {
599
718
public:
600
719
static void Init(v8::Isolate* isolate);
@@ -610,6 +729,8 @@ can probe private values after unwrapping the object:
610
729
double value_;
611
730
};
612
731
732
+ } // namespace demo
733
+
613
734
#endif
614
735
615
736
The implementation of ` myobject.cc ` is similar as before:
@@ -618,7 +739,18 @@ The implementation of `myobject.cc` is similar as before:
618
739
#include <node.h>
619
740
#include "myobject.h"
620
741
621
- using namespace v8;
742
+ namespace demo {
743
+
744
+ using v8::Function;
745
+ using v8::FunctionCallbackInfo;
746
+ using v8::FunctionTemplate;
747
+ using v8::HandleScope;
748
+ using v8::Isolate;
749
+ using v8::Local;
750
+ using v8::Object;
751
+ using v8::Persistent;
752
+ using v8::String;
753
+ using v8::Value;
622
754
623
755
Persistent<Function> MyObject::constructor;
624
756
@@ -666,6 +798,8 @@ The implementation of `myobject.cc` is similar as before:
666
798
args.GetReturnValue().Set(instance);
667
799
}
668
800
801
+ } // namespace demo
802
+
669
803
Test it with:
670
804
671
805
// test.js
0 commit comments