From 7601cdfe8bd47a1eee0851029258d24553be5364 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Wed, 7 Nov 2018 07:09:40 -0500 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/24276 Reviewed-By: Joyee Cheung Reviewed-By: Colin Ihrig Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Refael Ackermann --- src/async_wrap.cc | 3 ++- src/base_object-inl.h | 2 +- src/env-inl.h | 2 +- src/heap_utils.cc | 2 +- src/memory_tracker.h | 2 +- src/node_api.cc | 2 +- src/node_contextify.cc | 4 ++-- src/node_contextify.h | 2 +- src/node_crypto.cc | 3 ++- src/node_internals.h | 8 -------- src/node_persistent.h | 36 ++++++++++++++++++++++++++++++++++++ src/node_zlib.cc | 4 ++-- src/util-inl.h | 25 ------------------------- src/util.h | 22 ---------------------- 14 files changed, 50 insertions(+), 67 deletions(-) diff --git a/src/async_wrap.cc b/src/async_wrap.cc index d23e59f69e721c..21206e7208ea68 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -358,7 +358,8 @@ void AsyncWrap::WeakCallback(const v8::WeakCallbackInfo& info) { HandleScope scope(info.GetIsolate()); std::unique_ptr p{info.GetParameter()}; - Local prop_bag = PersistentToLocal(info.GetIsolate(), p->propBag); + Local prop_bag = PersistentToLocal::Default(info.GetIsolate(), + p->propBag); Local val; if (!prop_bag->Get(p->env->context(), p->env->destroyed_string()) diff --git a/src/base_object-inl.h b/src/base_object-inl.h index 8c8fa1699c3069..0b8fbb8520c283 100644 --- a/src/base_object-inl.h +++ b/src/base_object-inl.h @@ -62,7 +62,7 @@ Persistent& BaseObject::persistent() { v8::Local BaseObject::object() const { - return PersistentToLocal(env_->isolate(), persistent_handle_); + return PersistentToLocal::Default(env_->isolate(), persistent_handle_); } v8::Local BaseObject::object(v8::Isolate* isolate) const { diff --git a/src/env-inl.h b/src/env-inl.h index 30fed57b71fbed..70a8e052258531 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -899,7 +899,7 @@ void Environment::ForEachBaseObject(T&& iterator) { #define V(PropertyName, TypeName) \ inline v8::Local Environment::PropertyName() const { \ - return StrongPersistentToLocal(PropertyName ## _); \ + return PersistentToLocal::Strong(PropertyName ## _); \ } \ inline void Environment::set_ ## PropertyName(v8::Local value) { \ PropertyName ## _.Reset(isolate(), value); \ diff --git a/src/heap_utils.cc b/src/heap_utils.cc index 72ad33c99a1e80..d1e3fad0980280 100644 --- a/src/heap_utils.cc +++ b/src/heap_utils.cc @@ -26,7 +26,7 @@ class JSGraphJSNode : public EmbedderGraph::Node { const char* Name() override { return ""; } size_t SizeInBytes() override { return 0; } bool IsEmbedderNode() override { return false; } - Local JSValue() { return StrongPersistentToLocal(persistent_); } + Local JSValue() { return PersistentToLocal::Strong(persistent_); } int IdentityHash() { Local v = JSValue(); diff --git a/src/memory_tracker.h b/src/memory_tracker.h index 17992792128809..11dd2be7af35c9 100644 --- a/src/memory_tracker.h +++ b/src/memory_tracker.h @@ -69,7 +69,7 @@ class NodeBIO; * // a BaseObject or an AsyncWrap class * bool IsRootNode() const override { return !wrapped_.IsWeak(); } * v8::Local WrappedObject() const override { - * return node::PersistentToLocal(wrapped_); + * return node::PersistentToLocal::Default(wrapped_); * } * private: * AnotherRetainerClass another_retainer_; diff --git a/src/node_api.cc b/src/node_api.cc index c023920da41f74..500312750974ef 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -30,7 +30,7 @@ struct napi_env__ { node::Persistent context_persistent; inline v8::Local context() const { - return StrongPersistentToLocal(context_persistent); + return node::PersistentToLocal::Strong(context_persistent); } inline node::Environment* node_env() const { diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 37afead80896d0..bc08e31a065306 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -767,7 +767,7 @@ void ContextifyScript::CreateCachedData( ContextifyScript* wrapped_script; ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder()); Local unbound_script = - PersistentToLocal(env->isolate(), wrapped_script->script_); + PersistentToLocal::Default(env->isolate(), wrapped_script->script_); std::unique_ptr cached_data( ScriptCompiler::CreateCodeCache(unbound_script)); if (!cached_data) { @@ -867,7 +867,7 @@ bool ContextifyScript::EvalMachine(Environment* env, ContextifyScript* wrapped_script; ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder(), false); Local unbound_script = - PersistentToLocal(env->isolate(), wrapped_script->script_); + PersistentToLocal::Default(env->isolate(), wrapped_script->script_); Local