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