Skip to content

Commit 0603c0a

Browse files
Gabriel Schulhofdanbev
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 bda4643 commit 0603c0a

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
@@ -346,7 +346,8 @@ void AsyncWrap::WeakCallback(const v8::WeakCallbackInfo<DestroyParam>& info) {
346346
HandleScope scope(info.GetIsolate());
347347

348348
std::unique_ptr<DestroyParam> p{info.GetParameter()};
349-
Local<Object> prop_bag = PersistentToLocal(info.GetIsolate(), p->propBag);
349+
Local<Object> prop_bag = PersistentToLocal::Default(info.GetIsolate(),
350+
p->propBag);
350351
Local<Value> val;
351352

352353
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
@@ -895,7 +895,7 @@ void Environment::ForEachBaseObject(T&& iterator) {
895895

896896
#define V(PropertyName, TypeName) \
897897
inline v8::Local<TypeName> Environment::PropertyName() const { \
898-
return StrongPersistentToLocal(PropertyName ## _); \
898+
return PersistentToLocal::Strong(PropertyName ## _); \
899899
} \
900900
inline void Environment::set_ ## PropertyName(v8::Local<TypeName> value) { \
901901
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
@@ -2348,7 +2348,8 @@ int SSLWrap<Base>::TLSExtStatusCallback(SSL* s, void* arg) {
23482348
if (w->ocsp_response_.IsEmpty())
23492349
return SSL_TLSEXT_ERR_NOACK;
23502350

2351-
Local<Object> obj = PersistentToLocal(env->isolate(), w->ocsp_response_);
2351+
Local<Object> obj = PersistentToLocal::Default(env->isolate(),
2352+
w->ocsp_response_);
23522353
char* resp = Buffer::Data(obj);
23532354
size_t len = Buffer::Length(obj);
23542355

src/node_internals.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,6 @@ extern std::shared_ptr<PerProcessOptions> per_process_opts;
182182
// Forward declaration
183183
class Environment;
184184

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

0 commit comments

Comments
 (0)