1 /*
2  * Copyright (c) 2022-2024 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 HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_UTIL_H
17 #define HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_UTIL_H
18 
19 #include <cstdint>
20 #include <string>
21 #include <unordered_set>
22 
23 #include "json/json.h"
24 #include "napi/native_api.h"
25 #include "napi/native_node_api.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 class AppEventPack;
30 namespace NapiUtil {
31 bool IsNull(const napi_env env, const napi_value value);
32 bool IsBoolean(const napi_env env, const napi_value value);
33 bool IsNumber(const napi_env env, const napi_value value);
34 bool IsString(const napi_env env, const napi_value value);
35 bool IsObject(const napi_env env, const napi_value value);
36 bool IsFunction(const napi_env env, const napi_value value);
37 bool IsArray(const napi_env env, const napi_value value);
38 bool IsArrayType(const napi_env env, const napi_value value, napi_valuetype type);
39 bool HasProperty(const napi_env env, const napi_value object, const std::string& name);
40 
41 bool GetBoolean(const napi_env env, const napi_value value);
42 void GetBooleans(const napi_env env, const napi_value arr, std::vector<bool>& bools);
43 int32_t GetInt32(const napi_env env, const napi_value value);
44 int64_t GetInt64(const napi_env env, const napi_value value);
45 void GetInt32s(const napi_env env, const napi_value arr, std::vector<int32_t>& ints);
46 double GetDouble(const napi_env env, const napi_value value);
47 void GetDoubles(const napi_env env, const napi_value arr, std::vector<double>& doubles);
48 std::string GetString(const napi_env env, const napi_value value);
49 void GetStrings(const napi_env env, const napi_value arr, std::vector<std::string>& strs);
50 void GetStringsToSet(
51     const napi_env env, const napi_value arr, std::unordered_set<std::string>& strs);
52 napi_valuetype GetType(const napi_env env, const napi_value value);
53 napi_valuetype GetArrayType(const napi_env env, const napi_value value);
54 uint32_t GetArrayLength(const napi_env env, const napi_value arr);
55 napi_value GetElement(const napi_env env, const napi_value arr, uint32_t index);
56 napi_value GetProperty(const napi_env env, const napi_value object, const std::string& name);
57 void GetPropertyNames(const napi_env env, const napi_value object, std::vector<std::string>& names);
58 napi_value GetReferenceValue(const napi_env env, const napi_ref funcRef);
59 size_t GetCbInfo(const napi_env env, napi_callback_info info, napi_value argv[], size_t argc = 4); // 4: default size
60 
61 napi_ref CreateReference(const napi_env env, const napi_value func);
62 napi_value CreateNull(const napi_env env);
63 napi_value CreateUndefined(const napi_env env);
64 napi_value CreateBoolean(const napi_env env, bool bValue);
65 napi_value CreateInt32(const napi_env env, int32_t num);
66 napi_value CreateInt64(const napi_env env, int64_t num);
67 napi_value CreateString(const napi_env env, const std::string& str);
68 napi_value CreateStrings(const napi_env env, const std::vector<std::string>& strs);
69 napi_value CreateObject(const napi_env env);
70 napi_value CreateObject(const napi_env env, const std::string& key, const napi_value value);
71 napi_value CreateArray(const napi_env env);
72 napi_value CreateDouble(const napi_env env, double dValue);
73 
74 void SetElement(const napi_env env, const napi_value obj, uint32_t index, const napi_value value);
75 void SetNamedProperty(const napi_env env, const napi_value obj, const std::string& key, const napi_value value);
76 std::string ConvertToString(const napi_env env, const napi_value value);
77 
78 void ThrowError(napi_env env, int code, const std::string& msg, bool isThrow = true);
79 napi_value CreateError(napi_env env, int code, const std::string& msg);
80 std::string CreateErrMsg(const std::string& name);
81 std::string CreateErrMsg(const std::string& name, const std::string& type);
82 std::string CreateErrMsg(const std::string& name, const napi_valuetype type);
83 
84 napi_value CreateBaseValueByJson(const napi_env env, const Json::Value& jsonValue);
85 napi_value CreateValueByJson(napi_env env, const Json::Value& jsonValue);
86 napi_value CreateValueByJsonStr(napi_env env, const std::string& jsonStr);
87 napi_value CreateEventInfo(napi_env env, std::shared_ptr<AppEventPack> event);
88 napi_value CreateEventInfoArray(napi_env env, const std::vector<std::shared_ptr<AppEventPack>>& events);
89 napi_value CreateEventGroups(napi_env env, const std::vector<std::shared_ptr<AppEventPack>>& events);
90 } // namespace NapiUtil
91 } // namespace HiviewDFX
92 } // namespace OHOS
93 #endif // HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_UTIL_H
94