使用全局范围内的对象的原型实例化新的JS对象

时间:2018-12-05 19:41:59

标签: javascript c++ v8 embedded-v8

在我使用附加功能扩展该对象的全局原型的情况下,如何实例化新对象?

这是我的代码:

R

ProtocolGame :: createTemplate函数:

//global initialization
Local<ObjectTemplate> global = ObjectTemplate::New(_isolate);
Eternal<FunctionTemplate> ProtocolGame_global(_isolate, ProtocolGame::createTemplate(_isolate));

global->Set(String::NewFromUtf8(_isolate, "ProtocolGame", NewStringType::kNormal).ToLocalChecked(), ProtocolGame_global.Get(_isolate));

Local<Context> context = Context::New(_isolate, NULL, global);

这是我用来将对象包装到JS的包装函数:

Local<FunctionTemplate> ProtocolGame_class = FunctionTemplate::New(isolate);
Local<ObjectTemplate> ProtocolGame_proto = ProtocolGame_class->PrototypeTemplate();
Local<ObjectTemplate> ProtocolGame_inst = ProtocolGame_class->InstanceTemplate();

ProtocolGame_class->SetClassName((String::NewFromUtf8(isolate, "ProtocolGame", NewStringType::kInternalized).ToLocalChecked()));
ProtocolGame_inst->SetInternalFieldCount(1);
ProtocolGame_proto->Set(Symbol::GetToStringTag(isolate), String::NewFromUtf8(isolate, "ProtocolGame", NewStringType::kNormal).ToLocalChecked(), static_cast<PropertyAttribute>(ReadOnly | DontEnum));
ProtocolGame_proto->Set(String::NewFromUtf8(isolate, "getOutputBuffer", NewStringType::kInternalized).ToLocalChecked(), FunctionTemplate::New(isolate, &ProtocolGame::__getOutputBuffer));
ProtocolGame_proto->Set(String::NewFromUtf8(isolate, "disconnect", NewStringType::kInternalized).ToLocalChecked(), FunctionTemplate::New(isolate, &ProtocolGame::__disconnect));
ProtocolGame_proto->Set(String::NewFromUtf8(isolate, "disconnectClient", NewStringType::kInternalized).ToLocalChecked(), FunctionTemplate::New(isolate, &ProtocolGame::__disconnectClient));
ProtocolGame_proto->Set(String::NewFromUtf8(isolate, "send", NewStringType::kInternalized).ToLocalChecked(), FunctionTemplate::New(isolate, &ProtocolGame::__send));
ProtocolGame_proto->Set(String::NewFromUtf8(isolate, "writeToOutputBuffer", NewStringType::kInternalized).ToLocalChecked(), FunctionTemplate::New(isolate, &ProtocolGame::__writeToOutputBuffer));

ProtocolGame_class->Set(String::NewFromUtf8(isolate, "bindOnSpawnInWorld", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, &ProtocolGame::__bindOnSpawnInWorld));
ProtocolGame_class->Set(String::NewFromUtf8(isolate, "bindOnPacket", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, &ProtocolGame::__bindOnPacket));
return ProtocolGame_class;

然后在JS中,我向原型添加函数,例如:

if (protocol_game_template.IsEmpty()) {
    Local<ObjectTemplate> raw_template = createTemplate(GetIsolate());
    protocol_game_template.Reset(GetIsolate(), raw_template);
}
Local<ObjectTemplate> templ = Local<ObjectTemplate>::New(GetIsolate(), protocol_game_template);
Local<Object> result = templ->NewInstance(GetIsolate()->GetCurrentContext()).ToLocalChecked();
Local<External> protocol_ptr = External::New(GetIsolate(), this);
result->SetInternalField(0, protocol_ptr);

但是当我包装我的ProtocolGame对象(使用包装函数)并将其作为函数参数传递后,在遍历原型对象时看不到JS的函数(parseLogout)。我尝试过从全球范围获取原型,但未能按预期工作。

我已经像使用另一个对象那样工作了,不同之处在于我使用new运算符在JS中创建了该对象,因此它正在运行New函数是C ++,并且我可以轻松地获得原型,因为我有一个对象引用用于创建新实例:

ProtocolGame.prototype.parseLogout = function() {
    this.disconnect();
}

但是我找不到从该全局实例获取原型的方法。

注意:当我遍历ProtocolGame.prototype的键时,可以看到该函数已添加。

0 个答案:

没有答案