11cb0ef41Sopenharmony_ci#include <node_api.h> 21cb0ef41Sopenharmony_ci#include <assert.h> 31cb0ef41Sopenharmony_ci#include "../../js-native-api/common.h" 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#define MAX_ARGUMENTS 10 61cb0ef41Sopenharmony_ci#define RESERVED_ARGS 3 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_cistatic napi_value MakeCallback(napi_env env, napi_callback_info info) { 91cb0ef41Sopenharmony_ci size_t argc = MAX_ARGUMENTS; 101cb0ef41Sopenharmony_ci size_t n; 111cb0ef41Sopenharmony_ci napi_value args[MAX_ARGUMENTS]; 121cb0ef41Sopenharmony_ci // NOLINTNEXTLINE (readability/null_usage) 131cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci NODE_API_ASSERT(env, argc > 0, "Wrong number of arguments"); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci napi_value resource = args[0]; 181cb0ef41Sopenharmony_ci napi_value recv = args[1]; 191cb0ef41Sopenharmony_ci napi_value func = args[2]; 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci napi_value argv[MAX_ARGUMENTS - RESERVED_ARGS]; 221cb0ef41Sopenharmony_ci for (n = RESERVED_ARGS; n < argc; n += 1) { 231cb0ef41Sopenharmony_ci argv[n - RESERVED_ARGS] = args[n]; 241cb0ef41Sopenharmony_ci } 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci napi_valuetype func_type; 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_typeof(env, func, &func_type)); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci napi_value resource_name; 311cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_create_string_utf8( 321cb0ef41Sopenharmony_ci env, "test", NAPI_AUTO_LENGTH, &resource_name)); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci napi_async_context context; 351cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_async_init(env, resource, resource_name, &context)); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci napi_value result; 381cb0ef41Sopenharmony_ci if (func_type == napi_function) { 391cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_make_callback( 401cb0ef41Sopenharmony_ci env, context, recv, func, argc - RESERVED_ARGS, argv, &result)); 411cb0ef41Sopenharmony_ci } else { 421cb0ef41Sopenharmony_ci NODE_API_ASSERT(env, false, "Unexpected argument type"); 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_async_destroy(env, context)); 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci return result; 481cb0ef41Sopenharmony_ci} 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_cistatic 511cb0ef41Sopenharmony_cinapi_value Init(napi_env env, napi_value exports) { 521cb0ef41Sopenharmony_ci napi_value fn; 531cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_create_function( 541cb0ef41Sopenharmony_ci // NOLINTNEXTLINE (readability/null_usage) 551cb0ef41Sopenharmony_ci env, NULL, NAPI_AUTO_LENGTH, MakeCallback, NULL, &fn)); 561cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn)); 571cb0ef41Sopenharmony_ci return exports; 581cb0ef41Sopenharmony_ci} 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ciNAPI_MODULE(NODE_GYP_MODULE_NAME, Init) 61