我正在使用Node-API構建一個Node.js插件。
基本上,我的演算法將一個 js 陣列作為輸入,然后在插件內部對其進行處理并回傳它。
要對陣列執行任何邏輯,我需要遍歷它。但我不知道怎么做。因為我在他們的檔案或示例中沒有找到任何與陣列迭代相關的檔案。
所以我認為這更多的是在C. 我已經在下面的代碼中添加了我嘗試過的內容,但沒有作業,我已經在下面的代碼中注釋掉了它。
我還嘗試在nodejs 源代碼和node-addon-api中找到任何有用的東西,但由于我對此的了解有限,所以我沒有。
請指導我如何回圈和訪問該陣列中的每個物件。
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庫。
提前致謝。
uj5u.com熱心網友回復:
我發現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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/418420.html
標籤:
上一篇:如何解決IntellijGit錯誤來自服務器的空回復
下一篇:用nvm安裝最新的偶數節點?
