1 /* 2 * Copyright (c) 2021-2024 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 ECMASCRIPT_OBJECT_FACTORY_H 17 #define ECMASCRIPT_OBJECT_FACTORY_H 18 19 #include "ecmascript/base/error_type.h" 20 #include "ecmascript/base/number_helper.h" 21 #include "ecmascript/compiler/builtins/builtins_call_signature.h" 22 #include "ecmascript/ecma_string.h" 23 #include "ecmascript/js_function_kind.h" 24 #include "ecmascript/js_handle.h" 25 #include "ecmascript/js_hclass.h" 26 #include "ecmascript/js_native_pointer.h" 27 #include "ecmascript/js_tagged_value.h" 28 #include "ecmascript/mem/heap_region_allocator.h" 29 #include "ecmascript/mem/machine_code.h" 30 #include "ecmascript/mem/native_area_allocator.h" 31 #include "ecmascript/mem/space.h" 32 #include "ecmascript/shared_objects/js_shared_object.h" 33 #include "ecmascript/tagged_array.h" 34 #include "ecmascript/byte_array.h" 35 #include "ecmascript/pgo_profiler/pgo_extra_profiler.h" 36 37 namespace panda::ecmascript { 38 struct MethodLiteral; 39 class Method; 40 class JSObject; 41 class JSArray; 42 class JSAPIPlainArray; 43 class JSSymbol; 44 class JSFunctionBase; 45 class JSFunction; 46 class JSBoundFunction; 47 class JSProxyRevocFunction; 48 class JSAsyncAwaitStatusFunction; 49 class JSPrimitiveRef; 50 class GlobalEnv; 51 class GlobalEnvConstants; 52 class AccessorData; 53 class JSGlobalObject; 54 class LexicalEnv; 55 class SendableEnv; 56 class JSDate; 57 class JSProxy; 58 class JSRealm; 59 class JSArguments; 60 class TaggedQueue; 61 class JSForInIterator; 62 class JSSet; 63 class JSSharedSet; 64 class JSMap; 65 class JSSharedMap; 66 class JSRegExp; 67 class JSSetIterator; 68 class JSSharedSetIterator; 69 class JSRegExpIterator; 70 class JSMapIterator; 71 class JSSharedMapIterator; 72 class JSArrayIterator; 73 class JSSharedArrayIterator; 74 class JSAPIPlainArrayIterator; 75 class JSStringIterator; 76 class JSGeneratorObject; 77 class CompletionRecord; 78 class GeneratorContext; 79 class JSArrayBuffer; 80 class JSSendableArrayBuffer; 81 class JSDataView; 82 class JSPromise; 83 class JSPromiseReactionsFunction; 84 class JSPromiseExecutorFunction; 85 class JSAsyncModuleFulfilledFunction; 86 class JSAsyncModuleRejectedFunction; 87 class JSPromiseAllResolveElementFunction; 88 class JSAsyncGeneratorResNextRetProRstFtn; 89 class JSPromiseAnyRejectElementFunction; 90 class JSPromiseAllSettledElementFunction; 91 class JSPromiseFinallyFunction; 92 class JSPromiseValueThunkOrThrowerFunction; 93 class PromiseReaction; 94 class PromiseCapability; 95 class PromiseIteratorRecord; 96 class JSAsyncFuncObject; 97 class JSAsyncFunction; 98 class JSAsyncFromSyncIterUnwarpFunction; 99 class PromiseRecord; 100 class JSLocale; 101 class ResolvingFunctionsRecord; 102 class EcmaVM; 103 class Heap; 104 class SharedHeap; 105 class ConstantPool; 106 class Program; 107 class LayoutInfo; 108 class JSIntlBoundFunction; 109 class FreeObject; 110 class JSNativePointer; 111 class JSAPIArrayList; 112 class JSAPIArrayListIterator; 113 class JSAPIDeque; 114 class JSAPIDequeIterator; 115 class TaggedHashArray; 116 class LinkedNode; 117 class RBTreeNode; 118 class JSAPIHashMap; 119 class JSAPIHashSet; 120 class JSAPIHashMapIterator; 121 class JSAPIHashSetIterator; 122 class JSAPILightWeightMap; 123 class JSAPILightWeightMapIterator; 124 class JSAPILightWeightSet; 125 class JSAPILightWeightSetIterator; 126 class JSAPIQueue; 127 class JSAPIQueueIterator; 128 class JSAPIStack; 129 class JSAPIStackIterator; 130 class JSAPITreeSet; 131 class JSAPITreeMap; 132 class JSAPITreeSetIterator; 133 class JSAPITreeMapIterator; 134 class JSAPIVector; 135 class JSAPIVectorIterator; 136 class JSAPIBitVector; 137 class JSAPIBitVectorIterator; 138 class JSAPILinkedList; 139 class JSAPIList; 140 class JSAPILinkedListIterator; 141 class JSAPIListIterator; 142 class ModuleNamespace; 143 class NativeModuleFailureInfo; 144 class ImportEntry; 145 class LocalExportEntry; 146 class IndirectExportEntry; 147 class StarExportEntry; 148 class SourceTextModule; 149 class CjsModule; 150 class CjsRequire; 151 class CjsExports; 152 class ResolvedBinding; 153 class ResolvedIndexBinding; 154 class ResolvedRecordIndexBinding; 155 class ResolvedRecordBinding; 156 class BigInt; 157 class AsyncGeneratorRequest; 158 class AsyncIteratorRecord; 159 class JSAsyncGeneratorFunction; 160 class JSAsyncGeneratorObject; 161 class CellRecord; 162 class ClassLiteral; 163 164 namespace job { 165 class MicroJobQueue; 166 class PendingJob; 167 } // namespace job 168 class TransitionHandler; 169 class PrototypeHandler; 170 class TransWithProtoHandler; 171 class StoreAOTHandler; 172 class PropertyBox; 173 class ProtoChangeMarker; 174 class ProtoChangeDetails; 175 class MarkerCell; 176 class ProfileTypeInfo; 177 class MachineCode; 178 class ClassInfoExtractor; 179 class AOTLiteralInfo; 180 class ProfileTypeInfoCell; 181 class FunctionTemplate; 182 class VTable; 183 namespace kungfu { 184 class TSHClassGenerator; 185 } // namespace kungfu 186 187 enum class CompletionRecordType : uint8_t; 188 enum class PrimitiveType : uint8_t; 189 enum class IterationKind : uint8_t; 190 enum class MethodIndex : uint8_t; 191 enum class SharedTypes : uint8_t; 192 using ErrorType = base::ErrorType; 193 using base::ErrorType; 194 using ExtraProfileTypeInfo = pgo::ExtraProfileTypeInfo; 195 196 enum class RemoveSlots { YES, NO }; 197 enum class GrowMode { KEEP, GROW }; 198 enum class StackCheck { YES, NO }; 199 200 class ObjectFactory { 201 public: 202 static constexpr JSTaggedType FREE_MEMMORY_ADDRESS_ZAM_VALUE = 0xDEADFACE; 203 ObjectFactory(JSThread *thread, Heap *heap, SharedHeap *sHeap); 204 ~ObjectFactory() = default; 205 JSHandle<Method> NewMethodForNativeFunction(const void *func, FunctionKind kind = FunctionKind::NORMAL_FUNCTION, 206 kungfu::BuiltinsStubCSigns::ID builtinId = 207 kungfu::BuiltinsStubCSigns::INVALID, 208 MemSpaceType methodSpaceType = SHARED_OLD_SPACE); 209 210 JSHandle<ProfileTypeInfo> NewProfileTypeInfo(uint32_t length); 211 JSHandle<ConstantPool> NewConstantPool(uint32_t capacity); 212 JSHandle<Program> NewProgram(); 213 214 JSHandle<JSObject> PUBLIC_API GetJSError(const ErrorType &errorType, const char *data = nullptr, 215 StackCheck needCheckStack = StackCheck::YES); 216 217 JSHandle<JSObject> NewJSError(const ErrorType &errorType, const JSHandle<EcmaString> &message, 218 StackCheck needCheckStack = StackCheck::YES); 219 220 JSHandle<JSObject> NewJSAggregateError(); 221 222 JSHandle<TransitionHandler> NewTransitionHandler(); 223 224 JSHandle<PrototypeHandler> NewPrototypeHandler(); 225 226 JSHandle<TransWithProtoHandler> NewTransWithProtoHandler(); 227 228 JSHandle<StoreAOTHandler> NewStoreAOTHandler(); 229 230 JSHandle<JSObject> NewEmptyJSObject(uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 231 232 JSHandle<JSHClass> GetHClassByFunctionKind(const JSHandle<GlobalEnv> &env, FunctionKind kind); 233 234 // use for others create, prototype is Function.prototype 235 // use for native function 236 JSHandle<JSFunction> NewJSFunction(const JSHandle<GlobalEnv> &env, const void *nativeFunc = nullptr, 237 FunctionKind kind = FunctionKind::NORMAL_FUNCTION, 238 kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID, 239 MemSpaceType methodSpaceType = SHARED_OLD_SPACE); 240 JSHandle<JSFunction> NewSFunction(const JSHandle<GlobalEnv> &env, 241 const void *nativeFunc = nullptr, 242 FunctionKind kind = FunctionKind::NORMAL_FUNCTION, 243 kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID, 244 MemSpaceType spaceType = SHARED_OLD_SPACE); 245 void InitializeMethod(const MethodLiteral *methodLiteral, JSHandle<Method> &method); 246 // use for method 247 JSHandle<JSFunction> NewJSFunction(const JSHandle<GlobalEnv> &env, const JSHandle<Method> &method); 248 249 JSHandle<JSFunction> NewJSFunction(const JSHandle<Method> &methodHandle); 250 251 JSHandle<JSFunction> NewJSFunction(const JSHandle<Method> &methodHandle, 252 const JSHandle<JSTaggedValue> &homeObject); 253 254 JSHandle<JSFunction> NewJSNativeErrorFunction(const JSHandle<GlobalEnv> &env, const void *nativeFunc = nullptr); 255 256 JSHandle<JSFunction> NewSpecificTypedArrayFunction(const JSHandle<GlobalEnv> &env, 257 const void *nativeFunc = nullptr, 258 kungfu::BuiltinsStubCSigns::ID builtinId = 259 kungfu::BuiltinsStubCSigns::INVALID); 260 261 JSHandle<JSObject> OrdinaryNewJSObjectCreate(const JSHandle<JSTaggedValue> &proto); 262 263 JSHandle<JSObject> CreateNapiObject(); 264 JSHandle<JSObject> CreateNullJSObject(); 265 266 JSHandle<JSFunction> NewAotFunction(uint32_t numArgs, uintptr_t codeEntry); 267 268 JSHandle<JSBoundFunction> NewJSBoundFunction(const JSHandle<JSTaggedValue> &target, 269 const JSHandle<JSTaggedValue> &boundThis, 270 const JSHandle<TaggedArray> &args); 271 272 JSHandle<JSIntlBoundFunction> NewJSIntlBoundFunction(MethodIndex idx, int functionLength = 1); 273 274 JSHandle<JSProxyRevocFunction> NewJSProxyRevocFunction(const JSHandle<JSProxy> &proxy); 275 276 JSHandle<JSAsyncAwaitStatusFunction> NewJSAsyncAwaitStatusFunction(MethodIndex idx); 277 278 JSHandle<JSGeneratorObject> NewJSGeneratorObject(JSHandle<JSTaggedValue> generatorFunction); 279 280 JSHandle<JSAsyncFuncObject> NewJSAsyncFuncObject(); 281 JSHandle<JSAsyncGeneratorObject> NewJSAsyncGeneratorObject(JSHandle<JSTaggedValue> generatorFunction); 282 283 JSHandle<JSPrimitiveRef> NewJSPrimitiveRef(const JSHandle<JSFunction> &function, 284 const JSHandle<JSTaggedValue> &object); 285 JSHandle<JSPrimitiveRef> NewJSPrimitiveRef(PrimitiveType type, const JSHandle<JSTaggedValue> &object); 286 287 // get JSHClass for Ecma ClassLinker 288 JSHandle<GlobalEnv> NewGlobalEnv(JSHClass *globalEnvClass); 289 290 // get JSHClass for Ecma ClassLinker 291 JSHandle<LexicalEnv> NewLexicalEnv(int numSlots); 292 293 inline LexicalEnv *InlineNewLexicalEnv(int numSlots); 294 295 JSHandle<JSSymbol> NewEmptySymbol(); 296 297 JSHandle<JSSymbol> NewJSSymbol(); 298 299 JSHandle<JSSymbol> NewPrivateSymbol(); 300 301 JSHandle<JSSymbol> NewPrivateNameSymbol(const JSHandle<JSTaggedValue> &name); 302 303 JSHandle<JSSymbol> NewWellKnownSymbol(const JSHandle<JSTaggedValue> &name); 304 305 JSHandle<JSSymbol> NewPublicSymbol(const JSHandle<JSTaggedValue> &name); 306 307 JSHandle<JSSymbol> NewSymbolWithTable(const JSHandle<JSTaggedValue> &name); 308 309 JSHandle<JSSymbol> NewPrivateNameSymbolWithChar(std::string_view description); 310 311 JSHandle<JSSymbol> NewWellKnownSymbolWithChar(std::string_view description); 312 313 JSHandle<JSSymbol> NewPublicSymbolWithChar(std::string_view description); 314 315 JSHandle<JSSymbol> NewSymbolWithTableWithChar(std::string_view description); 316 317 JSHandle<AccessorData> NewAccessorData(); 318 JSHandle<AccessorData> NewInternalAccessor(void *setter, void *getter); 319 320 JSHandle<PromiseCapability> NewPromiseCapability(); 321 322 JSHandle<PromiseReaction> NewPromiseReaction(); 323 324 JSHandle<PromiseRecord> NewPromiseRecord(); 325 JSHandle<AsyncGeneratorRequest> NewAsyncGeneratorRequest(); 326 327 JSHandle<AsyncIteratorRecord> NewAsyncIteratorRecord(const JSHandle<JSTaggedValue> &itor, 328 const JSHandle<JSTaggedValue> &next, bool done); 329 330 JSHandle<ResolvingFunctionsRecord> NewResolvingFunctionsRecord(); 331 332 JSHandle<PromiseIteratorRecord> NewPromiseIteratorRecord(const JSHandle<JSTaggedValue> &itor, bool done); 333 334 JSHandle<job::MicroJobQueue> NewMicroJobQueue(); 335 336 JSHandle<job::PendingJob> NewPendingJob(const JSHandle<JSFunction> &func, const JSHandle<TaggedArray> &argv); 337 338 JSHandle<JSArray> NewJSArray(); 339 JSHandle<JSSharedArray> PUBLIC_API NewJSSArray(); 340 JSHandle<JSArray> PUBLIC_API NewJSArray(size_t length, JSHandle<JSHClass> &hclass); 341 JSHandle<TaggedArray> PUBLIC_API NewJsonFixedArray(size_t start, size_t length, 342 const std::vector<JSHandle<JSTaggedValue>> &vec); 343 JSHandle<TaggedArray> PUBLIC_API NewSJsonFixedArray(size_t start, size_t length, 344 const std::vector<JSHandle<JSTaggedValue>> &vec); 345 346 JSHandle<JSProxy> NewJSProxy(const JSHandle<JSTaggedValue> &target, const JSHandle<JSTaggedValue> &handler); 347 JSHandle<JSRealm> NewJSRealm(); 348 349 JSHandle<JSArguments> NewJSArguments(); 350 351 JSHandle<JSPrimitiveRef> NewJSString(const JSHandle<JSTaggedValue> &str, const JSHandle<JSTaggedValue> &newTarget); 352 353 template <typename Derived> ConvertListToArray(const JSThread * thread,const JSHandle<Derived> & list,uint32_t numberOfNodes)354 JSHandle<TaggedArray> ConvertListToArray(const JSThread *thread, const JSHandle<Derived> &list, 355 uint32_t numberOfNodes) 356 { 357 MemSpaceType spaceType = numberOfNodes < LENGTH_THRESHOLD ? MemSpaceType::SEMI_SPACE : MemSpaceType::OLD_SPACE; 358 JSHandle<TaggedArray> dstElements = NewTaggedArrayWithoutInit(numberOfNodes, spaceType); 359 if (numberOfNodes == 0) { 360 return dstElements; 361 } 362 int dataIndex = Derived::ELEMENTS_START_INDEX; 363 for (uint32_t i = 0; i < numberOfNodes; i++) { 364 dataIndex = list->GetElement(dataIndex + Derived::NEXT_PTR_OFFSET).GetInt(); 365 dstElements->Set(thread, i, list->GetElement(dataIndex)); 366 } 367 return dstElements; 368 } 369 370 JSHandle<JSObject> NewAndCopyJSArrayObject(JSHandle<JSObject> thisObjHandle, uint32_t newLength, 371 uint32_t oldLength, uint32_t k = 0); 372 JSHandle<TaggedArray> NewAndCopyTaggedArray(JSHandle<TaggedArray> &srcElements, uint32_t newLength, 373 uint32_t oldLength, uint32_t k = 0); 374 JSHandle<TaggedArray> NewAndCopyTaggedArraySkipBarrier(JSHandle<TaggedArray> &srcElements, uint32_t newLength, 375 uint32_t oldLength, uint32_t k = 0); 376 JSHandle<TaggedArray> NewAndCopySNameDictionary(JSHandle<TaggedArray> &srcElements, uint32_t length); 377 JSHandle<TaggedArray> NewAndCopyTaggedArrayByObject(JSHandle<JSObject> thisObjHandle, uint32_t newLength, 378 uint32_t oldLength, uint32_t k = 0); 379 JSHandle<MutantTaggedArray> NewAndCopyMutantTaggedArrayByObject(JSHandle<JSObject> thisObjHandle, 380 uint32_t newLength, uint32_t oldLength, 381 uint32_t k = 0); 382 JSHandle<TaggedArray> PUBLIC_API NewTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 383 JSHandle<TaggedArray> NewTaggedArray(uint32_t length, JSTaggedValue initVal, bool nonMovable); 384 JSHandle<TaggedArray> NewTaggedArray(uint32_t length, JSTaggedValue initVal, MemSpaceType spaceType); 385 // Copy on write array is allocated in nonmovable space by default. 386 JSHandle<COWTaggedArray> NewCOWTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 387 JSHandle<COWMutantTaggedArray> NewCOWMutantTaggedArray(uint32_t length, JSTaggedType initVal = base::SPECIAL_HOLE); 388 JSHandle<MutantTaggedArray> NewMutantTaggedArray(uint32_t length, JSTaggedType initVal = base::SPECIAL_HOLE); 389 JSHandle<TaggedArray> PUBLIC_API NewDictionaryArray(uint32_t length); 390 JSHandle<JSForInIterator> NewJSForinIterator(const JSHandle<JSTaggedValue> &obj, 391 const JSHandle<JSTaggedValue> keys, 392 const JSHandle<JSTaggedValue> cachedHclass); 393 394 JSHandle<ByteArray> NewByteArray(uint32_t length, uint32_t size, void *srcData = nullptr, 395 MemSpaceType spaceType = MemSpaceType::SEMI_SPACE); 396 397 JSHandle<PropertyBox> NewPropertyBox(const JSHandle<JSTaggedValue> &name); 398 399 JSHandle<ProtoChangeMarker> NewProtoChangeMarker(); 400 401 JSHandle<ProtoChangeDetails> NewProtoChangeDetails(); 402 403 JSHandle<MarkerCell> NewMarkerCell(); 404 JSHandle<BigInt> NewBigInt(uint32_t length); 405 // use for copy properties keys's array to another array 406 JSHandle<TaggedArray> ExtendArray(const JSHandle<TaggedArray> &old, uint32_t length, 407 JSTaggedValue initVal = JSTaggedValue::Hole(), 408 MemSpaceType type = MemSpaceType::SEMI_SPACE, 409 ElementsKind kind = ElementsKind::GENERIC); 410 JSHandle<TaggedArray> CopyPartArray(const JSHandle<TaggedArray> &old, uint32_t start, uint32_t end); 411 JSHandle<TaggedArray> PUBLIC_API CopyArray(const JSHandle<TaggedArray> &old, uint32_t oldLength, uint32_t newLength, 412 JSTaggedValue initVal = JSTaggedValue::Hole(), 413 MemSpaceType type = MemSpaceType::SEMI_SPACE, 414 ElementsKind kind = ElementsKind::GENERIC); 415 JSHandle<TaggedArray> CopyFromEnumCache(const JSHandle<TaggedArray> &old); 416 JSHandle<TaggedArray> CloneProperties(const JSHandle<TaggedArray> &old); 417 JSHandle<TaggedArray> CloneProperties(const JSHandle<TaggedArray> &old, const JSHandle<JSTaggedValue> &env, 418 const JSHandle<JSObject> &obj); 419 420 JSHandle<LayoutInfo> PUBLIC_API CreateLayoutInfo(int properties, MemSpaceType type = MemSpaceType::SEMI_SPACE, 421 GrowMode mode = GrowMode::GROW); 422 423 JSHandle<LayoutInfo> ExtendLayoutInfo(const JSHandle<LayoutInfo> &old, int properties); 424 425 JSHandle<LayoutInfo> CopyLayoutInfo(const JSHandle<LayoutInfo> &old); 426 427 JSHandle<LayoutInfo> CopyAndReSort(const JSHandle<LayoutInfo> &old, int end, int capacity); 428 429 JSHandle<EcmaString> GetEmptyString() const; 430 431 JSHandle<TaggedArray> EmptyArray() const; 432 433 JSHandle<MutantTaggedArray> EmptyMutantArray() const; 434 435 FreeObject *FillFreeObject(uintptr_t address, size_t size, RemoveSlots removeSlots = RemoveSlots::NO, 436 uintptr_t hugeObjectHead = 0); 437 438 TaggedObject *NewObject(const JSHandle<JSHClass> &hclass); 439 440 TaggedObject *NewNonMovableObject(const JSHandle<JSHClass> &hclass, uint32_t inobjPropCount = 0); 441 442 void InitializeExtraProperties(const JSHandle<JSHClass> &hclass, TaggedObject *obj, uint32_t inobjPropCount); 443 444 JSHandle<TaggedQueue> NewTaggedQueue(uint32_t length); 445 446 JSHandle<TaggedQueue> GetEmptyTaggedQueue() const; 447 448 JSHandle<JSSetIterator> NewJSSetIterator(const JSHandle<JSSet> &set, IterationKind kind); 449 JSHandle<JSSharedSetIterator> NewJSSetIterator(const JSHandle<JSSharedSet> &set, IterationKind kind); 450 451 JSHandle<JSRegExpIterator> NewJSRegExpIterator(const JSHandle<JSTaggedValue> &matcher, 452 const JSHandle<EcmaString> &inputStr, bool global, 453 bool fullUnicode); 454 455 JSHandle<JSMapIterator> NewJSMapIterator(const JSHandle<JSMap> &map, IterationKind kind); 456 457 JSHandle<JSSharedMapIterator> NewJSMapIterator(const JSHandle<JSSharedMap> &map, IterationKind kind); 458 459 JSHandle<JSArrayIterator> NewJSArrayIterator(const JSHandle<JSObject> &array, IterationKind kind); 460 JSHandle<JSSharedArrayIterator> NewJSSharedArrayIterator(const JSHandle<JSObject> &array, IterationKind kind); 461 462 JSHandle<CompletionRecord> NewCompletionRecord(CompletionRecordType type, JSHandle<JSTaggedValue> value); 463 464 JSHandle<GeneratorContext> NewGeneratorContext(); 465 466 JSHandle<JSPromiseReactionsFunction> CreateJSPromiseReactionsFunction(MethodIndex idx); 467 468 JSHandle<JSPromiseExecutorFunction> CreateJSPromiseExecutorFunction(); 469 470 JSHandle<JSAsyncModuleFulfilledFunction> CreateJSAsyncModuleFulfilledFunction(); 471 472 JSHandle<JSAsyncModuleRejectedFunction> CreateJSAsyncModuleRejectedFunction(); 473 474 JSHandle<JSPromiseAllResolveElementFunction> NewJSPromiseAllResolveElementFunction(); 475 476 JSHandle<JSPromiseAnyRejectElementFunction> NewJSPromiseAnyRejectElementFunction(); 477 478 JSHandle<JSPromiseAllSettledElementFunction> NewJSPromiseAllSettledResolveElementFunction(); 479 480 JSHandle<JSPromiseAllSettledElementFunction> NewJSPromiseAllSettledRejectElementFunction(); 481 482 JSHandle<JSPromiseFinallyFunction> NewJSPromiseThenFinallyFunction(); 483 484 JSHandle<JSPromiseFinallyFunction> NewJSPromiseCatchFinallyFunction(); 485 486 JSHandle<JSPromiseValueThunkOrThrowerFunction> NewJSPromiseValueThunkFunction(); 487 488 JSHandle<JSPromiseValueThunkOrThrowerFunction> NewJSPromiseThrowerFunction(); 489 490 JSHandle<JSAsyncGeneratorResNextRetProRstFtn> NewJSAsyGenResNextRetProRstFulfilledFtn(); 491 492 JSHandle<JSAsyncGeneratorResNextRetProRstFtn> NewJSAsyGenResNextRetProRstRejectedFtn(); 493 494 JSHandle<JSAsyncFromSyncIterUnwarpFunction> NewJSAsyncFromSyncIterUnwarpFunction(); 495 496 JSHandle<JSObject> CloneObjectLiteral(JSHandle<JSObject> object, const JSHandle<JSTaggedValue> &env, 497 bool canShareHClass = true); 498 JSHandle<JSObject> CloneObjectLiteral(JSHandle<JSObject> object); 499 JSHandle<JSArray> CloneArrayLiteral(JSHandle<JSArray> object); 500 JSHandle<JSFunction> CloneJSFunction(JSHandle<JSFunction> func); 501 JSHandle<JSFunction> CloneSFunction(JSHandle<JSFunction> func); 502 JSHandle<JSFunction> CloneClassCtor(JSHandle<JSFunction> ctor, const JSHandle<JSTaggedValue> &lexenv, 503 bool canShareHClass); 504 JSHandle<JSFunction> CreateJSFunctionFromTemplate(JSHandle<FunctionTemplate> funcTemp); 505 JSHandle<JSFunction> CreateSFunctionFromTemplate(JSHandle<FunctionTemplate> funcTemp); 506 507 void NewJSArrayBufferData(const JSHandle<JSArrayBuffer> &array, int32_t length); 508 void NewJSSendableArrayBufferData(const JSHandle<JSSendableArrayBuffer> &array, int32_t length); 509 JSHandle<JSSendableArrayBuffer> NewJSSendableArrayBuffer(int32_t length); 510 JSHandle<JSSendableArrayBuffer> NewJSSendableArrayBuffer(void *buffer, int32_t length, 511 const NativePointerCallback &deleter, void *data); 512 513 JSHandle<JSArrayBuffer> NewJSArrayBuffer(int32_t length); 514 515 JSHandle<JSArrayBuffer> NewJSArrayBuffer(void *buffer, int32_t length, const NativePointerCallback &deleter, 516 void *data, bool share = false); 517 518 JSHandle<JSDataView> NewJSDataView(JSHandle<JSArrayBuffer> buffer, uint32_t offset, uint32_t length); 519 520 void NewJSSharedArrayBufferData(const JSHandle<JSArrayBuffer> &array, int32_t length); 521 522 JSHandle<JSArrayBuffer> NewJSSharedArrayBuffer(int32_t length); 523 524 JSHandle<JSArrayBuffer> NewJSSharedArrayBuffer(void *buffer, int32_t length); 525 526 void NewJSRegExpByteCodeData(const JSHandle<JSRegExp> ®exp, void *buffer, size_t size); 527 528 template<typename T, typename S> 529 inline void NewJSIntlIcuData(const JSHandle<T> &obj, const S &icu, const NativePointerCallback &callback); 530 531 EcmaString *PUBLIC_API InternString(const JSHandle<JSTaggedValue> &key); 532 533 inline JSHandle<JSNativePointer> NewJSNativePointer(void *externalPointer, 534 const NativePointerCallback &callBack = nullptr, 535 void *data = nullptr, 536 bool nonMovable = false, 537 size_t nativeBindingsize = 0, 538 Concurrent isConcurrent = Concurrent::NO, 539 NativeFlag flag = NativeFlag::NO_DIV); 540 541 JSHandle<JSObject> NewOldSpaceObjLiteralByHClass(const JSHandle<JSHClass> &hclass); 542 JSHandle<JSHClass> SetLayoutInObjHClass(const JSHandle<TaggedArray> &properties, size_t length, 543 const JSHandle<JSHClass> &objClass); 544 static bool CanObjectLiteralHClassCache(size_t length); 545 JSHandle<JSHClass> CreateObjectLiteralRootHClass(size_t length); 546 JSHandle<JSHClass> GetObjectLiteralRootHClass(size_t literalLength, size_t maxPropsNum); 547 JSHandle<JSHClass> GetObjectLiteralHClass(const JSHandle<TaggedArray> &properties, size_t length); 548 // only use for creating Function.prototype and Function 549 JSHandle<JSFunction> NewJSFunctionByHClass(const JSHandle<Method> &method, const JSHandle<JSHClass> &clazz, 550 MemSpaceType type = MemSpaceType::SEMI_SPACE); 551 // for native function 552 JSTaggedValue GetReadOnlyMethodForNativeFunction(FunctionKind kind); 553 JSHandle<JSFunction> NewNativeFunctionByHClass(const JSHandle<JSHClass> &clazz, 554 const void *nativeFunc, 555 FunctionKind kind, 556 MemSpaceType type = MemSpaceType::SEMI_SPACE); 557 JSHandle<JSFunction> NewJSFunctionByHClass(const void *func, const JSHandle<JSHClass> &clazz, 558 FunctionKind kind = FunctionKind::NORMAL_FUNCTION); 559 JSHandle<JSFunction> NewJSFunctionByHClassWithoutAccessor(const void *func, 560 const JSHandle<JSHClass> &clazz, FunctionKind kind = FunctionKind::NORMAL_FUNCTION); 561 562 JSHandle<Method> NewMethod(const MethodLiteral *methodLiteral, MemSpaceType spaceType = OLD_SPACE); 563 564 JSHandle<Method> NewMethod(const JSPandaFile *jsPandaFile, MethodLiteral *methodLiteral, 565 JSHandle<ConstantPool> constpool, uint32_t entryIndex, 566 bool needSetAotFlag, bool *canFastCall = nullptr); 567 568 // used for creating jsobject by constructor 569 JSHandle<JSObject> PUBLIC_API NewJSObjectByConstructor(const JSHandle<JSFunction> &constructor, 570 const JSHandle<JSTaggedValue> &newTarget); 571 JSHandle<JSObject> NewJSObjectByConstructor(JSHandle<GlobalEnv> env, 572 const JSHandle<JSFunction> &constructor, uint32_t inlinedProps); 573 JSHandle<JSObject> NewJSObjectByConstructor(const JSHandle<JSFunction> &constructor, 574 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 575 void InitializeJSObject(const JSHandle<JSObject> &obj, const JSHandle<JSHClass> &jshclass); 576 577 JSHandle<JSObject> NewJSObjectWithInit(const JSHandle<JSHClass> &jshclass); 578 uintptr_t NewSpaceBySnapshotAllocator(size_t size); 579 TaggedObject *NewMachineCodeObject(size_t length, MachineCodeDesc &desc); 580 JSHandle<MachineCode> SetMachineCodeObjectData(TaggedObject *obj, size_t length, 581 const MachineCodeDesc &desc, JSHandle<Method> &method, RelocMap &relocInfo); 582 JSHandle<MachineCode> NewMachineCodeObject(size_t length, const MachineCodeDesc &desc, 583 JSHandle<Method> &method, RelocMap &relocInfo); 584 JSHandle<ClassInfoExtractor> NewClassInfoExtractor(JSHandle<JSTaggedValue> method); 585 JSHandle<ClassLiteral> NewClassLiteral(); 586 587 // ----------------------------------- new string ---------------------------------------- 588 JSHandle<EcmaString> PUBLIC_API NewFromASCII(std::string_view data); 589 JSHandle<EcmaString> NewFromUtf8WithoutStringTable(std::string_view data); 590 JSHandle<EcmaString> PUBLIC_API NewFromUtf8(std::string_view data); 591 JSHandle<EcmaString> NewFromUtf8(std::string_view data, bool canBeCompress); 592 JSHandle<EcmaString> NewFromUtf8ReadOnly(std::string_view data); 593 JSHandle<EcmaString> NewFromASCIISkippingStringTable(std::string_view data); 594 JSHandle<EcmaString> NewFromUtf16WithoutStringTable(std::u16string_view data); 595 JSHandle<EcmaString> NewFromUtf16(std::u16string_view data); 596 597 JSHandle<EcmaString> PUBLIC_API NewFromStdString(const std::string &data); 598 599 JSHandle<EcmaString> NewFromUtf8WithoutStringTable(const uint8_t *utf8Data, uint32_t utf8Len); 600 JSHandle<EcmaString> NewFromUtf8(const uint8_t *utf8Data, uint32_t utf8Len); 601 602 JSHandle<EcmaString> NewFromUtf16WithoutStringTable(const uint16_t *utf16Data, uint32_t utf16Len); 603 JSHandle<EcmaString> PUBLIC_API NewFromUtf16(const uint16_t *utf16Data, uint32_t utf16Len); 604 JSHandle<EcmaString> NewFromUtf16Compress(const uint16_t *utf16Data, uint32_t utf16Len); 605 JSHandle<EcmaString> NewFromUtf16NotCompress(const uint16_t *utf16Data, uint32_t utf16Len); 606 607 JSHandle<EcmaString> NewFromUtf8Literal(const uint8_t *utf8Data, uint32_t utf8Len); 608 JSHandle<EcmaString> PUBLIC_API NewFromUtf8LiteralCompress(const uint8_t *utf8Data, uint32_t utf8Len); 609 JSHandle<EcmaString> PUBLIC_API NewFromUtf8LiteralCompressSubString(const JSHandle<EcmaString> &string, 610 uint32_t offset, uint32_t utf8Len); 611 JSHandle<EcmaString> PUBLIC_API NewCompressedUtf8(const uint8_t *utf8Data, uint32_t utf8Len); 612 JSHandle<EcmaString> PUBLIC_API NewCompressedUtf8SubString(const JSHandle<EcmaString> &string, 613 uint32_t offset, uint32_t utf8Len); 614 JSHandle<EcmaString> NewFromUtf16Literal(const uint16_t *utf16Data, uint32_t utf16Len); 615 JSHandle<EcmaString> NewFromUtf16LiteralCompress(const uint16_t *utf16Data, uint32_t utf16Len); 616 JSHandle<EcmaString> PUBLIC_API NewFromUtf16LiteralNotCompress(const uint16_t *utf16Data, uint32_t utf16Len); 617 618 inline EcmaString *AllocLineStringObject(size_t size); 619 inline EcmaString *AllocLineStringObjectNoGC(size_t size); 620 inline EcmaString *AllocOldSpaceLineStringObject(size_t size); 621 inline EcmaString *AllocReadOnlyLineStringObject(size_t size); 622 inline EcmaString *AllocNonMovableLineStringObject(size_t size); 623 inline EcmaString *AllocSlicedStringObject(MemSpaceType type); 624 inline EcmaString *AllocConstantStringObject(MemSpaceType type); 625 inline EcmaString *AllocTreeStringObject(); 626 627 JSHandle<EcmaString> ConcatFromString(const JSHandle<EcmaString> &firstString, 628 const JSHandle<EcmaString> &secondString); 629 630 // used for creating Function 631 JSHandle<JSObject> PUBLIC_API NewJSObject(const JSHandle<JSHClass> &jshclass); 632 633 // used for creating jshclass in Builtins, Function, Class_Linker 634 JSHandle<JSHClass> NewEcmaHClass(uint32_t size, JSType type, const JSHandle<JSTaggedValue> &prototype); 635 JSHandle<JSHClass> NewEcmaHClass(uint32_t size, uint32_t inlinedProps, JSType type, 636 const JSHandle<JSTaggedValue> &prototype); 637 638 // used for creating jshclass in Builtins, Function, Class_Linker 639 JSHandle<JSHClass> PUBLIC_API NewEcmaHClass(uint32_t size, JSType type, 640 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 641 642 JSHandle<JSHClass> PUBLIC_API NewEcmaHClass(uint32_t size, uint32_t inlinedProps, JSType type, 643 const JSHandle<JSTaggedValue> &prototype, const JSHandle<JSTaggedValue> &layout); 644 645 // It is used to provide iterators for non ECMA standard jsapi containers. 646 JSHandle<JSAPIPlainArray> NewJSAPIPlainArray(uint32_t capacity); 647 JSHandle<JSAPIPlainArrayIterator> NewJSAPIPlainArrayIterator(const JSHandle<JSAPIPlainArray> &plainarray, 648 IterationKind kind); 649 JSHandle<JSAPIArrayList> NewJSAPIArrayList(uint32_t capacity); 650 651 JSHandle<JSAPILightWeightMapIterator> NewJSAPILightWeightMapIterator(const JSHandle<JSAPILightWeightMap> &obj, 652 IterationKind kind); 653 JSHandle<JSAPILightWeightSetIterator> NewJSAPILightWeightSetIterator(const JSHandle<JSAPILightWeightSet> &obj, 654 IterationKind kind); 655 JSHandle<TaggedArray> CopyQueue(const JSHandle<TaggedArray> &old, uint32_t newLength, 656 uint32_t front, uint32_t tail); 657 JSHandle<JSAPIQueueIterator> NewJSAPIQueueIterator(const JSHandle<JSAPIQueue> &queue); 658 JSHandle<TaggedArray> CopyDeque(const JSHandle<TaggedArray> &old, uint32_t newLength, uint32_t oldLength, 659 uint32_t first, uint32_t last); 660 JSHandle<JSAPIDequeIterator> NewJSAPIDequeIterator(const JSHandle<JSAPIDeque> &deque); 661 JSHandle<JSAPIArrayListIterator> NewJSAPIArrayListIterator(const JSHandle<JSAPIArrayList> &arrayList); 662 JSHandle<JSAPIList> NewJSAPIList(); 663 JSHandle<JSAPILinkedList> NewJSAPILinkedList(); 664 JSHandle<JSAPILinkedListIterator> NewJSAPILinkedListIterator(const JSHandle<JSAPILinkedList> &linkedList); 665 JSHandle<JSAPIListIterator> NewJSAPIListIterator(const JSHandle<JSAPIList> &list); 666 JSHandle<JSAPITreeMapIterator> NewJSAPITreeMapIterator(const JSHandle<JSAPITreeMap> &map, IterationKind kind); 667 JSHandle<JSAPITreeSetIterator> NewJSAPITreeSetIterator(const JSHandle<JSAPITreeSet> &set, IterationKind kind); 668 JSHandle<JSAPIStackIterator> NewJSAPIStackIterator(const JSHandle<JSAPIStack> &stack); 669 JSHandle<JSAPIVector> NewJSAPIVector(uint32_t capacity); 670 JSHandle<JSAPIVectorIterator> NewJSAPIVectorIterator(const JSHandle<JSAPIVector> &vector); 671 JSHandle<JSAPIBitVector> NewJSAPIBitVector(uint32_t capacity); 672 JSHandle<JSAPIBitVectorIterator> NewJSAPIBitVectorIterator(const JSHandle<JSAPIBitVector> &bitVector); 673 JSHandle<JSAPIHashMapIterator> NewJSAPIHashMapIterator(const JSHandle<JSAPIHashMap> &hashMap, IterationKind kind); 674 JSHandle<JSAPIHashSetIterator> NewJSAPIHashSetIterator(const JSHandle<JSAPIHashSet> &hashSet, IterationKind kind); 675 JSHandle<TaggedHashArray> NewTaggedHashArray(uint32_t length); 676 JSHandle<LinkedNode> NewLinkedNode(int hash, const JSHandle<JSTaggedValue> &key, 677 const JSHandle<JSTaggedValue> &value, 678 const JSHandle<LinkedNode> &next); 679 JSHandle<RBTreeNode> NewTreeNode(int hash, const JSHandle<JSTaggedValue> &key, 680 const JSHandle<JSTaggedValue> &value); 681 // --------------------------------------module-------------------------------------------- 682 JSHandle<ModuleNamespace> NewModuleNamespace(); 683 JSHandle<NativeModuleFailureInfo> NewNativeModuleFailureInfo(); 684 JSHandle<ImportEntry> NewImportEntry(); 685 JSHandle<ImportEntry> NewImportEntry(const uint32_t moduleRequestIdx, 686 const JSHandle<JSTaggedValue> &importName, 687 const JSHandle<JSTaggedValue> &localName, 688 SharedTypes sharedTypes); 689 JSHandle<LocalExportEntry> NewLocalExportEntry(); 690 JSHandle<LocalExportEntry> NewLocalExportEntry( 691 const JSHandle<JSTaggedValue> &exportName, const JSHandle<JSTaggedValue> &localName, 692 const uint32_t index, SharedTypes sharedTypes); 693 JSHandle<IndirectExportEntry> NewIndirectExportEntry(); 694 JSHandle<IndirectExportEntry> NewIndirectExportEntry(const JSHandle<JSTaggedValue> &exportName, 695 const uint32_t moduleRequestIdx, 696 const JSHandle<JSTaggedValue> &importName, 697 SharedTypes sharedTypes); 698 JSHandle<StarExportEntry> NewStarExportEntry(); 699 JSHandle<StarExportEntry> NewStarExportEntry(const uint32_t moduleRequestIdx, 700 SharedTypes sharedTypes); 701 JSHandle<SourceTextModule> NewSourceTextModule(); 702 JSHandle<ResolvedBinding> NewResolvedBindingRecord(); 703 JSHandle<ResolvedBinding> NewResolvedBindingRecord(const JSHandle<SourceTextModule> &module, 704 const JSHandle<JSTaggedValue> &bindingName); 705 JSHandle<ResolvedIndexBinding> NewResolvedIndexBindingRecord(); 706 JSHandle<ResolvedIndexBinding> NewResolvedIndexBindingRecord( 707 const JSHandle<SourceTextModule> &module, int32_t index); 708 709 JSHandle<CellRecord> NewCellRecord(); 710 JSHandle<JSFunction> NewJSAsyncGeneratorFunction(const JSHandle<Method> &method); 711 // --------------------------------------require-------------------------------------------- 712 JSHandle<CjsModule> NewCjsModule(); 713 JSHandle<CjsExports> NewCjsExports(); 714 JSHandle<CjsRequire> NewCjsRequire(); 715 716 JSHandle<JSHClass> CreateIteratorResultInstanceClass(const JSHandle<GlobalEnv> &env); 717 718 // --------------------------------------old space object-------------------------------------------- 719 JSHandle<JSObject> NewOldSpaceJSObject(const JSHandle<JSHClass> &jshclass); 720 TaggedObject *NewOldSpaceObject(const JSHandle<JSHClass> &hclass); 721 JSHandle<TaggedArray> NewOldSpaceTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 722 723 // ---------------------------------New objects used internally-------------------------------------- 724 JSHandle<JSArray> NewJSStableArrayWithElements(const JSHandle<TaggedArray> &elements); 725 726 // ---------------------------------------Used by AOT------------------------------------------------ 727 JSHandle<AOTLiteralInfo> NewAOTLiteralInfo(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 728 JSHandle<ExtraProfileTypeInfo> NewExtraProfileTypeInfo(); 729 JSHandle<ProfileTypeInfoCell> NewProfileTypeInfoCell(const JSHandle<JSTaggedValue> &value); 730 JSHandle<FunctionTemplate> NewFunctionTemplate( 731 const JSHandle<Method> &method, const JSHandle<JSTaggedValue> &module, int32_t length); 732 JSHandle<VTable> NewVTable(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 733 JSHandle<JSHClass> NewEcmaHClass(JSHClass *hclass, uint32_t size, JSType type, 734 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 735 736 // napi interface to create object with initial inline properties 737 JSHandle<JSTaggedValue> CreateJSObjectWithProperties(size_t propertyCount, const Local<JSValueRef> *keys, 738 const PropertyDescriptor *attributes); 739 JSHandle<JSTaggedValue> CreateJSObjectWithNamedProperties(size_t propertyCount, const char **keys, 740 const Local<JSValueRef> *values); 741 // Fill the given free memory range with special zam value. 742 void FillFreeMemoryRange(uintptr_t start, uintptr_t end); 743 744 // -----------------------------------shared object----------------------------------------- 745 JSHandle<JSObject> PUBLIC_API NewSharedOldSpaceJSObject(const JSHandle<JSHClass> &jshclass); 746 747 JSHandle<JSObject> PUBLIC_API NewSharedOldSpaceJSObjectWithInit(const JSHandle<JSHClass> &jshclass); 748 749 TaggedObject *NewSharedOldSpaceObject(const JSHandle<JSHClass> &hclass); 750 751 JSHandle<JSTaggedValue> CreateSObjectWithProperties(std::vector<PropertyDescriptor> &descs); 752 753 JSHandle<JSHClass> PUBLIC_API NewSEcmaHClass(uint32_t size, JSType type, uint32_t inlinedProps); 754 755 JSHandle<JSHClass> PUBLIC_API NewSEcmaHClass(JSHClass *hclass, uint32_t size, JSType type, 756 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 757 758 JSHandle<JSHClass> PUBLIC_API NewSEcmaHClass(uint32_t size, uint32_t inlinedProps, JSType type, 759 const JSHandle<JSTaggedValue> &prototype, const JSHandle<JSTaggedValue> &layout); 760 761 JSHandle<JSHClass> PUBLIC_API NewSEcmaHClassDictMode(uint32_t size, uint32_t inlinedProps, JSType type, 762 const JSHandle<JSTaggedValue> &prototype); 763 764 JSHandle<JSHClass> NewSEcmaHClassClass(JSHClass *hclass, uint32_t size, JSType type); 765 766 JSHandle<JSHClass> InitSClassClass(); 767 768 JSHandle<JSHClass> NewSEcmaReadOnlyHClass(JSHClass *hclass, uint32_t size, JSType type, 769 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 770 771 JSHandle<TaggedArray> SharedEmptyArray() const; 772 773 JSHandle<TaggedArray> CopySArray(const JSHandle<TaggedArray> &old, uint32_t oldLength, uint32_t newLength, 774 JSTaggedValue initVal = JSTaggedValue::Hole(), 775 ElementsKind kind = ElementsKind::GENERIC); 776 777 JSHandle<TaggedArray> ExtendSArray(const JSHandle<TaggedArray> &old, uint32_t length, 778 JSTaggedValue initVal, [[maybe_unused]] ElementsKind kind); 779 780 JSHandle<Method> NewSMethodForNativeFunction(const void *func, FunctionKind kind = FunctionKind::NORMAL_FUNCTION, 781 kungfu::BuiltinsStubCSigns::ID builtinId = 782 kungfu::BuiltinsStubCSigns::INVALID, 783 MemSpaceType spaceType = SHARED_OLD_SPACE); 784 785 JSHandle<JSFunction> NewSFunctionByHClass(const JSHandle<Method> &methodHandle, 786 const JSHandle<JSHClass> &hclass); 787 // for native function 788 JSHandle<JSFunction> NewNativeSFunctionByHClass(const JSHandle<JSHClass> &hclass, 789 const void *nativeFunc, 790 FunctionKind kind); 791 792 JSHandle<JSFunction> NewSFunctionByHClass(const void *func, const JSHandle<JSHClass> &hclass, 793 FunctionKind kind, 794 kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID, 795 MemSpaceType spaceType = SHARED_OLD_SPACE); 796 797 JSHandle<JSFunction> NewSFunctionWithAccessor( 798 const void *func, 799 const JSHandle<JSHClass> &hclass, 800 FunctionKind kind, 801 kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID, 802 MemSpaceType spaceType = SHARED_OLD_SPACE); 803 804 JSHandle<Method> NewSMethod(const MethodLiteral *methodLiteral, MemSpaceType methodSpaceType = SHARED_OLD_SPACE); 805 806 JSHandle<Method> PUBLIC_API NewSMethod(const JSPandaFile *jsPandaFile, 807 MethodLiteral *methodLiteral, 808 JSHandle<ConstantPool> constpool, 809 uint32_t entryIndex, 810 bool needSetAotFlag, 811 bool *canFastCall = nullptr); 812 813 JSHandle<ConstantPool> NewSConstantPool(uint32_t capacity); 814 815 JSHandle<AOTLiteralInfo> NewSAOTLiteralInfo(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 816 817 JSHandle<COWTaggedArray> NewSCOWTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 818 819 JSHandle<ClassLiteral> NewSClassLiteral(); 820 821 JSHandle<ClassInfoExtractor> NewSClassInfoExtractor(JSHandle<JSTaggedValue> method); 822 823 JSHandle<TaggedArray> NewSOldSpaceTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 824 825 JSHandle<TaggedArray> PUBLIC_API NewSTaggedArray(uint32_t length, 826 JSTaggedValue initVal = JSTaggedValue::Hole(), 827 MemSpaceType spaceType = SHARED_OLD_SPACE); 828 829 JSHandle<AccessorData> NewSAccessorData(); 830 831 JSHandle<SourceTextModule> NewSSourceTextModule(); 832 833 JSHandle<ModuleNamespace> NewSModuleNamespace(); 834 835 JSHandle<ImportEntry> NewSImportEntry(const uint32_t moduleRequestIdx, 836 const JSHandle<JSTaggedValue> &importName, 837 const JSHandle<JSTaggedValue> &localName); 838 839 JSHandle<LocalExportEntry> NewSLocalExportEntry(const JSHandle<JSTaggedValue> &exportName, 840 const JSHandle<JSTaggedValue> &localName, const uint32_t index); 841 842 JSHandle<IndirectExportEntry> NewSIndirectExportEntry(const JSHandle<JSTaggedValue> &exportName, 843 const uint32_t moduleRequestIdx, 844 const JSHandle<JSTaggedValue> &importName); 845 846 JSHandle<StarExportEntry> NewSStarExportEntry(const uint32_t moduleRequestIdx); 847 848 JSHandle<ResolvedIndexBinding> NewSResolvedIndexBindingRecord(); 849 850 JSHandle<ResolvedIndexBinding> NewSResolvedIndexBindingRecord(const JSHandle<SourceTextModule> &module, 851 int32_t index); 852 853 JSHandle<ResolvedBinding> NewSResolvedBindingRecord(); 854 855 JSHandle<ResolvedBinding> NewSResolvedBindingRecord(const JSHandle<SourceTextModule> &module, 856 const JSHandle<JSTaggedValue> &bindingName); 857 858 JSHandle<ResolvedRecordIndexBinding> NewSResolvedRecordIndexBindingRecord(); 859 860 JSHandle<ResolvedRecordIndexBinding> NewSResolvedRecordIndexBindingRecord( 861 const JSHandle<EcmaString> &moduleRecord, const JSHandle<EcmaString> &abcFileName, int32_t index); 862 863 JSHandle<ResolvedRecordBinding> NewSResolvedRecordBindingRecord(); 864 865 JSHandle<ResolvedRecordBinding> NewSResolvedRecordBindingRecord( 866 const JSHandle<EcmaString> &moduleRecord, const JSHandle<JSTaggedValue> &bindingName); 867 868 JSHandle<LayoutInfo> CopyAndReSortSLayoutInfo(const JSHandle<LayoutInfo> &old, int end, int capacity); 869 870 JSHandle<LayoutInfo> PUBLIC_API CreateSLayoutInfo(uint32_t properties); 871 872 JSHandle<LayoutInfo> PUBLIC_API NewSEmptyLayoutInfo(); 873 874 JSHandle<ProfileTypeInfoCell> NewSEmptyProfileTypeInfoCell(); 875 876 JSHandle<Method> NewSEmptyNativeFunctionMethod(FunctionKind kind); 877 878 JSHandle<FunctionTemplate> NewSFunctionTemplate( 879 const JSHandle<Method> &method, const JSHandle<JSTaggedValue> &module, int32_t length); 880 881 JSHandle<TaggedArray> NewSEmptyArray(); // only used for EcmaVM. 882 883 JSHandle<MutantTaggedArray> NewSEmptyMutantArray(); 884 885 JSHandle<TaggedArray> PUBLIC_API NewSDictionaryArray(uint32_t length); 886 887 JSHandle<TaggedArray> NewSTaggedArrayWithoutInit(uint32_t length, 888 MemSpaceType spaceType = MemSpaceType::SHARED_OLD_SPACE); 889 890 JSHandle<JSHClass> CreateSFunctionClass(uint32_t size, JSType type, 891 const JSHandle<JSTaggedValue> &prototype, 892 bool isAccessor = true, bool setProto = false); 893 894 JSHandle<JSNativePointer> NewSJSNativePointer(void *externalPointer, 895 const NativePointerCallback &callBack, 896 void *data = nullptr, 897 bool nonMovable = false, 898 size_t nativeBindingsize = 0, 899 NativeFlag flag = NativeFlag::NO_DIV); 900 901 JSHandle<JSNativePointer> NewSReadOnlyJSNativePointer(void *externalPointer); 902 903 JSHandle<AccessorData> NewSInternalAccessor(void *setter, void *getter); 904 905 JSHandle<JSSymbol> NewSWellKnownSymbol(const JSHandle<JSTaggedValue> &name); 906 JSHandle<JSSymbol> NewSConstantPrivateSymbol(); 907 JSHandle<JSSymbol> NewSEmptySymbol(); 908 JSHandle<JSSymbol> NewSWellKnownSymbolWithChar(std::string_view description); 909 JSHandle<JSSymbol> NewSPublicSymbolWithChar(std::string_view description); 910 JSHandle<JSSymbol> NewSPublicSymbol(const JSHandle<JSTaggedValue> &name); 911 JSHandle<SendableEnv> NewSendableEnv(int numSlots); 912 JSHandle<JSFunction> NewJSSendableFunction(const JSHandle<Method> &methodHandle); 913 914 void SetCodeEntryToFunctionFromMethod(const JSHandle<JSFunction> &func, const JSHandle<Method> &method); 915 void SetNativePointerToFunctionFromMethod(const JSHandle<JSFunctionBase> &func, const JSHandle<Method> &method); 916 917 private: 918 friend class GlobalEnv; 919 friend class GlobalEnvConstants; 920 friend class EcmaString; 921 friend class SnapshotProcessor; 922 friend class SingleCharTable; 923 void InitObjectFields(const TaggedObject *object); 924 925 JSThread *thread_ {nullptr}; 926 bool isTriggerGc_ {false}; 927 bool triggerSemiGC_ {false}; 928 929 EcmaVM *vm_ {nullptr}; 930 Heap *heap_ {nullptr}; 931 SharedHeap *sHeap_ {nullptr}; 932 933 static constexpr uint32_t LENGTH_THRESHOLD = 50; 934 935 NO_COPY_SEMANTIC(ObjectFactory); 936 NO_MOVE_SEMANTIC(ObjectFactory); 937 938 void NewObjectHook() const; 939 void NewSObjectHook() const; 940 // used for creating jshclass in GlobalEnv, EcmaVM 941 JSHandle<JSHClass> NewEcmaHClassClass(JSHClass *hclass, uint32_t size, JSType type); 942 943 JSHandle<JSHClass> NewEcmaReadOnlyHClass(JSHClass *hclass, uint32_t size, JSType type, 944 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 945 JSHandle<JSHClass> InitClassClass(); 946 947 // used to create nonmovable js_object 948 JSHandle<JSObject> NewNonMovableJSObject(const JSHandle<JSHClass> &jshclass); 949 950 // used to create read only utf8 string at global constants 951 JSHandle<EcmaString> NewFromASCIIReadOnly(std::string_view data); 952 953 // used for creating Function 954 JSHandle<JSFunction> NewJSFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &hclass); 955 JSHandle<JSHClass> CreateObjectClass(const JSHandle<TaggedArray> &keys, const JSHandle<TaggedArray> &values); 956 JSHandle<JSHClass> CreateObjectClass(const JSHandle<TaggedArray> &properties, size_t length); 957 JSHandle<JSHClass> CreateFunctionClass(FunctionKind kind, uint32_t size, JSType type, 958 const JSHandle<JSTaggedValue> &prototype); 959 JSHandle<JSHClass> CreateBoundFunctionClass(); 960 JSHandle<JSHClass> CreateDefaultClassPrototypeHClass(JSHClass *hclass); 961 JSHandle<JSHClass> CreateDefaultClassConstructorHClass(JSHClass *hclass); 962 963 // used for creating ref.prototype in buildins, such as Number.prototype 964 JSHandle<JSPrimitiveRef> NewJSPrimitiveRef(const JSHandle<JSHClass> &hclass, 965 const JSHandle<JSTaggedValue> &object); 966 967 JSHandle<EcmaString> GetStringFromStringTable(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress) const; 968 JSHandle<EcmaString> GetCompressedSubStringFromStringTable(const JSHandle<EcmaString> &string, uint32_t offset, 969 uint32_t utf8Len) const; 970 JSHandle<EcmaString> GetStringFromStringTableReadOnly(const uint8_t *utf8Data, uint32_t utf8Len, 971 bool canBeCompress = true) const; 972 // For MUtf-8 string data 973 EcmaString *PUBLIC_API GetRawStringFromStringTable(StringData sd, 974 MemSpaceType type = MemSpaceType::SHARED_OLD_SPACE, 975 bool isConstantString = false, uint32_t idOffset = 0) const; 976 EcmaString *GetRawStringFromStringTableWithoutJSHandle(StringData sd, 977 MemSpaceType type = MemSpaceType::SHARED_OLD_SPACE, 978 bool isConstantString = false, 979 uint32_t idOffset = 0) const; 980 981 JSHandle<EcmaString> GetStringFromStringTable(const uint16_t *utf16Data, uint32_t utf16Len, 982 bool canBeCompress) const; 983 984 JSHandle<EcmaString> GetStringFromStringTable(const JSHandle<EcmaString> &firstString, 985 const JSHandle<EcmaString> &secondString); 986 987 JSHandle<JSHClass> CreateJSArguments(const JSHandle<GlobalEnv> &env); 988 989 JSHandle<JSHClass> CreateJSArrayInstanceClass(JSHandle<JSTaggedValue> proto, 990 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 991 992 JSHandle<JSHClass> CreateJSRegExpInstanceClass(JSHandle<JSTaggedValue> proto); 993 994 inline TaggedObject *AllocObjectWithSpaceType(size_t size, JSHClass *cls, MemSpaceType type); 995 JSHandle<TaggedArray> NewTaggedArrayWithoutInit(uint32_t length, MemSpaceType spaceType); 996 997 // For object with many properties, directly create new HClass instead of searching on transitions 998 JSHandle<JSTaggedValue> CreateLargeJSObjectWithProperties(size_t propertyCount, 999 const Local<JSValueRef> *keys, 1000 const PropertyDescriptor *descs); 1001 JSHandle<JSTaggedValue> CreateLargeJSObjectWithNamedProperties(size_t propertyCount, const char **keys, 1002 const Local<JSValueRef> *values); 1003 // For object with numerous properties, directly create it in dictionary mode 1004 JSHandle<JSTaggedValue> CreateDictionaryJSObjectWithProperties(size_t propertyCount, 1005 const Local<JSValueRef> *keys, 1006 const PropertyDescriptor *descs); 1007 JSHandle<JSTaggedValue> CreateDictionaryJSObjectWithNamedProperties(size_t propertyCount, const char **keys, 1008 const Local<JSValueRef> *values); 1009 1010 JSHandle<MutantTaggedArray> NewMutantTaggedArrayWithoutInit(uint32_t length, MemSpaceType spaceType); 1011 void MergeSendableClassElementsDic(JSHandle<TaggedArray> &elements, 1012 const JSHandle<JSTaggedValue> &elementsDicOfCtorVal, 1013 const JSHandle<JSTaggedValue> &elementsDicOfTrgVal); 1014 1015 friend class Builtins; // create builtins object need hclass 1016 friend class JSFunction; // create prototype_or_hclass need hclass 1017 friend class JSHClass; // HC transition need hclass 1018 friend class EcmaVM; // hold the factory instance 1019 friend class JsVerificationTest; 1020 friend class PandaFileTranslator; 1021 friend class LiteralDataExtractor; 1022 friend class RuntimeStubs; 1023 friend class ClassInfoExtractor; 1024 friend class ModuleDataExtractor; 1025 friend class ModuleDataAccessor; 1026 friend class ConstantPool; 1027 friend class EcmaContext; 1028 friend class kungfu::TSHClassGenerator; 1029 friend class panda::FunctionRef; 1030 }; 1031 1032 class ClassLinkerFactory { 1033 private: 1034 friend class GlobalEnv; // root class in class_linker need hclass 1035 friend class EcmaVM; // root class in class_linker need hclass 1036 }; 1037 } // namespace panda::ecmascript 1038 #endif // ECMASCRIPT_OBJECT_FACTORY_H 1039