Skip to content

Commit

Permalink
Fix Node.js v10.12.0 deprecation warnings.
Browse files Browse the repository at this point in the history
v10.12.0 turns on a number of V8 deprecation warnings. This commit fixes
them in NAN.

Fixes: #810
PR-URL: #825
Refs: #811
Reviewed-By: Benjamin Byholm <bbyholm@abo.fi>
  • Loading branch information
bnoordhuis committed Nov 26, 2018
1 parent e6ef6a4 commit 509859c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
27 changes: 17 additions & 10 deletions nan.h
Original file line number Diff line number Diff line change
Expand Up @@ -1060,8 +1060,10 @@ class Utf8String {
length_(0), str_(str_st_) {
HandleScope scope;
if (!from.IsEmpty()) {
#if V8_MAJOR_VERSION >= 7
v8::Local<v8::String> string = from->ToString(v8::Isolate::GetCurrent());
#if NODE_MAJOR_VERSION >= 10
v8::Local<v8::Context> context = GetCurrentContext();
v8::Local<v8::String> string =
from->ToString(context).FromMaybe(v8::Local<v8::String>());
#else
v8::Local<v8::String> string = from->ToString();
#endif
Expand All @@ -1074,7 +1076,7 @@ class Utf8String {
}
const int flags =
v8::String::NO_NULL_TERMINATION | imp::kReplaceInvalidUtf8;
#if V8_MAJOR_VERSION >= 7
#if NODE_MAJOR_VERSION >= 10
length_ = string->WriteUtf8(v8::Isolate::GetCurrent(), str_, static_cast<int>(len), 0, flags);
#else
length_ = string->WriteUtf8(str_, static_cast<int>(len), 0, flags);
Expand Down Expand Up @@ -1852,36 +1854,41 @@ inline MaybeLocal<v8::Value> Call(
inline void SaveToPersistent(
const char *key, const v8::Local<v8::Value> &value) {
HandleScope scope;
New(persistentHandle)->Set(New(key).ToLocalChecked(), value);
Set(New(persistentHandle), New(key).ToLocalChecked(), value).FromJust();
}

inline void SaveToPersistent(
const v8::Local<v8::String> &key, const v8::Local<v8::Value> &value) {
HandleScope scope;
New(persistentHandle)->Set(key, value);
Set(New(persistentHandle), key, value).FromJust();
}

inline void SaveToPersistent(
uint32_t index, const v8::Local<v8::Value> &value) {
HandleScope scope;
New(persistentHandle)->Set(index, value);
Set(New(persistentHandle), index, value).FromJust();
}

inline v8::Local<v8::Value> GetFromPersistent(const char *key) const {
EscapableHandleScope scope;
return scope.Escape(
New(persistentHandle)->Get(New(key).ToLocalChecked()));
Get(New(persistentHandle), New(key).ToLocalChecked())
.FromMaybe(v8::Local<v8::Value>()));
}

inline v8::Local<v8::Value>
GetFromPersistent(const v8::Local<v8::String> &key) const {
EscapableHandleScope scope;
return scope.Escape(New(persistentHandle)->Get(key));
return scope.Escape(
Get(New(persistentHandle), key)
.FromMaybe(v8::Local<v8::Value>()));
}

inline v8::Local<v8::Value> GetFromPersistent(uint32_t index) const {
EscapableHandleScope scope;
return scope.Escape(New(persistentHandle)->Get(index));
return scope.Escape(
Get(New(persistentHandle), index)
.FromMaybe(v8::Local<v8::Value>()));
}

virtual void Execute() = 0;
Expand Down Expand Up @@ -2375,7 +2382,7 @@ SetMethodAux(T recv,
v8::Local<v8::String> name,
v8::Local<v8::FunctionTemplate> tpl,
...) {
recv->Set(name, GetFunction(tpl).ToLocalChecked());
Set(recv, name, GetFunction(tpl).ToLocalChecked());
}

} // end of namespace imp
Expand Down
33 changes: 27 additions & 6 deletions nan_implementation_12_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,17 @@ Factory<v8::Function>::New( FunctionCallback callback
obj->SetInternalField(imp::kDataIndex, val);
}

return scope.Escape(v8::Function::New( isolate
, imp::FunctionCallbackWrapper
, obj));
#if NODE_MAJOR_VERSION >= 10
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Function> function =
v8::Function::New(context, imp::FunctionCallbackWrapper, obj)
.ToLocalChecked();
#else
v8::Local<v8::Function> function =
v8::Function::New(isolate, imp::FunctionCallbackWrapper, obj);
#endif

return scope.Escape(function);
}

//=== Function Template ========================================================
Expand Down Expand Up @@ -332,12 +340,25 @@ Factory<v8::String>::New(ExternalOneByteStringResource * value) {

//=== String Object ============================================================

// See https://github.com/nodejs/nan/pull/811#discussion_r224594980.
// Disable the warning as there is no way around it.
// TODO(bnoordhuis) Use isolate-based version in Node.js v12.
Factory<v8::StringObject>::return_t
Factory<v8::StringObject>::New(v8::Local<v8::String> value) {
#if V8_MAJOR_VERSION >= 7
return v8::StringObject::New(v8::Isolate::GetCurrent(), value).As<v8::StringObject>();
#else
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
#endif
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return v8::StringObject::New(value).As<v8::StringObject>();
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#ifdef _MSC_VER
#pragma warning(pop)
#endif
}

Expand Down
3 changes: 3 additions & 0 deletions nan_object_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ class ObjectWrap {
inline void MakeWeak() {
persistent().v8::PersistentBase<v8::Object>::SetWeak(
this, WeakCallback, v8::WeakCallbackType::kParameter);
#if NODE_MAJOR_VERSION < 10
// FIXME(bnoordhuis) Probably superfluous in older Node.js versions too.
persistent().MarkIndependent();
#endif
}

#elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
Expand Down

2 comments on commit 509859c

@erikdubbelboer
Copy link

Choose a reason for hiding this comment

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

@bnoordhuis when is this going to be released in a new version?

@bnoordhuis
Copy link
Member Author

Choose a reason for hiding this comment

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

@kkoopa ^?

Please sign in to comment.