11cb0ef41Sopenharmony_ci#include <node_api.h>
21cb0ef41Sopenharmony_ci#include <assert.h>
31cb0ef41Sopenharmony_ci#include <stdlib.h>
41cb0ef41Sopenharmony_ci#include <stdio.h>
51cb0ef41Sopenharmony_ci#include <string.h>
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cistatic napi_value CallWithString(napi_env env, napi_callback_info info) {
81cb0ef41Sopenharmony_ci  napi_status status;
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci  size_t argc = 1;
111cb0ef41Sopenharmony_ci  napi_value args[1];
121cb0ef41Sopenharmony_ci  status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
131cb0ef41Sopenharmony_ci  assert(status == napi_ok);
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  napi_valuetype types[1];
161cb0ef41Sopenharmony_ci  status = napi_typeof(env, args[0], types);
171cb0ef41Sopenharmony_ci  assert(status == napi_ok);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  assert(types[0] == napi_string);
201cb0ef41Sopenharmony_ci  if (types[0] == napi_string) {
211cb0ef41Sopenharmony_ci    size_t len = 0;
221cb0ef41Sopenharmony_ci    // Get the length
231cb0ef41Sopenharmony_ci    status = napi_get_value_string_utf8(env, args[0], NULL, 0, &len);
241cb0ef41Sopenharmony_ci    assert(status == napi_ok);
251cb0ef41Sopenharmony_ci    char* buf = (char*)malloc(len + 1);
261cb0ef41Sopenharmony_ci    status = napi_get_value_string_utf8(env, args[0], buf, len + 1, &len);
271cb0ef41Sopenharmony_ci    assert(status == napi_ok);
281cb0ef41Sopenharmony_ci    free(buf);
291cb0ef41Sopenharmony_ci  }
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  return NULL;
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_cistatic napi_value CallWithArray(napi_env env, napi_callback_info info) {
351cb0ef41Sopenharmony_ci  napi_status status;
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  size_t argc = 1;
381cb0ef41Sopenharmony_ci  napi_value args[1];
391cb0ef41Sopenharmony_ci  status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
401cb0ef41Sopenharmony_ci  assert(status == napi_ok);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  napi_value array = args[0];
431cb0ef41Sopenharmony_ci  bool is_array = false;
441cb0ef41Sopenharmony_ci  status = napi_is_array(env, array, &is_array);
451cb0ef41Sopenharmony_ci  assert(status == napi_ok);
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  assert(is_array);
481cb0ef41Sopenharmony_ci  if (is_array) {
491cb0ef41Sopenharmony_ci    uint32_t length;
501cb0ef41Sopenharmony_ci    status = napi_get_array_length(env, array, &length);
511cb0ef41Sopenharmony_ci    assert(status == napi_ok);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci    uint32_t i;
541cb0ef41Sopenharmony_ci    for (i = 0; i < length; ++i) {
551cb0ef41Sopenharmony_ci      napi_value v;
561cb0ef41Sopenharmony_ci      status = napi_get_element(env, array, i, &v);
571cb0ef41Sopenharmony_ci      assert(status == napi_ok);
581cb0ef41Sopenharmony_ci    }
591cb0ef41Sopenharmony_ci  }
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  return NULL;
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_cistatic napi_value CallWithNumber(napi_env env, napi_callback_info info) {
651cb0ef41Sopenharmony_ci  napi_status status;
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  size_t argc = 1;
681cb0ef41Sopenharmony_ci  napi_value args[1];
691cb0ef41Sopenharmony_ci  status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
701cb0ef41Sopenharmony_ci  assert(status == napi_ok);
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  napi_valuetype types[1];
731cb0ef41Sopenharmony_ci  status = napi_typeof(env, args[0], types);
741cb0ef41Sopenharmony_ci  assert(status == napi_ok);
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  assert(types[0] == napi_number);
771cb0ef41Sopenharmony_ci  if (types[0] == napi_number) {
781cb0ef41Sopenharmony_ci    double value = 0.0;
791cb0ef41Sopenharmony_ci    status = napi_get_value_double(env, args[0], &value);
801cb0ef41Sopenharmony_ci    assert(status == napi_ok);
811cb0ef41Sopenharmony_ci  }
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  return NULL;
841cb0ef41Sopenharmony_ci}
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_cistatic napi_value CallWithObject(napi_env env, napi_callback_info info) {
871cb0ef41Sopenharmony_ci  napi_status status;
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  size_t argc = 1;
901cb0ef41Sopenharmony_ci  napi_value args[1];
911cb0ef41Sopenharmony_ci  status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
921cb0ef41Sopenharmony_ci  assert(status == napi_ok);
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  napi_valuetype types[1];
951cb0ef41Sopenharmony_ci  status = napi_typeof(env, args[0], types);
961cb0ef41Sopenharmony_ci  assert(status == napi_ok);
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  assert(argc == 1 && types[0] == napi_object);
991cb0ef41Sopenharmony_ci  if (argc == 1 && types[0] == napi_object) {
1001cb0ef41Sopenharmony_ci    napi_value value;
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci    status = napi_get_named_property(env, args[0], "map", &value);
1031cb0ef41Sopenharmony_ci    assert(status == napi_ok);
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci    status = napi_get_named_property(env, args[0], "operand", &value);
1061cb0ef41Sopenharmony_ci    assert(status == napi_ok);
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci    status = napi_get_named_property(env, args[0], "data", &value);
1091cb0ef41Sopenharmony_ci    assert(status == napi_ok);
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci    status = napi_get_named_property(env, args[0], "reduce", &value);
1121cb0ef41Sopenharmony_ci    assert(status == napi_ok);
1131cb0ef41Sopenharmony_ci  }
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci  return NULL;
1161cb0ef41Sopenharmony_ci}
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_cistatic napi_value CallWithTypedarray(napi_env env, napi_callback_info info) {
1191cb0ef41Sopenharmony_ci  napi_status status;
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci  size_t argc = 1;
1221cb0ef41Sopenharmony_ci  napi_value args[1];
1231cb0ef41Sopenharmony_ci  status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
1241cb0ef41Sopenharmony_ci  assert(status == napi_ok);
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci  bool is_typedarray = false;
1271cb0ef41Sopenharmony_ci  status = napi_is_typedarray(env, args[0], &is_typedarray);
1281cb0ef41Sopenharmony_ci  assert(status == napi_ok);
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  assert(is_typedarray);
1311cb0ef41Sopenharmony_ci  if (is_typedarray) {
1321cb0ef41Sopenharmony_ci    napi_typedarray_type type;
1331cb0ef41Sopenharmony_ci    napi_value input_buffer;
1341cb0ef41Sopenharmony_ci    size_t byte_offset = 0;
1351cb0ef41Sopenharmony_ci    size_t length = 0;
1361cb0ef41Sopenharmony_ci    status = napi_get_typedarray_info(env, args[0], &type, &length,
1371cb0ef41Sopenharmony_ci        NULL, &input_buffer, &byte_offset);
1381cb0ef41Sopenharmony_ci    assert(status == napi_ok);
1391cb0ef41Sopenharmony_ci    assert(length > 0);
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci    void* data = NULL;
1421cb0ef41Sopenharmony_ci    size_t byte_length = 0;
1431cb0ef41Sopenharmony_ci    status = napi_get_arraybuffer_info(env,
1441cb0ef41Sopenharmony_ci        input_buffer, &data, &byte_length);
1451cb0ef41Sopenharmony_ci    assert(status == napi_ok);
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci    uint32_t* input_integers = (uint32_t*)((uint8_t*)(data) + byte_offset);
1481cb0ef41Sopenharmony_ci    assert(input_integers);
1491cb0ef41Sopenharmony_ci  }
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci  return NULL;
1521cb0ef41Sopenharmony_ci}
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_cistatic napi_value CallWithArguments(napi_env env, napi_callback_info info) {
1551cb0ef41Sopenharmony_ci  napi_status status;
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci  size_t argc = 1;
1581cb0ef41Sopenharmony_ci  napi_value args[1000];
1591cb0ef41Sopenharmony_ci  // Get the length
1601cb0ef41Sopenharmony_ci  status = napi_get_cb_info(env, info, &argc, NULL, NULL, NULL);
1611cb0ef41Sopenharmony_ci  assert(status == napi_ok);
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci  status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
1641cb0ef41Sopenharmony_ci  assert(status == napi_ok);
1651cb0ef41Sopenharmony_ci  assert(argc <= 1000);
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci  napi_valuetype types[1];
1681cb0ef41Sopenharmony_ci  status = napi_typeof(env, args[0], types);
1691cb0ef41Sopenharmony_ci  assert(status == napi_ok);
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci  assert(argc > 1 && types[0] == napi_number);
1721cb0ef41Sopenharmony_ci  if (argc > 1 && types[0] == napi_number) {
1731cb0ef41Sopenharmony_ci    uint32_t loop = 0;
1741cb0ef41Sopenharmony_ci    status = napi_get_value_uint32(env, args[0], &loop);
1751cb0ef41Sopenharmony_ci    assert(status == napi_ok);
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci    uint32_t i;
1781cb0ef41Sopenharmony_ci    for (i = 1; i < loop; ++i) {
1791cb0ef41Sopenharmony_ci      assert(i < argc);
1801cb0ef41Sopenharmony_ci      status = napi_typeof(env, args[i], types);
1811cb0ef41Sopenharmony_ci      assert(status == napi_ok);
1821cb0ef41Sopenharmony_ci      assert(types[0] == napi_number);
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci      uint32_t value = 0;
1851cb0ef41Sopenharmony_ci      status = napi_get_value_uint32(env, args[i], &value);
1861cb0ef41Sopenharmony_ci      assert(status == napi_ok);
1871cb0ef41Sopenharmony_ci    }
1881cb0ef41Sopenharmony_ci  }
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ci  return NULL;
1911cb0ef41Sopenharmony_ci}
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci#define EXPORT_FUNC(env, exports, name, func)       \
1951cb0ef41Sopenharmony_ci  do {                                              \
1961cb0ef41Sopenharmony_ci    napi_status status;                             \
1971cb0ef41Sopenharmony_ci    napi_value js_func;                             \
1981cb0ef41Sopenharmony_ci    status = napi_create_function((env),            \
1991cb0ef41Sopenharmony_ci                                  (name),           \
2001cb0ef41Sopenharmony_ci                                  NAPI_AUTO_LENGTH, \
2011cb0ef41Sopenharmony_ci                                  (func),           \
2021cb0ef41Sopenharmony_ci                                  NULL,             \
2031cb0ef41Sopenharmony_ci                                  &js_func);        \
2041cb0ef41Sopenharmony_ci    assert(status == napi_ok);                      \
2051cb0ef41Sopenharmony_ci    status = napi_set_named_property((env),         \
2061cb0ef41Sopenharmony_ci                                     (exports),     \
2071cb0ef41Sopenharmony_ci                                     (name),        \
2081cb0ef41Sopenharmony_ci                                     js_func);      \
2091cb0ef41Sopenharmony_ci    assert(status == napi_ok);                      \
2101cb0ef41Sopenharmony_ci  } while (0);
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ci
2131cb0ef41Sopenharmony_ciNAPI_MODULE_INIT() {
2141cb0ef41Sopenharmony_ci  EXPORT_FUNC(env, exports, "callWithString", CallWithString);
2151cb0ef41Sopenharmony_ci  EXPORT_FUNC(env, exports, "callWithLongString", CallWithString);
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ci  EXPORT_FUNC(env, exports, "callWithArray", CallWithArray);
2181cb0ef41Sopenharmony_ci  EXPORT_FUNC(env, exports, "callWithLargeArray", CallWithArray);
2191cb0ef41Sopenharmony_ci  EXPORT_FUNC(env, exports, "callWithHugeArray", CallWithArray);
2201cb0ef41Sopenharmony_ci
2211cb0ef41Sopenharmony_ci  EXPORT_FUNC(env, exports, "callWithNumber", CallWithNumber);
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci  EXPORT_FUNC(env, exports, "callWithObject", CallWithObject);
2241cb0ef41Sopenharmony_ci  EXPORT_FUNC(env, exports, "callWithTypedarray", CallWithTypedarray);
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ci  EXPORT_FUNC(env, exports, "callWith10Numbers", CallWithArguments);
2271cb0ef41Sopenharmony_ci  EXPORT_FUNC(env, exports, "callWith100Numbers", CallWithArguments);
2281cb0ef41Sopenharmony_ci  EXPORT_FUNC(env, exports, "callWith1000Numbers", CallWithArguments);
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ci  return exports;
2311cb0ef41Sopenharmony_ci}
232