Search code examples
javascriptnode.jsv8node-gypnode.js-addon

How to silence "'NewInstance' is deprecated" warning in `node-gyp rebuild`? What is the alternative to NewInstance in v8?


Hello V8 programmers and node-gyp'ers. I'm running OS X 10.12.6, Node v6.11.1 with npm v3.10.10, nan v2.6.2, gcc as a part of XCode with this version output:

$ > gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix

Please help me understand how to properly utilize NewInstance and eliminate warnings during the npm install or node-gyp rebuild process of my custom package installation?

> node-gyp rebuild

  CXX(target) Release/obj.target/cellcrypt/src/cellcrypt.o
  CC(target) Release/obj.target/cellcrypt/src/decode.o
  CXX(target) Release/obj.target/cellcrypt/src/DecryptionWrapper.o
../src/DecryptionWrapper.cpp:55:44: warning: 'NewInstance' is deprecated [-Wdeprecated-declarations]
    v8::Local<v8::Object> instance = cons->NewInstance();
                                       ^
/Users/sjcbsolo/.node-gyp/6.11.1/include/node/v8.h:3276:52: note: 'NewInstance' has been explicitly marked deprecated here
  V8_DEPRECATED("Use maybe version", Local<Object> NewInstance() const);
                                               ^
1 warning generated.
  CC(target) Release/obj.target/cellcrypt/src/Encryption.o
  SOLINK_MODULE(target) Release/cellcrypt.node
clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9 [-Wdeprecated]

I don't want to see those warnings if I don't have to. I found an open ticket on github detailing a fix to another addon package by requiring the way NewInstance is invoked:

info.GetReturnValue().Set(cons->NewInstance(argc, argv));
info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());

What's the bester way to implement Nan::NewInstance() without violating speed and efficiency too much?


Solution

  • The error message itself gives you the short form of the answer: "Use maybe version". It's trying to tell you that there's an overloaded version of NewInstance that's returning a MaybeLocal (instead of a Local), and that's what you should be using.

    The background is that most operations can fail, typically when an exception is thrown. The old V8 API made it relatively difficult for an embedder to be sure that they were checking for exceptions in all relevant places; so a new API based on MaybeLocal return types was introduced. Whenever you get a MaybeLocal, you should check whether it actually contains a value. If you simply use .ToLocalChecked (without first manually checking), that means you're willing to simply crash if something failed (which is fine if you can guarantee that nothing will ever fail). On the bright side, that's no worse than what your code has apparently always been doing ;-)