• 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 
22 #include "../../../third_party/node/src/js_native_api.h"
23 
24 class NativeValue;
25 class NativeEngine;
26 
27 struct NativePropertyDescriptor;
28 struct NativeCallbackInfo;
29 
30 typedef NativeValue* (*NativeCallback)(NativeEngine* engine, NativeCallbackInfo*);
31 typedef void (*NativeFinalize)(NativeEngine* engine, void* data, void* hint);
32 
33 typedef void (*NativeAsyncExecuteCallback)(NativeEngine* engine, void* data);
34 typedef void (*NativeAsyncCompleteCallback)(NativeEngine* engine, int status, void* data);
35 using NativeThreadSafeFunctionCallJs =
36     void (*)(NativeEngine* env, NativeValue* js_callback, void* context, void* data);
37 
38 struct NativeObjectInfo {
CreateNewInstanceNativeObjectInfo39     static NativeObjectInfo* CreateNewInstance() { return new NativeObjectInfo(); }
40     NativeEngine* engine = nullptr;
41     void* nativeObject = nullptr;
42     NativeFinalize callback = nullptr;
43     void* hint = nullptr;
44 };
45 
46 struct NativeFunctionInfo {
CreateNewInstanceNativeFunctionInfo47     static NativeFunctionInfo* CreateNewInstance() { return new NativeFunctionInfo(); }
48     NativeEngine* engine = nullptr;
49     NativeCallback callback = nullptr;
50     void* data = nullptr;
51 };
52 
53 struct NativeCallbackInfo {
54     size_t argc = 0;
55     NativeValue** argv = nullptr;
56     NativeValue* thisVar = nullptr;
57     NativeValue* function = nullptr;
58     NativeFunctionInfo* functionInfo = nullptr;
59 };
60 
61 typedef void (*NaitveFinalize)(NativeEngine* env, void* data, void* hint);
62 
63 enum NativeValueType {
64     NATIVE_UNDEFINED,
65     NATIVE_NULL,
66     NATIVE_BOOLEAN,
67     NATIVE_NUMBER,
68     NATIVE_STRING,
69     NATIVE_SYMBOL,
70     NATIVE_OBJECT,
71     NATIVE_FUNCTION,
72     NATIVE_EXTERNAL,
73     NATIVE_BIGINT,
74 };
75 
76 enum NativeThreadSafeFunctionCallMode {
77     NATIVE_TSFUNC_NONBLOCKING,
78     NATIVE_TSFUNC_BLOCKING,
79 };
80 
81 enum NativeThreadSafeFunctionReleaseMode {
82     NATIVE_TSFUNC_RELEASE,
83     NATIVE_TSFUNC_ABORT,
84 };
85 
86 struct JSValueWrapper {
JSValueWrapperJSValueWrapper87     JSValueWrapper()
88     {
89         u.ptr = nullptr;
90         tag = 0;
91     }
92     template<typename T>
JSValueWrapperJSValueWrapper93     JSValueWrapper(T value)
94     {
95         *(T*)this = value;
96     }
TJSValueWrapper97     template<typename T> operator T()
98     {
99         return *(T*)this;
100     }
101     template<typename T> JSValueWrapper& operator=(T value)
102     {
103         *(T*)this = value;
104         return *this;
105     }
106     union {
107         int32_t int32;
108         double float64;
109         void* ptr;
110     } u;
111     int64_t tag = 0;
112 };
113 
114 struct NapiTypeTag {
115     uint64_t lower;
116     uint64_t upper;
117 };
118 
119 class NativeValue {
120 public:
~NativeValue()121     virtual ~NativeValue() {}
122 
T()123     template<typename T> operator T()
124     {
125         return value_;
126     }
127 
128     template<typename T> NativeValue& operator=(T value)
129     {
130         value_ = value;
131         return *this;
132     }
133 
134     virtual void* GetInterface(int interfaceId) = 0;
135 
136     virtual NativeValueType TypeOf() = 0;
137     virtual bool InstanceOf(NativeValue* obj) = 0;
138 
139     virtual bool IsArray() = 0;
140     virtual bool IsArrayBuffer() = 0;
141     virtual bool IsDate() = 0;
142     virtual bool IsError() = 0;
143     virtual bool IsTypedArray() = 0;
144     virtual bool IsDataView() = 0;
145     virtual bool IsPromise() = 0;
146     virtual bool IsCallable() = 0;
147     virtual bool IsArgumentsObject() = 0;
148     virtual bool IsAsyncFunction() = 0;
149     virtual bool IsBooleanObject() = 0;
150     virtual bool IsGeneratorFunction() = 0;
151     virtual bool IsMapIterator() = 0;
152     virtual bool IsSetIterator() = 0;
153     virtual bool IsGeneratorObject() = 0;
154     virtual bool IsModuleNamespaceObject() = 0;
155     virtual bool IsProxy() = 0;
156     virtual bool IsRegExp() = 0;
157     virtual bool IsNumberObject() = 0;
158     virtual bool IsMap() = 0;
159     virtual bool IsBuffer() = 0;
160     virtual bool IsStringObject() = 0;
161     virtual bool IsSymbolObject() = 0;
162     virtual bool IsWeakMap() = 0;
163     virtual bool IsWeakSet() = 0;
164     virtual bool IsSet() = 0;
165 
166     virtual NativeValue* ToBoolean() = 0;
167     virtual NativeValue* ToNumber() = 0;
168     virtual NativeValue* ToString() = 0;
169     virtual NativeValue* ToObject() = 0;
170 
171     virtual bool StrictEquals(NativeValue* value) = 0;
172 
173 protected:
174     JSValueWrapper value_;
175 };
176 
177 class NativeBoolean {
178 public:
179     static const int INTERFACE_ID = 0;
180 
181     virtual operator bool() = 0;
182 };
183 
184 class NativeNumber {
185 public:
186     static const int INTERFACE_ID = 1;
187 
188     virtual operator int32_t() = 0;
189     virtual operator uint32_t() = 0;
190     virtual operator int64_t() = 0;
191     virtual operator double() = 0;
192 };
193 
194 class NativeString {
195 public:
196     static const int INTERFACE_ID = 2;
197 
198     virtual void GetCString(char* buffer, size_t size, size_t* length) = 0;
199     virtual size_t GetLength() = 0;
200     virtual size_t EncodeWriteUtf8(char* buffer, size_t bufferSize, int32_t* nchars) = 0;
201     virtual void GetCString16(char16_t* buffer, size_t size, size_t* length) = 0;
202 };
203 
204 class NativeObject {
205 public:
206     static const int INTERFACE_ID = 3;
207 
208     virtual void SetNativePointer(void* pointer, NativeFinalize cb, void* hint) = 0;
209     virtual void* GetNativePointer() = 0;
210 
211     virtual void AddFinalizer(void* pointer, NativeFinalize cb, void* hint) = 0;
212 
213     virtual NativeValue* GetPropertyNames() = 0;
214 
215     virtual NativeValue* GetPrototype() = 0;
216 
217     virtual bool DefineProperty(NativePropertyDescriptor propertyDescriptor) = 0;
218 
219     virtual bool SetProperty(NativeValue* key, NativeValue* value) = 0;
220     virtual NativeValue* GetProperty(NativeValue* key) = 0;
221     virtual bool HasProperty(NativeValue* key) = 0;
222     virtual bool DeleteProperty(NativeValue* key) = 0;
223 
224     virtual bool SetProperty(const char* name, NativeValue* value) = 0;
225     virtual NativeValue* GetProperty(const char* name) = 0;
226     virtual bool HasProperty(const char* name) = 0;
227     virtual bool DeleteProperty(const char* name) = 0;
228 
229     virtual bool SetPrivateProperty(const char* name, NativeValue* value) = 0;
230     virtual NativeValue* GetPrivateProperty(const char* name) = 0;
231     virtual bool HasPrivateProperty(const char* name) = 0;
232     virtual bool DeletePrivateProperty(const char* name) = 0;
233 
234     virtual NativeValue* GetAllPropertyNames(
235         napi_key_collection_mode keyMode, napi_key_filter keyFilter, napi_key_conversion keyConversion) = 0;
236 
237     virtual bool AssociateTypeTag(NapiTypeTag* typeTag) = 0;
238     virtual bool CheckTypeTag(NapiTypeTag* typeTag) = 0;
239     virtual void Freeze() = 0;
240     virtual void Seal() = 0;
241 };
242 
243 class NativeArray {
244 public:
245     static const int INTERFACE_ID = 4;
246 
247     virtual bool SetElement(uint32_t index, NativeValue* value) = 0;
248     virtual NativeValue* GetElement(uint32_t index) = 0;
249     virtual bool HasElement(uint32_t index) = 0;
250     virtual bool DeleteElement(uint32_t index) = 0;
251 
252     virtual uint32_t GetLength() = 0;
253 };
254 
255 class NativeArrayBuffer {
256 public:
257     static const int INTERFACE_ID = 5;
258 
259     virtual void* GetBuffer() = 0;
260     virtual size_t GetLength() = 0;
261     virtual bool IsDetachedArrayBuffer() = 0;
262     virtual bool DetachArrayBuffer() = 0;
263 };
264 
265 enum NativeTypedArrayType {
266     NATIVE_INT8_ARRAY,
267     NATIVE_UINT8_ARRAY,
268     NATIVE_UINT8_CLAMPED_ARRAY,
269     NATIVE_INT16_ARRAY,
270     NATIVE_UINT16_ARRAY,
271     NATIVE_INT32_ARRAY,
272     NATIVE_UINT32_ARRAY,
273     NATIVE_FLOAT32_ARRAY,
274     NATIVE_FLOAT64_ARRAY,
275     NATIVE_BIGINT64_ARRAY,
276     NATIVE_BIGUINT64_ARRAY,
277 };
278 
279 class NativeTypedArray {
280 public:
281     static const int INTERFACE_ID = 6;
282 
283     virtual NativeTypedArrayType GetTypedArrayType() = 0;
284     virtual size_t GetLength() = 0;
285     virtual NativeValue* GetArrayBuffer() = 0;
286     virtual void* GetData() = 0;
287     virtual size_t GetOffset() = 0;
288 };
289 
290 class NativeDataView {
291 public:
292     static const int INTERFACE_ID = 7;
293 
294     virtual void* GetBuffer() = 0;
295     virtual size_t GetLength() = 0;
296     virtual NativeValue* GetArrayBuffer() = 0;
297     virtual size_t GetOffset() = 0;
298 };
299 
300 class NativeFunction {
301 public:
302     static const int INTERFACE_ID = 8;
303 };
304 
305 class NativeBigint {
306 public:
307     static const int INTERFACE_ID = 9;
308 
309     virtual operator int64_t() = 0;
310     virtual operator uint64_t() = 0;
311     virtual void Uint64Value(uint64_t* cValue, bool* lossless = nullptr) = 0;
312     virtual void Int64Value(int64_t* cValue, bool* lossless = nullptr) = 0;
313     virtual bool GetWordsArray(int* signBit, size_t* wordCount, uint64_t* words) = 0;
314 };
315 
316 class NativeDate {
317 public:
318     static const int INTERFACE_ID = 10;
319 
320     virtual double GetTime() = 0;
321 };
322 
323 class NativeExternal {
324 public:
325     static const int INTERFACE_ID = 11;
326 
327     virtual operator void*() = 0;
328 };
329 
330 class NativeBuffer {
331 public:
332     static const int INTERFACE_ID = 12;
333 
334     virtual void* GetBuffer() = 0;
335     virtual size_t GetLength() = 0;
336 };
337 
338 enum NativeErrorType {
339     NATIVE_COMMON_ERROR,
340     NATIVE_TYPE_ERROR,
341     NATIVE_RANGE_ERROR,
342 };
343 
344 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_VALUE_H */
345