From 409d4133631ca2580488deba317eb1a0d55bcd55 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 11 Mar 2015 13:41:32 +0100 Subject: [PATCH 1/5] doc: remove uses of v8::Isolate::GetCurrent() v8::Isolate::GetCurrent() is slated for deprecation. Replace its uses in the addons documentation with v8::Object::GetIsolate(), etc. PR-URL: https://github.com/iojs/io.js/pull/1125 Reviewed-By: Rod Vagg Reviewed-By: Trevor Norris --- doc/api/addons.markdown | 49 +++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/doc/api/addons.markdown b/doc/api/addons.markdown index 5c77c7f22f2609..d47b6300c530b7 100644 --- a/doc/api/addons.markdown +++ b/doc/api/addons.markdown @@ -44,7 +44,7 @@ First we create a file `hello.cc`: using namespace v8; void Method(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world")); } @@ -145,7 +145,7 @@ function calls and return a result. This is the main and only needed source using namespace v8; void Add(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); if (args.Length() < 2) { @@ -191,7 +191,7 @@ there. Here's `addon.cc`: using namespace v8; void RunCallback(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); Local cb = Local::Cast(args[0]); @@ -233,7 +233,7 @@ the string passed to `createObject()`: using namespace v8; void CreateObject(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); Local obj = Object::New(isolate); @@ -269,13 +269,13 @@ wraps a C++ function: using namespace v8; void MyFunction(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello world")); } void CreateFunction(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); Local tpl = FunctionTemplate::New(isolate, MyFunction); @@ -363,7 +363,7 @@ prototype: } void MyObject::Init(Handle exports) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = exports->GetIsolate(); // Prepare constructor template Local tpl = FunctionTemplate::New(isolate, New); @@ -379,7 +379,7 @@ prototype: } void MyObject::New(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); if (args.IsConstructCall()) { @@ -398,7 +398,7 @@ prototype: } void MyObject::PlusOne(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); MyObject* obj = ObjectWrap::Unwrap(args.Holder()); @@ -435,13 +435,13 @@ Let's register our `createObject` method in `addon.cc`: using namespace v8; void CreateObject(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); MyObject::NewInstance(args); } void InitAll(Handle exports, Handle module) { - MyObject::Init(); + MyObject::Init(exports->GetIsolate()); NODE_SET_METHOD(module, "exports", CreateObject); } @@ -460,7 +460,7 @@ care of instantiating the object (i.e. it does the job of `new` in JavaScript): class MyObject : public node::ObjectWrap { public: - static void Init(); + static void Init(v8::Isolate* isolate); static void NewInstance(const v8::FunctionCallbackInfo& args); private: @@ -491,8 +491,7 @@ The implementation is similar to the above in `myobject.cc`: MyObject::~MyObject() { } - void MyObject::Init() { - Isolate* isolate = Isolate::GetCurrent(); + void MyObject::Init(Isolate* isolate) { // Prepare constructor template Local tpl = FunctionTemplate::New(isolate, New); tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject")); @@ -505,7 +504,7 @@ The implementation is similar to the above in `myobject.cc`: } void MyObject::New(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); if (args.IsConstructCall()) { @@ -524,7 +523,7 @@ The implementation is similar to the above in `myobject.cc`: } void MyObject::NewInstance(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); const unsigned argc = 1; @@ -536,7 +535,7 @@ The implementation is similar to the above in `myobject.cc`: } void MyObject::PlusOne(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); MyObject* obj = ObjectWrap::Unwrap(args.Holder()); @@ -576,13 +575,13 @@ In the following `addon.cc` we introduce a function `add()` that can take on two using namespace v8; void CreateObject(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); MyObject::NewInstance(args); } void Add(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); MyObject* obj1 = node::ObjectWrap::Unwrap( @@ -595,7 +594,7 @@ In the following `addon.cc` we introduce a function `add()` that can take on two } void InitAll(Handle exports) { - MyObject::Init(); + MyObject::Init(exports->GetIsolate()); NODE_SET_METHOD(exports, "createObject", CreateObject); NODE_SET_METHOD(exports, "add", Add); @@ -615,7 +614,7 @@ can probe private values after unwrapping the object: class MyObject : public node::ObjectWrap { public: - static void Init(); + static void Init(v8::Isolate* isolate); static void NewInstance(const v8::FunctionCallbackInfo& args); inline double value() const { return value_; } @@ -646,9 +645,7 @@ The implementation of `myobject.cc` is similar as before: MyObject::~MyObject() { } - void MyObject::Init() { - Isolate* isolate = Isolate::GetCurrent(); - + void MyObject::Init(Isolate* isolate) { // Prepare constructor template Local tpl = FunctionTemplate::New(isolate, New); tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject")); @@ -658,7 +655,7 @@ The implementation of `myobject.cc` is similar as before: } void MyObject::New(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); if (args.IsConstructCall()) { @@ -677,7 +674,7 @@ The implementation of `myobject.cc` is similar as before: } void MyObject::NewInstance(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = args.GetIsolate(); HandleScope scope(isolate); const unsigned argc = 1; From 2f1b78347c3ce5bd250a6bc501c5b2f38a7b1d1c Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 11 Mar 2015 13:56:37 +0100 Subject: [PATCH 2/5] doc: remove unnecessary v8::HandleScopes Remove unnecessary v8::HandleScope uses from the addons documentation. C++ API callbacks run in an implicit v8::HandleScope, there is no need to declare one in the callback function. PR-URL: https://github.com/iojs/io.js/pull/1125 Reviewed-By: Rod Vagg Reviewed-By: Trevor Norris --- doc/api/addons.markdown | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/doc/api/addons.markdown b/doc/api/addons.markdown index d47b6300c530b7..d81847cda2c3f8 100644 --- a/doc/api/addons.markdown +++ b/doc/api/addons.markdown @@ -45,7 +45,6 @@ First we create a file `hello.cc`: void Method(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world")); } @@ -146,7 +145,6 @@ function calls and return a result. This is the main and only needed source void Add(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); if (args.Length() < 2) { isolate->ThrowException(Exception::TypeError( @@ -192,8 +190,6 @@ there. Here's `addon.cc`: void RunCallback(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); - Local cb = Local::Cast(args[0]); const unsigned argc = 1; Local argv[argc] = { String::NewFromUtf8(isolate, "hello world") }; @@ -234,7 +230,6 @@ the string passed to `createObject()`: void CreateObject(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); Local obj = Object::New(isolate); obj->Set(String::NewFromUtf8(isolate, "msg"), args[0]->ToString()); @@ -270,13 +265,11 @@ wraps a C++ function: void MyFunction(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello world")); } void CreateFunction(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); Local tpl = FunctionTemplate::New(isolate, MyFunction); Local fn = tpl->GetFunction(); @@ -380,7 +373,6 @@ prototype: void MyObject::New(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); if (args.IsConstructCall()) { // Invoked as constructor: `new MyObject(...)` @@ -399,7 +391,6 @@ prototype: void MyObject::PlusOne(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); MyObject* obj = ObjectWrap::Unwrap(args.Holder()); obj->value_ += 1; @@ -435,8 +426,6 @@ Let's register our `createObject` method in `addon.cc`: using namespace v8; void CreateObject(const FunctionCallbackInfo& args) { - Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); MyObject::NewInstance(args); } @@ -505,7 +494,6 @@ The implementation is similar to the above in `myobject.cc`: void MyObject::New(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); if (args.IsConstructCall()) { // Invoked as constructor: `new MyObject(...)` @@ -524,7 +512,6 @@ The implementation is similar to the above in `myobject.cc`: void MyObject::NewInstance(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); const unsigned argc = 1; Handle argv[argc] = { args[0] }; @@ -536,7 +523,6 @@ The implementation is similar to the above in `myobject.cc`: void MyObject::PlusOne(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); MyObject* obj = ObjectWrap::Unwrap(args.Holder()); obj->value_ += 1; @@ -575,14 +561,11 @@ In the following `addon.cc` we introduce a function `add()` that can take on two using namespace v8; void CreateObject(const FunctionCallbackInfo& args) { - Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); MyObject::NewInstance(args); } void Add(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); MyObject* obj1 = node::ObjectWrap::Unwrap( args[0]->ToObject()); @@ -656,7 +639,6 @@ The implementation of `myobject.cc` is similar as before: void MyObject::New(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); if (args.IsConstructCall()) { // Invoked as constructor: `new MyObject(...)` @@ -675,7 +657,6 @@ The implementation of `myobject.cc` is similar as before: void MyObject::NewInstance(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); const unsigned argc = 1; Handle argv[argc] = { args[0] }; From c4e1b82120ae4d14e0f29b4715a54397728a0f76 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 11 Mar 2015 14:03:02 +0100 Subject: [PATCH 3/5] doc: replace v8::Handle with v8::Local v8::Handle is on its way out, to be replaced with v8::Local. Update the addons documentation accordingly. PR-URL: https://github.com/iojs/io.js/pull/1125 Reviewed-By: Rod Vagg Reviewed-By: Trevor Norris --- doc/api/addons.markdown | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/doc/api/addons.markdown b/doc/api/addons.markdown index d81847cda2c3f8..58e99e60f1566d 100644 --- a/doc/api/addons.markdown +++ b/doc/api/addons.markdown @@ -48,7 +48,7 @@ First we create a file `hello.cc`: args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world")); } - void init(Handle exports) { + void init(Local exports) { NODE_SET_METHOD(exports, "hello", Method); } @@ -56,7 +56,7 @@ First we create a file `hello.cc`: Note that all io.js addons must export an initialization function: - void Initialize (Handle exports); + void Initialize(Local exports); NODE_MODULE(module_name, Initialize) There is no semi-colon after `NODE_MODULE` as it's not a function (see @@ -164,7 +164,7 @@ function calls and return a result. This is the main and only needed source args.GetReturnValue().Set(num); } - void Init(Handle exports) { + void Init(Local exports) { NODE_SET_METHOD(exports, "add", Add); } @@ -196,7 +196,7 @@ there. Here's `addon.cc`: cb->Call(isolate->GetCurrentContext()->Global(), argc, argv); } - void Init(Handle exports, Handle module) { + void Init(Local exports, Local module) { NODE_SET_METHOD(module, "exports", RunCallback); } @@ -237,7 +237,7 @@ the string passed to `createObject()`: args.GetReturnValue().Set(obj); } - void Init(Handle exports, Handle module) { + void Init(Local exports, Local module) { NODE_SET_METHOD(module, "exports", CreateObject); } @@ -280,7 +280,7 @@ wraps a C++ function: args.GetReturnValue().Set(fn); } - void Init(Handle exports, Handle module) { + void Init(Local exports, Local module) { NODE_SET_METHOD(module, "exports", CreateFunction); } @@ -307,7 +307,7 @@ module `addon.cc`: using namespace v8; - void InitAll(Handle exports) { + void InitAll(Local exports) { MyObject::Init(exports); } @@ -324,7 +324,7 @@ Then in `myobject.h` make your wrapper inherit from `node::ObjectWrap`: class MyObject : public node::ObjectWrap { public: - static void Init(v8::Handle exports); + static void Init(v8::Local exports); private: explicit MyObject(double value = 0); @@ -355,7 +355,7 @@ prototype: MyObject::~MyObject() { } - void MyObject::Init(Handle exports) { + void MyObject::Init(Local exports) { Isolate* isolate = exports->GetIsolate(); // Prepare constructor template @@ -429,7 +429,7 @@ Let's register our `createObject` method in `addon.cc`: MyObject::NewInstance(args); } - void InitAll(Handle exports, Handle module) { + void InitAll(Local exports, Local module) { MyObject::Init(exports->GetIsolate()); NODE_SET_METHOD(module, "exports", CreateObject); @@ -514,7 +514,7 @@ The implementation is similar to the above in `myobject.cc`: Isolate* isolate = args.GetIsolate(); const unsigned argc = 1; - Handle argv[argc] = { args[0] }; + Local argv[argc] = { args[0] }; Local cons = Local::New(isolate, constructor); Local instance = cons->NewInstance(argc, argv); @@ -576,7 +576,7 @@ In the following `addon.cc` we introduce a function `add()` that can take on two args.GetReturnValue().Set(Number::New(isolate, sum)); } - void InitAll(Handle exports) { + void InitAll(Local exports) { MyObject::Init(exports->GetIsolate()); NODE_SET_METHOD(exports, "createObject", CreateObject); @@ -659,7 +659,7 @@ The implementation of `myobject.cc` is similar as before: Isolate* isolate = args.GetIsolate(); const unsigned argc = 1; - Handle argv[argc] = { args[0] }; + Local argv[argc] = { args[0] }; Local cons = Local::New(isolate, constructor); Local instance = cons->NewInstance(argc, argv); From 55abf34be5f56d500cacd668fd9192ddd065c002 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 11 Mar 2015 14:14:18 +0100 Subject: [PATCH 4/5] 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: https://github.com/iojs/io.js/pull/1125 Reviewed-By: Rod Vagg Reviewed-By: Trevor Norris --- doc/api/addons.markdown | 156 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 145 insertions(+), 11 deletions(-) diff --git a/doc/api/addons.markdown b/doc/api/addons.markdown index 58e99e60f1566d..8a008c2977dc39 100644 --- a/doc/api/addons.markdown +++ b/doc/api/addons.markdown @@ -41,7 +41,15 @@ First we create a file `hello.cc`: // hello.cc #include - 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& args) { Isolate* isolate = args.GetIsolate(); @@ -54,6 +62,8 @@ First we create a file `hello.cc`: NODE_MODULE(addon, init) + } // namespace demo + Note that all io.js addons must export an initialization function: void Initialize(Local exports); @@ -141,7 +151,17 @@ function calls and return a result. This is the main and only needed source // addon.cc #include - 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& args) { Isolate* isolate = args.GetIsolate(); @@ -170,6 +190,8 @@ function calls and return a result. This is the main and only needed source NODE_MODULE(addon, Init) + } // namespace demo + You can test it with the following JavaScript snippet: // test.js @@ -186,7 +208,16 @@ there. Here's `addon.cc`: // addon.cc #include - 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& args) { Isolate* isolate = args.GetIsolate(); @@ -202,6 +233,8 @@ there. Here's `addon.cc`: 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 @@ -226,7 +259,15 @@ the string passed to `createObject()`: // addon.cc #include - 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& args) { Isolate* isolate = args.GetIsolate(); @@ -243,6 +284,8 @@ the string passed to `createObject()`: NODE_MODULE(addon, Init) + } // namespace demo + To test it in JavaScript: // test.js @@ -261,7 +304,17 @@ wraps a C++ function: // addon.cc #include - 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& args) { Isolate* isolate = args.GetIsolate(); @@ -286,6 +339,8 @@ wraps a C++ function: NODE_MODULE(addon, Init) + } // namespace demo + To test: // test.js @@ -305,7 +360,10 @@ module `addon.cc`: #include #include "myobject.h" - using namespace v8; + namespace demo { + + using v8::Local; + using v8::Object; void InitAll(Local exports) { MyObject::Init(exports); @@ -313,6 +371,8 @@ module `addon.cc`: NODE_MODULE(addon, InitAll) + } // namespace demo + Then in `myobject.h` make your wrapper inherit from `node::ObjectWrap`: // myobject.h @@ -322,6 +382,8 @@ Then in `myobject.h` make your wrapper inherit from `node::ObjectWrap`: #include #include + namespace demo { + class MyObject : public node::ObjectWrap { public: static void Init(v8::Local exports); @@ -336,6 +398,8 @@ Then in `myobject.h` make your wrapper inherit from `node::ObjectWrap`: double value_; }; + } // namespace demo + #endif And in `myobject.cc` implement the various methods that you want to expose. @@ -345,7 +409,19 @@ prototype: // 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 MyObject::constructor; @@ -398,6 +474,8 @@ prototype: args.GetReturnValue().Set(Number::New(isolate, obj->value_)); } + } // namespace demo + Test it with: // test.js @@ -423,7 +501,15 @@ Let's register our `createObject` method in `addon.cc`: #include #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& args) { MyObject::NewInstance(args); @@ -437,6 +523,8 @@ Let's register our `createObject` method in `addon.cc`: 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): @@ -447,6 +535,8 @@ care of instantiating the object (i.e. it does the job of `new` in JavaScript): #include #include + namespace demo { + class MyObject : public node::ObjectWrap { public: 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): double value_; }; + } // namespace demo + #endif The implementation is similar to the above in `myobject.cc`: @@ -470,7 +562,19 @@ The implementation is similar to the above in `myobject.cc`: #include #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 MyObject::constructor; @@ -530,6 +634,8 @@ The implementation is similar to the above in `myobject.cc`: args.GetReturnValue().Set(Number::New(isolate, obj->value_)); } + } // namespace demo + Test it with: // test.js @@ -558,7 +664,16 @@ In the following `addon.cc` we introduce a function `add()` that can take on two #include #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& args) { MyObject::NewInstance(args); @@ -585,6 +700,8 @@ In the following `addon.cc` we introduce a function `add()` that can take on two 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: @@ -595,6 +712,8 @@ can probe private values after unwrapping the object: #include #include + namespace demo { + class MyObject : public node::ObjectWrap { public: static void Init(v8::Isolate* isolate); @@ -610,6 +729,8 @@ can probe private values after unwrapping the object: double value_; }; + } // namespace demo + #endif The implementation of `myobject.cc` is similar as before: @@ -618,7 +739,18 @@ The implementation of `myobject.cc` is similar as before: #include #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 MyObject::constructor; @@ -666,6 +798,8 @@ The implementation of `myobject.cc` is similar as before: args.GetReturnValue().Set(instance); } + } // namespace demo + Test it with: // test.js From 99c79f8d41c706ddcffed7137f971a6cb62e2230 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 11 Mar 2015 14:19:21 +0100 Subject: [PATCH 5/5] doc: call js function in null context It's good practice now to call JS functions that don't execute in a specific scope with v8::Null() as the receiver. Update the addons documentation. PR-URL: https://github.com/iojs/io.js/pull/1125 Reviewed-By: Rod Vagg Reviewed-By: Trevor Norris --- doc/api/addons.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/api/addons.markdown b/doc/api/addons.markdown index 8a008c2977dc39..1f0562ede2c938 100644 --- a/doc/api/addons.markdown +++ b/doc/api/addons.markdown @@ -215,6 +215,7 @@ there. Here's `addon.cc`: using v8::HandleScope; using v8::Isolate; using v8::Local; + using v8::Null; using v8::Object; using v8::String; using v8::Value; @@ -224,7 +225,7 @@ there. Here's `addon.cc`: Local cb = Local::Cast(args[0]); const unsigned argc = 1; Local argv[argc] = { String::NewFromUtf8(isolate, "hello world") }; - cb->Call(isolate->GetCurrentContext()->Global(), argc, argv); + cb->Call(Null(isolate), argc, argv); } void Init(Local exports, Local module) {