Skip to content

Commit ca9d241

Browse files
committed
Fix Node.js v10.12.0 deprecation warnings.
v10.12.0 turns on a number of V8 deprecation warnings. This commit fixes them in NAN. Fixes: nodejs/nan#810 PR-URL: nodejs/nan#825 Refs: nodejs/nan#811 Reviewed-By: Benjamin Byholm <[email protected]>
1 parent e9f8570 commit ca9d241

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

nan.h

+17-10
Original file line numberDiff line numberDiff line change
@@ -1060,8 +1060,10 @@ class Utf8String {
10601060
length_(0), str_(str_st_) {
10611061
HandleScope scope;
10621062
if (!from.IsEmpty()) {
1063-
#if V8_MAJOR_VERSION >= 7
1064-
v8::Local<v8::String> string = from->ToString(v8::Isolate::GetCurrent());
1063+
#if NODE_MAJOR_VERSION >= 10
1064+
v8::Local<v8::Context> context = GetCurrentContext();
1065+
v8::Local<v8::String> string =
1066+
from->ToString(context).FromMaybe(v8::Local<v8::String>());
10651067
#else
10661068
v8::Local<v8::String> string = from->ToString();
10671069
#endif
@@ -1074,7 +1076,7 @@ class Utf8String {
10741076
}
10751077
const int flags =
10761078
v8::String::NO_NULL_TERMINATION | imp::kReplaceInvalidUtf8;
1077-
#if V8_MAJOR_VERSION >= 7
1079+
#if NODE_MAJOR_VERSION >= 10
10781080
length_ = string->WriteUtf8(v8::Isolate::GetCurrent(), str_, static_cast<int>(len), 0, flags);
10791081
#else
10801082
length_ = string->WriteUtf8(str_, static_cast<int>(len), 0, flags);
@@ -1852,36 +1854,41 @@ inline MaybeLocal<v8::Value> Call(
18521854
inline void SaveToPersistent(
18531855
const char *key, const v8::Local<v8::Value> &value) {
18541856
HandleScope scope;
1855-
New(persistentHandle)->Set(New(key).ToLocalChecked(), value);
1857+
Set(New(persistentHandle), New(key).ToLocalChecked(), value).FromJust();
18561858
}
18571859

18581860
inline void SaveToPersistent(
18591861
const v8::Local<v8::String> &key, const v8::Local<v8::Value> &value) {
18601862
HandleScope scope;
1861-
New(persistentHandle)->Set(key, value);
1863+
Set(New(persistentHandle), key, value).FromJust();
18621864
}
18631865

18641866
inline void SaveToPersistent(
18651867
uint32_t index, const v8::Local<v8::Value> &value) {
18661868
HandleScope scope;
1867-
New(persistentHandle)->Set(index, value);
1869+
Set(New(persistentHandle), index, value).FromJust();
18681870
}
18691871

18701872
inline v8::Local<v8::Value> GetFromPersistent(const char *key) const {
18711873
EscapableHandleScope scope;
18721874
return scope.Escape(
1873-
New(persistentHandle)->Get(New(key).ToLocalChecked()));
1875+
Get(New(persistentHandle), New(key).ToLocalChecked())
1876+
.FromMaybe(v8::Local<v8::Value>()));
18741877
}
18751878

18761879
inline v8::Local<v8::Value>
18771880
GetFromPersistent(const v8::Local<v8::String> &key) const {
18781881
EscapableHandleScope scope;
1879-
return scope.Escape(New(persistentHandle)->Get(key));
1882+
return scope.Escape(
1883+
Get(New(persistentHandle), key)
1884+
.FromMaybe(v8::Local<v8::Value>()));
18801885
}
18811886

18821887
inline v8::Local<v8::Value> GetFromPersistent(uint32_t index) const {
18831888
EscapableHandleScope scope;
1884-
return scope.Escape(New(persistentHandle)->Get(index));
1889+
return scope.Escape(
1890+
Get(New(persistentHandle), index)
1891+
.FromMaybe(v8::Local<v8::Value>()));
18851892
}
18861893

18871894
virtual void Execute() = 0;
@@ -2375,7 +2382,7 @@ SetMethodAux(T recv,
23752382
v8::Local<v8::String> name,
23762383
v8::Local<v8::FunctionTemplate> tpl,
23772384
...) {
2378-
recv->Set(name, GetFunction(tpl).ToLocalChecked());
2385+
Set(recv, name, GetFunction(tpl).ToLocalChecked());
23792386
}
23802387

23812388
} // end of namespace imp

nan_implementation_12_inl.h

+27-6
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,17 @@ Factory<v8::Function>::New( FunctionCallback callback
100100
obj->SetInternalField(imp::kDataIndex, val);
101101
}
102102

103-
return scope.Escape(v8::Function::New( isolate
104-
, imp::FunctionCallbackWrapper
105-
, obj));
103+
#if NODE_MAJOR_VERSION >= 10
104+
v8::Local<v8::Context> context = isolate->GetCurrentContext();
105+
v8::Local<v8::Function> function =
106+
v8::Function::New(context, imp::FunctionCallbackWrapper, obj)
107+
.ToLocalChecked();
108+
#else
109+
v8::Local<v8::Function> function =
110+
v8::Function::New(isolate, imp::FunctionCallbackWrapper, obj);
111+
#endif
112+
113+
return scope.Escape(function);
106114
}
107115

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

333341
//=== String Object ============================================================
334342

343+
// See https://github.com/nodejs/nan/pull/811#discussion_r224594980.
344+
// Disable the warning as there is no way around it.
345+
// TODO(bnoordhuis) Use isolate-based version in Node.js v12.
335346
Factory<v8::StringObject>::return_t
336347
Factory<v8::StringObject>::New(v8::Local<v8::String> value) {
337-
#if V8_MAJOR_VERSION >= 7
338-
return v8::StringObject::New(v8::Isolate::GetCurrent(), value).As<v8::StringObject>();
339-
#else
348+
#ifdef _MSC_VER
349+
#pragma warning(push)
350+
#pragma warning(disable : 4996)
351+
#endif
352+
#ifdef __GNUC__
353+
#pragma GCC diagnostic push
354+
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
355+
#endif
340356
return v8::StringObject::New(value).As<v8::StringObject>();
357+
#ifdef __GNUC__
358+
#pragma GCC diagnostic pop
359+
#endif
360+
#ifdef _MSC_VER
361+
#pragma warning(pop)
341362
#endif
342363
}
343364

nan_object_wrap.h

+3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ class ObjectWrap {
6464
inline void MakeWeak() {
6565
persistent().v8::PersistentBase<v8::Object>::SetWeak(
6666
this, WeakCallback, v8::WeakCallbackType::kParameter);
67+
#if NODE_MAJOR_VERSION < 10
68+
// FIXME(bnoordhuis) Probably superfluous in older Node.js versions too.
6769
persistent().MarkIndependent();
70+
#endif
6871
}
6972

7073
#elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION

0 commit comments

Comments
 (0)