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