• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
28 
29 class NativeEngine;
30 class NativeReference;
31 
32 struct NapiNativeCallbackInfo;
33 
34 // To be refactor
35 typedef napi_value (*NapiNativeCallback)(napi_env env, NapiNativeCallbackInfo*);
36 typedef void (*NativeFinalize)(NativeEngine* engine, void* data, void* hint);
37 typedef void (*NativeAsyncExecuteCallback)(NativeEngine* engine, void* data);
38 typedef void (*NativeAsyncCompleteCallback)(NativeEngine* engine, int status, void* data);
39 typedef void* (*DetachCallback)(NativeEngine* engine, void* value, void* hint);
40 
41 using ErrorPos = std::pair<uint32_t, uint32_t>;
42 using NativeThreadSafeFunctionCallJs =
43     void (*)(NativeEngine* env, napi_value js_callback, void* context, void* data);
44 
45 struct NativeObjectInfo {
CreateNewInstanceNativeObjectInfo46     static NativeObjectInfo* CreateNewInstance() { return new(std::nothrow) NativeObjectInfo(); }
47     NativeEngine* engine = nullptr;
48     void* nativeObject = nullptr;
49     NativeFinalize callback = nullptr;
50     void* hint = nullptr;
51 };
52 
53 struct JsFrameInfo {
54     std::string functionName;
55     std::string fileName;
56     std::string pos;
57     uintptr_t *nativePointer = nullptr;
58 };
59 
60 struct NapiFunctionInfo {
CreateNewInstanceNapiFunctionInfo61     static NapiFunctionInfo* CreateNewInstance() { return new(std::nothrow) NapiFunctionInfo(); }
62     napi_env env = nullptr;
63     NapiNativeCallback callback = nullptr;
64     void* data = nullptr;
65 };
66 
67 // To be refactor
68 struct NapiNativeCallbackInfo {
NapiNativeCallbackInfoNapiNativeCallbackInfo69     NapiNativeCallbackInfo(panda::JsiRuntimeCallInfo* runtimeInfo): info(runtimeInfo) { };
70     size_t GetArgc() const;
71     size_t GetArgv(napi_value* argv, size_t argc);
72     napi_value GetThisVar();
73     napi_value GetFunction();
74     NapiFunctionInfo* GetFunctionInfo();
75 private:
76     panda::JsiRuntimeCallInfo* info = nullptr;
77 };
78 
79 typedef void (*NaitveFinalize)(NativeEngine* env, void* data, void* hint);
80 
81 // To be delete
82 enum NativeValueType {
83     NATIVE_UNDEFINED,
84     NATIVE_NULL,
85     NATIVE_BOOLEAN,
86     NATIVE_NUMBER,
87     NATIVE_STRING,
88     NATIVE_SYMBOL,
89     NATIVE_OBJECT,
90     NATIVE_FUNCTION,
91     NATIVE_EXTERNAL,
92     NATIVE_BIGINT,
93 };
94 
95 // To be delete
96 enum NativeThreadSafeFunctionCallMode {
97     NATIVE_TSFUNC_NONBLOCKING,
98     NATIVE_TSFUNC_BLOCKING,
99 };
100 
101 // To be delete
102 enum NativeThreadSafeFunctionReleaseMode {
103     NATIVE_TSFUNC_RELEASE,
104     NATIVE_TSFUNC_ABORT,
105 };
106 
107 struct JSValueWrapper {
JSValueWrapperJSValueWrapper108     JSValueWrapper()
109     {
110         u.ptr = nullptr;
111         tag = 0;
112     }
113     template<typename T>
JSValueWrapperJSValueWrapper114     JSValueWrapper(T value)
115     {
116         *(T*)this = value;
117     }
TJSValueWrapper118     template<typename T> operator T()
119     {
120         return *(T*)this;
121     }
122     template<typename T> JSValueWrapper& operator=(T value)
123     {
124         *(T*)this = value;
125         return *this;
126     }
127     union {
128         int32_t int32;
129         double float64;
130         void* ptr;
131     } u;
132     int64_t tag = 0;
133 };
134 
135 struct NapiTypeTag {
136     uint64_t lower;
137     uint64_t upper;
138 };
139 
140 // To be delete
141 enum NativeTypedArrayType {
142     NATIVE_INT8_ARRAY,
143     NATIVE_UINT8_ARRAY,
144     NATIVE_UINT8_CLAMPED_ARRAY,
145     NATIVE_INT16_ARRAY,
146     NATIVE_UINT16_ARRAY,
147     NATIVE_INT32_ARRAY,
148     NATIVE_UINT32_ARRAY,
149     NATIVE_FLOAT32_ARRAY,
150     NATIVE_FLOAT64_ARRAY,
151     NATIVE_BIGINT64_ARRAY,
152     NATIVE_BIGUINT64_ARRAY,
153 };
154 
155 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_VALUE_H */