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 <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: David Carlier <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Stephen Belanger <[email protected]>
  • Loading branch information
addaleax authored and BridgeAR committed Jan 3, 2020
1 parent 614b2c5 commit a27edd8
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
};

Expand Down
3 changes: 1 addition & 2 deletions test/addons/async-hello-world/binding.cc
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 a27edd8

Please sign in to comment.