Skip to content

Commit 5622543

Browse files
addaleaxtargos
authored andcommitted
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 <[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]>
1 parent ccf1178 commit 5622543

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

src/api/async_resource.cc

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ AsyncResource::AsyncResource(Isolate* isolate,
2424

2525
AsyncResource::~AsyncResource() {
2626
EmitAsyncDestroy(env_, async_context_);
27-
resource_.Reset();
2827
}
2928

3029
MaybeLocal<Value> AsyncResource::MakeCallback(Local<Function> callback,

src/node.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ class NODE_EXTERN AsyncResource {
870870

871871
private:
872872
Environment* env_;
873-
v8::Persistent<v8::Object> resource_;
873+
v8::Global<v8::Object> resource_;
874874
async_context async_context_;
875875
};
876876

test/addons/async-hello-world/binding.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct async_req {
1414
int input;
1515
int output;
1616
v8::Isolate* isolate;
17-
v8::Persistent<v8::Function> callback;
17+
v8::Global<v8::Function> callback;
1818
node::async_context context;
1919
};
2020

@@ -61,7 +61,6 @@ void AfterAsync(uv_work_t* r) {
6161
v8::SealHandleScope seal_handle_scope(isolate);
6262
// cleanup
6363
node::EmitAsyncDestroy(isolate, req->context);
64-
req->callback.Reset();
6564
delete req;
6665

6766
if (try_catch.HasCaught()) {

test/addons/callback-scope/binding.cc

+16-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <assert.h>
66
#include <vector>
7+
#include <memory>
78

89
namespace {
910

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

34-
static v8::Persistent<v8::Promise::Resolver> persistent;
35-
3635
static void Callback(uv_work_t* req, int ignored) {
3736
v8::Isolate* isolate = v8::Isolate::GetCurrent();
3837
v8::HandleScope scope(isolate);
3938
node::CallbackScope callback_scope(isolate, v8::Object::New(isolate),
4039
node::async_context{0, 0});
41-
42-
v8::Local<v8::Promise::Resolver> local =
43-
v8::Local<v8::Promise::Resolver>::New(isolate, persistent);
40+
std::unique_ptr<v8::Global<v8::Promise::Resolver>> persistent {
41+
static_cast<v8::Global<v8::Promise::Resolver>*>(req->data) };
42+
v8::Local<v8::Promise::Resolver> local = persistent->Get(isolate);
4443
local->Resolve(isolate->GetCurrentContext(),
4544
v8::Undefined(isolate)).ToChecked();
4645
delete req;
@@ -49,20 +48,21 @@ static void Callback(uv_work_t* req, int ignored) {
4948
static void TestResolveAsync(const v8::FunctionCallbackInfo<v8::Value>& args) {
5049
v8::Isolate* isolate = args.GetIsolate();
5150

52-
if (persistent.IsEmpty()) {
53-
persistent.Reset(isolate, v8::Promise::Resolver::New(
54-
isolate->GetCurrentContext()).ToLocalChecked());
51+
v8::Global<v8::Promise::Resolver>* persistent =
52+
new v8::Global<v8::Promise::Resolver>(
53+
isolate,
54+
v8::Promise::Resolver::New(
55+
isolate->GetCurrentContext()).ToLocalChecked());
5556

56-
uv_work_t* req = new uv_work_t;
57+
uv_work_t* req = new uv_work_t;
58+
req->data = static_cast<void*>(persistent);
5759

58-
uv_queue_work(node::GetCurrentEventLoop(isolate),
59-
req,
60-
[](uv_work_t*) {},
61-
Callback);
62-
}
60+
uv_queue_work(node::GetCurrentEventLoop(isolate),
61+
req,
62+
[](uv_work_t*) {},
63+
Callback);
6364

64-
v8::Local<v8::Promise::Resolver> local =
65-
v8::Local<v8::Promise::Resolver>::New(isolate, persistent);
65+
v8::Local<v8::Promise::Resolver> local = persistent->Get(isolate);
6666

6767
args.GetReturnValue().Set(local->GetPromise());
6868
}

0 commit comments

Comments
 (0)