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 NAPI_PRINT_UTILS_H 17 #define NAPI_PRINT_UTILS_H 18 19 #include <string> 20 21 #include "napi/native_api.h" 22 #include "napi/native_common.h" 23 24 namespace OHOS::Print { 25 class NapiPrintUtils { 26 public: 27 static constexpr size_t MAX_ARGC = 6; 28 static constexpr size_t ARGC_ZERO = 0; 29 static constexpr size_t ARGC_ONE = 1; 30 static constexpr size_t ARGC_TWO = 2; 31 static constexpr size_t ARGC_THREE = 3; 32 static constexpr size_t ARGC_FOUR = 4; 33 static constexpr size_t ARGC_FIVE = 5; 34 static constexpr size_t ARGC_SIX = 6; 35 36 static constexpr uint32_t INDEX_ZERO = 0; 37 static constexpr uint32_t INDEX_ONE = 1; 38 static constexpr uint32_t INDEX_TWO = 2; 39 static constexpr uint32_t INDEX_THREE = 3; 40 static constexpr uint32_t INDEX_FOUR = 4; 41 static constexpr uint32_t INDEX_FIVE = 5; 42 43 static napi_valuetype GetValueType(napi_env env, napi_value value); 44 static bool HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName); 45 static napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName); 46 static void SetNamedProperty(napi_env env, napi_value object, const std::string &name, napi_value value); 47 static std::vector<std::string> GetPropertyNames(napi_env env, napi_value object); 48 static napi_value CreateUint32(napi_env env, uint32_t code); 49 static uint32_t GetUint32FromValue(napi_env env, napi_value value); 50 static uint32_t GetUint32Property(napi_env env, napi_value object, const std::string &propertyName); 51 static void SetUint32Property(napi_env env, napi_value object, const std::string &name, uint32_t value); 52 static napi_value CreateInt32(napi_env env, int32_t code); 53 static int32_t GetInt32FromValue(napi_env env, napi_value value); 54 static int32_t GetInt32Property(napi_env env, napi_value object, const std::string &propertyName); 55 static void SetInt32Property(napi_env env, napi_value object, const std::string &name, int32_t value); 56 static napi_value CreateStringUtf8(napi_env env, const std::string &str); 57 static std::string GetStringFromValueUtf8(napi_env env, napi_value value); 58 static std::string GetStringPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName); 59 static void SetStringPropertyUtf8( 60 napi_env env, napi_value object, const std::string &name, const std::string &value); 61 static std::string GetValueString(napi_env env, napi_value value); 62 static bool ValueIsArrayBuffer(napi_env env, napi_value value); 63 static void *GetInfoFromArrayBufferValue(napi_env env, napi_value value, size_t *length); 64 static napi_value CreateArrayBuffer(napi_env env, size_t length, void **data); 65 static napi_value CreateObject(napi_env env); 66 static napi_value GetUndefined(napi_env env); 67 static napi_value CallFunction(napi_env env, napi_value recv, napi_value func, size_t argc, const napi_value *argv); 68 static napi_value CreateFunction(napi_env env, const std::string &name, napi_callback func, void *arg); 69 static napi_ref CreateReference(napi_env env, napi_value callback); 70 static napi_value GetReference(napi_env env, napi_ref callbackRef); 71 static void DeleteReference(napi_env env, napi_ref callbackRef); 72 static bool GetBooleanProperty(napi_env env, napi_value object, const std::string &propertyName); 73 static void SetBooleanProperty(napi_env env, napi_value object, const std::string &name, bool value); 74 static void DefineProperties( 75 napi_env env, napi_value object, const std::initializer_list<napi_property_descriptor> &properties); 76 }; 77 } // namespace OHOS::Print 78 #endif // NAPI_PRINT_UTILS_H