簡體   English   中英

如何使用 Node-API 迭代 JavaScript 數組

[英]How to iterate JavaScript array using Node-API

我正在使用Node-API構建一個Node.js插件。

基本上,我的算法將一個 js 數組作為輸入,然后在插件內部對其進行處理並返回它。

要對數組執行任何邏輯,我需要遍歷它。 但我不知道怎么做。 因為我在他們的文檔或示例中沒有找到任何與數組迭代相關的文檔。

所以我認為這更多的是在C中進行。 我已經在下面的代碼中添加了我嘗試過的內容,但沒有工作,我已經在下面的代碼中注釋掉了它。

我還嘗試在nodejs 源代碼node-addon-api中找到任何有用的東西,但由於我對此的了解有限,所以我沒有。

請指導我如何循環和訪問該陣列中的每個 object。

napi_value avm_simplify(napi_env env, napi_callback_info info) {
  napi_status status;
  napi_value value;

  size_t argc = 1;
  napi_value args[1];
  status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
  STATUS_CHECK

  if (argc < 1) {
    napi_throw_type_error(env, NULL, WRONG_ARGUMENTS_ERROR);
    return NULL;
  }

  bool is_array = 0;
  status = napi_is_array(env, args[0], &is_array);
  STATUS_CHECK

  if (!is_array) {
    napi_throw_type_error(env, NULL, "Argument type should be an array");
    return NULL;
  }

  // args[0] equals to something like
  // [1, 2, 3, 4, 5]
  // which is the user input (js array)
  napi_value array = args[0];

  /*
  // This is what I wan't to do.
  int length = sizeof(array) / sizeof(array[0]);
  for (size_t i = 0; i < length; i++) {
      napi_value object = array[i];
      // do some logic with object
  }
  */

  return value;
}

我知道使用C++應該會容易得多,但我的模塊將來主要是指C庫。

提前致謝。

我發現https://nodejs.org/api/n-api.html 讀起來不錯

這是我將如何迭代(添加狀態檢查)

uint32_t length;
status = napi_get_array_length(env, array, &length);
for (uint32_t i = 0; i < length; ++i) {
  napi_value ret;
  napi_get_element(env, array, i, &ret);

  int32_t myint;
  napi_get_value_int32(env, ret, &myint);

下面的示例將輸入數組的每個元素乘以 2

// hello.cc using Node-API
#include <node_api.h>
#include <assert.h>
namespace demo {

napi_value Method(napi_env env, napi_callback_info info) {
  napi_status status;
  napi_value value;

  size_t argc = 1;
  napi_value args[1];
  status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
  assert(status == napi_ok);

  if (argc < 1) {
    napi_throw_type_error(env, NULL, "Wrong number of arguments");
    return NULL;
  }

  bool is_array = 0;
  status = napi_is_array(env, args[0], &is_array);
  assert(status == napi_ok);

  if (!is_array) {
    napi_throw_type_error(env, NULL, "Argument type should be an array");
    return NULL;
  }

  // args[0] equals to something like
  // [1, 2, 3, 4, 5]
  // which is the user input (js array)
  napi_value array = args[0];

  if (!array) {
    std::cout<<"array is null"<<std::endl;
    return NULL;
  }


  uint32_t length;
  status = napi_get_array_length(env, array, &length);

  for (uint32_t i = 0; i < length; ++i) {
    napi_value ret;
    napi_get_element(env, array, i, &ret);

    int32_t myint;
    napi_get_value_int32(env, ret, &myint);


    napi_value result;
    napi_create_int32(env, myint * 2, &result);
    napi_set_element(env, array, i, result);
  }

  return array;
}

// copy pasted
napi_value init(napi_env env, napi_value exports) {
  napi_status status;
  napi_value fn;

  status = napi_create_function(env, nullptr, 0, Method, nullptr, &fn);
  if (status != napi_ok) return nullptr;

  status = napi_set_named_property(env, exports, "hello", fn);
  if (status != napi_ok) return nullptr;
  return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, init)

}  // namespace demo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM