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

SetAccessor functions not being called in C++ module for Node.js using NAN

我正在尝试用 C++ 为 Node.js 创建一个模块。我设置了模块并开始工作,Node.js 认为模块没问题。它可以被初始化和取消初始化,并且所有代码似乎都处于工作状态。但是,我试图在 C++ 中声明要在 Node.js 中访问的 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

我通过将构造函数变量更改为来修复它:

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 而不是 Function 变量有效,但我很高兴它有效。