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