在使用NAN的Node.js的C ++模块中不调用SetAccessor函数

我正在尝试使用C ++为Node.js创build一个模块。 我得到了模块设置和工作,Node.js看到模块没有问题。 它可以被初始化和去初始化,并且所有的代码似乎都处于工作状态。 不过,我试图用C ++来声明getter和setter来在Node.js中访问,但是它们不能按预期工作。 我使用NAN,所以我试图使用这个代码作为指导: https : //github.com/rvagg/nan/blob/master/test/cpp/settergetter.cpp

下面是代码。 我相当新,所以我可以肯定地使用一点帮助。 非常感谢!

node_opus.cc:

namespace nodeopus { Persistent<Function> NodeOpus::constructor; NodeOpus::NodeOpus() : encoder( NULL ), // We have no encoder yet sampleRate( 48000 ), // Highest bitrate? channels( 2 ), // Stereo bitrate( 64000 ), // Default bitrate of 64k signal( OPUS_SIGNAL_MUSIC ), // Default of music application( OPUS_APPLICATION_AUDIO ) { // Encoding audio fprintf( stderr, "C constructor.\n" ); } NodeOpus::~NodeOpus() { fprintf( stderr, "C destructor.\n" ); } void NodeOpus::Init( Handle<Object> exports ) { NanScope(); Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>( New ); tpl->SetClassName( NanNew( "NodeOpus" ) ); tpl->InstanceTemplate()->SetInternalFieldCount( 1 ); NanAssignPersistent( constructor, tpl->GetFunction() ); v8::Local<v8::ObjectTemplate> proto = tpl->PrototypeTemplate(); proto->SetAccessor( NanNew<v8::String>( "samplerate" ), NodeOpus::SampleRateGetter, NodeOpus::SampleRateSetter ); exports->Set( NanNew( "NodeOpus" ), tpl->GetFunction() ); fprintf( stderr, "Init called.\n" ); } NAN_METHOD( NodeOpus::New ) { NanScope(); if( args.IsConstructCall() ) { NodeOpus *obj = new NodeOpus(); obj->Wrap( args.This() ); NanReturnValue( args.This() ); fprintf( stderr, "New constructor called.\n" ); } else { const int argc = 0; Local<Value> argv[ argc ] = {}; Local<Function> cons = NanNew<Function>( constructor ); NanReturnValue( cons->NewInstance( argc, argv ) ); fprintf( stderr, "New not constructor called.\n" ); } } NAN_GETTER( NodeOpus::SampleRateGetter ) { NanScope(); NodeOpus *obj = ObjectWrap::Unwrap<NodeOpus>( args.This() ); NanReturnValue( NanNew<Int32>( obj->sampleRate ) ); } NAN_SETTER( NodeOpus::SampleRateSetter ) { NanScope(); NodeOpus *obj = ObjectWrap::Unwrap<NodeOpus>( args.This() ); if( !value->IsInt32() ) { NanThrowError( "Sample Rate must be an integer." ); return; } obj->sampleRate = value->Int32Value(); fprintf( stderr, "Value is %i\n", obj->sampleRate ); } } 

节点opus.js:

 var binding = require( 'bindings' )( 'nodeopus' ); var nopus = new binding.NodeOpus(); nopus.samplerate = 32; console.log( nopus.samplerate ); 

输出:

 node node-opus.js Init called. C constructor. 32 

我通过更改构造函数variables来修复它:

 Persistent<FunctionTemplate> NodeOpus::constructor; 

Init函数用来:

 void NodeOpus::Init( Handle<Object> exports ) { NanScope(); Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>( New ); NanAssignPersistent( constructor, tpl ); tpl->SetClassName( NanNew( "NodeOpus" ) ); tpl->InstanceTemplate()->SetInternalFieldCount( 1 ); Local<ObjectTemplate> proto = tpl->PrototypeTemplate(); proto->SetAccessor( NanNew( "samplerate" ), SampleRateGetter, SampleRateSetter ); exports->Set( NanNew( "NodeOpus" ), tpl->GetFunction() ); fprintf( stdout, "Init called.\n" ); } 

现在将采样率设置为一个值将触发C ++中的函数。 我不知道为什么使用FunctionTemplate而不是函数variables的作品,但我只是很高兴它的工作。