我是 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);
任何帮助,将不胜感激。
这是显示我跳过的“新建”函数的缺失代码,因为注释说它创建了一个新的 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());
}