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 template<typename T> GetValue(napi_env env,napi_value in,std::vector<T> & items)38 static bool GetValue(napi_env env, napi_value in, std::vector<T> &items) 39 { 40 uint32_t len = 0; 41 napi_get_array_length(env, in, &len); 42 items.resize(len); 43 for (uint32_t i = 0; i < len; i++) { 44 napi_value item = nullptr; 45 auto status = napi_get_element(env, in, i, &item); 46 T buff{}; 47 if (status != napi_ok || !GetValue(env, item, buff)) { 48 return false; 49 } 50 items[i] = std::move(buff); 51 } 52 return true; 53 } 54 55 // native to js 56 static napi_value GetValue(napi_env env, napi_value in); 57 static napi_value GetValue(napi_env env, const std::string &in); 58 static napi_value GetValue(napi_env env, int32_t in); 59 static napi_value GetValue(napi_env env, uint32_t in); 60 static napi_value GetValue(napi_env env, int64_t in); 61 static napi_value GetValue(napi_env env, bool in); 62 template<typename T> GetValue(napi_env env,const std::vector<T> & items)63 static napi_value GetValue(napi_env env, const std::vector<T> &items) 64 { 65 napi_value array = nullptr; 66 auto status = napi_create_array(env, &array); 67 if (status != napi_ok) { 68 return nullptr; 69 } 70 uint32_t index = 0; 71 for (const T &item : items) { 72 auto itemValue = GetValue(env, item); 73 if (itemValue == nullptr) { 74 return nullptr; 75 } 76 status = napi_set_element(env, array, index++, itemValue); 77 if (status != napi_ok) { 78 return nullptr; 79 } 80 } 81 return array; 82 } 83 class Object { 84 public: 85 template<typename T> WriteProperty(napi_env env,napi_value object,const std::string & property,const T & value)86 static bool WriteProperty(napi_env env, napi_value object, const std::string &property, const T &value) 87 { 88 return napi_set_named_property(env, object, property.c_str(), GetValue(env, value)) == napi_ok; 89 } 90 template<typename T> ReadProperty(napi_env env,napi_value object,const std::string & property,T & value)91 static bool ReadProperty(napi_env env, napi_value object, const std::string &property, T &value) 92 { 93 napi_value propValue = nullptr; 94 napi_get_named_property(env, object, property.c_str(), &propValue); 95 return GetValue(env, propValue, value); 96 } 97 }; 98 class Const { 99 public: Null(napi_env env)100 static napi_value Null(napi_env env) 101 { 102 napi_value value = nullptr; 103 napi_get_null(env, &value); 104 return value; 105 } Undefined(napi_env env)106 static napi_value Undefined(napi_env env) 107 { 108 napi_value value = nullptr; 109 napi_get_undefined(env, &value); 110 return value; 111 } 112 }; 113 class ScopeGuard { 114 public: ScopeGuard(napi_env env)115 explicit ScopeGuard(napi_env env) : env_(env), scope_(nullptr) 116 { 117 napi_open_handle_scope(env_, &scope_); 118 } ~ScopeGuard()119 ~ScopeGuard() 120 { 121 napi_close_handle_scope(env_, scope_); 122 } 123 124 private: 125 napi_env env_; 126 napi_handle_scope scope_; 127 }; 128 }; 129 } // namespace MiscServices 130 } // namespace OHOS 131 #endif // OHOS_JS_UTIL_H 132