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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_VALUE_H
17 #define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_VALUE_H
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <string>
22 
23 #include "interfaces/inner_api/napi/native_node_api.h"
24 
25 namespace panda {
26 class JsiRuntimeCallInfo;
27 namespace ecmascript {
28 struct JsFrameInfo;
29 }
30 }
31 
32 class NativeEngine;
33 class NativeReference;
34 
35 // To be refactor
36 typedef napi_value (*NapiNativeCallback)(napi_env env, panda::JsiRuntimeCallInfo*);
37 typedef void (*NativeFinalize)(NativeEngine* engine, void* data, void* hint);
38 typedef void (*NativeAsyncExecuteCallback)(NativeEngine* engine, void* data);
39 typedef void (*NativeAsyncCompleteCallback)(NativeEngine* engine, int status, void* data);
40 typedef void* (*DetachCallback)(NativeEngine* engine, void* value, void* hint);
41 
42 using ErrorPos = std::pair<uint32_t, uint32_t>;
43 using NativeThreadSafeFunctionCallJs =
44     void (*)(NativeEngine* env, napi_value js_callback, void* context, void* data);
45 
46 struct NativeObjectInfo {
CreateNewInstanceNativeObjectInfo47     static NativeObjectInfo* CreateNewInstance() { return new(std::nothrow) NativeObjectInfo(); }
48     NativeEngine* engine = nullptr;
49     void* nativeObject = nullptr;
50     NativeFinalize callback = nullptr;
51     void* hint = nullptr;
52 };
53 
54 struct NapiFunctionInfo {
CreateNewInstanceNapiFunctionInfo55     static NapiFunctionInfo* CreateNewInstance() { return new(std::nothrow) NapiFunctionInfo(); }
56     NapiNativeCallback callback = nullptr;
57     void* data = nullptr;
58 #ifdef ENABLE_CONTAINER_SCOPE
59     int32_t scopeId = -1;
60 #endif
61 };
62 
63 typedef void (*NaitveFinalize)(NativeEngine* env, void* data, void* hint);
64 
65 // To be delete
66 enum NativeValueType {
67     NATIVE_UNDEFINED,
68     NATIVE_NULL,
69     NATIVE_BOOLEAN,
70     NATIVE_NUMBER,
71     NATIVE_STRING,
72     NATIVE_SYMBOL,
73     NATIVE_OBJECT,
74     NATIVE_FUNCTION,
75     NATIVE_EXTERNAL,
76     NATIVE_BIGINT,
77 };
78 
79 // To be delete
80 enum NativeThreadSafeFunctionCallMode {
81     NATIVE_TSFUNC_NONBLOCKING,
82     NATIVE_TSFUNC_BLOCKING,
83 };
84 
85 // To be delete
86 enum NativeThreadSafeFunctionReleaseMode {
87     NATIVE_TSFUNC_RELEASE,
88     NATIVE_TSFUNC_ABORT,
89 };
90 
91 struct JSValueWrapper {
JSValueWrapperJSValueWrapper92     JSValueWrapper()
93     {
94         u.ptr = nullptr;
95         tag = 0;
96     }
97     template<typename T>
JSValueWrapperJSValueWrapper98     JSValueWrapper(T value)
99     {
100         *(T*)this = value;
101     }
TJSValueWrapper102     template<typename T> operator T()
103     {
104         return *(T*)this;
105     }
106     template<typename T> JSValueWrapper& operator=(T value)
107     {
108         *(T*)this = value;
109         return *this;
110     }
111     union {
112         int32_t int32;
113         double float64;
114         void* ptr;
115     } u;
116     int64_t tag = 0;
117 };
118 
119 struct NapiTypeTag {
120     uint64_t lower;
121     uint64_t upper;
122 };
123 
124 // To be delete
125 enum NativeTypedArrayType {
126     NATIVE_INT8_ARRAY,
127     NATIVE_UINT8_ARRAY,
128     NATIVE_UINT8_CLAMPED_ARRAY,
129     NATIVE_INT16_ARRAY,
130     NATIVE_UINT16_ARRAY,
131     NATIVE_INT32_ARRAY,
132     NATIVE_UINT32_ARRAY,
133     NATIVE_FLOAT32_ARRAY,
134     NATIVE_FLOAT64_ARRAY,
135     NATIVE_BIGINT64_ARRAY,
136     NATIVE_BIGUINT64_ARRAY,
137 };
138 
139 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_VALUE_H */