Skip to content

Commit 55abf34

Browse files
committed
doc: don't use using namespace v8
Wholesale importing an entire namespace with `using namespace` is a bad practice. Remove it from the addons documentation and replace it with proper `using` directives. Wrap code in a namespace while we are here. PR-URL: #1125 Reviewed-By: Rod Vagg <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent c4e1b82 commit 55abf34

File tree

1 file changed

+145
-11
lines changed

1 file changed

+145
-11
lines changed

doc/api/addons.markdown

+145-11
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ First we create a file `hello.cc`:
4141
// hello.cc
4242
#include <node.h>
4343

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;
4553

4654
void Method(const FunctionCallbackInfo<Value>& args) {
4755
Isolate* isolate = args.GetIsolate();
@@ -54,6 +62,8 @@ First we create a file `hello.cc`:
5462

5563
NODE_MODULE(addon, init)
5664

65+
} // namespace demo
66+
5767
Note that all io.js addons must export an initialization function:
5868

5969
void Initialize(Local<Object> exports);
@@ -141,7 +151,17 @@ function calls and return a result. This is the main and only needed source
141151
// addon.cc
142152
#include <node.h>
143153

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;
145165

146166
void Add(const FunctionCallbackInfo<Value>& args) {
147167
Isolate* isolate = args.GetIsolate();
@@ -170,6 +190,8 @@ function calls and return a result. This is the main and only needed source
170190

171191
NODE_MODULE(addon, Init)
172192

193+
} // namespace demo
194+
173195
You can test it with the following JavaScript snippet:
174196

175197
// test.js
@@ -186,7 +208,16 @@ there. Here's `addon.cc`:
186208
// addon.cc
187209
#include <node.h>
188210

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;
190221

191222
void RunCallback(const FunctionCallbackInfo<Value>& args) {
192223
Isolate* isolate = args.GetIsolate();
@@ -202,6 +233,8 @@ there. Here's `addon.cc`:
202233

203234
NODE_MODULE(addon, Init)
204235

236+
} // namespace demo
237+
205238
Note that this example uses a two-argument form of `Init()` that receives
206239
the full `module` object as the second argument. This allows the addon
207240
to completely overwrite `exports` with a single function instead of
@@ -226,7 +259,15 @@ the string passed to `createObject()`:
226259
// addon.cc
227260
#include <node.h>
228261

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;
230271

231272
void CreateObject(const FunctionCallbackInfo<Value>& args) {
232273
Isolate* isolate = args.GetIsolate();
@@ -243,6 +284,8 @@ the string passed to `createObject()`:
243284

244285
NODE_MODULE(addon, Init)
245286

287+
} // namespace demo
288+
246289
To test it in JavaScript:
247290

248291
// test.js
@@ -261,7 +304,17 @@ wraps a C++ function:
261304
// addon.cc
262305
#include <node.h>
263306

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;
265318

266319
void MyFunction(const FunctionCallbackInfo<Value>& args) {
267320
Isolate* isolate = args.GetIsolate();
@@ -286,6 +339,8 @@ wraps a C++ function:
286339

287340
NODE_MODULE(addon, Init)
288341

342+
} // namespace demo
343+
289344
To test:
290345

291346
// test.js
@@ -305,14 +360,19 @@ module `addon.cc`:
305360
#include <node.h>
306361
#include "myobject.h"
307362

308-
using namespace v8;
363+
namespace demo {
364+
365+
using v8::Local;
366+
using v8::Object;
309367

310368
void InitAll(Local<Object> exports) {
311369
MyObject::Init(exports);
312370
}
313371

314372
NODE_MODULE(addon, InitAll)
315373

374+
} // namespace demo
375+
316376
Then in `myobject.h` make your wrapper inherit from `node::ObjectWrap`:
317377

318378
// myobject.h
@@ -322,6 +382,8 @@ Then in `myobject.h` make your wrapper inherit from `node::ObjectWrap`:
322382
#include <node.h>
323383
#include <node_object_wrap.h>
324384

