11cb0ef41Sopenharmony_ci#ifndef JS_NATIVE_API_COMMON_INL_H_
21cb0ef41Sopenharmony_ci#define JS_NATIVE_API_COMMON_INL_H_
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#include <js_native_api.h>
51cb0ef41Sopenharmony_ci#include "common.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include <stdio.h>
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciinline void add_returned_status(napi_env env,
101cb0ef41Sopenharmony_ci                                const char* key,
111cb0ef41Sopenharmony_ci                                napi_value object,
121cb0ef41Sopenharmony_ci                                char* expected_message,
131cb0ef41Sopenharmony_ci                                napi_status expected_status,
141cb0ef41Sopenharmony_ci                                napi_status actual_status) {
151cb0ef41Sopenharmony_ci  char napi_message_string[100] = "";
161cb0ef41Sopenharmony_ci  napi_value prop_value;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  if (actual_status != expected_status) {
191cb0ef41Sopenharmony_ci    snprintf(napi_message_string,
201cb0ef41Sopenharmony_ci             sizeof(napi_message_string),
211cb0ef41Sopenharmony_ci             "Invalid status [%d]",
221cb0ef41Sopenharmony_ci             actual_status);
231cb0ef41Sopenharmony_ci  }
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  NODE_API_CALL_RETURN_VOID(
261cb0ef41Sopenharmony_ci      env,
271cb0ef41Sopenharmony_ci      napi_create_string_utf8(
281cb0ef41Sopenharmony_ci          env,
291cb0ef41Sopenharmony_ci          (actual_status == expected_status ? expected_message
301cb0ef41Sopenharmony_ci                                            : napi_message_string),
311cb0ef41Sopenharmony_ci          NAPI_AUTO_LENGTH,
321cb0ef41Sopenharmony_ci          &prop_value));
331cb0ef41Sopenharmony_ci  NODE_API_CALL_RETURN_VOID(
341cb0ef41Sopenharmony_ci      env, napi_set_named_property(env, object, key, prop_value));
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciinline void add_last_status(napi_env env,
381cb0ef41Sopenharmony_ci                            const char* key,
391cb0ef41Sopenharmony_ci                            napi_value return_value) {
401cb0ef41Sopenharmony_ci  napi_value prop_value;
411cb0ef41Sopenharmony_ci  const napi_extended_error_info* p_last_error;
421cb0ef41Sopenharmony_ci  NODE_API_CALL_RETURN_VOID(env, napi_get_last_error_info(env, &p_last_error));
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  NODE_API_CALL_RETURN_VOID(
451cb0ef41Sopenharmony_ci      env,
461cb0ef41Sopenharmony_ci      napi_create_string_utf8(
471cb0ef41Sopenharmony_ci          env,
481cb0ef41Sopenharmony_ci          (p_last_error->error_message == NULL ? "napi_ok"
491cb0ef41Sopenharmony_ci                                               : p_last_error->error_message),
501cb0ef41Sopenharmony_ci          NAPI_AUTO_LENGTH,
511cb0ef41Sopenharmony_ci          &prop_value));
521cb0ef41Sopenharmony_ci  NODE_API_CALL_RETURN_VOID(
531cb0ef41Sopenharmony_ci      env, napi_set_named_property(env, return_value, key, prop_value));
541cb0ef41Sopenharmony_ci}
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci#endif  // JS_NATIVE_API_COMMON_INL_H_
57