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 int64_t GetInt64FromValue(napi_env env, napi_value value); 48 49 int64_t GetInt64Property(napi_env env, napi_value object, const std::string &propertyName); 50 51 uint32_t GetUint32FromValue(napi_env env, napi_value value); 52 53 uint32_t GetUint32Property(napi_env env, napi_value object, const std::string &propertyName); 54 55 void SetUint32Property(napi_env env, napi_value object, const std::string &name, uint32_t value); 56 57 void SetUint64Property(napi_env env, napi_value object, const std::string &name, uint64_t value); 58 59 /* INT32 */ 60 napi_value CreateInt32(napi_env env, int32_t code); 61 62 int32_t GetInt32FromValue(napi_env env, napi_value value); 63 64 int32_t GetInt32Property(napi_env env, napi_value object, const std::string &propertyName); 65 66 void SetInt32Property(napi_env env, napi_value object, const std::string &name, int32_t value); 67 68 void SetDoubleProperty(napi_env env, napi_value object, const std::string &name, double value); 69 70 /* String UTF8 */ 71 napi_value CreateStringUtf8(napi_env env, const std::string &str); 72 73 std::string GetStringFromValueUtf8(napi_env env, napi_value value); 74 75 std::string GetStringPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName); 76 77 std::string NapiValueToString(napi_env env, napi_value value); 78 79 void SetStringPropertyUtf8(napi_env env, napi_value object, const std::string &name, const std::string &value); 80 81 std::vector<std::string> GetDnsServers(napi_env env, napi_value object, const std::string &propertyName); 82 83 /* array buffer */ 84 bool ValueIsArrayBuffer(napi_env env, napi_value value); 85 86 void *GetInfoFromArrayBufferValue(napi_env env, napi_value value, size_t *length); 87 88 napi_value CreateArrayBuffer(napi_env env, size_t length, void **data); 89 90 /* object */ 91 napi_value CreateObject(napi_env env); 92 93 /* undefined */ 94 napi_value GetUndefined(napi_env env); 95 96 /* function */ 97 napi_value CallFunction(napi_env env, napi_value recv, napi_value func, size_t argc, const napi_value *argv); 98 99 napi_value CreateFunction(napi_env env, const std::string &name, napi_callback func, void *arg); 100 101 /* reference */ 102 napi_ref CreateReference(napi_env env, napi_value callback); 103 104 napi_value GetReference(napi_env env, napi_ref callbackRef); 105 106 void DeleteReference(napi_env env, napi_ref callbackRef); 107 108 /* boolean */ 109 bool GetBooleanProperty(napi_env env, napi_value object, const std::string &propertyName); 110 111 void SetBooleanProperty(napi_env env, napi_value object, const std::string &name, bool value); 112 113 napi_value GetBoolean(napi_env env, bool value); 114 115 bool GetBooleanFromValue(napi_env env, napi_value value); 116 117 /* define properties */ 118 void DefineProperties(napi_env env, napi_value object, 119 const std::initializer_list<napi_property_descriptor> &properties); 120 121 /* array */ 122 napi_value CreateArray(napi_env env, size_t length); 123 void SetArrayElement(napi_env env, napi_value array, uint32_t index, napi_value value); 124 bool IsArray(napi_env env, napi_value value); 125 void SetArrayProperty(napi_env env, napi_value object, const std::string &name, napi_value value); 126 uint32_t GetArrayLength(napi_env env, napi_value arr); 127 napi_value GetArrayElement(napi_env env, napi_value arr, uint32_t index); 128 129 /* JSON */ 130 napi_value JsonStringify(napi_env env, napi_value object); 131 132 napi_value JsonParse(napi_env env, const std::string &inStr); 133 134 /* libuv */ 135 void CreateUvQueueWork(napi_env env, void *data, void(handler)(uv_work_t *, int status)); 136 137 void CreateUvQueueWorkEnhanced(napi_env env, void *data, void (*handler)(napi_env env, napi_status status, void *data)); 138 139 /* scope */ 140 napi_handle_scope OpenScope(napi_env env); 141 142 void CloseScope(napi_env env, napi_handle_scope scope); 143 144 /* error */ 145 napi_value CreateErrorMessage(napi_env env, int32_t errorCodeconst, const std::string &errorMessage); 146 147 napi_value GetGlobal(napi_env env); 148 } // namespace OHOS::NetStack::NapiUtils 149 150 #endif /* COMMUNICATIONNETSTACK_NETSTACK_NAPI_UTILS_H */ 151