• 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 <string>
20 #include <vector>
21 
22 #include "napi/native_api.h"
23 #include "napi/native_common.h"
24 #include "uv.h"
25 
26 namespace OHOS::NetStack::NapiUtils {
27 napi_valuetype GetValueType(napi_env env, napi_value value);
28 
29 /* named property */
30 bool HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
31 
32 napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
33 
34 void SetNamedProperty(napi_env env, napi_value object, const std::string &name, napi_value value);
35 
36 std::vector<std::string> GetPropertyNames(napi_env env, napi_value object);
37 
38 /* UINT32 */
39 napi_value CreateUint32(napi_env env, uint32_t code);
40 
41 uint32_t GetUint32FromValue(napi_env env, napi_value value);
42 
43 uint32_t GetUint32Property(napi_env env, napi_value object, const std::string &propertyName);
44 
45 void SetUint32Property(napi_env env, napi_value object, const std::string &name, uint32_t value);
46 
47 /* INT32 */
48 napi_value CreateInt32(napi_env env, int32_t code);
49 
50 int32_t GetInt32FromValue(napi_env env, napi_value value);
51 
52 int32_t GetInt32Property(napi_env env, napi_value object, const std::string &propertyName);
53 
54 void SetInt32Property(napi_env env, napi_value object, const std::string &name, int32_t value);
55 
56 /* String UTF8 */
57 napi_value CreateStringUtf8(napi_env env, const std::string &str);
58 
59 std::string GetStringFromValueUtf8(napi_env env, napi_value value);
60 
61 std::string GetStringPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName);
62 
63 void SetStringPropertyUtf8(napi_env env, napi_value object, const std::string &name, const std::string &value);
64 
65 /* array buffer */
66 bool ValueIsArrayBuffer(napi_env env, napi_value value);
67 
68 void *GetInfoFromArrayBufferValue(napi_env env, napi_value value, size_t *length);
69 
70 napi_value CreateArrayBuffer(napi_env env, size_t length, void **data);
71 
72 /* object */
73 napi_value CreateObject(napi_env env);
74 
75 /* undefined */
76 napi_value GetUndefined(napi_env env);
77 
78 /* function */
79 napi_value CallFunction(napi_env env, napi_value recv, napi_value func, size_t argc, const napi_value *argv);
80 
81 napi_value CreateFunction(napi_env env, const std::string &name, napi_callback func, void *arg);
82 
83 /* reference */
84 napi_ref CreateReference(napi_env env, napi_value callback);
85 
86 napi_value GetReference(napi_env env, napi_ref callbackRef);
87 
88 void DeleteReference(napi_env env, napi_ref callbackRef);
89 
90 /* boolean */
91 bool GetBooleanProperty(napi_env env, napi_value object, const std::string &propertyName);
92 
93 void SetBooleanProperty(napi_env env, napi_value object, const std::string &name, bool value);
94 
95 napi_value GetBoolean(napi_env env, bool value);
96 
97 /* define properties */
98 void DefineProperties(napi_env env,
99                       napi_value object,
100                       const std::initializer_list<napi_property_descriptor> &properties);
101 
102 /* JSON */
103 napi_value JsonStringify(napi_env env, napi_value object);
104 
105 napi_value JsonParse(napi_env env, napi_value str);
106 
107 /* libuv */
108 void CreateUvQueueWork(napi_env env, void *data, void(Handler)(uv_work_t *, int status));
109 
110 /* scope */
111 napi_handle_scope OpenScope(napi_env env);
112 
113 void CloseScope(napi_env env, napi_handle_scope scope);
114 } // namespace OHOS::NetStack::NapiUtils
115 
116 #endif /* COMMUNICATIONNETSTACK_NETSTACK_NAPI_UTILS_H */
117