1 /* 2 * Copyright (c) 2023 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 16 #ifndef OHOS_JS_UTIL_H 17 #define OHOS_JS_UTIL_H 18 #include <string> 19 #include <vector> 20 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 namespace OHOS { 24 namespace MiscServices { 25 class JsUtil { 26 public: 27 static napi_valuetype GetType(napi_env env, napi_value in); 28 static bool HasProperty(napi_env env, napi_value object, const std::string &property); 29 // js to native 30 static bool GetValue(napi_env env, napi_value in, std::string &out); 31 static bool GetValue(napi_env env, napi_value in, std::u16string &out); 32 static bool GetValue(napi_env env, napi_value in, int32_t &out); 33 static bool GetValue(napi_env env, napi_value in, uint32_t &out); 34 static bool GetValue(napi_env env, napi_value in, int64_t &out); 35 static bool GetValue(napi_env env, napi_value in, bool &out); 36 static bool GetValue(napi_env env, napi_value in, double &out); 37 static bool GetValueU16String(napi_env env, napi_value in, std::u16string &out); 38 template<typename T> GetValue(napi_env env,napi_value in,std::vector<T> & items)39 static bool GetValue(napi_env env, napi_value in, std::vector<T> &items) 40 { 41 uint32_t len = 0; 42 napi_get_array_length(env, in, &len); 43 items.resize(len); 44 for (uint32_t i = 0; i < len; i++) { 45 napi_value item = nullptr; 46 auto status = napi_get_element(env, in, i, &item); 47 T buff{}; 48 if (status != napi_ok || !GetValue(env, item, buff)) { 49 return false; 50 } 51 items[i] = std::move(buff); 52 } 53 return true; 54 } 55 56 // native to js 57 static napi_value GetValue(napi_env env, napi_value in); 58 static napi_value GetValue(napi_env env, const std::string &in); 59 static napi_value GetValue(napi_env env, int32_t in); 60 static napi_value GetValue(napi_env env, uint32_t in); 61 static napi_value GetValue(napi_env env, int64_t in); 62 static napi_value GetValue(napi_env env, bool in); 63 static napi_value GetValueU16String(napi_env env, const std::u16string &in); 64 template<typename T> GetValue(napi_env env,const std::vector<T> & items)65 static napi_value GetValue(napi_env env, const std::vector<T> &items) 66 { 67 napi_value array = nullptr; 68 auto status = napi_create_array(env, &array); 69 if (status != napi_ok) { 70 return nullptr; 71 } 72 uint32_t index = 0; 73 for (const T &item : items) { 74 auto itemValue = GetValue(env, item); 75 if (itemValue == nullptr) { 76 return nullptr; 77 } 78 status = napi_set_element(env, array, index++, itemValue); 79 if (status != napi_ok) { 80 return nullptr; 81 } 82 } 83 return array; 84 } 85 class Object { 86 public: 87 template<typename T> WriteProperty(napi_env env,napi_value object,const std::string & property,const T & value)88 static bool WriteProperty(napi_env env, napi_value object, const std::string &property, const T &value) 89 { 90 return napi_set_named_property(env, object, property.c_str(), GetValue(env, value)) == napi_ok; 91 } 92 template<typename T> ReadProperty(napi_env env,napi_value object,const std::string & property,T & value)93 static bool ReadProperty(napi_env env, napi_value object, const std::string &property, T &value) 94 { 95 napi_value propValue = nullptr; 96 napi_get_named_property(env, object, property.c_str(), &propValue); 97 return GetValue(env, propValue, value); 98 } 99 ReadPropertyU16String(napi_env env,napi_value object,const std::string & property,std::u16string & value)100 static bool ReadPropertyU16String(napi_env env, napi_value object, const std::string &property, 101 std::u16string &value) 102 { 103 napi_value propValue = nullptr; 104 napi_status status = napi_get_named_property(env, object, property.c_str(), &propValue); 105 if (status != napi_ok) { 106 value = u""; 107 return false; 108 } 109 return GetValueU16String(env, propValue, value); 110 } 111 WritePropertyU16String(napi_env env,napi_value object,const std::string & property,const std::u16string & value)112 static bool WritePropertyU16String(napi_env env, napi_value object, const std::string &property, 113 const std::u16string &value) 114 { 115 return napi_set_named_property(env, object, property.c_str(), GetValueU16String(env, value)) == napi_ok; 116 } 117 }; 118 class Const { 119 public: Null(napi_env env)120 static napi_value Null(napi_env env) 121 { 122 napi_value value = nullptr; 123 napi_get_null(env, &value); 124 return value; 125 } Undefined(napi_env env)126 static napi_value Undefined(napi_env env) 127 { 128 napi_value value = nullptr; 129 napi_get_undefined(env, &value); 130 return value; 131 } 132 }; 133 class ScopeGuard { 134 public: ScopeGuard(napi_env env)135 explicit ScopeGuard(napi_env env) : env_(env), scope_(nullptr) 136 { 137 napi_open_handle_scope(env_, &scope_); 138 } ~ScopeGuard()139 ~ScopeGuard() 140 { 141 napi_close_handle_scope(env_, scope_); 142 } 143 144 private: 145 napi_env env_; 146 napi_handle_scope scope_; 147 }; 148 }; 149 } // namespace MiscServices 150 } // namespace OHOS 151 #endif // OHOS_JS_UTIL_H 152