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