11cb0ef41Sopenharmony_ci#include <js_native_api.h>
21cb0ef41Sopenharmony_ci#include <string.h>
31cb0ef41Sopenharmony_ci#include "../common.h"
41cb0ef41Sopenharmony_ci#include "../entry_point.h"
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_cistatic napi_value RunCallback(napi_env env, napi_callback_info info) {
71cb0ef41Sopenharmony_ci  size_t argc = 2;
81cb0ef41Sopenharmony_ci  napi_value args[2];
91cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci  NODE_API_ASSERT(env, argc == 1,
121cb0ef41Sopenharmony_ci      "Wrong number of arguments. Expects a single argument.");
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  napi_valuetype valuetype0;
151cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0));
161cb0ef41Sopenharmony_ci  NODE_API_ASSERT(env, valuetype0 == napi_function,
171cb0ef41Sopenharmony_ci      "Wrong type of arguments. Expects a function as first argument.");
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  napi_valuetype valuetype1;
201cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_typeof(env, args[1], &valuetype1));
211cb0ef41Sopenharmony_ci  NODE_API_ASSERT(env, valuetype1 == napi_undefined,
221cb0ef41Sopenharmony_ci      "Additional arguments should be undefined.");
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  napi_value argv[1];
251cb0ef41Sopenharmony_ci  const char* str = "hello world";
261cb0ef41Sopenharmony_ci  size_t str_len = strlen(str);
271cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_create_string_utf8(env, str, str_len, argv));
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  napi_value global;
301cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_get_global(env, &global));
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  napi_value cb = args[0];
331cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_call_function(env, global, cb, 1, argv, NULL));
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  return NULL;
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cistatic napi_value RunCallbackWithRecv(napi_env env, napi_callback_info info) {
391cb0ef41Sopenharmony_ci  size_t argc = 2;
401cb0ef41Sopenharmony_ci  napi_value args[2];
411cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  napi_value cb = args[0];
441cb0ef41Sopenharmony_ci  napi_value recv = args[1];
451cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_call_function(env, recv, cb, 0, NULL, NULL));
461cb0ef41Sopenharmony_ci  return NULL;
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciEXTERN_C_START
501cb0ef41Sopenharmony_cinapi_value Init(napi_env env, napi_value exports) {
511cb0ef41Sopenharmony_ci  napi_property_descriptor desc[2] = {
521cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("RunCallback", RunCallback),
531cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("RunCallbackWithRecv", RunCallbackWithRecv),
541cb0ef41Sopenharmony_ci  };
551cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_define_properties(env, exports, 2, desc));
561cb0ef41Sopenharmony_ci  return exports;
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ciEXTERN_C_END
59