我有两个简单的类A
和B
我试图在node.js的本机模块中公开。 A
可以直接创建,但B
只能通过调用A::foo()
来创建。
class Internal {};
class B {
public:
Internal internal;
explicit B(Internal internal):internal(internal){}
};
class A {
public:
A() : internal() {};
B foo() { return B(internal); }
private:
Internal internal;
};
我希望能写下来:
const M = require('node_nan_minimal');
const a = new M.A();
const b = a.foo();
为此,我创建了两个派生自Nan::ObjectWrap
class AWrapper : public Nan::ObjectWrap { ... }
class BWrapper : public Nan::ObjectWrap { ... }
每个包含A
或B
的实例。有了这些,我可以在javascript中创建一个A类型的对象,但是我遇到了AWrapper::foo
的实现问题。
static NAN_METHOD(foo) {
AWrapper* obj = Nan::ObjectWrap::Unwrap<AWrapper>(info.Holder());
B b = obj->a_.foo();
BWrapper * result = new BWrapper(b);
// Something to get a B object to javascript
// ...
// info.GetReturnValue().Set(result->Wrap());
// ...
// doesn't work - so what should it be?
}
如何使此功能正常工作?
.cc文件的完整代码是
#include <node.h>
#include <nan.h>
class Internal {
};
class B {
public:
Internal internal;
explicit B(Internal internal):internal(internal){}
};
class A {
public:
A() : internal() {};
B foo() { return B(internal); }
private:
Internal internal;
};
class BWrapper : public Nan::ObjectWrap {
public:
B b_;
explicit BWrapper(B b) : b_(b) {}
~BWrapper() {}
};
class AWrapper : public Nan::ObjectWrap {
public:
A a_;
explicit AWrapper(A a) : a_(a) {}
~AWrapper() {}
static void register_class(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("A").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(tpl, "foo", foo);
constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
Nan::Set(target, Nan::New("A").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
}
private:
static NAN_METHOD(New) {
if (info.IsConstructCall()) {
A a;
AWrapper *obj = new AWrapper(a);
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
} else {
const int argc = 1;
v8::Local<v8::Value> argv[argc] = {info[0]};
v8::Local<v8::Function> cons = Nan::New(constructor());
info.GetReturnValue().Set(cons->NewInstance(argc, argv));
}
}
static NAN_METHOD(foo) {
AWrapper* obj = Nan::ObjectWrap::Unwrap<AWrapper>(info.Holder());
B b = obj->a_.foo();
BWrapper * result = new BWrapper(b);
// Something to get a B object to javascript
//...
//info.GetReturnValue().Set(result->Wrap());
}
static inline Nan::Persistent<v8::Function> & constructor() {
static Nan::Persistent<v8::Function> my_constructor;
return my_constructor;
}
};
NAN_MODULE_INIT(InitModule) {
AWrapper::register_class(target);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, InitModule);
可以在https://github.com/mikeando/node_nan_minimal找到该示例的完整存储库,您应该能够克隆,然后使用npm install
进行构建。
答案 0 :(得分:2)
使这项工作的一种方法是:
init_class
函数中。foo
函数调用BWrapper::NewInstance
。这等于以下添加
class BWrapper {
...
static void init_class() {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("B").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
}
static NAN_METHOD(New) {
if (!info.IsConstructCall()) {
return Nan::ThrowError("File() must be called as a constructor");
}
if (info.Length() != 1 || ! info[0]->IsExternal()) {
return Nan::ThrowError("File() can only be called internally");
}
B* b = static_cast<B*>(info[0].As<v8::External>()->Value());
BWrapper *obj = new BWrapper(*b);
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
static v8::Local<v8::Object> NewInstance(B* b) {
Nan::EscapableHandleScope scope;
const unsigned argc = 1;
v8::Local<v8::Value> argv[argc] = { Nan::New<v8::External>(b) };
v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor());
v8::Local<v8::Object> instance = cons->NewInstance(argc, argv);
return scope.Escape(instance);
}
static inline Nan::Persistent<v8::Function> & constructor() {
static Nan::Persistent<v8::Function> my_constructor;
return my_constructor;
}
}
AWrapper::foo
的更改是:
static NAN_METHOD(foo) {
AWrapper* obj = Nan::ObjectWrap::Unwrap<AWrapper>(info.Holder());
B b = obj->a_.foo();
info.GetReturnValue().Set(BWrapper::NewInstance(&b));
}
最后我们需要确保课程注册。
NAN_MODULE_INIT(InitModule) {
BWrapper::init_class();
AWrapper::register_class(target);
}
我怀疑这不是最干净的方法,我很乐意看到任何其他选择。我是否特别感兴趣的是,以这种方式将构造函数暴露给BWrapper是否有任何缺点。
部分原因是根据另一个https://github.com/tracelytics/node-traceview-bindings/blob/master/src/metadata.cc#L18的建议阅读question,然后是我自己的一些实验。