• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef OHOS_JS_UTIL_H
16 #define OHOS_JS_UTIL_H
17 #include <cstdint>
18 #include <map>
19 #include <string>
20 #include <variant>
21 #include <vector>
22 
23 #include "napi/native_api.h"
24 #include "napi/native_node_api.h"
25 
26 namespace OHOS::ObjectStore {
27 class JSUtil final {
28 public:
29     /* napi_value <-> bool */
30     static napi_status GetValue(napi_env env, napi_value in, bool &out);
31     static napi_status SetValue(napi_env env, const bool &in, napi_value &out);
32 
33     /* napi_value <-> double */
34     static napi_status GetValue(napi_env env, napi_value in, double &out);
35     static napi_status SetValue(napi_env env, const double &in, napi_value &out);
36 
37     /* napi_value <-> std::string */
38     static napi_status GetValue(napi_env env, napi_value in, std::string &out);
39     static napi_status SetValue(napi_env env, const std::string &in, napi_value &out);
40 
41     /* napi_value <-> std::vector<std::string> */
42     static napi_status GetValue(napi_env env, napi_value in, std::vector<std::string> &out);
43     static napi_status SetValue(napi_env env, const std::vector<std::string> &in, napi_value &out);
44 
45     /* napi_value <-> std::vector<uint8_t> */
46     static napi_status GetValue(napi_env env, napi_value in, std::vector<uint8_t> &out);
47     static napi_status SetValue(napi_env env, const std::vector<uint8_t> &in, napi_value &out);
48 
49     static void GenerateNapiError(napi_env env, int32_t status, int32_t &errCode, std::string &errMessage);
50 };
51 
52 #define NAPI_ASSERT_ERRCODE(env, assertion, version, err)                                                       \
53     do {                                                                                                        \
54         if (!(assertion)) {                                                                                     \
55             if ((version) >= 9) {                                                                               \
56                 napi_throw_error((env), std::to_string((err)->GetCode()).c_str(), (err)->GetMessage().c_str()); \
57             }                                                                                                   \
58             return nullptr;                                                                                     \
59         }                                                                                                       \
60     } while (0)
61 
62 #define CHECH_STATUS_ERRCODE(env, condition, err)                                                           \
63     do {                                                                                                    \
64         if (!(condition)) {                                                                                 \
65             napi_throw_error((env), std::to_string((err)->GetCode()).c_str(), (err)->GetMessage().c_str()); \
66             return nullptr;                                                                                 \
67         }                                                                                                   \
68     } while (0)
69 
70 #define CHECH_STATUS_RETURN_VOID(env, condition, ctxt, info) \
71     do {                                                     \
72         if (!(condition)) {                                  \
73             LOG_ERROR(info);                                 \
74             (ctxt)->status = napi_generic_failure;           \
75             (ctxt)->message = std::string(info);             \
76             return;                                          \
77         }                                                    \
78     } while (0)
79 
80 #define LOG_ERROR_RETURN(condition, message, retVal)             \
81     do {                                                         \
82         if (!(condition)) {                                      \
83             LOG_ERROR("test (" #condition ") failed: " message); \
84             return retVal;                                       \
85         }                                                        \
86     } while (0)
87 
88 #define LOG_ERROR_RETURN_VOID(condition, message)                \
89     do {                                                         \
90         if (!(condition)) {                                      \
91             LOG_ERROR("test (" #condition ") failed: " message); \
92             return;                                              \
93         }                                                        \
94     } while (0)
95 } // namespace OHOS::ObjectStore
96 #endif // OHOS_JS_UTIL_H
97