Skip to content

Commit 7601cdf

Browse files
Gabriel SchulhofBridgeAR
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent f5945c9 commit 7601cdf

14 files changed

+50
-67
lines changed

src/async_wrap.cc

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 0 additions & 8 deletions
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.

0 commit comments

Comments
 (0)