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

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

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

  1. void Method(const FunctionCallbackInfo<Value> &args){
  2. Isolate* isolate = args.GetIsolate();
  3. Local<Array> array = Local<Array>::Cast(args[0]);
  4. for(int i=0;i<(int)array->Length();i++){
  5. auto ele = array->Get(i);
  6. }

我得到了这个错误:

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

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

  1. class V8_EXPORT Array : public Object {
  2. public:
  3. uint32_t Length() const;
  4. /**
  5. * Creates a JavaScript array with the given length. If the length
  6. * is negative the returned array will have length 0.
  7. */
  8. static Local<Array> New(Isolate* isolate, int length = 0);
  9. /**
  10. * Creates a JavaScript array out of a Local<Value> array in C++
  11. * with a known length.
  12. */
  13. static Local<Array> New(Isolate* isolate, Local<Value>* elements,
  14. size_t length);
  15. V8_INLINE static Array* Cast(Value* obj);
  16. private:
  17. Array();
  18. static void CheckCast(Value* obj);
  19. };

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

6rvt4ljy

6rvt4ljy1#

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

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

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

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

相关问题