385+
namespace demo {
386+
325387
class MyObject : public node::ObjectWrap {
326388
public:
327389
static void Init(v8::Local<v8::Object> exports);
@@ -336,6 +398,8 @@ Then in `myobject.h` make your wrapper inherit from `node::ObjectWrap`:
336398
double value_;
337399
};
338400

401+
} // namespace demo
402+
339403
#endif
340404

341405
And in `myobject.cc` implement the various methods that you want to expose.
@@ -345,7 +409,19 @@ prototype:
345409
// myobject.cc
346410
#include "myobject.h"
347411

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;
349425

350426
Persistent<Function> MyObject::constructor;
351427

@@ -398,6 +474,8 @@ prototype:
398474
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
399475
}
400476

477+
} // namespace demo
478+
401479
Test it with:
402480

403481
// test.js
@@ -423,7 +501,15 @@ Let's register our `createObject` method in `addon.cc`:
423501
#include <node.h>
424502
#include "myobject.h"
425503

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;
427513

428514
void CreateObject(const FunctionCallbackInfo<Value>& args) {
429515
MyObject::NewInstance(args);
@@ -437,6 +523,8 @@ Let's register our `createObject` method in `addon.cc`:
437523

438524
NODE_MODULE(addon, InitAll)
439525

526+
} // namespace demo
527+
440528
In `myobject.h` we now introduce the static method `NewInstance` that takes
441529
care of instantiating the object (i.e. it does the job of `new` in JavaScript):
442530

@@ -447,6 +535,8 @@ care of instantiating the object (i.e. it does the job of `new` in JavaScript):
447535
#include <node.h>
448536
#include <node_object_wrap.h>
449537

538+
namespace demo {
539+
450540
class MyObject : public node::ObjectWrap {
451541
public:
452542
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):
462552
double value_;
463553
};
464554

555+
} // namespace demo
556+
465557
#endif
466558

467559
The implementation is similar to the above in `myobject.cc`:
@@ -470,7 +562,19 @@ The implementation is similar to the above in `myobject.cc`:
470562
#include <node.h>
471563
#include "myobject.h"
472564

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;
474578

475579
Persistent<Function> MyObject::constructor;
476580

@@ -530,6 +634,8 @@ The implementation is similar to the above in `myobject.cc`:
530634
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
531635
}
532636

637+
} // namespace demo
638+
533639
Test it with:
534640

535641
// test.js
@@ -558,7 +664,16 @@ In the following `addon.cc` we introduce a function `add()` that can take on two
558664
#include <node_object_wrap.h>
559665
#include "myobject.h"
560666

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;
562677

563678
void CreateObject(const FunctionCallbackInfo<Value>& args) {
564679
MyObject::NewInstance(args);
@@ -585,6 +700,8 @@ In the following `addon.cc` we introduce a function `add()` that can take on two
585700

586701
NODE_MODULE(addon, InitAll)
587702

703+
} // namespace demo
704+
588705
To make things interesting we introduce a public method in `myobject.h` so we
589706
can probe private values after unwrapping the object:
590707

@@ -595,6 +712,8 @@ can probe private values after unwrapping the object:
595712
#include <node.h>
596713
#include <node_object_wrap.h>
597714

715+
namespace demo {
716+
598717
class MyObject : public node::ObjectWrap {
599718
public:
600719
static void Init(v8::Isolate* isolate);
@@ -610,6 +729,8 @@ can probe private values after unwrapping the object:
610729
double value_;
611730
};
612731

732+
} // namespace demo
733+
613734
#endif
614735

615736
The implementation of `myobject.cc` is similar as before:
@@ -618,7 +739,18 @@ The implementation of `myobject.cc` is similar as before:
618739
#include <node.h>
619740
#include "myobject.h"
620741

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;
622754

623755
Persistent<Function> MyObject::constructor;
624756

@@ -666,6 +798,8 @@ The implementation of `myobject.cc` is similar as before:
666798
args.GetReturnValue().Set(instance);
667799
}
668800

801+
} // namespace demo
802+
669803
Test it with:
670804

671805
// test.js

0 commit comments

Comments
 (0)