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 <variant> 20 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 24 namespace OHOS::ObjectStore { 25 class JSUtil final { 26 public: 27 /* napi_value <-> bool */ 28 static napi_status GetValue(napi_env env, napi_value in, bool &out); 29 static napi_status SetValue(napi_env env, const bool &in, napi_value &out); 30 31 /* napi_value <-> double */ 32 static napi_status GetValue(napi_env env, napi_value in, double &out); 33 static napi_status SetValue(napi_env env, const double &in, napi_value &out); 34 35 /* napi_value <-> std::string */ 36 static napi_status GetValue(napi_env env, napi_value in, std::string &out); 37 static napi_status SetValue(napi_env env, const std::string &in, napi_value &out); 38 39 /* napi_value <-> std::vector<std::string> */ 40 static napi_status GetValue(napi_env env, napi_value in, std::vector<std::string> &out); 41 static napi_status SetValue(napi_env env, const std::vector<std::string> &in, napi_value &out); 42 43 /* napi_value <-> std::vector<uint8_t> */ 44 static napi_status GetValue(napi_env env, napi_value in, std::vector<uint8_t> &out); 45 static napi_status SetValue(napi_env env, const std::vector<uint8_t> &in, napi_value &out); 46 }; 47 48 #define LOG_ERROR_RETURN(condition, message, retVal) \ 49 do { \ 50 if (!(condition)) { \ 51 LOG_ERROR("test (" #condition ") failed: " message); \ 52 return retVal; \ 53 } \ 54 } while (0) 55 56 #define LOG_ERROR_RETURN_VOID(condition, message) \ 57 do { \ 58 if (!(condition)) { \ 59 LOG_ERROR("test (" #condition ") failed: " message); \ 60 return; \ 61 } \ 62 } while (0) 63 } // namespace OHOS::ObjectStore 64 #endif // OHOS_JS_UTIL_H 65