如何正确使用Nan :: HandleScope?

时间:2017-12-11 16:32:47

标签: javascript c++ node.js native abstraction

我正在使用NAN为节点编写c ++插件,但我很困惑如何正确使用Nan :: HandleScope(https://github.com/nodejs/nan/blob/master/doc/scopes.md#api_nan_handle_scope

在我的应用程序中,我正在做这样的事情(删除了错误检查,只显示了简化版本的代码):

static App* s_pApp = nullptr;

NAN_METHOD( init ) {
  s_pApp = new App();
} 

NAN_METHOD( getData ) {
  info.GetReturnValue().Set( s_pApp->getData() );
}

NAN_METHOD( close ) {
  delete s_pApp;
  s_pApp = nullptr;
}

//-------- This create a new json object based on the current state of _pRootNode which is a TreeNode class object previously populated
v8::Local<v8::Object> App::getData() const {
    Nan::EscapableHandleScope scope;
    v8::Local<v8::Object> obj = _pRootNode->getData();
    return scope.Escape( obj ); 
}

//-------- The TreeNode class 
v8::Local<v8::Object> TreeNode::getData() const {
  return getData( this );
}

//each node contains data plus a vector of children that are also TreeNodes
v8::Local<v8::Object> TreeNode::getData( const TreeNode* pNode ) const {
  v8::Local<v8::Object> jsonObject = Nan::New<v8::Object>();

  addProperty( jsonObject, "isImportant", _isImportant );
  addProperty( jsonObject, "value", _value );
  addProperty( jsonObject, _children );

  return jsonObject;
}

//The addProperty functions are used to populate the jsonObject which will eventually be returned back to JavaScript
template<class T>
void TreeNode::addProperty( v8::Local<v8::Object>& jsonObject, const char* szName, T propValue ) {
  v8::Local<v8::String> prop = Nan::New( szName ).ToLocalChecked();
  v8::Local<v8::Value> value = getValue( propValue ); //returns the appropriate v8 value
  Nan::Set( jsonObject, prop, value );  
}

void TreeNode::addProperty( v8::Local<v8::Object>& jsonObject, const std::vector<const TreeNode*>& children ) {
  if( children.empty() ) {
    return;
  }

  v8::Local<v8::String> prop = Nan::New( "children" ).ToLocalChecked();

  v8::Local<v8::Array> values = Nan::New<v8::Array>( children.size() );
  for( int i = 0, numChildren = children.size(); i < numChildren; ++i ) {
    values->Set( i, children.at( i )->getData() ); //recursive call
  }

  Nan::Set( jsonObject, prop, values );  
}

正如我们在上面的代码中看到的那样,我正在处理范围的唯一地方是在App :: getData()调用中,该调用返回由TreeNode类创建的json对象。它是否正确?或者我应该在任何地方使用HandleScope来调用Nan :: New?或者它是否也应该使用EscapableHandleScope而不是HandleScope,因为我实际上想要返回生成的值?

1 个答案:

答案 0 :(得分:0)

直接从文档降价处获取:

Nan::HandleScope - https://github.com/nodejs/nan/blob/master/doc/scopes.md#api_nan_handle_scope

每当您创建新的V8 JavaScript对象时,都会分配一个新的Nan :: HandleScope。请注意,在JavaScript可访问的方法上为您创建了隐式HandleScope,因此您无需自己插入。

Nan::EscapableHandleScope - https://github.com/nodejs/nan/blob/master/doc/scopes.md#api_nan_escapable_handle_scope

与Nan :: HandleScope类似,但应该在函数需要返回已在其中创建的V8 JavaScript类型的情况下使用。

所以,你在Nan::EscapableHandleScope中使用App::getData()是正确的。但是,我认为Nan::EscapableHandleScope中的App::getData()可以移至TreeNode::getData( const TreeNode* pNode ),因为这是实际创建V8 Javascript类型的函数。

我认为你在这里所做的事情没有任何问题,我只是倾向于让Nan::HandleScope / Nan::EscapableHndleScope更接近(最好是在)创建被返回对象的函数内。