From 8f4022e44b348119324ac36c49de190edbfe95e3 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 18 Aug 2015 20:29:10 +0200 Subject: [PATCH 1/2] test: lint addon tests Add files in test/addon to the `make cpplint` rule and fix up existing style issues. Tests scraped from doc/api/addon.md are filtered out because those are predominantly for illustrative purposes. PR-URL: https://github.com/nodejs/node/pull/2427 Reviewed-By: Trevor Norris --- Makefile | 15 ++++++-- test/addons/async-hello-world/binding.cc | 34 +++++++++---------- test/addons/at-exit/binding.cc | 2 +- .../hello-world-function-export/binding.cc | 12 +++---- test/addons/hello-world/binding.cc | 12 +++---- 5 files changed, 40 insertions(+), 35 deletions(-) diff --git a/Makefile b/Makefile index 286b6180a66e11..7027ba973f95c1 100644 --- a/Makefile +++ b/Makefile @@ -483,8 +483,19 @@ CPPLINT_EXCLUDE += src/node_win32_perfctr_provider.cc CPPLINT_EXCLUDE += src/queue.h CPPLINT_EXCLUDE += src/tree.h CPPLINT_EXCLUDE += src/v8abbr.h - -CPPLINT_FILES = $(filter-out $(CPPLINT_EXCLUDE), $(wildcard src/*.cc src/*.h src/*.c tools/icu/*.h tools/icu/*.cc deps/debugger-agent/include/* deps/debugger-agent/src/*)) +CPPLINT_EXCLUDE += $(wildcard test/addons/doc-*/*.cc test/addons/doc-*/*.h) + +CPPLINT_FILES = $(filter-out $(CPPLINT_EXCLUDE), $(wildcard \ + deps/debugger-agent/include/* \ + deps/debugger-agent/src/* \ + src/*.c \ + src/*.cc \ + src/*.h \ + test/addons/*/*.cc \ + test/addons/*/*.h \ + tools/icu/*.cc \ + tools/icu/*.h \ + )) cpplint: @$(PYTHON) tools/cpplint.py $(CPPLINT_FILES) diff --git a/test/addons/async-hello-world/binding.cc b/test/addons/async-hello-world/binding.cc index f458dc6a5632fd..049afab7e785bd 100644 --- a/test/addons/async-hello-world/binding.cc +++ b/test/addons/async-hello-world/binding.cc @@ -3,35 +3,33 @@ #include #include -using namespace v8; -using namespace node; - struct async_req { uv_work_t req; int input; int output; - Persistent callback; + v8::Persistent callback; }; void DoAsync(uv_work_t* r) { async_req* req = reinterpret_cast(r->data); - sleep(1); // simulate CPU intensive process... + sleep(1); // Simulate CPU intensive process... req->output = req->input * 2; } void AfterAsync(uv_work_t* r) { - Isolate* isolate = Isolate::GetCurrent(); - HandleScope scope(isolate); + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::HandleScope scope(isolate); async_req* req = reinterpret_cast(r->data); - Handle argv[2] = { - Null(isolate), - Integer::New(isolate, req->output) + v8::Handle argv[2] = { + v8::Null(isolate), + v8::Integer::New(isolate, req->output) }; - TryCatch try_catch; + v8::TryCatch try_catch(isolate); - Local callback = Local::New(isolate, req->callback); + v8::Local callback = + v8::Local::New(isolate, req->callback); callback->Call(isolate->GetCurrentContext()->Global(), 2, argv); // cleanup @@ -39,13 +37,13 @@ void AfterAsync(uv_work_t* r) { delete req; if (try_catch.HasCaught()) { - FatalException(isolate, try_catch); + node::FatalException(isolate, try_catch); } } -void Method(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); - HandleScope scope(isolate); +void Method(const v8::FunctionCallbackInfo& args) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::HandleScope scope(isolate); async_req* req = new async_req; req->req.data = req; @@ -53,7 +51,7 @@ void Method(const FunctionCallbackInfo& args) { req->input = args[0]->IntegerValue(); req->output = 0; - Local callback = Local::Cast(args[1]); + v8::Local callback = v8::Local::Cast(args[1]); req->callback.Reset(isolate, callback); uv_queue_work(uv_default_loop(), @@ -62,7 +60,7 @@ void Method(const FunctionCallbackInfo& args) { (uv_after_work_cb)AfterAsync); } -void init(Handle exports, Handle module) { +void init(v8::Handle exports, v8::Handle module) { NODE_SET_METHOD(module, "exports", Method); } diff --git a/test/addons/at-exit/binding.cc b/test/addons/at-exit/binding.cc index 156dbe4ff54bb8..9f192cdb14f864 100644 --- a/test/addons/at-exit/binding.cc +++ b/test/addons/at-exit/binding.cc @@ -21,7 +21,7 @@ static void at_exit_cb1(void* arg) { HandleScope handle_scope(isolate); assert(arg == 0); Local obj = Object::New(isolate); - assert(!obj.IsEmpty()); // assert VM is still alive + assert(!obj.IsEmpty()); // Assert VM is still alive. assert(obj->IsObject()); at_exit_cb1_called++; } diff --git a/test/addons/hello-world-function-export/binding.cc b/test/addons/hello-world-function-export/binding.cc index 91fc26cef652fb..88a6ba599928a4 100644 --- a/test/addons/hello-world-function-export/binding.cc +++ b/test/addons/hello-world-function-export/binding.cc @@ -1,15 +1,13 @@ #include #include -using namespace v8; - -void Method(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); - HandleScope scope(isolate); - args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world")); +void Method(const v8::FunctionCallbackInfo& args) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::HandleScope scope(isolate); + args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world")); } -void init(Handle exports, Handle module) { +void init(v8::Handle exports, v8::Handle module) { NODE_SET_METHOD(module, "exports", Method); } diff --git a/test/addons/hello-world/binding.cc b/test/addons/hello-world/binding.cc index 1a6d179abe264b..c8bbd554413438 100644 --- a/test/addons/hello-world/binding.cc +++ b/test/addons/hello-world/binding.cc @@ -1,15 +1,13 @@ #include #include -using namespace v8; - -void Method(const FunctionCallbackInfo& args) { - Isolate* isolate = Isolate::GetCurrent(); - HandleScope scope(isolate); - args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world")); +void Method(const v8::FunctionCallbackInfo& args) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::HandleScope scope(isolate); + args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world")); } -void init(Handle target) { +void init(v8::Handle target) { NODE_SET_METHOD(target, "hello", Method); } From 71119284f99129482d658033223461e931121fd9 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 18 Aug 2015 21:02:52 +0200 Subject: [PATCH 2/2] test: drop Isolate::GetCurrent() from addon tests v8::Isolate::GetCurrent() is not exactly deprecated at this point but its use is strongly discouraged. Update the addon tests so they no longer use it. PR-URL: https://github.com/nodejs/node/pull/2427 Reviewed-By: Trevor Norris --- test/addons/async-hello-world/binding.cc | 8 +++++--- test/addons/at-exit/binding.cc | 6 ++---- test/addons/hello-world-function-export/binding.cc | 2 +- test/addons/hello-world/binding.cc | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/addons/async-hello-world/binding.cc b/test/addons/async-hello-world/binding.cc index 049afab7e785bd..aee3a3763f4755 100644 --- a/test/addons/async-hello-world/binding.cc +++ b/test/addons/async-hello-world/binding.cc @@ -7,6 +7,7 @@ struct async_req { uv_work_t req; int input; int output; + v8::Isolate* isolate; v8::Persistent callback; }; @@ -17,9 +18,9 @@ void DoAsync(uv_work_t* r) { } void AfterAsync(uv_work_t* r) { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - v8::HandleScope scope(isolate); async_req* req = reinterpret_cast(r->data); + v8::Isolate* isolate = req->isolate; + v8::HandleScope scope(isolate); v8::Handle argv[2] = { v8::Null(isolate), @@ -42,7 +43,7 @@ void AfterAsync(uv_work_t* r) { } void Method(const v8::FunctionCallbackInfo& args) { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::Isolate* isolate = args.GetIsolate(); v8::HandleScope scope(isolate); async_req* req = new async_req; @@ -50,6 +51,7 @@ void Method(const v8::FunctionCallbackInfo& args) { req->input = args[0]->IntegerValue(); req->output = 0; + req->isolate = isolate; v8::Local callback = v8::Local::Cast(args[1]); req->callback.Reset(isolate, callback); diff --git a/test/addons/at-exit/binding.cc b/test/addons/at-exit/binding.cc index 9f192cdb14f864..d300aad3e8256f 100644 --- a/test/addons/at-exit/binding.cc +++ b/test/addons/at-exit/binding.cc @@ -16,10 +16,8 @@ static int at_exit_cb1_called = 0; static int at_exit_cb2_called = 0; static void at_exit_cb1(void* arg) { - // FIXME(bnoordhuis) Isolate::GetCurrent() is on its way out. - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = static_cast(arg); HandleScope handle_scope(isolate); - assert(arg == 0); Local obj = Object::New(isolate); assert(!obj.IsEmpty()); // Assert VM is still alive. assert(obj->IsObject()); @@ -37,7 +35,7 @@ static void sanity_check(void) { } void init(Handle target) { - AtExit(at_exit_cb1); + AtExit(at_exit_cb1, target->CreationContext()->GetIsolate()); AtExit(at_exit_cb2, cookie); AtExit(at_exit_cb2, cookie); atexit(sanity_check); diff --git a/test/addons/hello-world-function-export/binding.cc b/test/addons/hello-world-function-export/binding.cc index 88a6ba599928a4..68db22748fdbd3 100644 --- a/test/addons/hello-world-function-export/binding.cc +++ b/test/addons/hello-world-function-export/binding.cc @@ -2,7 +2,7 @@ #include void Method(const v8::FunctionCallbackInfo& args) { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::Isolate* isolate = args.GetIsolate(); v8::HandleScope scope(isolate); args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world")); } diff --git a/test/addons/hello-world/binding.cc b/test/addons/hello-world/binding.cc index c8bbd554413438..4982bc3e55d84a 100644 --- a/test/addons/hello-world/binding.cc +++ b/test/addons/hello-world/binding.cc @@ -2,7 +2,7 @@ #include void Method(const v8::FunctionCallbackInfo& args) { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::Isolate* isolate = args.GetIsolate(); v8::HandleScope scope(isolate); args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world")); }