Perfect your code
With built-in code review tools, GitHub makes it easy to raise the quality bar before you ship. Join the 36 million developers who've merged over 200 million pull requests.
Sign up for free See pricing for teams and enterprisessrc: bundle persistent-to-local methods as class #24276
Conversation
gabrielschulhof
requested a review
from
addaleax
Nov 9, 2018
This comment has been minimized.
This comment has been minimized.
nodejs-github-bot
added
C++
lib / src
labels
Nov 9, 2018
gabrielschulhof
referenced this pull request
Nov 9, 2018
Closed
src: factor out Node.js-agnostic N-APIs #23786
joyeecheung
approved these changes
Nov 9, 2018
|
LGTM with one suggestion, although I wonder if we can just make a Trait for |
| inline v8::Local<TypeName> WeakPersistentToLocal( | ||
| v8::Isolate* isolate, | ||
| const Persistent<TypeName>& persistent); | ||
| class PersistentToLocal { |
This comment has been minimized.
This comment has been minimized.
joyeecheung
Nov 9, 2018
Member
Shouldn't this be put into node_persistent.h? Since these are all node::Persistent
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
refack
Nov 9, 2018
•
Member
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
This comment has been minimized.
This comment has been minimized.
joyeecheung
Nov 9, 2018
Member
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?
This comment has been minimized.
This comment has been minimized.
refack
Nov 9, 2018
Member
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)
This comment has been minimized.
This comment has been minimized.
bnoordhuis
Nov 9, 2018
Member
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.
This comment has been minimized.
This comment has been minimized.
gabrielschulhof
Nov 9, 2018
•
Author
Contributor
@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-inl.h Outdated
| inline v8::Local<TypeName> WeakPersistentToLocal( | ||
| v8::Isolate* isolate, | ||
| const Persistent<TypeName>& persistent); | ||
| class PersistentToLocal { |
This comment has been minimized.
This comment has been minimized.
| inline v8::Local<TypeName> WeakPersistentToLocal( | ||
| v8::Isolate* isolate, | ||
| const Persistent<TypeName>& persistent); | ||
| class PersistentToLocal { |
This comment has been minimized.
This comment has been minimized.
refack
Nov 9, 2018
•
Member
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
cjihrig
approved these changes
Nov 9, 2018
This comment has been minimized.
This comment has been minimized.
|
@refack I made a class, because a class can be aliased with the |
addaleax
approved these changes
Nov 9, 2018
This comment has been minimized.
This comment has been minimized.
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>; |
This comment has been minimized.
This comment has been minimized.
|
I moved the definitions into |
jasnell
approved these changes
Nov 10, 2018
This comment has been minimized.
This comment has been minimized.
This works for aliasing the concrete function |
This comment has been minimized.
This comment has been minimized.
|
@joyeecheung I have heeded your advice and moved the code to |
refack
approved these changes
Nov 12, 2018
This comment has been minimized.
This comment has been minimized.
refack
added
the
author ready
label
Nov 12, 2018
This comment has been minimized.
This comment has been minimized.
|
Landed in 0603c0a. |
danbev
closed this
Nov 13, 2018
danbev
added a commit
that referenced
this pull request
Nov 13, 2018
BridgeAR
added a commit
that referenced
this pull request
Nov 14, 2018
kiyomizumia
added a commit
to kiyomizumia/node
that referenced
this pull request
Nov 15, 2018
This was referenced Nov 15, 2018
This was referenced Nov 16, 2018
This comment has been minimized.
This comment has been minimized.
|
@gabrielschulhof do you think this should be backported to 10.x? |
gabrielschulhof commentedNov 9, 2018
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