我是nodejs插件的新手,我正在使用现有代码动态学习。我有一种情况,我不明白如何调用特定的C ++构造函数,以及如何设置参数的值。
有两个纯C ++构造函数:
Nan::Persistent<v8::Function> Event::constructor;
// Construct a blank event from the context metadata
Event::Event() {
oboe_event_init(&event, oboe_context_get());
}
// Construct a new event and point an edge at another
Event::Event(const oboe_metadata_t *md, bool addEdge) {
// both methods copy metadata (version, task_id, flags) from
// md to this->event and create a new random op_id.
if (addEdge) {
// create_event automatically adds edge in event to md
// (so event points an edge to the op_id in md).
oboe_metadata_create_event(md, &event);
} else {
// initializes new Event with md's task_id & new random op_id;
// no edges set
oboe_event_init(&event, md);
}
}
// Remember to cleanup the struct when garbage collected
Event::~Event() {
oboe_event_destroy(&event);
}
有三个NewInstance
声明:一个有两个参数,一个有一个,一个没有。
v8::Local<v8::Object> Event::NewInstance(Metadata* md, bool addEdge) {
Nan::EscapableHandleScope scope;
const unsigned argc = 2;
v8::Local<v8::Value> argv[argc] = {
Nan::New<v8::External>(md),
Nan::New(addEdge)
};
v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
v8::Local<v8::Object> instance = cons->NewInstance(argc, argv);
return scope.Escape(instance);
}
v8::Local<v8::Object> Event::NewInstance(Metadata* md) {
Nan::EscapableHandleScope scope;
const unsigned argc = 1;
v8::Local<v8::Value> argv[argc] = { Nan::New<v8::External>(md) };
v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
v8::Local<v8::Object> instance = cons->NewInstance(argc, argv);
return scope.Escape(instance);
}
v8::Local<v8::Object> Event::NewInstance() {
Nan::EscapableHandleScope scope;
const unsigned argc = 0;
v8::Local<v8::Value> argv[argc] = {};
v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
v8::Local<v8::Object> instance = cons->NewInstance(argc, argv);
return scope.Escape(instance);
}
还有模块初始化代码,它可以执行一些魔术来构建构造函数模板:
// Wrap the C++ object so V8 can understand it
void Event::Init(v8::Local<v8::Object> exports) {
Nan::HandleScope scope;
// Prepare constructor template
v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(New);
ctor->InstanceTemplate()->SetInternalFieldCount(1);
ctor->SetClassName(Nan::New("Event").ToLocalChecked());
// Statics
Nan::SetMethod(ctor, "startTrace", Event::startTrace);
// Prototype
Nan::SetPrototypeMethod(ctor, "addInfo", Event::addInfo);
Nan::SetPrototypeMethod(ctor, "addEdge", Event::addEdge);
Nan::SetPrototypeMethod(ctor, "getMetadata", Event::getMetadata);
Nan::SetPrototypeMethod(ctor, "toString", Event::toString);
constructor.Reset(ctor->GetFunction());
Nan::Set(exports, Nan::New("Event").ToLocalChecked(), ctor->GetFunction());
}
最后,有代码调用Event::NewInstance
:
Metadata* md = new Metadata(oboe_context_get());
info.GetReturnValue().Set(Event::NewInstance(md));
通过日志记录,我看到NewInstance(md)
调用最终调用了两个参数C ++构造函数Event::Event(const oboe_metadata_t *md, bool addEdge)
,并且布尔值addEdge
为真。
单参数NewInstance
调用如何最终调用两个参数构造函数以及addEdge
如何设置为true?
我不清楚v8::Local<v8::Object> Event::NewInstance()
声明的三种变体如何映射到C ++构造函数,但它似乎必须在行v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(New);
任何帮助都将不胜感激。
这是缺少的代码,显示&#34; New&#34;我跳过的函数,因为评论说它创建了一个新的JavaScript实例。但它显然是缺失的部分。
// Creates a new Javascript instance
NAN_METHOD(Event::New) {
if (!info.IsConstructCall()) {
return Nan::ThrowError("Event() must be called as a constructor");
}
Event* event;
if (info.Length() > 0 && info[0]->IsExternal()) {
Metadata* md = static_cast<Metadata*>(info[0].As<v8::External>()->Value());
oboe_metadata_t* context = &md->metadata;
bool addEdge = true;
if (info.Length() == 2 && info[1]->IsBoolean()) {
addEdge = info[1]->BooleanValue();
}
event = new Event(context, addEdge);
} else {
event = new Event();
}
event->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}