2017-07-29 43 views
1

你好V8程序员和node-gyp'ers。我跑OS X 10.12.6Node v6.11.1npm v3.10.10nan v2.6.2gcc作为的XCode与此版本输出部分:如何在'node-gyp rebuild`中静默“'NewInstance'已被弃用”警告? v8中NewInstance的替代选择是什么?

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

请帮助我了解如何正确使用NewInstance方法和过程中的npm installnode-gyp rebuild过程消除警告我定制软件包安装?

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

我不想看到那些警告,如果我不必。我发现通过要求NewInstance方法被调用的方式上github开放售票细节修复到另一个插件包:

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

什么是实现Nan::NewInstance()不违反速度和效率太多贝斯特的方式?

回答

4

错误消息本身为您提供了答案的简短形式:“使用可能的版本”。它试图告诉你,有一个NewInstance的重载版本返回MaybeLocal(而不是Local),这就是你应该使用的。

背景是大多数操作可能会失败,通常是在抛出异常时。旧的V8 API使嵌入者相对难以确定他们在所有相关位置检查异常情况;因此引入了基于MaybeLocal返回类型的新API。每当你得到一个MaybeLocal,你应该检查它是否实际上包含一个值。如果您只是简单地使用.ToLocalChecked(没有首先手动检查),那意味着如果出现问题(如果可以保证没有任何事情会失败,那么这很好),那么您肯定会崩溃。在光明的一面,这并不比你的代码显然一直在做的更糟糕;-)

相关问题