c++ - 如何更新我的函数以使用新的 v8 FunctionTemplates?

标签 c++ node.js v8 node.js-addon

我有一些代码使用了 V8 FunctionTemplates。它是旧 Node.js 模块的一部分,充当 C++ 库的包装器。

void NNet::PrototypeInit(Local<FunctionTemplate> t)
{
    ...

    t->InstanceTemplate()->SetAccessor(Nan::New<String>("training_algorithm").ToLocalChecked(), GetTrainingAlgorithm, SetTrainingAlgorithm);
    t->InstanceTemplate()->SetAccessor(Nan::New<String>("learning_rate").ToLocalChecked(), GetLearningRate, SetLearningRate);
    t->InstanceTemplate()->SetAccessor(Nan::New<String>("learning_momentum").ToLocalChecked(), GetLearningMomentum, SetLearningMomentum);
    t->InstanceTemplate()->SetAccessor(Nan::New<String>("layers").ToLocalChecked(), GetLayerArray);
}

但是,当代码运行时,编译器会给出这些错误。

In static member function ‘static void NNet::PrototypeInit(v8::Local<v8::FunctionTemplate>)’:
error: invalid conversion from ‘Nan::NAN_GETTER_RETURN_TYPE (*)(v8::Local<v8::String>, Nan::NAN_GETTER_ARGS_TYPE) {aka void (*)(v8::Local<v8::String>, const Nan::PropertyCallbackInfo<v8::Value>&)}’ to ‘v8::AccessorGetterCallback {aka void (*)(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value>&)}’ [-fpermissive]
   t->InstanceTemplate()->SetAccessor(Nan::New<String>("training_algorithm").ToLocalChecked(), GetTrainingAlgorithm, SetTrainingAlgorithm);
                                                                                                                                         ^
error:   initializing argument 2 of ‘void v8::ObjectTemplate::SetAccessor(v8::Local<v8::String>, v8::AccessorGetterCallback, v8::AccessorSetterCallback, v8::Local<v8::Value>, v8::AccessControl, v8::PropertyAttribute, v8::Local<v8::AccessorSignature>)’ [-fpermissive]
   void SetAccessor(
        ^
error: invalid conversion from ‘Nan::NAN_SETTER_RETURN_TYPE (*)(v8::Local<v8::String>, v8::Local<v8::Value>, Nan::NAN_SETTER_ARGS_TYPE) {aka void (*)(v8::Local<v8::String>, v8::Local<v8::Value>, const Nan::PropertyCallbackInfo<void>&)}’ to ‘v8::AccessorSetterCallback {aka void (*)(v8::Local<v8::String>, v8::Local<v8::Value>, const v8::PropertyCallbackInfo<void>&)}’ [-fpermissive]
   t->InstanceTemplate()->SetAccessor(Nan::New<String>("training_algorithm").ToLocalChecked(), GetTrainingAlgorithm, SetTrainingAlgorithm);

V8 引擎的文档说

The following example shows how to use a FunctionTemplate:
 *
 * \code
 *    v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
 *    t->Set("func_property", v8::Number::New(1));
 *
 *    v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
 *    proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
 *    proto_t->Set("proto_const", v8::Number::New(2));
 *
 *    v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
 *    instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
 *    instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
 *    instance_t->Set("instance_property", Number::New(3));
 *
 *    v8::Local<v8::Function> function = t->GetFunction();
 *    v8::Local<v8::Object> instance = function->NewInstance();
 * \endcode

但我不确定如何使用它来修复我的功能。有什么想法吗?

最佳答案

SetTrainingAlgorithm 的签名是:

void SetTrainingAlgorithm(v8::Local<v8::String>,
                          const Nan::PropertyCallbackInfo<v8::Value>&);

但应该是:

void SetTrainingAlgorithm(v8::Local<v8::String>,
                          const v8::PropertyCallbackInfo<v8::Value>&);

不一样的命名空间。

关于c++ - 如何更新我的函数以使用新的 v8 FunctionTemplates?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33383493/

相关文章:

mysql - Node MySQL 池连接中的事务处理

javascript - Nodejs Profiling : What to do with v8. 日志文件

c++ - 将非静态 const 数组声明为类成员

javascript - 一个函数可以被认为类似于一个类吗?

c++ - 错误 "requested alignment is not an integer constant"

c++ - 使用 make 编译多个可执行文件

javascript - Express:实现 MongoDB session 存储

node.js - 将一些文档从 CouchDB 放入数组 (Node.JS)

javascript - 为什么我的 Node.js 内存使用基准看起来是错误的?

javascript - node.js v8 垃圾回收似乎没有触发