在使用NAN的Node.js的C++模块中未调用SetAccessor函数
SetAccessor functions not being called in C++ module for Node.js using NAN
我正在尝试用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变量有效,但我很高兴它有效。
相关文章:
- 如何用参数值调用函数(仅在运行时已知)
- 从python中调用C++函数并获取返回值
- 当使用通配符和null指针调用函数时,对输出的说明
- 从R调用C++函数并对其进行集成时出错
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 如何在qt中从另一个类调用函数
- 在 COUT 语句中使用 COUT 调用函数
- 如何从线程中的不同模块调用函数?
- C++从函数指针数组调用函数
- 当 A 在 for 循环中调用函数 B 时,如何计算函数 A 的空间复杂度?
- 如何在 C/C++ 中从外部库调用函数
- 如何使用运算符在同一行中多次调用函数
- 是否可以创建一个从不同类调用函数的线程?
- 无法为类成员调用函数
- 如何从另一个标头 c++ 调用函数
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 如何只允许在调用函数 B 后调用函数 A?
- 我可以这样调用函数吗?
- 如何在 c++ 的类中递归调用函数方法?
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?