gpt4 book ai didi

c++ - 从构造函数获取原型(prototype)而不在 v8 中创建实例

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:43:02 25 4
gpt4 key购买 nike

我正在编写一个导出对象构造函数的 v8 Node 扩展,如 this documentation 所示。 .一些函数将其他类的实例作为参数,所以我想检查这些参数的类型,类似于 this answer。建议。它建议的一件事是将对象的原型(prototype)与来自临时实例的已知正确原型(prototype)进行比较,例如

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 获得一个 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);

目前,如果我在 Node 中运行以下代码(在构建插件之后),我会收到类型错误:

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

有什么方法可以设置 MyObject::prototype,这样我就不会在不构造 MyObject 的临时实例的情况下得到错误?

最佳答案

你不能在第一次创建对象时设置静态原型(prototype)值,而不是在 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);

关于c++ - 从构造函数获取原型(prototype)而不在 v8 中创建实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25755516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com