Skip to content

Commit 7601cdf

Browse files
Gabriel SchulhofBridgeAR
Gabriel Schulhof
authored andcommitted
src: bundle persistent-to-local methods as class
Create a class `PersistentToLocal` which contains three methods, `Strong`, `Weak`, and `Default`: * `Strong` returns a `Local` from a strong persistent reference, * `Weak` returns a `Local` from a weak persistent reference, and * `Default` decides based on `IsWeak()` which of the above two to call. These replace `node::StrongPersistentToLocal()`, `node::WeakPersistentToLocal()`, and `node::PersistentToLocal()`, respectively. PR-URL: #24276 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent f5945c9 commit 7601cdf

14 files changed

+50
-67
lines changed

src/async_wrap.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,8 @@ void AsyncWrap::WeakCallback(const v8::WeakCallbackInfo<DestroyParam>& info) {
358358
HandleScope scope(info.GetIsolate());
359359

360360
std::unique_ptr<DestroyParam> p{info.GetParameter()};
361-
Local<Object> prop_bag = PersistentToLocal(info.GetIsolate(), p->propBag);
361+
Local<Object> prop_bag = PersistentToLocal::Default(info.GetIsolate(),
362+
p->propBag);
362363
Local<Value> val;
363364

364365
if (!prop_bag->Get(p->env->context(), p->env->destroyed_string())

src/base_object-inl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Persistent<v8::Object>& BaseObject::persistent() {
6262

6363

6464
v8::Local<v8::Object> BaseObject::object() const {
65-
return PersistentToLocal(env_->isolate(), persistent_handle_);
65+
return PersistentToLocal::Default(env_->isolate(), persistent_handle_);
6666
}
6767

6868
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {

src/env-inl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ void Environment::ForEachBaseObject(T&& iterator) {
899899

900900
#define V(PropertyName, TypeName) \
901901
inline v8::Local<TypeName> Environment::PropertyName() const { \
902-
return StrongPersistentToLocal(PropertyName ## _); \
902+
return PersistentToLocal::Strong(PropertyName ## _); \
903903
} \
904904
inline void Environment::set_ ## PropertyName(v8::Local<TypeName> value) { \
905905
PropertyName ## _.Reset(isolate(), value); \

src/heap_utils.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class JSGraphJSNode : public EmbedderGraph::Node {
2626
const char* Name() override { return "<JS Node>"; }
2727
size_t SizeInBytes() override { return 0; }
2828
bool IsEmbedderNode() override { return false; }
29-
Local<Value> JSValue() { return StrongPersistentToLocal(persistent_); }
29+
Local<Value> JSValue() { return PersistentToLocal::Strong(persistent_); }
3030

3131
int IdentityHash() {
3232
Local<Value> v = JSValue();

src/memory_tracker.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class NodeBIO;
6969
* // a BaseObject or an AsyncWrap class
7070
* bool IsRootNode() const override { return !wrapped_.IsWeak(); }
7171
* v8::Local<v8::Object> WrappedObject() const override {
72-
* return node::PersistentToLocal(wrapped_);
72+
* return node::PersistentToLocal::Default(wrapped_);
7373
* }
7474
* private:
7575
* AnotherRetainerClass another_retainer_;

src/node_api.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct napi_env__ {
3030
node::Persistent<v8::Context> context_persistent;
3131

3232
inline v8::Local<v8::Context> context() const {
33-
return StrongPersistentToLocal(context_persistent);
33+
return node::PersistentToLocal::Strong(context_persistent);
3434
}
3535

3636
inline node::Environment* node_env() const {

src/node_contextify.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ void ContextifyScript::CreateCachedData(
767767
ContextifyScript* wrapped_script;
768768
ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder());
769769
Local<UnboundScript> unbound_script =
770-
PersistentToLocal(env->isolate(), wrapped_script->script_);
770+
PersistentToLocal::Default(env->isolate(), wrapped_script->script_);
771771
std::unique_ptr<ScriptCompiler::CachedData> cached_data(
772772
ScriptCompiler::CreateCodeCache(unbound_script));
773773
if (!cached_data) {
@@ -867,7 +867,7 @@ bool ContextifyScript::EvalMachine(Environment* env,
867867
ContextifyScript* wrapped_script;
868868
ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder(), false);
869869
Local<UnboundScript> unbound_script =
870-
PersistentToLocal(env->isolate(), wrapped_script->script_);
870+
PersistentToLocal::Default(env->isolate(), wrapped_script->script_);
871871
Local<Script> script = unbound_script->BindToCurrentContext();
872872

873873
MaybeLocal<Value> result;

src/node_contextify.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ContextifyContext {
4040
}
4141

4242
inline v8::Local<v8::Context> context() const {
43-
return PersistentToLocal(env()->isolate(), context_);
43+
return PersistentToLocal::Default(env()->isolate(), context_);
4444
}
4545

4646
inline v8::Local<v8::Object> global_proxy() const {

src/node_crypto.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -2339,7 +2339,8 @@ int SSLWrap<Base>::TLSExtStatusCallback(SSL* s, void* arg) {
23392339
if (w->ocsp_response_.IsEmpty())
23402340
return SSL_TLSEXT_ERR_NOACK;
23412341

2342-
Local<Object> obj = PersistentToLocal(env->isolate(), w->ocsp_response_);
2342+
Local<Object> obj = PersistentToLocal::Default(env->isolate(),
2343+
w->ocsp_response_);
23432344
char* resp = Buffer::Data(obj);
23442345
size_t len = Buffer::Length(obj);
23452346

src/node_internals.h

-8
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,6 @@ extern std::shared_ptr<PerProcessOptions> per_process_opts;
183183
// Forward declaration
184184
class Environment;
185185

186-
// If persistent.IsWeak() == false, then do not call persistent.Reset()
187-
// while the returned Local<T> is still in scope, it will destroy the
188-
// reference to the object.
189-
template <class TypeName>
190-
inline v8::Local<TypeName> PersistentToLocal(
191-
v8::Isolate* isolate,
192-
const Persistent<TypeName>& persistent);
193-
194186
// Convert a struct sockaddr to a { address: '1.2.3.4', port: 1234 } JS object.
195187
// Sets address and port properties on the info object and returns it.
196188
// If |info| is omitted, a new object is returned.

src/node_persistent.h

+36
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,42 @@ struct ResetInDestructorPersistentTraits {
2323
template <typename T>
2424
using Persistent = v8::Persistent<T, ResetInDestructorPersistentTraits<T>>;
2525

26+
class PersistentToLocal {
27+
public:
28+
// If persistent.IsWeak() == false, then do not call persistent.Reset()
29+
// while the returned Local<T> is still in scope, it will destroy the
30+
// reference to the object.
31+
template <class TypeName>
32+
static inline v8::Local<TypeName> Default(
33+
v8::Isolate* isolate,
34+
const Persistent<TypeName>& persistent) {
35+
if (persistent.IsWeak()) {
36+
return PersistentToLocal::Weak(isolate, persistent);
37+
} else {
38+
return PersistentToLocal::Strong(persistent);
39+
}
40+
}
41+
42+
// Unchecked conversion from a non-weak Persistent<T> to Local<T>,
43+
// use with care!
44+
//
45+
// Do not call persistent.Reset() while the returned Local<T> is still in
46+
// scope, it will destroy the reference to the object.
47+
template <class TypeName>
48+
static inline v8::Local<TypeName> Strong(
49+
const Persistent<TypeName>& persistent) {
50+
return *reinterpret_cast<v8::Local<TypeName>*>(
51+
const_cast<Persistent<TypeName>*>(&persistent));
52+
}
53+
54+
template <class TypeName>
55+
static inline v8::Local<TypeName> Weak(
56+
v8::Isolate* isolate,
57+
const Persistent<TypeName>& persistent) {
58+
return v8::Local<TypeName>::New(isolate, persistent);
59+
}
60+
};
61+
2662
} // namespace node
2763

2864
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

src/node_zlib.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
313313
UpdateWriteResult();
314314

315315
// call the write() cb
316-
Local<Function> cb = PersistentToLocal(env()->isolate(),
317-
write_js_callback_);
316+
Local<Function> cb = PersistentToLocal::Default(env()->isolate(),
317+
write_js_callback_);
318318
MakeCallback(cb, 0, nullptr);
319319

320320
if (pending_close_)

src/util-inl.h

-25
Original file line numberDiff line numberDiff line change
@@ -165,31 +165,6 @@ inline ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
165165
return ContainerOfHelper<Inner, Outer>(field, pointer);
166166
}
167167

168-
template <class TypeName>
169-
inline v8::Local<TypeName> PersistentToLocal(
170-
v8::Isolate* isolate,
171-
const Persistent<TypeName>& persistent) {
172-
if (persistent.IsWeak()) {
173-
return WeakPersistentToLocal(isolate, persistent);
174-
} else {
175-
return StrongPersistentToLocal(persistent);
176-
}
177-
}
178-
179-
template <class TypeName>
180-
inline v8::Local<TypeName> StrongPersistentToLocal(
181-
const Persistent<TypeName>& persistent) {
182-
return *reinterpret_cast<v8::Local<TypeName>*>(
183-
const_cast<Persistent<TypeName>*>(&persistent));
184-
}
185-
186-
template <class TypeName>
187-
inline v8::Local<TypeName> WeakPersistentToLocal(
188-
v8::Isolate* isolate,
189-
const Persistent<TypeName>& persistent) {
190-
return v8::Local<TypeName>::New(isolate, persistent);
191-
}
192-
193168
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
194169
const char* data,
195170
int length) {

src/util.h

-22
Original file line numberDiff line numberDiff line change
@@ -201,28 +201,6 @@ template <typename Inner, typename Outer>
201201
inline ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
202202
Inner* pointer);
203203

204-
// If persistent.IsWeak() == false, then do not call persistent.Reset()
205-
// while the returned Local<T> is still in scope, it will destroy the
206-
// reference to the object.
207-
template <class TypeName>
208-
inline v8::Local<TypeName> PersistentToLocal(
209-
v8::Isolate* isolate,
210-
const Persistent<TypeName>& persistent);
211-
212-
// Unchecked conversion from a non-weak Persistent<T> to Local<T>,
213-
// use with care!
214-
//
215-
// Do not call persistent.Reset() while the returned Local<T> is still in
216-
// scope, it will destroy the reference to the object.
217-
template <class TypeName>
218-
inline v8::Local<TypeName> StrongPersistentToLocal(
219-
const Persistent<TypeName>& persistent);
220-
221-
template <class TypeName>
222-
inline v8::Local<TypeName> WeakPersistentToLocal(
223-
v8::Isolate* isolate,
224-
const Persistent<TypeName>& persistent);
225-
226204
// Convenience wrapper around v8::String::NewFromOneByte().
227205
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
228206
const char* data,

0 commit comments

Comments
 (0)