• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 NAPITUTORIALS_COMMON_H
17 #define NAPITUTORIALS_COMMON_H
18 
19 #include <string>
20 #include <stdio.h>
21 #include <js_native_api.h>
22 #include <js_native_api_types.h>
23 #include <vector>
24 #include "hilog/log.h"
25 #include "napi/native_api.h"
26 #include "rawfile/raw_file_manager.h"
27 #include "rawfile/raw_file.h"
28 #include "rawfile/raw_dir.h"
29 
30 #define GLOBAL_RESMGR (0xFFEE)
31 constexpr int32_t RGB_565 = 2;
32 constexpr int32_t RGBA_8888 = 3;
33 
34 constexpr int32_t STR_MAX_SIZE = 200;
35 constexpr int32_t LONG_STR_MAX_SIZE = 1024;
36 constexpr int32_t ERR_OK = 0;
37 constexpr int8_t NO_ERROR = 0;
38 constexpr int8_t ERROR = -1;
39 constexpr uint8_t PARAM0 = 0;
40 constexpr uint8_t PARAM1 = 1;
41 constexpr uint8_t PARAM2 = 2;
42 constexpr uint8_t PARAM3 = 3;
43 constexpr uint8_t PARAM4 = 4;
44 constexpr uint8_t PARAM5 = 5;
45 constexpr uint8_t PARAM6 = 6;
46 constexpr uint8_t PARAM7 = 7;
47 constexpr uint8_t PARAM8 = 8;
48 constexpr uint8_t PARAM9 = 9;
49 constexpr uint8_t PARAM10 = 10;
50 constexpr uint8_t PARAM11 = 11;
51 constexpr uint8_t PARAM12 = 12;
52 constexpr uint8_t PARAM60 = 60;
53 constexpr uint8_t PARAM100 = 100;
54 constexpr uint16_t PARAM1000 = 1000;
55 constexpr uint32_t PARAM100W = 1000000;
56 
57 constexpr int32_t ARGS_ONE = 1;
58 constexpr int32_t ARGS_TWO = 2;
59 constexpr int32_t ONLY_CALLBACK_MAX_PARA = 1;
60 constexpr int32_t ONLY_CALLBACK_MIN_PARA = 0;
61 
62 enum TestCaseTypes {
63     TCT_BASE = 1,
64     TCT_NADATATYPE,
65     TCT_NAENVLCAPI,
66     TCT_JSABSTARCTOPS,
67     TCT_JSPREOPERTY,
68     TCT_JSVALUES,
69     TCT_CJSON,
70     TCT_FFMPEG,
71     TCT_OPENCV
72 };
73 
74 struct CallbackPromiseInfo {
75     napi_ref callback = nullptr;
76     napi_deferred deferred = nullptr;
77     bool isCallback = false;
78     int32_t errorCode = 0;
79 };
80 
FreeMemory(T * p)81 template <typename T> void FreeMemory(T *p)
82 {
83     if (p == nullptr) {
84         return;
85     }
86     delete p;
87     p = nullptr;
88 }
89 
FreeMemoryArray(T * p)90 template <typename T> void FreeMemoryArray(T *p)
91 {
92     if (p == nullptr) {
93         return;
94     }
95     delete[] p;
96     p = nullptr;
97 }
98 #define NAPI_RETVAL_NOTHING
99 
100 extern bool CreateArrayBuffer(napi_env env, uint8_t *src, size_t srcLen, napi_value *res);
101 extern napi_value NapiGetUndefined(napi_env env);
102 extern napi_value GetCallbackErrorValue(napi_env env, int32_t errCode);
103 extern napi_value NapiGetBoolean(napi_env env, const bool &isValue);
104 
105 extern void SetCallback(const napi_env &env, const napi_ref &callbackIn, const int32_t &errorCode,
106                         const napi_value &result);
107 extern void SetPromise(const napi_env &env, const napi_deferred &deferred, const int32_t &errorCode,
108                        const napi_value &result);
109 extern void ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInfo &info, const napi_value &result);
110 extern napi_value JSParaError(const napi_env &env, const napi_ref &callback);
111 extern void PaddingCallbackPromiseInfo(const napi_env &env, const napi_ref &callback, CallbackPromiseInfo &info,
112                                        napi_value &promise);
113 
114 void getErrMsg(napi_status &status, napi_env &env, const napi_extended_error_info *&extended_error_info,
115                const char *info, const char *tag);
116 napi_status napiValueType2Str(const napi_env &env, const napi_valuetype type, napi_value *result);
117 
118 #endif //NAPITUTORIALS_COMMON_H
119