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

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

本文关键字:调用 函数 SetAccessor 模块 js NAN Node C++      更新时间:2023-10-16

我正在尝试用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 %in", obj->sampleRate );
}
}

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