如何通过包装的C++;对象到Javascript回调? 我试图用C++编写一个No.js模块,它封装和暴露了来自./P>的一些类。
我目前对libhdf5中的两个类感兴趣。第一个是如何通过包装的C++;对象到Javascript回调? 我试图用C++编写一个No.js模块,它封装和暴露了来自./P>的一些类。,c++,node.js,v8,C++,Node.js,V8,我目前对libhdf5中的两个类感兴趣。第一个是文件,它打开一个hdf5文件。第二个是组,它表示该文件中的组。可以从文件对象获取组对象 我编写了一些代码,在其中我创建了一个文件对象,并试图从中获取一个组。我正试图使Node.js模块尽可能的JavaScripty,所以我想使用回调返回组。因此,我试图对我的模块进行编码,使其使用方式如下: var hdf5 = require('hdf5'); var file = new hdf5.File('/tmp/example.h5'); file.ge
文件
,它打开一个hdf5文件。第二个是组
,它表示该文件中的组。可以从文件对象获取组对象
我编写了一些代码,在其中我创建了一个文件
对象,并试图从中获取一个组
。我正试图使Node.js模块尽可能的JavaScripty,所以我想使用回调返回组。因此,我试图对我的模块进行编码,使其使用方式如下:
var hdf5 = require('hdf5');
var file = new hdf5.File('/tmp/example.h5');
file.getGroup('foobar', function (err, group) { console.log(group); });
>在我的<代码>文件> /Cux>包装器的C++代码中,我将有一个映射到
代码进行了建模组
因此,我对我的
组包装进行了编码,但我一直在尝试实例化它。我还不知道如何避免使用v8Arguments
类作为函数参数。正因为如此,我似乎无法传递一些我需要的参数,因为我是用C++实例化C++的,而不是JS Land。您不需要将参数
传递给Group::Instantiate
。只需传递所需内容,并使用构造函数创建组的新实例
。例如:
Handle<Value> Group::Instantiate(const std::string& name) {
HandleScope scope;
Local<v8::Value> argv[1] = {
Local<v8::Value>::New(String::New(name.c_str()))
};
return scope.Close(Constructor->NewInstance(1, argv));
}
在File::OpenGroup
中,可以执行以下操作:
Handle<Value> File::OpenGroup (const Arguments& args) {
HandleScope scope;
if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsFunction()) {
ThrowException(Exception::SyntaxError(String::New("expected name, callback")));
return scope.Close(Undefined());
}
const std::string name(*(String::Utf8Value(args[0]->ToString())));
Local<Function> callback = Local<Function>::Cast(args[1]);
const unsigned argc = 2;
Local<Value> argv[argc] = {
Local<Value>::New(Null()),
Local<Value>::New(Group::Instantiate(name))
};
callback->Call(Context::GetCurrent()->Global(), argc, argv);
return scope.Close(Undefined());
}
处理文件::OpenGroup(常量参数和参数){
手镜镜;
如果(args.Length()!=2 | |!args[0]->IsString()| |!args[1]->IsFunction()){
ThrowException(Exception::SyntaxError(String::New(“预期的名称,回调”));
返回scope.Close(未定义());
}
常量std::string name(*(string::Utf8Value(args[0]->ToString()));
本地回调=Local::Cast(args[1]);
常量无符号argc=2;
本地argv[argc]={
Local::New(Null()),
本地::新建(组::实例化(名称))
};
回调->调用(Context::GetCurrent()->Global(),argc,argv);
返回scope.Close(未定义());
}
就是说您需要一个工厂方法(Group::Instantiate
)来返回另一个类的实例?嗨,Ryan。你的回购协议的链接都没有了,可能已经被删除了(这就是为什么不鼓励外部链接的原因)。你会修复这个问题以恢复它在被问及时的状态吗?理想情况下,代码将被纳入问题本身,以避免再次发生这种情况。删除最后三段并保留一些含义可能还可以,你认为呢?
Handle<Value> File::OpenGroup (const Arguments& args) {
HandleScope scope;
if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsFunction()) {
ThrowException(Exception::SyntaxError(String::New("expected name, callback")));
return scope.Close(Undefined());
}
const std::string name(*(String::Utf8Value(args[0]->ToString())));
Local<Function> callback = Local<Function>::Cast(args[1]);
const unsigned argc = 2;
Local<Value> argv[argc] = {
Local<Value>::New(Null()),
Local<Value>::New(Group::Instantiate(name))
};
callback->Call(Context::GetCurrent()->Global(), argc, argv);
return scope.Close(Undefined());
}