8000 src: prefer v8::Global over node::Persistent · nodejs/node@3370291 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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

-1
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

+3-2
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

+1-1
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

+3-4
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

+2-2
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

+2-1
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

+2-1
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

+3-4
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

+2-1
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

+1-1
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

src/memory_tracker-inl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ void MemoryTracker::TrackField(const char* edge_name,
184184
TrackField(edge_name, value.Get(isolate_));
185185
}
186186

187-
template <typename T, typename Traits>
187+
template <typename T>
188188
void MemoryTracker::TrackField(const char* edge_name,
189-
const v8::Persistent<T, Traits>& value,
189+
const v8::PersistentBase<T>& value,
190190
const char* node_name) {
191191
TrackField(edge_name, value.Get(isolate_));
192192
}

src/memory_tracker.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ class CleanupHookCallback;
9090
* NonPointerRetainerClass non_pointer_retainer;
9191
* InternalClass internal_member_;
9292
* std::vector<uv_async_t> vector_;
93-
* node::Persistent<Object> target_;
93+
* v8::Global<Object> target_;
9494
*
95-
* node::Persistent<Object> wrapped_;
95+
* v8::Global<Object> wrapped_;
9696
* }
9797
*
9898
* This creates the following graph:
@@ -185,9 +185,9 @@ class MemoryTracker {
185185
void TrackField(const char* edge_name,
186186
const v8::Eternal<T>& value,
187187
const char* node_name);
188-
template <typename T, typename Traits>
188+
template <typename T>
189189
inline void TrackField(const char* edge_name,
190-
const v8::Persistent<T, Traits>& value,
190+
const v8::PersistentBase<T>& value,
191191
const char* node_name = nullptr);
192192
template <typename T>
193193
inline void TrackField(const char* edge_name,

src/module_wrap.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ using v8::Context;
2525
using v8::Function;
2626
using v8::FunctionCallbackInfo;
2727
using v8::FunctionTemplate;
28+
using v8::Global;
2829
using v8::HandleScope;
2930
using v8::Integer;
3031
using v8::IntegrityLevel;
@@ -611,7 +612,7 @@ Maybe<const PackageConfig*> GetPackageConfig(Environment* env,
611612
env->exports_string()).ToLocal(&exports_v) &&
612613
(exports_v->IsObject() || exports_v->IsString() ||
613614
exports_v->IsBoolean())) {
614-
Persistent<Value> exports;
615+
Global<Value> exports;
615616
exports.Reset(env->isolate(), exports_v);
616617

617618
auto entry = env->package_json_cache.emplace(path,

src/module_wrap.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ class ModuleWrap : public BaseObject {
7575
v8::Local<v8::Module> referrer);
7676
static ModuleWrap* GetFromModule(node::Environment*, v8::Local<v8::Module>);
7777

78-
Persistent<v8::Module> module_;
79-
Persistent<v8::String> url_;
78+
v8::Global<v8::Module> module_;
79+
v8::Global<v8::String> url_;
8080
bool linked_ = false;
81-
std::unordered_map<std::string, Persistent<v8::Promise>> resolve_cache_;
82-
Persistent<v8::Context> context_;
81+
std::unordered_map<std::string, v8::Global<v8::Promise>> resolve_cache_;
82+
v8::Global<v8::Context> context_;
8383
uint32_t id_;
8484
};
8585

src/node_buffer.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ using v8::ArrayBufferView;
6262
using v8::Context;
6363
using v8::EscapableHandleScope;
6464
using v8::FunctionCallbackInfo;
65+
using v8::Global;
6566
using v8::Integer;
6667
using v8::Isolate;
6768
using v8::Just;
@@ -99,7 +100,7 @@ class CallbackInfo {
99100
FreeCallback callback,
100101
char* data,
101102
void* hint);
102-
Persistent<ArrayBuffer> persistent_;
103+
Global<ArrayBuffer> persistent_;
103104
FreeCallback const callback_;
104105
char* const data_;
105106
void* const hint_;

src/node_contextify.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class ContextifyContext {
100100
uint32_t index,
101101
const v8::PropertyCallbackInfo<v8::Boolean>& args);
102102
Environment* const env_;
103-
Persistent<v8::Context> context_;
103+
v8::Global<v8::Context> context_;
104104
};
105105

106106
class ContextifyScript : public BaseObject {
@@ -129,7 +129,7 @@ class ContextifyScript : public BaseObject {
129129
inline uint32_t id() { return id_; }
130130
10000
131131
private:
132-
node::Persistent<v8::UnboundScript> script_;
132+
v8::Global<v8::UnboundScript> script_;
133133
uint32_t id_;
134134
};
135135

src/node_crypto.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ class SSLWrap {
321321

322322
ClientHelloParser hello_parser_;
323323

324-
Persistent<v8::ArrayBufferView> ocsp_response_;
325-
Persistent<v8::Value> sni_context_;
324+
v8::Global<v8::ArrayBufferView> ocsp_response_;
325+
v8::Global<v8::Value> sni_context_;
326326

327327
friend class SecureContext;
328328
};

src/node_file.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ class FileHandle : public AsyncWrap, public StreamBase {
451451
CloseReq& operator=(const CloseReq&&) = delete;
452452

453453
private:
454-
Persistent<Promise> promise_{};
455-
Persistent<Value> ref_{};
454+
v8::Global<Promise> promise_{};
455+
v8::Global<Value> ref_{};
456456
};
457457

458458
// Asynchronous close

src/node_internals.h

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "node.h"
2929
#include "node_binding.h"
3030
#include "node_mutex.h"
31-
#include "node_persistent.h"
3231
#include "tracing/trace_event.h"
3332
#include "util-inl.h"
3433
#include "uv.h"

src/node_persistent.h

-66
This file was deleted.

src/node_util.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ using v8::Context;
1313
using v8::Function;
1414
using v8::FunctionCallbackInfo;
1515
using v8::FunctionTemplate;
16+
using v8::Global;
1617
using v8::IndexFilter;
1718
using v8::Integer;
1819
using v8::Isolate;
@@ -189,7 +190,7 @@ class WeakReference : public BaseObject {
189190
SET_NO_MEMORY_INFO()
190191

191192
private:
192-
Persistent<Object> target_;
193+
Global<Object> target_;
193194
};
194195

195196
static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {

0 commit comments

Comments
 (0)
0