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