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 COMMUNICATIONNETMANAGER_BASE_NETMANAGER_BASE_NAPI_UTILS_H 17 #define COMMUNICATIONNETMANAGER_BASE_NETMANAGER_BASE_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 { 27 namespace NetManagerStandard { 28 namespace NapiUtils { 29 napi_valuetype GetValueType(napi_env env, napi_value value); 30 31 /* named property */ 32 bool HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName); 33 napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName); 34 void SetNamedProperty(napi_env env, napi_value object, const std::string &name, napi_value value); 35 std::vector<std::string> GetPropertyNames(napi_env env, napi_value object); 36 37 /* UINT32 */ 38 napi_value CreateUint32(napi_env env, uint32_t code); 39 uint32_t GetUint32FromValue(napi_env env, napi_value value); 40 uint32_t GetUint32Property(napi_env env, napi_value object, const std::string &propertyName); 41 void SetUint32Property(napi_env env, napi_value object, const std::string &name, uint32_t value); 42 43 /* INT32 */ 44 napi_value CreateInt32(napi_env env, int32_t code); 45 int32_t GetInt32FromValue(napi_env env, napi_value value); 46 int32_t GetInt32Property(napi_env env, napi_value object, const std::string &propertyName); 47 void SetInt32Property(napi_env env, napi_value object, const std::string &name, int32_t value); 48 49 /* INT64 */ 50 napi_value CreateInt64(napi_env env, int64_t code); 51 int64_t GetInt64Property(napi_env env, napi_value object, const std::string &propertyName); 52 int64_t GetInt64FromValue(napi_env env, napi_value value); 53 void SetInt64Property(napi_env env, napi_value object, const std::string &name, int64_t value); 54 55 /* String UTF8 */ 56 napi_value CreateStringUtf8(napi_env env, const std::string &str); 57 std::string GetStringFromValueUtf8(napi_env env, napi_value value); 58 std::string GetStringPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName); 59 void SetStringPropertyUtf8(napi_env env, napi_value object, const std::string &name, const std::string &value); 60 61 /* array buffer */ 62 bool ValueIsArrayBuffer(napi_env env, napi_value value); 63 void *GetInfoFromArrayBufferValue(napi_env env, napi_value value, size_t *length); 64 napi_value CreateArrayBuffer(napi_env env, size_t length, void **data); 65 66 /* object */ 67 napi_value CreateObject(napi_env env); 68 69 /* undefined */ 70 napi_value GetUndefined(napi_env env); 71 72 /* function */ 73 napi_value CallFunction(napi_env env, napi_value recv, napi_value func, size_t argc, const napi_value *argv); 74 napi_value CreateFunction(napi_env env, const std::string &name, napi_callback func, void *arg); 75 76 /* reference */ 77 napi_ref CreateReference(napi_env env, napi_value callback); 78 napi_value GetReference(napi_env env, napi_ref callbackRef); 79 void DeleteReference(napi_env env, napi_ref callbackRef); 80 81 /* boolean */ 82 bool GetBooleanProperty(napi_env env, napi_value object, const std::string &propertyName); 83 void SetBooleanProperty(napi_env env, napi_value object, const std::string &name, bool value); 84 napi_value GetBoolean(napi_env env, bool value); 85 bool GetBooleanValue(napi_env env, napi_value value); 86 87 /* define properties */ 88 void DefineProperties(napi_env env, napi_value object, 89 const std::initializer_list<napi_property_descriptor> &properties); 90 91 /* array */ 92 napi_value CreateArray(napi_env env, size_t length); 93 void SetArrayElement(napi_env env, napi_value array, uint32_t index, napi_value value); 94 bool IsArray(napi_env env, napi_value value); 95 uint32_t GetArrayLength(napi_env env, napi_value arr); 96 napi_value GetArrayElement(napi_env env, napi_value arr, uint32_t index); 97 98 /* libuv */ 99 void CreateUvQueueWork(napi_env env, void *data, void(handler)(uv_work_t *, int status)); 100 101 /* scope */ 102 napi_handle_scope OpenScope(napi_env env); 103 void CloseScope(napi_env env, napi_handle_scope scope); 104 /* error */ 105 napi_value CreateErrorMessage(napi_env env, int32_t errorCodeconst, const std::string &errorMessage); 106 } // namespace NapiUtils 107 } // namespace NetManagerStandard 108 } // namespace OHOS 109 110 #endif // COMMUNICATIONNETMANAGER_BASE_NETMANAGER_BASE_NAPI_UTILS_H 111