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 size, size_t* length) = 0; 218 }; 219 220 class NativeObject { 221 public: 222 static const int INTERFACE_ID = 3; 223 static constexpr auto PANDA_MODULE_NAME = "_GLOBAL_MODULE_NAME"; 224 static const auto PANDA_MODULE_NAME_LEN = 32; 225 226 virtual bool ConvertToNativeBindingObject( 227 void* engine, DetachCallback detach, AttachCallback attach, void *object, void *hint) = 0; 228 virtual void SetNativePointer(void* pointer, NativeFinalize cb, 229 void* hint, NativeReference** reference = nullptr, size_t nativeBindingSize = 0) = 0; 230 virtual void* GetNativePointer() = 0; 231 virtual void SetNativeBindingPointer( 232 void* enginePointer, void* objPointer, void* hint, void* detachData = nullptr, void* attachData = nullptr) = 0; 233 virtual void* GetNativeBindingPointer(uint32_t index) = 0; 234 235 virtual void AddFinalizer(void* pointer, NativeFinalize cb, void* hint) = 0; 236 237 virtual NativeValue* GetPropertyNames() = 0; 238 virtual NativeValue* GetEnumerablePropertyNames() = 0; 239 240 virtual NativeValue* GetPrototype() = 0; 241 242 virtual bool DefineProperty(NativePropertyDescriptor propertyDescriptor) = 0; 243 244 virtual bool SetProperty(NativeValue* key, NativeValue* value) = 0; 245 virtual NativeValue* GetProperty(NativeValue* key) = 0; 246 virtual bool HasProperty(NativeValue* key) = 0; 247 virtual bool DeleteProperty(NativeValue* key) = 0; 248 249 virtual bool SetProperty(const char* name, NativeValue* value) = 0; 250 virtual NativeValue* GetProperty(const char* name) = 0; 251 virtual bool HasProperty(const char* name) = 0; 252 virtual bool DeleteProperty(const char* name) = 0; 253 254 virtual bool SetPrivateProperty(const char* name, NativeValue* value) = 0; 255 virtual NativeValue* GetPrivateProperty(const char* name) = 0; 256 virtual bool HasPrivateProperty(const char* name) = 0; 257 virtual bool DeletePrivateProperty(const char* name) = 0; 258 259 virtual NativeValue* GetAllPropertyNames( 260 napi_key_collection_mode keyMode, napi_key_filter keyFilter, napi_key_conversion keyConversion) = 0; 261 262 virtual bool AssociateTypeTag(NapiTypeTag* typeTag) = 0; 263 virtual bool CheckTypeTag(NapiTypeTag* typeTag) = 0; 264 virtual void Freeze() = 0; 265 virtual void Seal() = 0; 266 }; 267 268 class NativeArray { 269 public: 270 static const int INTERFACE_ID = 4; 271 272 virtual bool SetElement(uint32_t index, NativeValue* value) = 0; 273 virtual NativeValue* GetElement(uint32_t index) = 0; 274 virtual bool HasElement(uint32_t index) = 0; 275 virtual bool DeleteElement(uint32_t index) = 0; 276 277 virtual uint32_t GetLength() = 0; 278 }; 279 280 class NativeArrayBuffer { 281 public: 282 static const int INTERFACE_ID = 5; 283 284 virtual void* GetBuffer() = 0; 285 virtual size_t GetLength() = 0; 286 virtual bool IsDetachedArrayBuffer() = 0; 287 virtual bool DetachArrayBuffer() = 0; 288 }; 289 290 enum NativeTypedArrayType { 291 NATIVE_INT8_ARRAY, 292 NATIVE_UINT8_ARRAY, 293 NATIVE_UINT8_CLAMPED_ARRAY, 294 NATIVE_INT16_ARRAY, 295 NATIVE_UINT16_ARRAY, 296 NATIVE_INT32_ARRAY, 297 NATIVE_UINT32_ARRAY, 298 NATIVE_FLOAT32_ARRAY, 299 NATIVE_FLOAT64_ARRAY, 300 NATIVE_BIGINT64_ARRAY, 301 NATIVE_BIGUINT64_ARRAY, 302 }; 303 304 class NativeTypedArray { 305 public: 306 static const int INTERFACE_ID = 6; 307 308 virtual NativeTypedArrayType GetTypedArrayType() = 0; 309 virtual size_t GetLength() = 0; 310 virtual NativeValue* GetArrayBuffer() = 0; 311 virtual void* GetData() = 0; 312 virtual size_t GetOffset() = 0; 313 }; 314 315 class NativeDataView { 316 public: 317 static const int INTERFACE_ID = 7; 318 319 virtual void* GetBuffer() = 0; 320 virtual size_t GetLength() = 0; 321 virtual NativeValue* GetArrayBuffer() = 0; 322 virtual size_t GetOffset() = 0; 323 }; 324 325 class NativeFunction { 326 public: 327 static const int INTERFACE_ID = 8; 328 virtual std::string GetSourceCodeInfo(ErrorPos pos) = 0; 329 }; 330 331 class NativeBigint { 332 public: 333 static const int INTERFACE_ID = 9; 334 335 virtual operator int64_t() = 0; 336 virtual operator uint64_t() = 0; 337 virtual void Uint64Value(uint64_t* cValue, bool* lossless = nullptr) = 0; 338 virtual void Int64Value(int64_t* cValue, bool* lossless = nullptr) = 0; 339 virtual bool GetWordsArray(int* signBit, size_t* wordCount, uint64_t* words) = 0; 340 }; 341 342 class NativeDate { 343 public: 344 static const int INTERFACE_ID = 10; 345 346 virtual double GetTime() = 0; 347 }; 348 349 class NativeExternal { 350 public: 351 static const int INTERFACE_ID = 11; 352 353 virtual operator void*() = 0; 354 }; 355 356 class NativeBuffer { 357 public: 358 static const int INTERFACE_ID = 12; 359 360 virtual void* GetBuffer() = 0; 361 virtual size_t GetLength() = 0; 362 }; 363 364 enum NativeErrorType { 365 NATIVE_COMMON_ERROR, 366 NATIVE_TYPE_ERROR, 367 NATIVE_RANGE_ERROR, 368 }; 369 370 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_VALUE_H */ 371