NoDEJS C++插件:不能访问数组元素

xqk2d5yq  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(277)

我想访问作为参数从js端传递到函数中的数组元素。代码如下所示:

void Method(const FunctionCallbackInfo<Value> &args){
    Isolate* isolate = args.GetIsolate();
    Local<Array> array = Local<Array>::Cast(args[0]);

    for(int i=0;i<(int)array->Length();i++){
        auto ele = array->Get(i);
    }

我得到了这个错误:

error: no matching function for call to ‘v8::Array::Get(int&)’

在阅读了v8阵列的实现之后,我知道没有 Get 方法 Array .
以下是v8源代码中阵列的实现:

class V8_EXPORT Array : public Object {
 public:
  uint32_t Length() const;

  /**
   * Creates a JavaScript array with the given length. If the length
   * is negative the returned array will have length 0.
   */
  static Local<Array> New(Isolate* isolate, int length = 0);

  /**
   * Creates a JavaScript array out of a Local<Value> array in C++
   * with a known length.
   */
  static Local<Array> New(Isolate* isolate, Local<Value>* elements,
                          size_t length);
  V8_INLINE static Array* Cast(Value* obj);
 private:
  Array();
  static void CheckCast(Value* obj);
};

我是新来的。我浏览了一些教程,对他们来说效果很好。有人能帮我找出它的毛病吗?如果我们不能使用 Local<Array> 那么还有什么可以达到这个目的呢?

6rvt4ljy

6rvt4ljy1#

如果不知道您的目标是哪个版本的v8,很难回答,但是在当前的doxygen文档中,有两个重载 v8::Object::Get :

MaybeLocal< Value >     Get (Local< Context > context, Local< Value > key)
MaybeLocal< Value >     Get (Local< Context > context, uint32_t index)

因此,我认为你可以做到以下几点:

Local<Context> ctx = isolate->GetCurrentContext();
auto ele = array->Get(ctx, i);
...

相关问题