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

时间:2015-04-11 19:51:56

标签: c++ node.js v8

我正在尝试使用C ++为Node.js创建一个模块。我得到了模块设置和工作,Node.js看到模块没问题。它可以初始化和取消初始化,所有代码似乎都在运行。但是,我试图在Node.js中声明C ++中的getter和setter,但它们没有按预期工作。我正在使用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

1 个答案:

答案 0 :(得分:1)

我通过将构造函数变量更改为:

来修复它
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" );
}

现在将samplerate设置为一个值将触发C ++中的函数。我不确定为什么使用FunctionTemplate代替Function变量,但我很高兴它能够正常工作。