1 /* 2 * Copyright (c) 2021 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 HIVIEWDFX_NAPI_ARG 17 #define HIVIEWDFX_NAPI_ARG 18 19 #pragma once 20 21 #include <functional> 22 #include <map> 23 #include "n_val.h" 24 #include "uni_header.h" 25 26 namespace OHOS { 27 namespace HiviewDFX { 28 enum NARG_CNT { 29 ZERO = 0, 30 ONE = 1, 31 TWO = 2, 32 THREE = 3, 33 FOUR = 4, 34 FIVE = 5, 35 SIX = 6, 36 SEVEN = 7, 37 EIGHT = 8, 38 NINE = 9, 39 TEN = 10, 40 ELEVEN = 11, 41 TWELVE = 12, 42 }; 43 44 enum NARG_POS { 45 FIRST = 0, 46 SECOND = 1, 47 THIRD = 2, 48 FOURTH = 3, 49 FIFTH = 4, 50 SIXTH = 5, 51 SEVENTH = 6, 52 EIGHTH = 7, 53 NINTH = 8, 54 TENTH = 9, 55 ELEVENTH = 10, 56 TWELVETH = 11, 57 }; 58 59 class NFuncArg final { 60 public: 61 NFuncArg(napi_env env, napi_callback_info info); 62 63 virtual ~NFuncArg(); 64 65 bool InitArgs(size_t argc); 66 67 bool InitArgs(size_t minArgc, size_t maxArgc); 68 69 size_t GetArgc() const; 70 71 napi_value GetThisVar() const; 72 73 napi_value operator[](size_t idx) const; 74 75 napi_value GetArg(size_t argPos) const; 76 77 private: 78 napi_env env_ = nullptr; 79 napi_callback_info info_ = nullptr; 80 81 size_t argc_ = 0; 82 std::unique_ptr<napi_value[]> argv_ = {nullptr}; 83 napi_value thisVar_ = nullptr; 84 85 bool InitArgs(std::function<bool()> argcChecker); 86 87 void SetArgc(size_t argc); 88 89 void SetThisVar(napi_value thisVar); 90 }; 91 } // namespace HiviewDFX 92 } // namespace OHOS 93 94 #endif // HIVIEWDFX_NAPI_ARG 95