// hello.cc
#include <node.h>
- using namespace v8;
+ namespace demo {
+
+ using v8::FunctionCallbackInfo;
+ using v8::HandleScope;
+ using v8::Isolate;
+ using v8::Local;
+ using v8::Object;
+ using v8::String;
+ using v8::Value;
void Method(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
NODE_MODULE(addon, init)
+ } // namespace demo
+
Note that all io.js addons must export an initialization function:
void Initialize(Local<Object> exports);
// addon.cc
#include <node.h>
- using namespace v8;
+ namespace demo {
+
+ using v8::Exception;
+ using v8::FunctionCallbackInfo;
+ using v8::HandleScope;
+ using v8::Isolate;
+ using v8::Local;
+ using v8::Number;
+ using v8::Object;
+ using v8::String;
+ using v8::Value;
void Add(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
NODE_MODULE(addon, Init)
+ } // namespace demo
+
You can test it with the following JavaScript snippet:
// test.js
// addon.cc
#include <node.h>
- using namespace v8;
+ namespace demo {
+
+ using v8::Function;
+ using v8::FunctionCallbackInfo;
+ using v8::HandleScope;
+ using v8::Isolate;
+ using v8::Local;
+ using v8::Object;
+ using v8::String;
+ using v8::Value;
void RunCallback(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
NODE_MODULE(addon, Init)
+ } // namespace demo
+
Note that this example uses a two-argument form of `Init()` that receives
the full `module` object as the second argument. This allows the addon
to completely overwrite `exports` with a single function instead of
// addon.cc
#include <node.h>
- using namespace v8;
+ namespace demo {
+
+ using v8::FunctionCallbackInfo;
+ using v8::HandleScope;
+ using v8::Isolate;
+ using v8::Local;
+ using v8::Object;
+ using v8::String;
+ using v8::Value;
void CreateObject(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
NODE_MODULE(addon, Init)
+ } // namespace demo
+
To test it in JavaScript:
// test.js
// addon.cc
#include <node.h>
- using namespace v8;
+ namespace demo {
+
+ using v8::Function;
+ using v8::FunctionCallbackInfo;
+ using v8::FunctionTemplate;
+ using v8::HandleScope;
+ using v8::Isolate;
+ using v8::Local;
+ using v8::Object;
+ using v8::String;
+ using v8::Value;
void MyFunction(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
NODE_MODULE(addon, Init)
+ } // namespace demo
+
To test:
// test.js
#include <node.h>
#include "myobject.h"
- using namespace v8;
+ namespace demo {
+
+ using v8::Local;
+ using v8::Object;
void InitAll(Local<Object> exports) {
MyObject::Init(exports);
NODE_MODULE(addon, InitAll)
+ } // namespace demo
+
Then in `myobject.h` make your wrapper inherit from `node::ObjectWrap`:
// myobject.h
#include <node.h>
#include <node_object_wrap.h>
+ namespace demo {
+
class MyObject : public node::ObjectWrap {
public:
static void Init(v8::Local<v8::Object> exports);
double value_;
};
+ } // namespace demo
+
#endif
And in `myobject.cc` implement the various methods that you want to expose.
// myobject.cc
#include "myobject.h"
- using namespace v8;
+ namespace demo {
+
+ using v8::Function;
+ using v8::FunctionCallbackInfo;
+ using v8::FunctionTemplate;
+ using v8::HandleScope;
+ using v8::Isolate;
+ using v8::Local;
+ using v8::Number;
+ using v8::Object;
+ using v8::Persistent;
+ using v8::String;
+ using v8::Value;
Persistent<Function> MyObject::constructor;
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
}
+ } // namespace demo
+
Test it with:
// test.js
#include <node.h>
#include "myobject.h"
- using namespace v8;
+ namespace demo {
+
+ using v8::FunctionCallbackInfo;
+ using v8::HandleScope;
+ using v8::Isolate;
+ using v8::Local;
+ using v8::Object;
+ using v8::String;
+ using v8::Value;
void CreateObject(const FunctionCallbackInfo<Value>& args) {
MyObject::NewInstance(args);
NODE_MODULE(addon, InitAll)
+ } // namespace demo
+
In `myobject.h` we now introduce the static method `NewInstance` that takes
care of instantiating the object (i.e. it does the job of `new` in JavaScript):
#include <node.h>
#include <node_object_wrap.h>
+ namespace demo {
+
class MyObject : public node::ObjectWrap {
public:
static void Init(v8::Isolate* isolate);
double value_;
};
+ } // namespace demo
+
#endif
The implementation is similar to the above in `myobject.cc`:
#include <node.h>
#include "myobject.h"
- using namespace v8;
+ namespace demo {
+
+ using v8::Function;
+ using v8::FunctionCallbackInfo;
+ using v8::FunctionTemplate;
+ using v8::HandleScope;
+ using v8::Isolate;
+ using v8::Local;
+ using v8::Number;
+ using v8::Object;
+ using v8::Persistent;
+ using v8::String;
+ using v8::Value;
Persistent<Function> MyObject::constructor;
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
}
+ } // namespace demo
+
Test it with:
// test.js
#include <node_object_wrap.h>
#include "myobject.h"
- using namespace v8;
+ namespace demo {
+
+ using v8::FunctionCallbackInfo;
+ using v8::HandleScope;
+ using v8::Isolate;
+ using v8::Local;
+ using v8::Number;
+ using v8::Object;
+ using v8::String;
+ using v8::Value;
void CreateObject(const FunctionCallbackInfo<Value>& args) {
MyObject::NewInstance(args);
NODE_MODULE(addon, InitAll)
+ } // namespace demo
+
To make things interesting we introduce a public method in `myobject.h` so we
can probe private values after unwrapping the object:
#include <node.h>
#include <node_object_wrap.h>
+ namespace demo {
+
class MyObject : public node::ObjectWrap {
public:
static void Init(v8::Isolate* isolate);
double value_;
};
+ } // namespace demo
+
#endif
The implementation of `myobject.cc` is similar as before:
#include <node.h>
#include "myobject.h"
- using namespace v8;
+ namespace demo {
+
+ using v8::Function;
+ using v8::FunctionCallbackInfo;
+ using v8::FunctionTemplate;
+ using v8::HandleScope;
+ using v8::Isolate;
+ using v8::Local;
+ using v8::Object;
+ using v8::Persistent;
+ using v8::String;
+ using v8::Value;
Persistent<Function> MyObject::constructor;
args.GetReturnValue().Set(instance);
}
+ } // namespace demo
+
Test it with:
// test.js