src: bundle persistent-to-local methods as class#24276
src: bundle persistent-to-local methods as class#24276gabrielschulhof wants to merge 2 commits intonodejs:masterfrom
Conversation
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.
joyeecheung
left a comment
There was a problem hiding this comment.
LGTM with one suggestion, although I wonder if we can just make a Trait for node::Persistent..
src/util.h
Outdated
| inline v8::Local<TypeName> WeakPersistentToLocal( | ||
| v8::Isolate* isolate, | ||
| const Persistent<TypeName>& persistent); | ||
| class PersistentToLocal { |
There was a problem hiding this comment.
Shouldn't this be put into node_persistent.h? Since these are all node::Persistent
There was a problem hiding this comment.
Why a class and not a namespace? they are all static templates?
There was a problem hiding this comment.
P.S. why not move the implementations here? They are all single lines (some could even be constexpr on C++17 compilers)
You can do feature detection with either __cpp_constexpr < 201304 or __cpp_constexpr < 201603 (I think 201603 is needed for v8:Local<> return values)
// Wordaround a GCC4.9 bug that C++14 N3652 was not implemented
// Refs: https://www.gnu.org/software/gcc/projects/cxx-status.html#cxx14
// Refs: https://isocpp.org/files/papers/N3652.html
#if __cpp_constexpr < 201304
# define constexpr
#endif
.
.
.
#undef constexprThere was a problem hiding this comment.
Doesn't that bring more complexity than necessary? (the constexpr approach) How is it different from doing process.versions.v8 detection in JS and use harmony features?
There was a problem hiding this comment.
Doesn't that bring more complexity than necessary? (the constexpr approach)
A little bit, yes. So up to author. BTW I think there's a CONSTEXPR macro defined by V8 based of this condition, so we could reuse.
How is it different from doing
process.versions.v8detection in JS and use harmony features?
It's compile time only, 0 run time cost, only benefits. (only cost is the above mentioned code complexity)
There was a problem hiding this comment.
Shouldn't this be put into node_persistent.h? Since these are all node::Persistent
You mean as node::Persistent methods? I'd say that's the best/most logical place for it.
There was a problem hiding this comment.
@bnoordhuis I tried adding them as node::Persistent methods, but node::Persistent is merely an alias to v8::Persistent, so I can't really extend it, AFAIK.
src/util.h
Outdated
| inline v8::Local<TypeName> WeakPersistentToLocal( | ||
| v8::Isolate* isolate, | ||
| const Persistent<TypeName>& persistent); | ||
| class PersistentToLocal { |
There was a problem hiding this comment.
Why a class and not a namespace? they are all static templates?
src/util.h
Outdated
| inline v8::Local<TypeName> WeakPersistentToLocal( | ||
| v8::Isolate* isolate, | ||
| const Persistent<TypeName>& persistent); | ||
| class PersistentToLocal { |
There was a problem hiding this comment.
P.S. why not move the implementations here? They are all single lines (some could even be constexpr on C++17 compilers)
You can do feature detection with either __cpp_constexpr < 201304 or __cpp_constexpr < 201603 (I think 201603 is needed for v8:Local<> return values)
// Wordaround a GCC4.9 bug that C++14 N3652 was not implemented
// Refs: https://www.gnu.org/software/gcc/projects/cxx-status.html#cxx14
// Refs: https://isocpp.org/files/papers/N3652.html
#if __cpp_constexpr < 201304
# define constexpr
#endif
.
.
.
#undef constexpr|
@refack I made a class, because a class can be aliased with the |
If I understand what you mean, I think that you could alias a namespaced template function just as you can a class: namespace PersistentToLocal {
// If persistent.IsWeak() == false, then do not call persistent.Reset()
// while the returned Local<T> is still in scope, it will destroy the
// reference to the object.
template <class TypeName>
static inline v8::Local<TypeName> Default(
v8::Isolate* isolate,
const v8::Persistent<TypeName>& persistent);
};
using Pdo = PersistentToLocal::Default<v8::Object>; |
|
I moved the definitions into |
This works for aliasing the concrete function |
|
@joyeecheung I have heeded your advice and moved the code to |
|
Landed in 0603c0a. |
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>
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>
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: nodejs#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>
|
@gabrielschulhof do you think this should be backported to 10.x? |
Create a class
PersistentToLocalwhich contains three methods,Strong,Weak, andDefault:Strongreturns aLocalfrom a strong persistent reference,Weakreturns aLocalfrom a weak persistent reference, andDefaultdecides based onIsWeak()which of the above two to call.These replace
node::StrongPersistentToLocal(),node::WeakPersistentToLocal(), andnode::PersistentToLocal(),respectively.
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passes