11cb0ef41Sopenharmony_ci#include <js_native_api.h>
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci#include "../common.h"
41cb0ef41Sopenharmony_ci#include "test_null.h"
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#define GEN_NULL_CHECK_BINDING(binding_name, output_type, api)            \
71cb0ef41Sopenharmony_ci  static napi_value binding_name(napi_env env, napi_callback_info info) { \
81cb0ef41Sopenharmony_ci    napi_value return_value;                                              \
91cb0ef41Sopenharmony_ci    output_type result;                                                   \
101cb0ef41Sopenharmony_ci    NODE_API_CALL(env, napi_create_object(env, &return_value));           \
111cb0ef41Sopenharmony_ci    add_returned_status(env,                                              \
121cb0ef41Sopenharmony_ci                        "envIsNull",                                      \
131cb0ef41Sopenharmony_ci                        return_value,                                     \
141cb0ef41Sopenharmony_ci                        "Invalid argument",                               \
151cb0ef41Sopenharmony_ci                        napi_invalid_arg,                                 \
161cb0ef41Sopenharmony_ci                        api(NULL, return_value, &result));                \
171cb0ef41Sopenharmony_ci    api(env, NULL, &result);                                              \
181cb0ef41Sopenharmony_ci    add_last_status(env, "valueIsNull", return_value);                    \
191cb0ef41Sopenharmony_ci    api(env, return_value, NULL);                                         \
201cb0ef41Sopenharmony_ci    add_last_status(env, "resultIsNull", return_value);                   \
211cb0ef41Sopenharmony_ci    api(env, return_value, &result);                                      \
221cb0ef41Sopenharmony_ci    add_last_status(env, "inputTypeCheck", return_value);                 \
231cb0ef41Sopenharmony_ci    return return_value;                                                  \
241cb0ef41Sopenharmony_ci  }
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciGEN_NULL_CHECK_BINDING(GetValueBool, bool, napi_get_value_bool)
271cb0ef41Sopenharmony_ciGEN_NULL_CHECK_BINDING(GetValueInt32, int32_t, napi_get_value_int32)
281cb0ef41Sopenharmony_ciGEN_NULL_CHECK_BINDING(GetValueUint32, uint32_t, napi_get_value_uint32)
291cb0ef41Sopenharmony_ciGEN_NULL_CHECK_BINDING(GetValueInt64, int64_t, napi_get_value_int64)
301cb0ef41Sopenharmony_ciGEN_NULL_CHECK_BINDING(GetValueDouble, double, napi_get_value_double)
311cb0ef41Sopenharmony_ciGEN_NULL_CHECK_BINDING(CoerceToBool, napi_value, napi_coerce_to_bool)
321cb0ef41Sopenharmony_ciGEN_NULL_CHECK_BINDING(CoerceToObject, napi_value, napi_coerce_to_object)
331cb0ef41Sopenharmony_ciGEN_NULL_CHECK_BINDING(CoerceToString, napi_value, napi_coerce_to_string)
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci#define GEN_NULL_CHECK_STRING_BINDING(binding_name, arg_type, api)         \
361cb0ef41Sopenharmony_ci  static napi_value binding_name(napi_env env, napi_callback_info info) {  \
371cb0ef41Sopenharmony_ci    napi_value return_value;                                               \
381cb0ef41Sopenharmony_ci    NODE_API_CALL(env, napi_create_object(env, &return_value));            \
391cb0ef41Sopenharmony_ci    arg_type buf1[4];                                                      \
401cb0ef41Sopenharmony_ci    size_t length1 = 3;                                                    \
411cb0ef41Sopenharmony_ci    add_returned_status(env,                                               \
421cb0ef41Sopenharmony_ci                        "envIsNull",                                       \
431cb0ef41Sopenharmony_ci                        return_value,                                      \
441cb0ef41Sopenharmony_ci                        "Invalid argument",                                \
451cb0ef41Sopenharmony_ci                        napi_invalid_arg,                                  \
461cb0ef41Sopenharmony_ci                        api(NULL, return_value, buf1, length1, &length1)); \
471cb0ef41Sopenharmony_ci    arg_type buf2[4];                                                      \
481cb0ef41Sopenharmony_ci    size_t length2 = 3;                                                    \
491cb0ef41Sopenharmony_ci    api(env, NULL, buf2, length2, &length2);                               \
501cb0ef41Sopenharmony_ci    add_last_status(env, "valueIsNull", return_value);                     \
511cb0ef41Sopenharmony_ci    api(env, return_value, NULL, 3, NULL);                                 \
521cb0ef41Sopenharmony_ci    add_last_status(env, "wrongTypeIn", return_value);                     \
531cb0ef41Sopenharmony_ci    napi_value string;                                                     \
541cb0ef41Sopenharmony_ci    NODE_API_CALL(env,                                                     \
551cb0ef41Sopenharmony_ci              napi_create_string_utf8(env,                                 \
561cb0ef41Sopenharmony_ci                                      "Something",                         \
571cb0ef41Sopenharmony_ci                                      NAPI_AUTO_LENGTH,                    \
581cb0ef41Sopenharmony_ci                                      &string));                           \
591cb0ef41Sopenharmony_ci    api(env, string, NULL, 3, NULL);                                       \
601cb0ef41Sopenharmony_ci    add_last_status(env, "bufAndOutLengthIsNull", return_value);           \
611cb0ef41Sopenharmony_ci    return return_value;                                                   \
621cb0ef41Sopenharmony_ci  }
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ciGEN_NULL_CHECK_STRING_BINDING(GetValueStringUtf8,
651cb0ef41Sopenharmony_ci                              char,
661cb0ef41Sopenharmony_ci                              napi_get_value_string_utf8)
671cb0ef41Sopenharmony_ciGEN_NULL_CHECK_STRING_BINDING(GetValueStringLatin1,
681cb0ef41Sopenharmony_ci                              char,
691cb0ef41Sopenharmony_ci                              napi_get_value_string_latin1)
701cb0ef41Sopenharmony_ciGEN_NULL_CHECK_STRING_BINDING(GetValueStringUtf16,
711cb0ef41Sopenharmony_ci                              char16_t,
721cb0ef41Sopenharmony_ci                              napi_get_value_string_utf16)
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_civoid init_test_null(napi_env env, napi_value exports) {
751cb0ef41Sopenharmony_ci  napi_value test_null;
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  const napi_property_descriptor test_null_props[] = {
781cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("getValueBool", GetValueBool),
791cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("getValueInt32", GetValueInt32),
801cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("getValueUint32", GetValueUint32),
811cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("getValueInt64", GetValueInt64),
821cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("getValueDouble", GetValueDouble),
831cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("coerceToBool", CoerceToBool),
841cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("coerceToObject", CoerceToObject),
851cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("coerceToString", CoerceToString),
861cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("getValueStringUtf8", GetValueStringUtf8),
871cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("getValueStringLatin1", GetValueStringLatin1),
881cb0ef41Sopenharmony_ci    DECLARE_NODE_API_PROPERTY("getValueStringUtf16", GetValueStringUtf16),
891cb0ef41Sopenharmony_ci  };
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci  NODE_API_CALL_RETURN_VOID(env, napi_create_object(env, &test_null));
921cb0ef41Sopenharmony_ci  NODE_API_CALL_RETURN_VOID(env, napi_define_properties(
931cb0ef41Sopenharmony_ci      env, test_null, sizeof(test_null_props) / sizeof(*test_null_props),
941cb0ef41Sopenharmony_ci      test_null_props));
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  const napi_property_descriptor test_null_set = {
971cb0ef41Sopenharmony_ci    "testNull", NULL, NULL, NULL, NULL, test_null, napi_enumerable, NULL
981cb0ef41Sopenharmony_ci  };
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci  NODE_API_CALL_RETURN_VOID(env,
1011cb0ef41Sopenharmony_ci      napi_define_properties(env, exports, 1, &test_null_set));
1021cb0ef41Sopenharmony_ci}
103