• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace NapiUtil {
28 bool IsNull(const napi_env env, const napi_value value);
29 bool IsBoolean(const napi_env env, const napi_value value);
30 bool IsNumber(const napi_env env, const napi_value value);
31 bool IsString(const napi_env env, const napi_value value);
32 bool IsObject(const napi_env env, const napi_value value);
33 bool IsFunction(const napi_env env, const napi_value value);
34 bool IsArray(const napi_env env, const napi_value value);
35 bool IsArrayType(const napi_env env, const napi_value value, napi_valuetype type);
36 bool HasProperty(const napi_env env, const napi_value object, const std::string& name);
37 
38 bool GetBoolean(const napi_env env, const napi_value value);
39 void GetBooleans(const napi_env env, const napi_value arr, std::vector<bool>& bools);
40 int32_t GetInt32(const napi_env env, const napi_value value);
41 void GetInt32s(const napi_env env, const napi_value arr, std::vector<int32_t>& ints);
42 double GetDouble(const napi_env env, const napi_value value);
43 void GetDoubles(const napi_env env, const napi_value arr, std::vector<double>& doubles);
44 std::string GetString(const napi_env env, const napi_value value, size_t bufsize = 100); // 100 means default size
45 void GetStrings(const napi_env env, const napi_value arr, std::vector<std::string>& strs, size_t bufsize);
46 napi_valuetype GetType(const napi_env env, const napi_value value);
47 napi_valuetype GetArrayType(const napi_env env, const napi_value value);
48 uint32_t GetArrayLength(const napi_env env, const napi_value arr);
49 napi_value GetElement(const napi_env env, const napi_value arr, uint32_t index);
50 napi_value GetProperty(const napi_env env, const napi_value object, const std::string& name);
51 void GetPropertyNames(const napi_env env, const napi_value object, std::vector<std::string>& names);
52 napi_value GetReferenceValue(const napi_env env, const napi_ref funcRef);
53 size_t GetCbInfo(const napi_env env, napi_callback_info info, napi_value argv[], size_t argc = 4); // 4: default size
54 
55 napi_ref CreateReference(const napi_env env, const napi_value func);
56 napi_value CreateNull(const napi_env env);
57 napi_value CreateUndefined(const napi_env env);
58 napi_value CreateBoolean(const napi_env env, bool bValue);
59 napi_value CreateInt32(const napi_env env, int32_t num);
60 napi_value CreateString(const napi_env env, const std::string& str);
61 napi_value CreateStrings(const napi_env env, const std::vector<std::string>& strs);
62 napi_value CreateObject(const napi_env env);
63 napi_value CreateObject(const napi_env env, const std::string& key, const napi_value value);
64 napi_value CreateArray(const napi_env env);
65 
66 void SetElement(const napi_env env, const napi_value obj, uint32_t index, const napi_value value);
67 void SetNamedProperty(const napi_env env, const napi_value obj, const std::string& key, const napi_value value);
68 std::string ConvertToString(const napi_env env, const napi_value value);
69 
70 void ThrowError(napi_env env, int code, const std::string& msg, bool isThrow = true);
71 napi_value CreateError(napi_env env, int code, const std::string& msg);
72 std::string CreateErrMsg(const std::string name);
73 std::string CreateErrMsg(const std::string name, const std::string& type);
74 std::string CreateErrMsg(const std::string name, const napi_valuetype type);
75 } // namespace NapiUtil
76 } // namespace HiviewDFX
77 } // namespace OHOS
78 #endif // HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_UTIL_H
79