Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: bundle persistent-to-local methods as class #24276

Conversation

gabrielschulhof
Copy link
Contributor

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.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines

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.
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. labels Nov 9, 2018
Copy link
Member

@joyeecheung joyeecheung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be put into node_persistent.h? Since these are all node::Persistent

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a class and not a namespace? they are all static templates?

Copy link
Contributor

@refack refack Nov 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.v8 detection 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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

@gabrielschulhof gabrielschulhof Nov 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Contributor

@refack refack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have questions

src/util-inl.h Outdated Show resolved Hide resolved
src/util.h Outdated
inline v8::Local<TypeName> WeakPersistentToLocal(
v8::Isolate* isolate,
const Persistent<TypeName>& persistent);
class PersistentToLocal {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {
Copy link
Contributor

@refack refack Nov 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@gabrielschulhof
Copy link
Contributor Author

@refack I made a class, because a class can be aliased with the using keyword. It's true that we might want to move this into node_persistent.h.

@refack
Copy link
Contributor

refack commented Nov 9, 2018

because a class can be aliased with the using keyword

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>;

@gabrielschulhof
Copy link
Contributor Author

gabrielschulhof commented Nov 10, 2018

I moved the definitions into node_persistent.h and placed them inline.

@gabrielschulhof
Copy link
Contributor Author

@refack

using Pdo = PersistentToLocal::Default<v8::Object>;

This works for aliasing the concrete function PersistentToLocal::Default<v8::Object> but it doesn't work if you want to alias the entire templated function.

@gabrielschulhof
Copy link
Contributor Author

gabrielschulhof commented Nov 11, 2018

@joyeecheung I have heeded your advice and moved the code to node_persistent.h.

@refack
Copy link
Contributor

refack commented Nov 12, 2018

@refack refack added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Nov 12, 2018
@danbev
Copy link
Contributor

danbev commented Nov 13, 2018

Landed in 0603c0a.

@danbev danbev closed this Nov 13, 2018
danbev pushed a commit that referenced this pull request Nov 13, 2018
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>
BridgeAR pushed a commit that referenced this pull request Nov 14, 2018
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>
kiyomizumia pushed a commit to kiyomizumia/node that referenced this pull request Nov 15, 2018
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>
@codebytere
Copy link
Member

codebytere commented Jan 12, 2019

@gabrielschulhof do you think this should be backported to 10.x?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants