如何在nodejs插件中调用两个参数构造函数?
我是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); }
还有一些模块的init代码会执行一些魔术来创build一个构造函数模板:
// 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));
通过日志logging,我看到NewInstance(md)
调用最终调用了两个参数C ++构造函数Event::Event(const oboe_metadata_t *md, bool addEdge)
,并且布尔addEdge
为true。
单参数NewInstance
调用如何最终调用两个参数构造函数,以及addEdge
如何设置为true?
我不清楚如何将v8::Local<v8::Object> Event::NewInstance()
声明的三个变体映射到C ++构造函数,但似乎必须在v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(New);
任何帮助,将不胜感激。
这里是缺less的代码,显示我已经跳过的“新”function,因为评论说它创build了一个新的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()); }