2

我正在学习如何为 nodejs 构建 C/C++ 插件。

我可以在一个简单的 hello world 程序上成功地配置 node-gyp,构建 node-gyp 并运行 node index.js。所以,我的基本设置正在工作。下面的代码(从官方 nodejs 文档复制粘贴)是我的 C++ 代码的工作版本。

//hello.cc
//#include <node.h>
//#include <nan.h>
#include "./node_modules/nan/nan.h"
#include <iostream> 
using namespace v8;

namespace demo {
  using v8::FunctionCallbackInfo;
  using v8::Isolate;
  using v8::Local;
  using v8::Object;
  using v8::String;
  using v8::Value;

  void Method (const FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();
    std::cout << "Executing some stupid func..." << std::endl;    
    args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
  }

  void init(Local<Object> exports) {
    NODE_SET_METHOD(exports, "hello", Method);
  }

  NODE_MODULE(addon, init)
} // namespace demo

但是,当我使用Nan模块和 nan 站点中记录的代码版本时,我收到编译错误,表明 NanScope 未在此范围内声明。

//hello.cc
#include "./node_modules/nan/nan.h"
#include <iostream> 
using namespace v8;

  NAN_METHOD(Method) {
          Nan::NanScope();
    NanReturnValue(String::New("world"));
  }

  void init(Handle<Object> exports) {
    exports->Set(NanSymbol("hello"), 
        FunctionTemplate::New(Method)->GetFunction());
  }

  NODE_MODULE(hello, init)

部分错误输出...

make: Entering directory '/home/rvnath/projects/comviva/node-addons/hello-world/build'
  CXX(target) Release/obj.target/addon/hello1.o
../hello1.cc: In function ‘Nan::NAN_METHOD_RETURN_TYPE Method(Nan::NAN_METHOD_ARGS_TYPE)’:
../hello1.cc:7:14: error: ‘NanScope’ was not declared in this scope
     NanScope();
              ^

经过一番谷歌搜索,一些网站指出我们应该使用 Nan::Scope,并且 Nan 文档已经过时。我尝试了更改,但仍然无法正常工作。它给出了一个错误,说“Scope 不是 Nan 的成员”。

我找不到,如何正确使用 Nan 版本。这里的任何帮助将不胜感激。

4

0 回答 0