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

时间:2021-11-26 06:57:00

I have some code that used V8 FunctionTemplates. It is part of an older Node.js module that acts as a wrapper for a C++ library.

我有一些使用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);
}

However, when the code is run, the compiler gives these errors.

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

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);

The documentation for the V8 engine says

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

but I'm not sure how to use this to fix my function. Any ideas?

但我不知道如何使用它来修复我的功能。有任何想法吗?

2 个解决方案

#1


1  

Signature of SetTrainingAlgorithm is:

SetTrainingAlgorithm的签名是:

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

but should be:

但应该是:

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

Not the same namespace.

不同的命名空间。

#2


1  

Solution was to use

解决方案是使用

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

instead. This was changed from nan library version 1 to version 2.

代替。这已从nan库版本1更改为版本2。

#1


1  

Signature of SetTrainingAlgorithm is:

SetTrainingAlgorithm的签名是:

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

but should be:

但应该是:

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

Not the same namespace.

不同的命名空间。

#2


1  

Solution was to use

解决方案是使用

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

instead. This was changed from nan library version 1 to version 2.

代替。这已从nan库版本1更改为版本2。