• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 COMMUNICATIONNETSTACK_NETSTACK_NAPI_UTILS_H
17 #define COMMUNICATIONNETSTACK_NETSTACK_NAPI_UTILS_H
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <iosfwd>
22 #include <vector>
23 
24 #include "initializer_list"
25 #include "napi/native_api.h"
26 #include "uv.h"
27 
28 namespace OHOS::NetStack::NapiUtils {
29 static constexpr int NETSTACK_NAPI_INTERNAL_ERROR = 2300002;
30 
31 napi_valuetype GetValueType(napi_env env, napi_value value);
32 
33 /* named property */
34 bool HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
35 
36 napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
37 
38 void SetNamedProperty(napi_env env, napi_value object, const std::string &name, napi_value value);
39 
40 std::vector<std::string> GetPropertyNames(napi_env env, napi_value object);
41 
42 /* UINT32 */
43 napi_value CreateUint32(napi_env env, uint32_t code);
44 
45 napi_value CreateUint64(napi_env env, uint64_t code);
46 
47 uint32_t GetUint32FromValue(napi_env env, napi_value value);
48 
49 uint32_t GetUint32Property(napi_env env, napi_value object, const std::string &propertyName);
50 
51 void SetUint32Property(napi_env env, napi_value object, const std::string &name, uint32_t value);
52 
53 void SetUint64Property(napi_env env, napi_value object, const std::string &name, uint64_t value);
54 
55 /* INT32 */
56 napi_value CreateInt32(napi_env env, int32_t code);
57 
58 int32_t GetInt32FromValue(napi_env env, napi_value value);
59 
60 int32_t GetInt32Property(napi_env env, napi_value object, const std::string &propertyName);
61 
62 void SetInt32Property(napi_env env, napi_value object, const std::string &name, int32_t value);
63 
64 /* String UTF8 */
65 napi_value CreateStringUtf8(napi_env env, const std::string &str);
66 
67 std::string GetStringFromValueUtf8(napi_env env, napi_value value);
68 
69 std::string GetStringPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName);
70 
71 void SetStringPropertyUtf8(napi_env env, napi_value object, const std::string &name, const std::string &value);
72 
73 /* array buffer */
74 bool ValueIsArrayBuffer(napi_env env, napi_value value);
75 
76 void *GetInfoFromArrayBufferValue(napi_env env, napi_value value, size_t *length);
77 
78 napi_value CreateArrayBuffer(napi_env env, size_t length, void **data);
79 
80 /* object */
81 napi_value CreateObject(napi_env env);
82 
83 /* undefined */
84 napi_value GetUndefined(napi_env env);
85 
86 /* function */
87 napi_value CallFunction(napi_env env, napi_value recv, napi_value func, size_t argc, const napi_value *argv);
88 
89 napi_value CreateFunction(napi_env env, const std::string &name, napi_callback func, void *arg);
90 
91 /* reference */
92 napi_ref CreateReference(napi_env env, napi_value callback);
93 
94 napi_value GetReference(napi_env env, napi_ref callbackRef);
95 
96 void DeleteReference(napi_env env, napi_ref callbackRef);
97 
98 /* boolean */
99 bool GetBooleanProperty(napi_env env, napi_value object, const std::string &propertyName);
100 
101 void SetBooleanProperty(napi_env env, napi_value object, const std::string &name, bool value);
102 
103 napi_value GetBoolean(napi_env env, bool value);
104 
105 bool GetBooleanFromValue(napi_env env, napi_value value);
106 
107 /* define properties */
108 void DefineProperties(napi_env env, napi_value object,
109                       const std::initializer_list<napi_property_descriptor> &properties);
110 
111 /* array */
112 napi_value CreateArray(napi_env env, size_t length);
113 void SetArrayElement(napi_env env, napi_value array, uint32_t index, napi_value value);
114 bool IsArray(napi_env env, napi_value value);
115 uint32_t GetArrayLength(napi_env env, napi_value arr);
116 napi_value GetArrayElement(napi_env env, napi_value arr, uint32_t index);
117 
118 /* JSON */
119 napi_value JsonStringify(napi_env env, napi_value object);
120 
121 napi_value JsonParse(napi_env env, napi_value str);
122 
123 /* libuv */
124 void CreateUvQueueWork(napi_env env, void *data, void(handler)(uv_work_t *, int status));
125 
126 void CreateUvQueueWorkEnhanced(napi_env env, void *data, void (*handler)(napi_env env, napi_status status, void *data));
127 
128 /* scope */
129 napi_handle_scope OpenScope(napi_env env);
130 
131 void CloseScope(napi_env env, napi_handle_scope scope);
132 
133 /* error */
134 napi_value CreateErrorMessage(napi_env env, int32_t errorCodeconst, const std::string &errorMessage);
135 } // namespace OHOS::NetStack::NapiUtils
136 
137 #endif /* COMMUNICATIONNETSTACK_NETSTACK_NAPI_UTILS_H */
138