From 56225439c473c9caa15dcc6834bccaf422af2cd0 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 18 Dec 2019 15:16:02 +0100 Subject: [PATCH] src,test: use v8::Global instead of v8::Persistent This is in preparation for a lint rule forbidding usage of `v8::Persistent`. PR-URL: https://github.com/nodejs/node/pull/31018 Reviewed-By: Colin Ihrig Reviewed-By: Richard Lau Reviewed-By: James M Snell Reviewed-By: David Carlier Reviewed-By: Rich Trott Reviewed-By: Gus Caplan Reviewed-By: Joyee Cheung Reviewed-By: Ben Noordhuis Reviewed-By: Stephen Belanger --- src/api/async_resource.cc | 1 - src/node.h | 2 +- test/addons/async-hello-world/binding.cc | 3 +-- test/addons/callback-scope/binding.cc | 32 ++++++++++++------------ 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/api/async_resource.cc b/src/api/async_resource.cc index eef80c85f44f4c..0a2437fe6eda5c 100644 --- a/src/api/async_resource.cc +++ b/src/api/async_resource.cc @@ -24,7 +24,6 @@ AsyncResource::AsyncResource(Isolate* isolate, AsyncResource::~AsyncResource() { EmitAsyncDestroy(env_, async_context_); - resource_.Reset(); } MaybeLocal AsyncResource::MakeCallback(Local callback, diff --git a/src/node.h b/src/node.h index 782d22412ec69a..d6ec670ae73b7a 100644 --- a/src/node.h +++ b/src/node.h @@ -870,7 +870,7 @@ class NODE_EXTERN AsyncResource { private: Environment* env_; - v8::Persistent resource_; + v8::Global resource_; async_context async_context_; }; diff --git a/test/addons/async-hello-world/binding.cc b/test/addons/async-hello-world/binding.cc index ff899628c4d56d..2f77aee52ab2d9 100644 --- a/test/addons/async-hello-world/binding.cc +++ b/test/addons/async-hello-world/binding.cc @@ -14,7 +14,7 @@ struct async_req { int input; int output; v8::Isolate* isolate; - v8::Persistent callback; + v8::Global callback; node::async_context context; }; @@ -61,7 +61,6 @@ void AfterAsync(uv_work_t* r) { v8::SealHandleScope seal_handle_scope(isolate); // cleanup node::EmitAsyncDestroy(isolate, req->context); - req->callback.Reset(); delete req; if (try_catch.HasCaught()) { diff --git a/test/addons/callback-scope/binding.cc b/test/addons/callback-scope/binding.cc index 94d5ec91d7f3a2..6bbf5fed35face 100644 --- a/test/addons/callback-scope/binding.cc +++ b/test/addons/callback-scope/binding.cc @@ -4,6 +4,7 @@ #include #include +#include namespace { @@ -31,16 +32,14 @@ void RunInCallbackScope(const v8::FunctionCallbackInfo& args) { args.GetReturnValue().Set(ret.ToLocalChecked()); } -static v8::Persistent persistent; - static void Callback(uv_work_t* req, int ignored) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::HandleScope scope(isolate); node::CallbackScope callback_scope(isolate, v8::Object::New(isolate), node::async_context{0, 0}); - - v8::Local local = - v8::Local::New(isolate, persistent); + std::unique_ptr> persistent { + static_cast*>(req->data) }; + v8::Local local = persistent->Get(isolate); local->Resolve(isolate->GetCurrentContext(), v8::Undefined(isolate)).ToChecked(); delete req; @@ -49,20 +48,21 @@ static void Callback(uv_work_t* req, int ignored) { static void TestResolveAsync(const v8::FunctionCallbackInfo& args) { v8::Isolate* isolate = args.GetIsolate(); - if (persistent.IsEmpty()) { - persistent.Reset(isolate, v8::Promise::Resolver::New( - isolate->GetCurrentContext()).ToLocalChecked()); + v8::Global* persistent = + new v8::Global( + isolate, + v8::Promise::Resolver::New( + isolate->GetCurrentContext()).ToLocalChecked()); - uv_work_t* req = new uv_work_t; + uv_work_t* req = new uv_work_t; + req->data = static_cast(persistent); - uv_queue_work(node::GetCurrentEventLoop(isolate), - req, - [](uv_work_t*) {}, - Callback); - } + uv_queue_work(node::GetCurrentEventLoop(isolate), + req, + [](uv_work_t*) {}, + Callback); - v8::Local local = - v8::Local::New(isolate, persistent); + v8::Local local = persistent->Get(isolate); args.GetReturnValue().Set(local->GetPromise()); }