Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
move definitions to node_persistent.h
  • Loading branch information
Gabriel Schulhof committed Nov 10, 2018
commit 002129419efda6b17fe8afd3cd2d1d68e6af0029
36 changes: 36 additions & 0 deletions src/node_persistent.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,42 @@ struct ResetInDestructorPersistentTraits {
template <typename T>
using Persistent = v8::Persistent<T, ResetInDestructorPersistentTraits<T>>;

class PersistentToLocal {
public:
// 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 Persistent<TypeName>& persistent) {
if (persistent.IsWeak()) {
return PersistentToLocal::Weak(isolate, persistent);
} else {
return PersistentToLocal::Strong(persistent);
}
}

// Unchecked conversion from a non-weak Persistent<T> to Local<T>,
// use with care!
//
// 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> Strong(
const Persistent<TypeName>& persistent) {
return *reinterpret_cast<v8::Local<TypeName>*>(
const_cast<Persistent<TypeName>*>(&persistent));
}

template <class TypeName>
static inline v8::Local<TypeName> Weak(
v8::Isolate* isolate,
const Persistent<TypeName>& persistent) {
return v8::Local<TypeName>::New(isolate, persistent);
}
};

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
Expand Down
25 changes: 0 additions & 25 deletions src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,31 +165,6 @@ constexpr ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
return ContainerOfHelper<Inner, Outer>(field, pointer);
}

template <class TypeName>
inline v8::Local<TypeName> PersistentToLocal::Default(
v8::Isolate* isolate,
const Persistent<TypeName>& persistent) {
if (persistent.IsWeak()) {
return PersistentToLocal::Weak(isolate, persistent);
} else {
return PersistentToLocal::Strong(persistent);
}
}

template <class TypeName>
inline v8::Local<TypeName> PersistentToLocal::Strong(
const Persistent<TypeName>& persistent) {
return *reinterpret_cast<v8::Local<TypeName>*>(
const_cast<Persistent<TypeName>*>(&persistent));
}

template <class TypeName>
inline v8::Local<TypeName> PersistentToLocal::Weak(
v8::Isolate* isolate,
const Persistent<TypeName>& persistent) {
return v8::Local<TypeName>::New(isolate, persistent);
}

inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
const char* data,
int length) {
Expand Down
25 changes: 0 additions & 25 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,31 +201,6 @@ template <typename Inner, typename Outer>
constexpr ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
Inner* pointer);

class PersistentToLocal {
public:
// 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 Persistent<TypeName>& persistent);

// Unchecked conversion from a non-weak Persistent<T> to Local<T>,
// use with care!
//
// 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> Strong(
const Persistent<TypeName>& persistent);

template <class TypeName>
static inline v8::Local<TypeName> Weak(
v8::Isolate* isolate,
const Persistent<TypeName>& persistent);
};

// Convenience wrapper around v8::String::NewFromOneByte().
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
const char* data,
Expand Down