Skip to content

Commit

Permalink
src,test: use v8::Global instead of v8::Persistent
Browse files Browse the repository at this point in the history
This is in preparation for a lint rule forbidding usage of
`v8::Persistent`.

PR-URL: #31018
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
  • Loading branch information
addaleax committed Dec 24, 2019
1 parent db109e8 commit bbca4a8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/api/async_resource.cc
Expand Up @@ -24,7 +24,6 @@ AsyncResource::AsyncResource(Isolate* isolate,

AsyncResource::~AsyncResource() {
EmitAsyncDestroy(env_, async_context_);
resource_.Reset();
}

MaybeLocal<Value> AsyncResource::MakeCallback(Local<Function> callback,
Expand Down
2 changes: 1 addition & 1 deletion src/node.h
Expand Up @@ -893,7 +893,7 @@ class NODE_EXTERN AsyncResource {

private:
Environment* env_;
v8::Persistent<v8::Object> resource_;
v8::Global<v8::Object> resource_;
async_context async_context_;
};

Expand Down
3 changes: 1 addition & 2 deletions test/addons/async-hello-world/binding.cc
Expand Up @@ -14,7 +14,7 @@ struct async_req {
int input;
int output;
v8::Isolate* isolate;
v8::Persistent<v8::Function> callback;
v8::Global<v8::Function> callback;
node::async_context context;
};

Expand Down Expand Up @@ -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()) {
Expand Down
32 changes: 16 additions & 16 deletions test/addons/callback-scope/binding.cc
Expand Up @@ -4,6 +4,7 @@

#include <assert.h>
#include <vector>
#include <memory>

namespace {

Expand Down Expand Up @@ -31,16 +32,14 @@ void RunInCallbackScope(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().Set(ret.ToLocalChecked());
}

static v8::Persistent<v8::Promise::Resolver> 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<v8::Promise::Resolver> local =
v8::Local<v8::Promise::Resolver>::New(isolate, persistent);
std::unique_ptr<v8::Global<v8::Promise::Resolver>> persistent {
static_cast<v8::Global<v8::Promise::Resolver>*>(req->data) };
v8::Local<v8::Promise::Resolver> local = persistent->Get(isolate);
local->Resolve(isolate->GetCurrentContext(),
v8::Undefined(isolate)).ToChecked();
delete req;
Expand All @@ -49,20 +48,21 @@ static void Callback(uv_work_t* req, int ignored) {
static void TestResolveAsync(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();

if (persistent.IsEmpty()) {
persistent.Reset(isolate, v8::Promise::Resolver::New(
isolate->GetCurrentContext()).ToLocalChecked());
v8::Global<v8::Promise::Resolver>* persistent =
new v8::Global<v8::Promise::Resolver>(
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<void*>(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<v8::Promise::Resolver> local =
v8::Local<v8::Promise::Resolver>::New(isolate, persistent);
v8::Local<v8::Promise::Resolver> local = persistent->Get(isolate);

args.GetReturnValue().Set(local->GetPromise());
}
Expand Down

0 comments on commit bbca4a8

Please sign in to comment.