从构造函数获取原型,而无需在v8中创build实例

我正在编写一个导出对象构造函数的v8节点扩展,如本文档中所示。 一些函数将其他类的实例作为参数,所以我想检查这些参数的types,类似于这个答案的build议。 它暗示的一件事是将对象的原型与临时实例中已知的正确原型进行比较

Local<Object> obj = constructor->NewInstance(); prototype = Persistent<Value>::New(obj->GetPrototype()); if (instance->GetPrototype() == prototype) { //... 

然而,我的一些构造函数是复杂的,并将其他类的实例作为参数,所以这将是非常不方便的。

我的类初始化函数看起来像这样(从链接模板中获取):

 void MyObject::Init(Handle<Object> exports) { // Prepare constructor template Local<FunctionTemplate> tpl = FunctionTemplate::New(New); tpl->SetClassName(String::NewSymbol("MyObject")); tpl->InstanceTemplate()->SetInternalFieldCount(1); // Prototype tpl->PrototypeTemplate()->Set(String::NewSymbol("plusOne"), FunctionTemplate::New(PlusOne)->GetFunction()); constructor = Persistent<Function>::New(tpl->GetFunction()); exports->Set(String::NewSymbol("MyObject"), constructor); } 

是否有可能从FunctionTemplateconstructor FunctionTemplate获得一个prototype对象,如果我得到一个新的MyObject实例obj obj->GetPrototype()将比较相等与prototype

我尝试使用tpl->PrototypeTemplate()->NewInstance()constructor->GetPrototype() ,但似乎都没有正确的值。


这是我想要做的一个完整的例子:

 #include <node.h> #include <v8.h> using namespace v8; class MyObject : public node::ObjectWrap { public: static void Init(Handle<Object> exports); static Persistent<Value> prototype; private: static Handle<Value> New(const v8::Arguments& args); static Handle<Value> TypeTest(const v8::Arguments& args); static Persistent<Function> constructor; }; Persistent<Function> MyObject::constructor; Persistent<Value> MyObject::prototype; void MyObject::Init(Handle<Object> exports) { Local<FunctionTemplate> tpl = FunctionTemplate::New(New); tpl->SetClassName(String::NewSymbol("MyObject")); tpl->InstanceTemplate()->SetInternalFieldCount(1); tpl->PrototypeTemplate()->Set( String::NewSymbol("typeTest"), FunctionTemplate::New(TypeTest)->GetFunction()); constructor = Persistent<Function>::New(tpl->GetFunction()); prototype = Persistent<Value>::New(tpl->PrototypeTemplate()->NewInstance()); exports->Set(String::NewSymbol("MyObject"), constructor); } Handle<Value> MyObject::New(const Arguments& args) { HandleScope scope; if (args.IsConstructCall()) { MyObject *obj = new MyObject(); obj->Wrap(args.This()); return args.This(); } else { return scope.Close(constructor->NewInstance()); } } Handle<Value> MyObject::TypeTest(const Arguments& args) { HandleScope scope; if(args.This()->GetPrototype() != prototype) { return ThrowException(Exception::TypeError( String::New("This should not happen"))); } return Undefined(); } NODE_MODULE(hello, MyObject::Init); 

目前,如果我在节点中运行下面的代码(在构build插件之后),我得到types错误:

 var hello = require('./build/Release/hello'); var obj = new hello.MyObject(); obj.typeTest(); 

有没有什么办法来设置MyObject::prototype这样我不会得到错误,而不构buildMyObject的临时实例?

你能不能在第一次创build对象的时候设置静态原型的值,而不是在Init里面? 像这样的东西:

 class MyObject : public node::ObjectWrap { // ... as before ... }; Persistent<Function> MyObject::constructor; Persistent<Value> MyObject::prototype; void MyObject::Init(Handle<Object> exports) // ... as before but without setting prototype ... } Handle<Value> MyObject::New(const Arguments& args) { HandleScope scope; if (args.IsConstructCall()) { MyObject *obj = new MyObject(); obj->Wrap(args.This()); prototype = Persistent<Value>::New(args.This()->GetPrototype()); return args.This(); } else { return scope.Close(constructor->NewInstance()); } } Handle<Value> MyObject::TypeTest(const Arguments& args) { HandleScope scope; if(args.This()->GetPrototype() != prototype) { return ThrowException(Exception::TypeError( String::New("This should not happen"))); } return Undefined(); } NODE_MODULE(hello, MyObject::Init);