Skip to content
Permalink
Browse files

src,test: use v8::Global instead of v8::Persistent

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 authored and BridgeAR committed Dec 18, 2019
1 parent 614b2c5 commit a27edd833571dbee79b5102834f89c46343727dd
Showing with 18 additions and 20 deletions.
  1. +0 −1 src/api/async_resource.cc
  2. +1 −1 src/node.h
  3. +1 −2 test/addons/async-hello-world/binding.cc
  4. +16 −16 test/addons/callback-scope/binding.cc
@@ -24,7 +24,6 @@ AsyncResource::AsyncResource(Isolate* isolate,

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

MaybeLocal<Value> AsyncResource::MakeCallback(Local<Function> callback,
@@ -881,7 +881,7 @@ class NODE_EXTERN AsyncResource {

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

@@ -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;
};

@@ -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()) {
@@ -4,6 +4,7 @@

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

namespace {

@@ -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;
@@ -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());
}

0 comments on commit a27edd8

Please sign in to comment.
You can’t perform that action at this time.