1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ECMASCRIPT_OBJECT_FACTORY_H 17 #define ECMASCRIPT_OBJECT_FACTORY_H 18 19 #include "ecmascript/base/error_type.h" 20 #include "ecmascript/ecma_string.h" 21 #include "ecmascript/js_function_kind.h" 22 #include "ecmascript/js_handle.h" 23 #include "ecmascript/js_hclass.h" 24 #include "ecmascript/js_native_pointer.h" 25 #include "ecmascript/js_tagged_value.h" 26 #include "ecmascript/mem/heap_region_allocator.h" 27 #include "ecmascript/mem/machine_code.h" 28 #include "ecmascript/mem/native_area_allocator.h" 29 #include "ecmascript/mem/space.h" 30 #include "ecmascript/tagged_array.h" 31 #include "ecmascript/byte_array.h" 32 33 namespace panda::ecmascript { 34 struct MethodLiteral; 35 class Method; 36 class JSObject; 37 class JSArray; 38 class JSAPIPlainArray; 39 class JSSymbol; 40 class JSFunctionBase; 41 class JSFunction; 42 class JSBoundFunction; 43 class JSProxyRevocFunction; 44 class JSAsyncAwaitStatusFunction; 45 class JSPrimitiveRef; 46 class GlobalEnv; 47 class GlobalEnvConstants; 48 class AccessorData; 49 class JSGlobalObject; 50 class LexicalEnv; 51 class JSDate; 52 class JSProxy; 53 class JSRealm; 54 class JSArguments; 55 class TaggedQueue; 56 class JSForInIterator; 57 class JSSet; 58 class JSMap; 59 class JSRegExp; 60 class JSSetIterator; 61 class JSRegExpIterator; 62 class JSMapIterator; 63 class JSArrayIterator; 64 class JSAPIPlainArrayIterator; 65 class JSStringIterator; 66 class JSGeneratorObject; 67 class CompletionRecord; 68 class GeneratorContext; 69 class JSArrayBuffer; 70 class JSDataView; 71 class JSPromise; 72 class JSPromiseReactionsFunction; 73 class JSPromiseExecutorFunction; 74 class JSPromiseAllResolveElementFunction; 75 class JSAsyncGeneratorResNextRetProRstFtn; 76 class JSPromiseAnyRejectElementFunction; 77 class JSPromiseAllSettledElementFunction; 78 class JSPromiseFinallyFunction; 79 class JSPromiseValueThunkOrThrowerFunction; 80 class PromiseReaction; 81 class PromiseCapability; 82 class PromiseIteratorRecord; 83 class JSAsyncFuncObject; 84 class JSAsyncFunction; 85 class JSAsyncFromSyncIterUnwarpFunction; 86 class PromiseRecord; 87 class JSLocale; 88 class ResolvingFunctionsRecord; 89 class EcmaVM; 90 class Heap; 91 class ConstantPool; 92 class Program; 93 class LayoutInfo; 94 class JSIntlBoundFunction; 95 class FreeObject; 96 class JSNativePointer; 97 class TSType; 98 class TSObjectType; 99 class TSClassType; 100 class TSUnionType; 101 class TSInterfaceType; 102 class TSTypeTable; 103 class TSClassInstanceType; 104 class TSFunctionType; 105 class TSArrayType; 106 class TSObjLayoutInfo; 107 class TSModuleTable; 108 class TSFunctionType; 109 class TSArrayType; 110 class TSIteratorInstanceType; 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 JSAPILinkedList; 137 class JSAPIList; 138 class JSAPILinkedListIterator; 139 class JSAPIListIterator; 140 class ModuleNamespace; 141 class ImportEntry; 142 class LocalExportEntry; 143 class IndirectExportEntry; 144 class StarExportEntry; 145 class SourceTextModule; 146 class CjsModule; 147 class CjsRequire; 148 class CjsExports; 149 class ResolvedBinding; 150 class ResolvedIndexBinding; 151 class BigInt; 152 class AsyncGeneratorRequest; 153 class AsyncIteratorRecord; 154 class JSAsyncGeneratorFunction; 155 class JSAsyncGeneratorObject; 156 class CellRecord; 157 class ClassLiteral; 158 159 namespace job { 160 class MicroJobQueue; 161 class PendingJob; 162 } // namespace job 163 class TransitionHandler; 164 class PrototypeHandler; 165 class TransWithProtoHandler; 166 class StoreTSHandler; 167 class PropertyBox; 168 class ProtoChangeMarker; 169 class ProtoChangeDetails; 170 class ProfileTypeInfo; 171 class MachineCode; 172 class ClassInfoExtractor; 173 class AOTLiteralInfo; 174 175 enum class CompletionRecordType : uint8_t; 176 enum class PrimitiveType : uint8_t; 177 enum class IterationKind : uint8_t; 178 enum class MethodIndex : uint8_t; 179 180 using ErrorType = base::ErrorType; 181 using base::ErrorType; 182 using DeleteEntryPoint = void (*)(void *, void *); 183 184 enum class RemoveSlots { YES, NO }; 185 enum class GrowMode { KEEP, GROW }; 186 187 class ObjectFactory { 188 public: 189 ObjectFactory(JSThread *thread, Heap *heap); 190 ~ObjectFactory() = default; 191 JSHandle<Method> NewMethodForNativeFunction(const void *func, FunctionKind kind = FunctionKind::NORMAL_FUNCTION, 192 kungfu::BuiltinsStubCSigns::ID builtinId = 193 kungfu::BuiltinsStubCSigns::INVALID); 194 195 JSHandle<ProfileTypeInfo> NewProfileTypeInfo(uint32_t length); 196 JSHandle<ConstantPool> NewConstantPool(uint32_t capacity); 197 JSHandle<Program> NewProgram(); 198 199 JSHandle<JSObject> GetJSError(const ErrorType &errorType, const char *data = nullptr, bool needCheckStack = true); 200 201 JSHandle<JSObject> NewJSError(const ErrorType &errorType, const JSHandle<EcmaString> &message, 202 bool needCheckStack = true); 203 204 JSHandle<JSObject> NewJSAggregateError(); 205 206 JSHandle<TransitionHandler> NewTransitionHandler(); 207 208 JSHandle<PrototypeHandler> NewPrototypeHandler(); 209 210 JSHandle<TransWithProtoHandler> NewTransWithProtoHandler(); 211 212 JSHandle<StoreTSHandler> NewStoreTSHandler(); 213 214 JSHandle<JSObject> NewEmptyJSObject(); 215 216 // use for others create, prototype is Function.prototype 217 // use for native function 218 JSHandle<JSFunction> NewJSFunction(const JSHandle<GlobalEnv> &env, const void *nativeFunc = nullptr, 219 FunctionKind kind = FunctionKind::NORMAL_FUNCTION, 220 kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID); 221 // use for method 222 JSHandle<JSFunction> NewJSFunction(const JSHandle<GlobalEnv> &env, const JSHandle<Method> &method); 223 224 JSHandle<JSFunction> NewJSNativeErrorFunction(const JSHandle<GlobalEnv> &env, const void *nativeFunc = nullptr); 225 226 JSHandle<JSFunction> NewSpecificTypedArrayFunction(const JSHandle<GlobalEnv> &env, 227 const void *nativeFunc = nullptr); 228 229 JSHandle<JSObject> OrdinaryNewJSObjectCreate(const JSHandle<JSTaggedValue> &proto); 230 231 JSHandle<JSObject> CreateNullJSObject(); 232 233 JSHandle<JSFunction> NewAotFunction(uint32_t numArgs, uintptr_t codeEntry); 234 235 JSHandle<JSBoundFunction> NewJSBoundFunction(const JSHandle<JSFunctionBase> &target, 236 const JSHandle<JSTaggedValue> &boundThis, 237 const JSHandle<TaggedArray> &args); 238 239 JSHandle<JSIntlBoundFunction> NewJSIntlBoundFunction(MethodIndex idx, int functionLength = 1); 240 241 JSHandle<JSProxyRevocFunction> NewJSProxyRevocFunction(const JSHandle<JSProxy> &proxy); 242 243 JSHandle<JSAsyncAwaitStatusFunction> NewJSAsyncAwaitStatusFunction(MethodIndex idx); 244 245 JSHandle<JSGeneratorObject> NewJSGeneratorObject(JSHandle<JSTaggedValue> generatorFunction); 246 247 JSHandle<JSAsyncFuncObject> NewJSAsyncFuncObject(); 248 JSHandle<JSAsyncGeneratorObject> NewJSAsyncGeneratorObject(JSHandle<JSTaggedValue> generatorFunction); 249 250 JSHandle<JSPrimitiveRef> NewJSPrimitiveRef(const JSHandle<JSFunction> &function, 251 const JSHandle<JSTaggedValue> &object); 252 JSHandle<JSPrimitiveRef> NewJSPrimitiveRef(PrimitiveType type, const JSHandle<JSTaggedValue> &object); 253 254 // get JSHClass for Ecma ClassLinker 255 JSHandle<GlobalEnv> NewGlobalEnv(JSHClass *globalEnvClass); 256 257 // get JSHClass for Ecma ClassLinker 258 JSHandle<LexicalEnv> NewLexicalEnv(int numSlots); 259 260 inline LexicalEnv *InlineNewLexicalEnv(int numSlots); 261 262 JSHandle<JSSymbol> NewJSSymbol(); 263 264 JSHandle<JSSymbol> NewPrivateSymbol(); 265 266 JSHandle<JSSymbol> NewPrivateNameSymbol(const JSHandle<JSTaggedValue> &name); 267 268 JSHandle<JSSymbol> NewWellKnownSymbol(const JSHandle<JSTaggedValue> &name); 269 270 JSHandle<JSSymbol> NewPublicSymbol(const JSHandle<JSTaggedValue> &name); 271 272 JSHandle<JSSymbol> NewSymbolWithTable(const JSHandle<JSTaggedValue> &name); 273 274 JSHandle<JSSymbol> NewPrivateNameSymbolWithChar(const char *description); 275 276 JSHandle<JSSymbol> NewWellKnownSymbolWithChar(const char *description); 277 278 JSHandle<JSSymbol> NewPublicSymbolWithChar(const char *description); 279 280 JSHandle<JSSymbol> NewSymbolWithTableWithChar(const char *description); 281 282 JSHandle<AccessorData> NewAccessorData(); 283 JSHandle<AccessorData> NewInternalAccessor(void *setter, void *getter); 284 285 JSHandle<PromiseCapability> NewPromiseCapability(); 286 287 JSHandle<PromiseReaction> NewPromiseReaction(); 288 289 JSHandle<PromiseRecord> NewPromiseRecord(); 290 JSHandle<AsyncGeneratorRequest> NewAsyncGeneratorRequest(); 291 292 JSHandle<AsyncIteratorRecord> NewAsyncIteratorRecord(const JSHandle<JSTaggedValue> &itor, 293 const JSHandle<JSTaggedValue> &next, bool done); 294 295 JSHandle<ResolvingFunctionsRecord> NewResolvingFunctionsRecord(); 296 297 JSHandle<PromiseIteratorRecord> NewPromiseIteratorRecord(const JSHandle<JSTaggedValue> &itor, bool done); 298 299 JSHandle<job::MicroJobQueue> NewMicroJobQueue(); 300 301 JSHandle<job::PendingJob> NewPendingJob(const JSHandle<JSFunction> &func, const JSHandle<TaggedArray> &argv); 302 303 JSHandle<JSArray> NewJSArray(); 304 305 JSHandle<JSProxy> NewJSProxy(const JSHandle<JSTaggedValue> &target, const JSHandle<JSTaggedValue> &handler); 306 JSHandle<JSRealm> NewJSRealm(); 307 308 JSHandle<JSArguments> NewJSArguments(); 309 310 JSHandle<JSPrimitiveRef> NewJSString(const JSHandle<JSTaggedValue> &str); 311 312 template <typename Derived> ConvertListToArray(const JSThread * thread,const JSHandle<Derived> & list,uint32_t numberOfNodes)313 JSHandle<TaggedArray> ConvertListToArray(const JSThread *thread, const JSHandle<Derived> &list, 314 uint32_t numberOfNodes) 315 { 316 MemSpaceType spaceType = numberOfNodes < LENGTH_THRESHOLD ? MemSpaceType::SEMI_SPACE : MemSpaceType::OLD_SPACE; 317 JSHandle<TaggedArray> dstElements = NewTaggedArrayWithoutInit(numberOfNodes, spaceType); 318 if (numberOfNodes == 0) { 319 return dstElements; 320 } 321 int dataIndex = Derived::ELEMENTS_START_INDEX; 322 for (uint32_t i = 0; i < numberOfNodes; i++) { 323 dataIndex = list->GetElement(dataIndex + Derived::NEXT_PTR_OFFSET).GetInt(); 324 dstElements->Set(thread, i, list->GetElement(dataIndex)); 325 } 326 return dstElements; 327 } 328 329 void RemoveElementByIndex(JSHandle<TaggedArray> &srcArray, uint32_t index, uint32_t effectiveLength); 330 JSHandle<TaggedArray> InsertElementByIndex(JSHandle<TaggedArray> &srcArray, const JSHandle<JSTaggedValue> &value, 331 uint32_t index, uint32_t effectiveLength); 332 void CopyTaggedArrayElement(JSHandle<TaggedArray> &srcElements, JSHandle<TaggedArray> &dstElements, 333 uint32_t effectiveLength); 334 JSHandle<TaggedArray> NewAndCopyTaggedArray(JSHandle<TaggedArray> &srcElements, uint32_t newLength, 335 uint32_t oldLength); 336 JSHandle<TaggedArray> NewTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 337 JSHandle<TaggedArray> NewTaggedArray(uint32_t length, JSTaggedValue initVal, bool nonMovable); 338 JSHandle<TaggedArray> NewTaggedArray(uint32_t length, JSTaggedValue initVal, MemSpaceType spaceType); 339 // Copy on write array is allocated in nonmovable space by default. 340 JSHandle<COWTaggedArray> NewCOWTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 341 JSHandle<TaggedArray> NewDictionaryArray(uint32_t length); 342 JSHandle<JSForInIterator> NewJSForinIterator(const JSHandle<JSTaggedValue> &obj); 343 344 JSHandle<ByteArray> NewByteArray(uint32_t length, uint32_t size); 345 346 JSHandle<PropertyBox> NewPropertyBox(const JSHandle<JSTaggedValue> &name); 347 348 JSHandle<ProtoChangeMarker> NewProtoChangeMarker(); 349 350 JSHandle<ProtoChangeDetails> NewProtoChangeDetails(); 351 JSHandle<BigInt> NewBigInt(uint32_t length); 352 // use for copy properties keys's array to another array 353 JSHandle<TaggedArray> ExtendArray(const JSHandle<TaggedArray> &old, uint32_t length, 354 JSTaggedValue initVal = JSTaggedValue::Hole(), 355 MemSpaceType type = MemSpaceType::SEMI_SPACE); 356 JSHandle<TaggedArray> CopyPartArray(const JSHandle<TaggedArray> &old, uint32_t start, uint32_t end); 357 JSHandle<TaggedArray> CopyArray(const JSHandle<TaggedArray> &old, uint32_t oldLength, uint32_t newLength, 358 JSTaggedValue initVal = JSTaggedValue::Hole(), 359 MemSpaceType type = MemSpaceType::SEMI_SPACE); 360 JSHandle<TaggedArray> CloneProperties(const JSHandle<TaggedArray> &old); 361 JSHandle<TaggedArray> CloneProperties(const JSHandle<TaggedArray> &old, const JSHandle<JSTaggedValue> &env, 362 const JSHandle<JSObject> &obj); 363 364 JSHandle<LayoutInfo> CreateLayoutInfo(int properties, MemSpaceType type = MemSpaceType::SEMI_SPACE, 365 GrowMode mode = GrowMode::GROW, 366 JSTaggedValue initVal = JSTaggedValue::Hole()); 367 368 JSHandle<LayoutInfo> ExtendLayoutInfo(const JSHandle<LayoutInfo> &old, int properties, 369 JSTaggedValue initVal = JSTaggedValue::Hole()); 370 371 JSHandle<LayoutInfo> CopyLayoutInfo(const JSHandle<LayoutInfo> &old); 372 373 JSHandle<LayoutInfo> CopyAndReSort(const JSHandle<LayoutInfo> &old, int end, int capacity); 374 375 JSHandle<EcmaString> GetEmptyString() const; 376 377 JSHandle<TaggedArray> EmptyArray() const; 378 379 FreeObject *FillFreeObject(uintptr_t address, size_t size, RemoveSlots removeSlots = RemoveSlots::NO, 380 uintptr_t hugeObjectHead = 0); 381 382 TaggedObject *NewObject(const JSHandle<JSHClass> &hclass); 383 384 TaggedObject *NewNonMovableObject(const JSHandle<JSHClass> &hclass, uint32_t inobjPropCount = 0); 385 386 void InitializeExtraProperties(const JSHandle<JSHClass> &hclass, TaggedObject *obj, uint32_t inobjPropCount); 387 388 JSHandle<TaggedQueue> NewTaggedQueue(uint32_t length); 389 390 JSHandle<TaggedQueue> GetEmptyTaggedQueue() const; 391 392 JSHandle<JSSetIterator> NewJSSetIterator(const JSHandle<JSSet> &set, IterationKind kind); 393 394 JSHandle<JSRegExpIterator> NewJSRegExpIterator(const JSHandle<JSTaggedValue> &matcher, 395 const JSHandle<EcmaString> &inputStr, bool global, 396 bool fullUnicode); 397 398 JSHandle<JSMapIterator> NewJSMapIterator(const JSHandle<JSMap> &map, IterationKind kind); 399 400 JSHandle<JSArrayIterator> NewJSArrayIterator(const JSHandle<JSObject> &array, IterationKind kind); 401 402 JSHandle<CompletionRecord> NewCompletionRecord(CompletionRecordType type, JSHandle<JSTaggedValue> value); 403 404 JSHandle<GeneratorContext> NewGeneratorContext(); 405 406 JSHandle<JSPromiseReactionsFunction> CreateJSPromiseReactionsFunction(MethodIndex idx); 407 408 JSHandle<JSPromiseExecutorFunction> CreateJSPromiseExecutorFunction(); 409 410 JSHandle<JSPromiseAllResolveElementFunction> NewJSPromiseAllResolveElementFunction(); 411 412 JSHandle<JSPromiseAnyRejectElementFunction> NewJSPromiseAnyRejectElementFunction(); 413 414 JSHandle<JSPromiseAllSettledElementFunction> NewJSPromiseAllSettledResolveElementFunction(); 415 416 JSHandle<JSPromiseAllSettledElementFunction> NewJSPromiseAllSettledRejectElementFunction(); 417 418 JSHandle<JSPromiseFinallyFunction> NewJSPromiseThenFinallyFunction(); 419 420 JSHandle<JSPromiseFinallyFunction> NewJSPromiseCatchFinallyFunction(); 421 422 JSHandle<JSPromiseValueThunkOrThrowerFunction> NewJSPromiseValueThunkFunction(); 423 424 JSHandle<JSPromiseValueThunkOrThrowerFunction> NewJSPromiseThrowerFunction(); 425 426 JSHandle<JSAsyncGeneratorResNextRetProRstFtn> NewJSAsyGenResNextRetProRstFulfilledFtn(); 427 428 JSHandle<JSAsyncGeneratorResNextRetProRstFtn> NewJSAsyGenResNextRetProRstRejectedFtn(); 429 430 JSHandle<JSAsyncFromSyncIterUnwarpFunction> NewJSAsyncFromSyncIterUnwarpFunction(); 431 432 JSHandle<JSObject> CloneObjectLiteral(JSHandle<JSObject> object, const JSHandle<JSTaggedValue> &env, 433 bool canShareHClass = true); 434 JSHandle<JSObject> CloneObjectLiteral(JSHandle<JSObject> object); 435 JSHandle<JSArray> CloneArrayLiteral(JSHandle<JSArray> object); 436 JSHandle<JSFunction> CloneJSFuction(JSHandle<JSFunction> func); 437 JSHandle<JSFunction> CloneClassCtor(JSHandle<JSFunction> ctor, const JSHandle<JSTaggedValue> &lexenv, 438 bool canShareHClass); 439 440 void NewJSArrayBufferData(const JSHandle<JSArrayBuffer> &array, int32_t length); 441 442 JSHandle<JSArrayBuffer> NewJSArrayBuffer(int32_t length); 443 444 JSHandle<JSArrayBuffer> NewJSArrayBuffer(void *buffer, int32_t length, const DeleteEntryPoint &deleter, void *data, 445 bool share = false); 446 447 JSHandle<JSDataView> NewJSDataView(JSHandle<JSArrayBuffer> buffer, uint32_t offset, uint32_t length); 448 449 void NewJSSharedArrayBufferData(const JSHandle<JSArrayBuffer> &array, int32_t length); 450 451 JSHandle<JSArrayBuffer> NewJSSharedArrayBuffer(int32_t length); 452 453 JSHandle<JSArrayBuffer> NewJSSharedArrayBuffer(void *buffer, int32_t length); 454 455 void NewJSRegExpByteCodeData(const JSHandle<JSRegExp> ®exp, void *buffer, size_t size); 456 457 template<typename T, typename S> 458 inline void NewJSIntlIcuData(const JSHandle<T> &obj, const S &icu, const DeleteEntryPoint &callback); 459 460 EcmaString *InternString(const JSHandle<JSTaggedValue> &key); 461 462 inline JSHandle<JSNativePointer> NewJSNativePointer(void *externalPointer, 463 const DeleteEntryPoint &callBack = nullptr, 464 void *data = nullptr, 465 bool nonMovable = false, 466 size_t nativeBindingsize = 0); 467 468 JSHandle<JSObject> NewOldSpaceObjLiteralByHClass(const JSHandle<TaggedArray> &properties, size_t length); 469 JSHandle<JSHClass> SetLayoutInObjHClass(const JSHandle<TaggedArray> &properties, size_t length, 470 const JSHandle<JSHClass> &objClass); 471 JSHandle<JSHClass> GetObjectLiteralHClass(const JSHandle<TaggedArray> &properties, size_t length); 472 // only use for creating Function.prototype and Function 473 JSHandle<JSFunction> NewJSFunctionByHClass(const JSHandle<Method> &method, const JSHandle<JSHClass> &clazz, 474 MemSpaceType type = MemSpaceType::SEMI_SPACE); 475 JSHandle<JSFunction> NewJSFunctionByHClass(const void *func, const JSHandle<JSHClass> &clazz, 476 FunctionKind kind = FunctionKind::NORMAL_FUNCTION); 477 JSHandle<Method> NewMethod(const MethodLiteral *methodLiteral); 478 479 // used for creating jsobject by constructor 480 JSHandle<JSObject> NewJSObjectByConstructor(const JSHandle<JSFunction> &constructor, 481 const JSHandle<JSTaggedValue> &newTarget); 482 JSHandle<JSObject> NewJSObjectByConstructor(const JSHandle<JSFunction> &constructor); 483 void InitializeJSObject(const JSHandle<JSObject> &obj, const JSHandle<JSHClass> &jshclass); 484 485 JSHandle<JSObject> NewJSObjectWithInit(const JSHandle<JSHClass> &jshclass); 486 uintptr_t NewSpaceBySnapshotAllocator(size_t size); 487 JSHandle<MachineCode> NewMachineCodeObject(size_t length, const uint8_t *data); 488 JSHandle<ClassInfoExtractor> NewClassInfoExtractor(JSHandle<JSTaggedValue> method); 489 JSHandle<ClassLiteral> NewClassLiteral(); 490 491 // ----------------------------------- new TSType ---------------------------------------- 492 JSHandle<TSObjLayoutInfo> CreateTSObjLayoutInfo(int propNum, JSTaggedValue initVal = JSTaggedValue::Hole()); 493 JSHandle<TSObjectType> NewTSObjectType(uint32_t numOfKeys); 494 JSHandle<TSClassType> NewTSClassType(); 495 JSHandle<TSUnionType> NewTSUnionType(uint32_t length); 496 JSHandle<TSInterfaceType> NewTSInterfaceType(); 497 JSHandle<TSClassInstanceType> NewTSClassInstanceType(); 498 JSHandle<TSTypeTable> NewTSTypeTable(uint32_t length); 499 JSHandle<TSModuleTable> NewTSModuleTable(uint32_t length); 500 JSHandle<TSFunctionType> NewTSFunctionType(uint32_t length); 501 JSHandle<TSArrayType> NewTSArrayType(); 502 JSHandle<TSIteratorInstanceType> NewTSIteratorInstanceType(); 503 504 // ----------------------------------- new string ---------------------------------------- 505 JSHandle<EcmaString> NewFromASCII(const CString &data); 506 JSHandle<EcmaString> NewFromUtf8(const CString &data); 507 508 JSHandle<EcmaString> NewFromStdString(const std::string &data); 509 510 JSHandle<EcmaString> NewFromUtf8(const uint8_t *utf8Data, uint32_t utf8Len); 511 512 JSHandle<EcmaString> NewFromUtf16(const uint16_t *utf16Data, uint32_t utf16Len); 513 JSHandle<EcmaString> NewFromUtf16Compress(const uint16_t *utf16Data, uint32_t utf16Len); 514 JSHandle<EcmaString> NewFromUtf16NotCompress(const uint16_t *utf16Data, uint32_t utf16Len); 515 516 JSHandle<EcmaString> NewFromUtf8Literal(const uint8_t *utf8Data, uint32_t utf8Len); 517 JSHandle<EcmaString> NewFromUtf8LiteralCompress(const uint8_t *utf8Data, uint32_t utf8Len); 518 519 JSHandle<EcmaString> NewFromUtf16Literal(const uint16_t *utf16Data, uint32_t utf16Len); 520 JSHandle<EcmaString> NewFromUtf16LiteralCompress(const uint16_t *utf16Data, uint32_t utf16Len); 521 JSHandle<EcmaString> NewFromUtf16LiteralNotCompress(const uint16_t *utf16Data, uint32_t utf16Len); 522 523 JSHandle<EcmaString> ConcatFromString(const JSHandle<EcmaString> &firstString, 524 const JSHandle<EcmaString> &secondString); 525 526 // used for creating Function 527 JSHandle<JSObject> NewJSObject(const JSHandle<JSHClass> &jshclass); 528 529 // used for creating jshclass in Builtins, Function, Class_Linker 530 JSHandle<JSHClass> NewEcmaHClass(uint32_t size, JSType type, const JSHandle<JSTaggedValue> &prototype); 531 532 // It is used to provide iterators for non ECMA standard jsapi containers. 533 JSHandle<JSAPIPlainArray> NewJSAPIPlainArray(uint32_t capacity); 534 JSHandle<JSAPIPlainArrayIterator> NewJSAPIPlainArrayIterator(const JSHandle<JSAPIPlainArray> &plainarray, 535 IterationKind kind); 536 JSHandle<JSAPIArrayList> NewJSAPIArrayList(uint32_t capacity); 537 538 JSHandle<JSAPILightWeightMapIterator> NewJSAPILightWeightMapIterator(const JSHandle<JSAPILightWeightMap> &obj, 539 IterationKind kind); 540 JSHandle<JSAPILightWeightSetIterator> NewJSAPILightWeightSetIterator(const JSHandle<JSAPILightWeightSet> &obj, 541 IterationKind kind); 542 JSHandle<TaggedArray> CopyQueue(const JSHandle<TaggedArray> &old, uint32_t newLength, 543 uint32_t front, uint32_t tail); 544 JSHandle<JSAPIQueueIterator> NewJSAPIQueueIterator(const JSHandle<JSAPIQueue> &queue); 545 JSHandle<TaggedArray> CopyDeque(const JSHandle<TaggedArray> &old, uint32_t newLength, uint32_t oldLength, 546 uint32_t first, uint32_t last); 547 JSHandle<JSAPIDequeIterator> NewJSAPIDequeIterator(const JSHandle<JSAPIDeque> &deque); 548 JSHandle<JSAPIArrayListIterator> NewJSAPIArrayListIterator(const JSHandle<JSAPIArrayList> &arrayList); 549 JSHandle<JSAPIList> NewJSAPIList(); 550 JSHandle<JSAPILinkedList> NewJSAPILinkedList(); 551 JSHandle<JSAPILinkedListIterator> NewJSAPILinkedListIterator(const JSHandle<JSAPILinkedList> &linkedList); 552 JSHandle<JSAPIListIterator> NewJSAPIListIterator(const JSHandle<JSAPIList> &list); 553 JSHandle<JSAPITreeMapIterator> NewJSAPITreeMapIterator(const JSHandle<JSAPITreeMap> &map, IterationKind kind); 554 JSHandle<JSAPITreeSetIterator> NewJSAPITreeSetIterator(const JSHandle<JSAPITreeSet> &set, IterationKind kind); 555 JSHandle<JSAPIStackIterator> NewJSAPIStackIterator(const JSHandle<JSAPIStack> &stack); 556 JSHandle<JSAPIVector> NewJSAPIVector(uint32_t capacity); 557 JSHandle<JSAPIVectorIterator> NewJSAPIVectorIterator(const JSHandle<JSAPIVector> &vector); 558 JSHandle<JSAPIHashMapIterator> NewJSAPIHashMapIterator(const JSHandle<JSAPIHashMap> &hashMap, IterationKind kind); 559 JSHandle<JSAPIHashSetIterator> NewJSAPIHashSetIterator(const JSHandle<JSAPIHashSet> &hashSet, IterationKind kind); 560 JSHandle<TaggedHashArray> NewTaggedHashArray(uint32_t length); 561 JSHandle<LinkedNode> NewLinkedNode(int hash, const JSHandle<JSTaggedValue> &key, 562 const JSHandle<JSTaggedValue> &value, 563 const JSHandle<LinkedNode> &next); 564 JSHandle<RBTreeNode> NewTreeNode(int hash, const JSHandle<JSTaggedValue> &key, 565 const JSHandle<JSTaggedValue> &value); 566 // --------------------------------------module-------------------------------------------- 567 JSHandle<ModuleNamespace> NewModuleNamespace(); 568 JSHandle<ImportEntry> NewImportEntry(); 569 JSHandle<ImportEntry> NewImportEntry(const JSHandle<JSTaggedValue> &moduleRequest, 570 const JSHandle<JSTaggedValue> &importName, 571 const JSHandle<JSTaggedValue> &localName); 572 JSHandle<LocalExportEntry> NewLocalExportEntry(); 573 JSHandle<LocalExportEntry> NewLocalExportEntry( 574 const JSHandle<JSTaggedValue> &exportName, const JSHandle<JSTaggedValue> &localName, 575 const uint32_t index = 0); 576 JSHandle<IndirectExportEntry> NewIndirectExportEntry(); 577 JSHandle<IndirectExportEntry> NewIndirectExportEntry(const JSHandle<JSTaggedValue> &exportName, 578 const JSHandle<JSTaggedValue> &moduleRequest, 579 const JSHandle<JSTaggedValue> &importName); 580 JSHandle<StarExportEntry> NewStarExportEntry(); 581 JSHandle<StarExportEntry> NewStarExportEntry(const JSHandle<JSTaggedValue> &moduleRequest); 582 JSHandle<SourceTextModule> NewSourceTextModule(); 583 JSHandle<ResolvedBinding> NewResolvedBindingRecord(); 584 JSHandle<ResolvedBinding> NewResolvedBindingRecord(const JSHandle<SourceTextModule> &module, 585 const JSHandle<JSTaggedValue> &bindingName); 586 JSHandle<ResolvedIndexBinding> NewResolvedIndexBindingRecord(); 587 JSHandle<ResolvedIndexBinding> NewResolvedIndexBindingRecord(const JSHandle<SourceTextModule> &module, 588 int32_t index); 589 590 JSHandle<CellRecord> NewCellRecord(); 591 JSHandle<JSFunction> NewJSAsyncGeneratorFunction(const JSHandle<Method> &method); 592 // --------------------------------------require-------------------------------------------- 593 JSHandle<CjsModule> NewCjsModule(); 594 JSHandle<CjsExports> NewCjsExports(); 595 JSHandle<CjsRequire> NewCjsRequire(); 596 597 JSHandle<JSHClass> CreateIteratorResultInstanceClass(); 598 599 // --------------------------------------old space object-------------------------------------------- 600 JSHandle<JSObject> NewOldSpaceJSObject(const JSHandle<JSHClass> &jshclass); 601 TaggedObject *NewOldSpaceObject(const JSHandle<JSHClass> &hclass); 602 JSHandle<TaggedArray> NewOldSpaceTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 603 604 // ---------------------------------New objects used internally-------------------------------------- 605 JSHandle<JSArray> NewJSStableArrayWithElements(const JSHandle<TaggedArray> &elements); 606 607 // ---------------------------------AOT snapshot----------------------------------------------------- 608 JSHandle<AOTLiteralInfo> NewAOTLiteralInfo(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 609 610 private: 611 friend class GlobalEnv; 612 friend class GlobalEnvConstants; 613 friend class EcmaString; 614 friend class SnapshotProcessor; 615 friend class TSManager; 616 void InitObjectFields(const TaggedObject *object); 617 618 JSThread *thread_ {nullptr}; 619 bool isTriggerGc_ {false}; 620 bool triggerSemiGC_ {false}; 621 622 EcmaVM *vm_ {nullptr}; 623 Heap *heap_ {nullptr}; 624 625 static constexpr uint32_t LENGTH_THRESHOLD = 50; 626 627 NO_COPY_SEMANTIC(ObjectFactory); 628 NO_MOVE_SEMANTIC(ObjectFactory); 629 630 void NewObjectHook() const; 631 632 // used for creating jshclass in Builtins, Function, Class_Linker 633 JSHandle<JSHClass> NewEcmaHClass(uint32_t size, JSType type, 634 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 635 // used for creating jshclass in GlobalEnv, EcmaVM 636 JSHandle<JSHClass> NewEcmaHClassClass(JSHClass *hclass, uint32_t size, JSType type); 637 JSHandle<JSHClass> NewEcmaHClass(JSHClass *hclass, uint32_t size, JSType type, 638 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 639 JSHandle<JSHClass> NewEcmaReadOnlyHClass(JSHClass *hclass, uint32_t size, JSType type, 640 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 641 JSHandle<JSHClass> InitClassClass(); 642 643 // used to create nonmovable js_object 644 JSHandle<JSObject> NewNonMovableJSObject(const JSHandle<JSHClass> &jshclass); 645 646 // used to create nonmovable utf8 string at global constants 647 JSHandle<EcmaString> NewFromASCIINonMovable(const CString &data); 648 649 // used for creating Function 650 JSHandle<JSFunction> NewJSFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &hclass); 651 JSHandle<JSHClass> CreateObjectClass(const JSHandle<TaggedArray> &keys, const JSHandle<TaggedArray> &values); 652 JSHandle<JSHClass> CreateObjectClass(const JSHandle<TaggedArray> &properties, size_t length); 653 JSHandle<JSHClass> CreateFunctionClass(FunctionKind kind, uint32_t size, JSType type, 654 const JSHandle<JSTaggedValue> &prototype); 655 JSHandle<JSHClass> CreateDefaultClassPrototypeHClass(JSHClass *hclass); 656 JSHandle<JSHClass> CreateDefaultClassConstructorHClass(JSHClass *hclass); 657 658 // used for creating ref.prototype in buildins, such as Number.prototype 659 JSHandle<JSPrimitiveRef> NewJSPrimitiveRef(const JSHandle<JSHClass> &hclass, 660 const JSHandle<JSTaggedValue> &object); 661 662 JSHandle<EcmaString> GetStringFromStringTable(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress) const; 663 JSHandle<EcmaString> GetStringFromStringTableNonMovable(const uint8_t *utf8Data, uint32_t utf8Len) const; 664 // For MUtf-8 string data 665 EcmaString* PUBLIC_API GetRawStringFromStringTable(const uint8_t *mutf8Data, uint32_t utf16Len, 666 bool canBeCompressed, 667 MemSpaceType type = MemSpaceType::SEMI_SPACE) const; 668 669 JSHandle<EcmaString> GetStringFromStringTable(const uint16_t *utf16Data, uint32_t utf16Len, 670 bool canBeCompress) const; 671 672 JSHandle<EcmaString> GetStringFromStringTable(EcmaString *string) const; 673 674 JSHandle<EcmaString> GetStringFromStringTable(const JSHandle<EcmaString> &firstString, 675 const JSHandle<EcmaString> &secondString); 676 677 inline EcmaString *AllocStringObject(size_t size); 678 inline EcmaString *AllocOldSpaceStringObject(size_t size); 679 inline EcmaString *AllocNonMovableStringObject(size_t size); 680 JSHandle<TaggedArray> NewEmptyArray(); // only used for EcmaVM. 681 682 JSHandle<JSHClass> CreateJSArguments(); 683 JSHandle<JSHClass> CreateJSArrayInstanceClass(JSHandle<JSTaggedValue> proto); 684 JSHandle<JSHClass> CreateJSRegExpInstanceClass(JSHandle<JSTaggedValue> proto); 685 686 inline TaggedObject *AllocObjectWithSpaceType(size_t size, JSHClass *cls, MemSpaceType type); 687 JSHandle<TaggedArray> NewTaggedArrayWithoutInit(uint32_t length, MemSpaceType spaceType); 688 689 friend class Builtins; // create builtins object need hclass 690 friend class JSFunction; // create prototype_or_hclass need hclass 691 friend class JSHClass; // HC transition need hclass 692 friend class EcmaVM; // hold the factory instance 693 friend class JsVerificationTest; 694 friend class PandaFileTranslator; 695 friend class LiteralDataExtractor; 696 friend class RuntimeStubs; 697 friend class ClassInfoExtractor; 698 friend class TSObjectType; 699 friend class ModuleDataExtractor; 700 friend class ModuleDataAccessor; 701 friend class ConstantPool; 702 }; 703 704 class ClassLinkerFactory { 705 private: 706 friend class GlobalEnv; // root class in class_linker need hclass 707 friend class EcmaVM; // root class in class_linker need hclass 708 }; 709 } // namespace panda::ecmascript 710 #endif // ECMASCRIPT_OBJECT_FACTORY_H 711