Skip to content

Commit 3370291

Browse files
addaleaxtargos
authored andcommitted
src: prefer v8::Global over node::Persistent
`v8::Global` is essentially a nicer variant of `node::Persistent` that, in addition to reset-on-destroy, also implements move semantics. This commit makes the necessary replacements, removes `node::Persistent` and (now-)unnecessary inclusions of the `node_persistent.h` header, and makes some of the functions that take Persistents as arguments more generic so that they work with all `v8::PersistentBase` flavours. PR-URL: #27287 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent eeab007 commit 3370291

23 files changed

+81
-106
lines changed

node.gyp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,6 @@
606606
'src/node_options-inl.h',
607607
'src/node_perf.h',
608608
'src/node_perf_common.h',
609-
'src/node_persistent.h',
610609
'src/node_platform.h',
611610
'src/node_process.h',
612611
'src/node_revert.h',

src/async_wrap.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ using v8::EscapableHandleScope;
3535
using v8::Function;
3636
using v8::FunctionCallbackInfo;
3737
using v8::FunctionTemplate;
38+
using v8::Global;
3839
using v8::HandleScope;
3940
using v8::Integer;
4041
using v8::Isolate;
@@ -346,8 +347,8 @@ class DestroyParam {
346347
public:
347348
double asyncId;
348349
Environment* env;
349-
Persistent<Object> target;
350-
Persistent<Object> propBag;
350+
Global<Object> target;
351+
Global<Object> propBag;
351352
};
352353

353354
void AsyncWrap::WeakCallback(const WeakCallbackInfo<DestroyParam>& info) {

src/base_object-inl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ BaseObject::~BaseObject() {
5656
}
5757

5858

59-
Persistent<v8::Object>& BaseObject::persistent() {
59+
v8::Global<v8::Object>& BaseObject::persistent() {
6060
return persistent_handle_;
6161
}
6262

src/base_object.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
2626

27-
#include "node_persistent.h"
2827
#include "memory_tracker-inl.h"
2928
#include "v8.h"
3029
#include <type_traits> // std::remove_reference
@@ -50,7 +49,7 @@ class BaseObject : public MemoryRetainer {
5049
// is associated with the passed Isolate in debug mode.
5150
inline v8::Local<v8::Object> object(v8::Isolate* isolate) const;
5251

53-
inline Persistent<v8::Object>& persistent();
52+
inline v8::Global<v8::Object>& persistent();
5453

5554
inline Environment* env() const;
5655

@@ -62,7 +61,7 @@ class BaseObject : public MemoryRetainer {
6261
template <typename T>
6362
static inline T* FromJSObject(v8::Local<v8::Object> object);
6463

65-
// Make the `Persistent` a weak reference and, `delete` this object once
64+
// Make the `v8::Global` a weak reference and, `delete` this object once
6665
// the JS object has been garbage collected.
6766
inline void MakeWeak();
6867

@@ -96,7 +95,7 @@ class BaseObject : public MemoryRetainer {
9695
friend int GenDebugSymbols();
9796
friend class CleanupHookCallback;
9897

99-
Persistent<v8::Object> persistent_handle_;
98+
v8::Global<v8::Object> persistent_handle_;
10099
Environment* env_;
101100
};
102101

src/env.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ class Environment : public MemoryRetainer {
929929
std::unordered_map<uint32_t, contextify::ContextifyScript*>
930930
id_to_script_map;
931931
std::unordered_set<CompileFnEntry*> compile_fn_entries;
932-
std::unordered_map<uint32_t, Persistent<v8::Function>> id_to_function_map;
932+
std::unordered_map<uint32_t, v8::Global<v8::Function>> id_to_function_map;
933933

934934
inline uint32_t get_next_module_id();
935935
inline uint32_t get_next_script_id();
@@ -1292,7 +1292,7 @@ class Environment : public MemoryRetainer {
12921292
template <typename T>
12931293
void ForEachBaseObject(T&& iterator);
12941294

1295-
#define V(PropertyName, TypeName) Persistent<TypeName> PropertyName ## _;
1295+
#define V(PropertyName, TypeName) v8::Global<TypeName> PropertyName ## _;
12961296
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
12971297
ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
12981298
#undef V

src/heap_utils.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ using v8::EmbedderGraph;
88
using v8::EscapableHandleScope;
99
using v8::FunctionCallbackInfo;
1010
using v8::FunctionTemplate;
11+
using v8::Global;
1112
using v8::HandleScope;
1213
using v8::HeapSnapshot;
1314
using v8::Isolate;
@@ -56,7 +57,7 @@ class JSGraphJSNode : public EmbedderGraph::Node {
5657
};
5758

5859
private:
59-
Persistent<Value> persistent_;
60+
Global<Value> persistent_;
6061
};
6162

6263
class JSGraph : public EmbedderGraph {

src/inspector_agent.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ using node::FatalError;
3434

3535
using v8::Context;
3636
using v8::Function;
37+
using v8::Global;
3738
using v8::HandleScope;
3839
using v8::Isolate;
3940
using v8::Local;
@@ -834,7 +835,7 @@ void Agent::DisableAsyncHook() {
834835
}
835836

836837
void Agent::ToggleAsyncHook(Isolate* isolate,
837-
const node::Persistent<Function>& fn) {
838+
const Global<Function>& fn) {
838839
CHECK(parent_env_->has_run_bootstrapping_code());
839840
HandleScope handle_scope(isolate);
840841
CHECK(!fn.IsEmpty());

src/inspector_agent.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#endif
88

99
#include "node_options-inl.h"
10-
#include "node_persistent.h"
1110
#include "v8.h"
1211

1312
#include <cstddef>
@@ -114,7 +113,7 @@ class Agent {
114113

115114
private:
116115
void ToggleAsyncHook(v8::Isolate* isolate,
117-
const node::Persistent<v8::Function>& fn);
116+
const v8::Global<v8::Function>& fn);
118117

119118
node::Environment* parent_env_;
120119
// Encapsulates majority of the Inspector functionality
@@ -133,8 +132,8 @@ class Agent {
133132

134133
bool pending_enable_async_hook_ = false;
135134
bool pending_disable_async_hook_ = false;
136-
node::Persistent<v8::Function> enable_async_hook_function_;
137-
node::Persistent<v8::Function> disable_async_hook_function_;
135+
v8::Global<v8::Function> enable_async_hook_function_;
136+
v8::Global<v8::Function> disable_async_hook_function_;
138137
};
139138

140139
} // namespace inspector

src/inspector_js_api.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ using v8::Context;
1515
using v8::Function;
1616
using v8::FunctionCallbackInfo;
1717
using v8::FunctionTemplate;
18+
using v8::Global;
1819
using v8::HandleScope;
1920
using v8::Isolate;
2021
using v8::Local;
@@ -116,7 +117,7 @@ class JSBindingsConnection : public AsyncWrap {
116117

117118
private:
118119
std::unique_ptr<InspectorSession> session_;
119-
Persistent<Function> callback_;
120+
Global<Function> callback_;
120121
};
121122

122123
static bool InspectorEnabled(Environment* env) {

src/js_native_api_v8_internals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
namespace v8impl {
3030

3131
template <typename T>
32-
using Persistent = node::Persistent<T>;
32+
using Persistent = v8::Global<T>;
3333

3434
using PersistentToLocal = node::PersistentToLocal;
3535

0 commit comments

Comments
 (0)