• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/base/number_helper.h"
21 #include "ecmascript/ecma_string.h"
22 #include "ecmascript/js_function_kind.h"
23 #include "ecmascript/js_handle.h"
24 #include "ecmascript/js_hclass.h"
25 #include "ecmascript/js_native_pointer.h"
26 #include "ecmascript/js_shared_object.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/tagged_array.h"
33 #include "ecmascript/byte_array.h"
34 
35 namespace panda::ecmascript {
36 struct MethodLiteral;
37 class Method;
38 class JSObject;
39 class JSArray;
40 class JSAPIPlainArray;
41 class JSSymbol;
42 class JSFunctionBase;
43 class JSFunction;
44 class JSBoundFunction;
45 class JSProxyRevocFunction;
46 class JSAsyncAwaitStatusFunction;
47 class JSPrimitiveRef;
48 class GlobalEnv;
49 class GlobalEnvConstants;
50 class AccessorData;
51 class JSGlobalObject;
52 class LexicalEnv;
53 class JSDate;
54 class JSProxy;
55 class JSRealm;
56 class JSArguments;
57 class TaggedQueue;
58 class JSForInIterator;
59 class JSSet;
60 class JSMap;
61 class JSRegExp;
62 class JSSetIterator;
63 class JSRegExpIterator;
64 class JSMapIterator;
65 class JSArrayIterator;
66 class JSAPIPlainArrayIterator;
67 class JSStringIterator;
68 class JSGeneratorObject;
69 class CompletionRecord;
70 class GeneratorContext;
71 class JSArrayBuffer;
72 class JSDataView;
73 class JSPromise;
74 class JSPromiseReactionsFunction;
75 class JSPromiseExecutorFunction;
76 class JSAsyncModuleFulfilledFunction;
77 class JSAsyncModuleRejectedFunction;
78 class JSPromiseAllResolveElementFunction;
79 class JSAsyncGeneratorResNextRetProRstFtn;
80 class JSPromiseAnyRejectElementFunction;
81 class JSPromiseAllSettledElementFunction;
82 class JSPromiseFinallyFunction;
83 class JSPromiseValueThunkOrThrowerFunction;
84 class PromiseReaction;
85 class PromiseCapability;
86 class PromiseIteratorRecord;
87 class JSAsyncFuncObject;
88 class JSAsyncFunction;
89 class JSAsyncFromSyncIterUnwarpFunction;
90 class PromiseRecord;
91 class JSLocale;
92 class ResolvingFunctionsRecord;
93 class EcmaVM;
94 class Heap;
95 class ConstantPool;
96 class Program;
97 class LayoutInfo;
98 class JSIntlBoundFunction;
99 class FreeObject;
100 class JSNativePointer;
101 class TSModuleTable;
102 class TSTypeTable;
103 class TSObjLayoutInfo;
104 class TSType;
105 class TSObjectType;
106 class TSClassType;
107 class TSUnionType;
108 class TSInterfaceType;
109 class TSClassInstanceType;
110 class TSFunctionType;
111 class TSArrayType;
112 class TSIteratorInstanceType;
113 class TSNamespaceType;
114 class JSAPIArrayList;
115 class JSAPIArrayListIterator;
116 class JSAPIDeque;
117 class JSAPIDequeIterator;
118 class TaggedHashArray;
119 class LinkedNode;
120 class RBTreeNode;
121 class JSAPIHashMap;
122 class JSAPIHashSet;
123 class JSAPIHashMapIterator;
124 class JSAPIHashSetIterator;
125 class JSAPILightWeightMap;
126 class JSAPILightWeightMapIterator;
127 class JSAPILightWeightSet;
128 class JSAPILightWeightSetIterator;
129 class JSAPIQueue;
130 class JSAPIQueueIterator;
131 class JSAPIStack;
132 class JSAPIStackIterator;
133 class JSAPITreeSet;
134 class JSAPITreeMap;
135 class JSAPITreeSetIterator;
136 class JSAPITreeMapIterator;
137 class JSAPIVector;
138 class JSAPIVectorIterator;
139 class JSAPILinkedList;
140 class JSAPIList;
141 class JSAPILinkedListIterator;
142 class JSAPIListIterator;
143 class ModuleNamespace;
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 BigInt;
155 class AsyncGeneratorRequest;
156 class AsyncIteratorRecord;
157 class JSAsyncGeneratorFunction;
158 class JSAsyncGeneratorObject;
159 class CellRecord;
160 class ClassLiteral;
161 
162 namespace job {
163 class MicroJobQueue;
164 class PendingJob;
165 }  // namespace job
166 class TransitionHandler;
167 class PrototypeHandler;
168 class TransWithProtoHandler;
169 class StoreTSHandler;
170 class PropertyBox;
171 class ProtoChangeMarker;
172 class ProtoChangeDetails;
173 class MarkerCell;
174 class ProfileTypeInfo;
175 class MachineCode;
176 class ClassInfoExtractor;
177 class AOTLiteralInfo;
178 class VTable;
179 namespace kungfu {
180 class TSHClassGenerator;
181 }  // namespace kungfu
182 
183 enum class CompletionRecordType : uint8_t;
184 enum class PrimitiveType : uint8_t;
185 enum class IterationKind : uint8_t;
186 enum class MethodIndex : uint8_t;
187 
188 using ErrorType = base::ErrorType;
189 using base::ErrorType;
190 using DeleteEntryPoint = void (*)(void *, void *);
191 
192 enum class RemoveSlots { YES, NO };
193 enum class GrowMode { KEEP, GROW };
194 
195 class ObjectFactory {
196 public:
197     static constexpr JSTaggedType FREE_MEMMORY_ADDRESS_ZAM_VALUE = 0xDEADFACE;
198     ObjectFactory(JSThread *thread, Heap *heap);
199     ~ObjectFactory() = default;
200     JSHandle<Method> NewMethodForNativeFunction(const void *func, FunctionKind kind = FunctionKind::NORMAL_FUNCTION,
201                                                 kungfu::BuiltinsStubCSigns::ID builtinId =
202                                                 kungfu::BuiltinsStubCSigns::INVALID,
203                                                 MemSpaceType spaceType = OLD_SPACE);
204 
205     JSHandle<ProfileTypeInfo> NewProfileTypeInfo(uint32_t length);
206     JSHandle<ConstantPool> NewConstantPool(uint32_t capacity);
207     JSHandle<Program> NewProgram();
208 
209     JSHandle<JSObject> GetJSError(const ErrorType &errorType, const char *data = nullptr, bool needCheckStack = true);
210 
211     JSHandle<JSObject> NewJSError(const ErrorType &errorType, const JSHandle<EcmaString> &message,
212         bool needCheckStack = true);
213 
214     JSHandle<JSObject> NewJSAggregateError();
215 
216     JSHandle<TransitionHandler> NewTransitionHandler();
217 
218     JSHandle<PrototypeHandler> NewPrototypeHandler();
219 
220     JSHandle<TransWithProtoHandler> NewTransWithProtoHandler();
221 
222     JSHandle<StoreTSHandler> NewStoreTSHandler();
223 
224     JSHandle<JSObject> NewEmptyJSObject(uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS);
225 
226     // use for others create, prototype is Function.prototype
227     // use for native function
228     JSHandle<JSFunction> NewJSFunction(const JSHandle<GlobalEnv> &env, const void *nativeFunc = nullptr,
229                                        FunctionKind kind = FunctionKind::NORMAL_FUNCTION,
230                                        kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID,
231                                        MemSpaceType spaceType = OLD_SPACE);
232     JSHandle<JSFunction> NewSFunction(const JSHandle<GlobalEnv> &env, const void *nativeFunc = nullptr,
233         FunctionKind kind = FunctionKind::NORMAL_FUNCTION,
234         kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID,
235         MemSpaceType spaceType = OLD_SPACE);
236     // use for method
237     JSHandle<JSFunction> NewJSFunction(const JSHandle<GlobalEnv> &env, const JSHandle<Method> &method);
238 
239     JSHandle<JSFunction> NewJSFunction(const JSHandle<Method> &methodHandle);
240 
241     JSHandle<JSFunction> NewJSFunction(const JSHandle<Method> &methodHandle,
242                                        const JSHandle<JSTaggedValue> &homeObject);
243 
244     JSHandle<JSFunction> NewJSNativeErrorFunction(const JSHandle<GlobalEnv> &env, const void *nativeFunc = nullptr);
245 
246     JSHandle<JSFunction> NewSpecificTypedArrayFunction(const JSHandle<GlobalEnv> &env,
247                                                        const void *nativeFunc = nullptr);
248 
249     JSHandle<JSObject> OrdinaryNewJSObjectCreate(const JSHandle<JSTaggedValue> &proto);
250 
251     JSHandle<JSObject> CreateNullJSObject();
252 
253     JSHandle<JSFunction> NewAotFunction(uint32_t numArgs, uintptr_t codeEntry);
254 
255     JSHandle<JSBoundFunction> NewJSBoundFunction(const JSHandle<JSTaggedValue> &target,
256                                                  const JSHandle<JSTaggedValue> &boundThis,
257                                                  const JSHandle<TaggedArray> &args);
258 
259     JSHandle<JSIntlBoundFunction> NewJSIntlBoundFunction(MethodIndex idx, int functionLength = 1);
260 
261     JSHandle<JSProxyRevocFunction> NewJSProxyRevocFunction(const JSHandle<JSProxy> &proxy);
262 
263     JSHandle<JSAsyncAwaitStatusFunction> NewJSAsyncAwaitStatusFunction(MethodIndex idx);
264 
265     JSHandle<JSGeneratorObject> NewJSGeneratorObject(JSHandle<JSTaggedValue> generatorFunction);
266 
267     JSHandle<JSAsyncFuncObject> NewJSAsyncFuncObject();
268     JSHandle<JSAsyncGeneratorObject> NewJSAsyncGeneratorObject(JSHandle<JSTaggedValue> generatorFunction);
269 
270     JSHandle<JSPrimitiveRef> NewJSPrimitiveRef(const JSHandle<JSFunction> &function,
271                                                const JSHandle<JSTaggedValue> &object);
272     JSHandle<JSPrimitiveRef> NewJSPrimitiveRef(PrimitiveType type, const JSHandle<JSTaggedValue> &object);
273 
274     // get JSHClass for Ecma ClassLinker
275     JSHandle<GlobalEnv> NewGlobalEnv(JSHClass *globalEnvClass);
276 
277     // get JSHClass for Ecma ClassLinker
278     JSHandle<LexicalEnv> NewLexicalEnv(int numSlots);
279 
280     inline LexicalEnv *InlineNewLexicalEnv(int numSlots);
281 
282     JSHandle<JSSymbol> NewEmptySymbol();
283 
284     JSHandle<JSSymbol> NewJSSymbol();
285 
286     JSHandle<JSSymbol> NewPrivateSymbol();
287 
288     JSHandle<JSSymbol> NewPrivateNameSymbol(const JSHandle<JSTaggedValue> &name);
289 
290     JSHandle<JSSymbol> NewWellKnownSymbol(const JSHandle<JSTaggedValue> &name);
291 
292     JSHandle<JSSymbol> NewPublicSymbol(const JSHandle<JSTaggedValue> &name);
293 
294     JSHandle<JSSymbol> NewSymbolWithTable(const JSHandle<JSTaggedValue> &name);
295 
296     JSHandle<JSSymbol> NewPrivateNameSymbolWithChar(std::string_view description);
297 
298     JSHandle<JSSymbol> NewWellKnownSymbolWithChar(std::string_view description);
299 
300     JSHandle<JSSymbol> NewPublicSymbolWithChar(std::string_view description);
301 
302     JSHandle<JSSymbol> NewSymbolWithTableWithChar(std::string_view description);
303 
304     JSHandle<AccessorData> NewAccessorData();
305     JSHandle<AccessorData> NewInternalAccessor(void *setter, void *getter);
306 
307     JSHandle<PromiseCapability> NewPromiseCapability();
308 
309     JSHandle<PromiseReaction> NewPromiseReaction();
310 
311     JSHandle<PromiseRecord> NewPromiseRecord();
312     JSHandle<AsyncGeneratorRequest> NewAsyncGeneratorRequest();
313 
314     JSHandle<AsyncIteratorRecord> NewAsyncIteratorRecord(const JSHandle<JSTaggedValue> &itor,
315                                                          const JSHandle<JSTaggedValue> &next, bool done);
316 
317     JSHandle<ResolvingFunctionsRecord> NewResolvingFunctionsRecord();
318 
319     JSHandle<PromiseIteratorRecord> NewPromiseIteratorRecord(const JSHandle<JSTaggedValue> &itor, bool done);
320 
321     JSHandle<job::MicroJobQueue> NewMicroJobQueue();
322 
323     JSHandle<job::PendingJob> NewPendingJob(const JSHandle<JSFunction> &func, const JSHandle<TaggedArray> &argv);
324 
325     JSHandle<JSArray> NewJSArray();
326     JSHandle<JSArray> NewJSArray(size_t length, JSHandle<JSHClass> &hclass);
327     JSHandle<TaggedArray> NewJsonFixedArray(size_t start, size_t length,
328                                             const std::vector<JSHandle<JSTaggedValue>> &vec);
329 
330     JSHandle<JSProxy> NewJSProxy(const JSHandle<JSTaggedValue> &target, const JSHandle<JSTaggedValue> &handler);
331     JSHandle<JSRealm> NewJSRealm();
332 
333     JSHandle<JSArguments> NewJSArguments();
334 
335     JSHandle<JSPrimitiveRef> NewJSString(const JSHandle<JSTaggedValue> &str, const JSHandle<JSTaggedValue> &newTarget);
336 
337     template <typename Derived>
ConvertListToArray(const JSThread * thread,const JSHandle<Derived> & list,uint32_t numberOfNodes)338     JSHandle<TaggedArray> ConvertListToArray(const JSThread *thread, const JSHandle<Derived> &list,
339                                              uint32_t numberOfNodes)
340     {
341         MemSpaceType spaceType = numberOfNodes < LENGTH_THRESHOLD ? MemSpaceType::SEMI_SPACE : MemSpaceType::OLD_SPACE;
342         JSHandle<TaggedArray> dstElements = NewTaggedArrayWithoutInit(numberOfNodes, spaceType);
343         if (numberOfNodes == 0) {
344             return dstElements;
345         }
346         int dataIndex = Derived::ELEMENTS_START_INDEX;
347         for (uint32_t i = 0; i < numberOfNodes; i++) {
348             dataIndex = list->GetElement(dataIndex + Derived::NEXT_PTR_OFFSET).GetInt();
349             dstElements->Set(thread, i, list->GetElement(dataIndex));
350         }
351         return dstElements;
352     }
353 
354     JSHandle<JSObject> NewAndCopyJSArrayObject(JSHandle<JSObject> thisObjHandle, uint32_t newLength,
355                                                uint32_t oldLength, uint32_t k = 0);
356     JSHandle<TaggedArray> NewAndCopyTaggedArray(JSHandle<TaggedArray> &srcElements, uint32_t newLength,
357                                                 uint32_t oldLength, uint32_t k = 0);
358     JSHandle<TaggedArray> NewAndCopyNameDictionary(JSHandle<TaggedArray> &srcElements, uint32_t length);
359     JSHandle<TaggedArray> NewAndCopyTaggedArrayByObject(JSHandle<JSObject> thisObjHandle, uint32_t newLength,
360                                                         uint32_t oldLength, uint32_t k = 0);
361     JSHandle<MutantTaggedArray> NewAndCopyMutantTaggedArrayByObject(JSHandle<JSObject> thisObjHandle,
362                                                                     uint32_t newLength, uint32_t oldLength,
363                                                                     uint32_t k = 0);
364     JSHandle<TaggedArray> NewTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole());
365     JSHandle<TaggedArray> NewTaggedArray(uint32_t length, JSTaggedValue initVal, bool nonMovable);
366     JSHandle<TaggedArray> NewTaggedArray(uint32_t length, JSTaggedValue initVal, MemSpaceType spaceType);
367     // Copy on write array is allocated in nonmovable space by default.
368     JSHandle<COWTaggedArray> NewCOWTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole());
369     JSHandle<COWMutantTaggedArray> NewCOWMutantTaggedArray(uint32_t length, JSTaggedType initVal = base::SPECIAL_HOLE);
370     JSHandle<MutantTaggedArray> NewMutantTaggedArray(uint32_t length, JSTaggedType initVal = base::SPECIAL_HOLE);
371     JSHandle<TaggedArray> NewDictionaryArray(uint32_t length);
372     JSHandle<JSForInIterator> NewJSForinIterator(const JSHandle<JSTaggedValue> &obj,
373                                                  const JSHandle<JSTaggedValue> keys,
374                                                  const JSHandle<JSTaggedValue> cachedHclass);
375 
376     JSHandle<ByteArray> NewByteArray(uint32_t length, uint32_t size, void *srcData = nullptr,
377                                      MemSpaceType spaceType = MemSpaceType::SEMI_SPACE);
378 
379     JSHandle<PropertyBox> NewPropertyBox(const JSHandle<JSTaggedValue> &name);
380 
381     JSHandle<ProtoChangeMarker> NewProtoChangeMarker();
382 
383     JSHandle<ProtoChangeDetails> NewProtoChangeDetails();
384 
385     JSHandle<MarkerCell> NewMarkerCell();
386     JSHandle<BigInt> NewBigInt(uint32_t length);
387     // use for copy properties keys's array to another array
388     JSHandle<TaggedArray> ExtendArray(const JSHandle<TaggedArray> &old, uint32_t length,
389                                       JSTaggedValue initVal = JSTaggedValue::Hole(),
390                                       MemSpaceType type = MemSpaceType::SEMI_SPACE,
391                                       ElementsKind kind = ElementsKind::GENERIC);
392     JSHandle<TaggedArray> CopyPartArray(const JSHandle<TaggedArray> &old, uint32_t start, uint32_t end);
393     JSHandle<TaggedArray> CopyArray(const JSHandle<TaggedArray> &old, uint32_t oldLength, uint32_t newLength,
394                                     JSTaggedValue initVal = JSTaggedValue::Hole(),
395                                     MemSpaceType type = MemSpaceType::SEMI_SPACE,
396                                     ElementsKind kind = ElementsKind::GENERIC);
397     JSHandle<TaggedArray> CopyFromEnumCache(const JSHandle<TaggedArray> &old);
398     JSHandle<TaggedArray> CloneProperties(const JSHandle<TaggedArray> &old);
399     JSHandle<TaggedArray> CloneProperties(const JSHandle<TaggedArray> &old, const JSHandle<JSTaggedValue> &env,
400                                           const JSHandle<JSObject> &obj);
401 
402     JSHandle<LayoutInfo> CreateLayoutInfo(int properties, MemSpaceType type = MemSpaceType::SEMI_SPACE,
403                                           GrowMode mode = GrowMode::GROW);
404 
405     JSHandle<LayoutInfo> ExtendLayoutInfo(const JSHandle<LayoutInfo> &old, int properties);
406 
407     JSHandle<LayoutInfo> CopyLayoutInfo(const JSHandle<LayoutInfo> &old);
408 
409     JSHandle<LayoutInfo> CopyAndReSort(const JSHandle<LayoutInfo> &old, int end, int capacity);
410 
411     JSHandle<EcmaString> GetEmptyString() const;
412 
413     JSHandle<TaggedArray> EmptyArray() const;
414 
415     JSHandle<MutantTaggedArray> EmptyMutantArray() const;
416 
417     FreeObject *FillFreeObject(uintptr_t address, size_t size, RemoveSlots removeSlots = RemoveSlots::NO,
418                                uintptr_t hugeObjectHead = 0);
419 
420     TaggedObject *NewObject(const JSHandle<JSHClass> &hclass);
421 
422     TaggedObject *NewNonMovableObject(const JSHandle<JSHClass> &hclass, uint32_t inobjPropCount = 0);
423 
424     void InitializeExtraProperties(const JSHandle<JSHClass> &hclass, TaggedObject *obj, uint32_t inobjPropCount);
425 
426     JSHandle<TaggedQueue> NewTaggedQueue(uint32_t length);
427 
428     JSHandle<TaggedQueue> GetEmptyTaggedQueue() const;
429 
430     JSHandle<JSSetIterator> NewJSSetIterator(const JSHandle<JSSet> &set, IterationKind kind);
431 
432     JSHandle<JSRegExpIterator> NewJSRegExpIterator(const JSHandle<JSTaggedValue> &matcher,
433                                                    const JSHandle<EcmaString> &inputStr, bool global,
434                                                    bool fullUnicode);
435 
436     JSHandle<JSMapIterator> NewJSMapIterator(const JSHandle<JSMap> &map, IterationKind kind);
437 
438     JSHandle<JSArrayIterator> NewJSArrayIterator(const JSHandle<JSObject> &array, IterationKind kind);
439 
440     JSHandle<CompletionRecord> NewCompletionRecord(CompletionRecordType type, JSHandle<JSTaggedValue> value);
441 
442     JSHandle<GeneratorContext> NewGeneratorContext();
443 
444     JSHandle<JSPromiseReactionsFunction> CreateJSPromiseReactionsFunction(MethodIndex idx);
445 
446     JSHandle<JSPromiseExecutorFunction> CreateJSPromiseExecutorFunction();
447 
448     JSHandle<JSAsyncModuleFulfilledFunction> CreateJSAsyncModuleFulfilledFunction();
449 
450     JSHandle<JSAsyncModuleRejectedFunction> CreateJSAsyncModuleRejectedFunction();
451 
452     JSHandle<JSPromiseAllResolveElementFunction> NewJSPromiseAllResolveElementFunction();
453 
454     JSHandle<JSPromiseAnyRejectElementFunction> NewJSPromiseAnyRejectElementFunction();
455 
456     JSHandle<JSPromiseAllSettledElementFunction> NewJSPromiseAllSettledResolveElementFunction();
457 
458     JSHandle<JSPromiseAllSettledElementFunction> NewJSPromiseAllSettledRejectElementFunction();
459 
460     JSHandle<JSPromiseFinallyFunction> NewJSPromiseThenFinallyFunction();
461 
462     JSHandle<JSPromiseFinallyFunction> NewJSPromiseCatchFinallyFunction();
463 
464     JSHandle<JSPromiseValueThunkOrThrowerFunction> NewJSPromiseValueThunkFunction();
465 
466     JSHandle<JSPromiseValueThunkOrThrowerFunction> NewJSPromiseThrowerFunction();
467 
468     JSHandle<JSAsyncGeneratorResNextRetProRstFtn> NewJSAsyGenResNextRetProRstFulfilledFtn();
469 
470     JSHandle<JSAsyncGeneratorResNextRetProRstFtn> NewJSAsyGenResNextRetProRstRejectedFtn();
471 
472     JSHandle<JSAsyncFromSyncIterUnwarpFunction> NewJSAsyncFromSyncIterUnwarpFunction();
473 
474     JSHandle<JSObject> CloneObjectLiteral(JSHandle<JSObject> object, const JSHandle<JSTaggedValue> &env,
475                                           bool canShareHClass = true);
476     JSHandle<JSObject> CloneObjectLiteral(JSHandle<JSObject> object);
477     JSHandle<JSArray> CloneArrayLiteral(JSHandle<JSArray> object);
478     JSHandle<JSFunction> CloneJSFuction(JSHandle<JSFunction> func);
479     JSHandle<JSFunction> CloneSFunction(JSHandle<JSFunction> func);
480     JSHandle<JSFunction> CloneClassCtor(JSHandle<JSFunction> ctor, const JSHandle<JSTaggedValue> &lexenv,
481                                         bool canShareHClass);
482 
483     void NewJSArrayBufferData(const JSHandle<JSArrayBuffer> &array, int32_t length);
484 
485     JSHandle<JSArrayBuffer> NewJSArrayBuffer(int32_t length);
486 
487     JSHandle<JSArrayBuffer> NewJSArrayBuffer(void *buffer, int32_t length, const DeleteEntryPoint &deleter, void *data,
488                                              bool share = false);
489 
490     JSHandle<JSDataView> NewJSDataView(JSHandle<JSArrayBuffer> buffer, uint32_t offset, uint32_t length);
491 
492     void NewJSSharedArrayBufferData(const JSHandle<JSArrayBuffer> &array, int32_t length);
493 
494     JSHandle<JSArrayBuffer> NewJSSharedArrayBuffer(int32_t length);
495 
496     JSHandle<JSArrayBuffer> NewJSSharedArrayBuffer(void *buffer, int32_t length);
497 
498     void NewJSRegExpByteCodeData(const JSHandle<JSRegExp> &regexp, void *buffer, size_t size);
499 
500     template<typename T, typename S>
501     inline void NewJSIntlIcuData(const JSHandle<T> &obj, const S &icu, const DeleteEntryPoint &callback);
502 
503     EcmaString *InternString(const JSHandle<JSTaggedValue> &key);
504 
505     inline JSHandle<JSNativePointer> NewJSNativePointer(void *externalPointer,
506                                                         const DeleteEntryPoint &callBack = nullptr,
507                                                         void *data = nullptr,
508                                                         bool nonMovable = false,
509                                                         size_t nativeBindingsize = 0,
510                                                         NativeFlag flag = NativeFlag::NO_DIV);
511 
512     JSHandle<JSObject> NewOldSpaceObjLiteralByHClass(const JSHandle<JSHClass> &hclass);
513     JSHandle<JSHClass> SetLayoutInObjHClass(const JSHandle<TaggedArray> &properties, size_t length,
514                                             const JSHandle<JSHClass> &objClass);
515     JSHandle<JSHClass> CreateObjectLiteralRootHClass(size_t length);
516     JSHandle<JSHClass> GetObjectLiteralRootHClass(size_t length);
517     JSHandle<JSHClass> GetObjectLiteralHClass(const JSHandle<TaggedArray> &properties, size_t length);
518     // only use for creating Function.prototype and Function
519     JSHandle<JSFunction> NewJSFunctionByHClass(const JSHandle<Method> &method, const JSHandle<JSHClass> &clazz,
520                                                MemSpaceType type = MemSpaceType::SEMI_SPACE);
521     JSHandle<JSFunction> NewJSFunctionByHClass(const void *func, const JSHandle<JSHClass> &clazz,
522                                                FunctionKind kind = FunctionKind::NORMAL_FUNCTION);
523     JSHandle<Method> NewMethod(const MethodLiteral *methodLiteral, MemSpaceType spaceType = OLD_SPACE);
524 
525     JSHandle<Method> NewMethod(const JSPandaFile *jsPandaFile, MethodLiteral *methodLiteral,
526                                JSHandle<ConstantPool> constpool, JSHandle<JSTaggedValue> module,
527                                uint32_t entryIndex, bool needSetAotFlag, bool *canFastCall = nullptr);
528 
529     // used for creating jsobject by constructor
530     JSHandle<JSObject> NewJSObjectByConstructor(const JSHandle<JSFunction> &constructor,
531                                                 const JSHandle<JSTaggedValue> &newTarget);
532     JSHandle<JSObject> NewJSObjectByConstructor(const JSHandle<JSFunction> &constructor,
533                                                 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS);
534     void InitializeJSObject(const JSHandle<JSObject> &obj, const JSHandle<JSHClass> &jshclass);
535 
536     JSHandle<JSObject> NewJSObjectWithInit(const JSHandle<JSHClass> &jshclass);
537     uintptr_t NewSpaceBySnapshotAllocator(size_t size);
538     JSHandle<MachineCode> NewMachineCodeObject(size_t length, const MachineCodeDesc *desc, JSHandle<Method> &method);
539     JSHandle<ClassInfoExtractor> NewClassInfoExtractor(JSHandle<JSTaggedValue> method);
540     JSHandle<ClassLiteral> NewClassLiteral();
541 
542     // ----------------------------------- new TSType ----------------------------------------
543     JSHandle<TSObjLayoutInfo> CreateTSObjLayoutInfo(int propNum, JSTaggedValue initVal = JSTaggedValue::Hole());
544     JSHandle<TSObjectType> NewTSObjectType(uint32_t numOfKeys);
545     JSHandle<TSClassType> NewTSClassType();
546     JSHandle<TSUnionType> NewTSUnionType(uint32_t length);
547     JSHandle<TSInterfaceType> NewTSInterfaceType();
548     JSHandle<TSClassInstanceType> NewTSClassInstanceType();
549     JSHandle<TSTypeTable> NewTSTypeTable(uint32_t length);
550     JSHandle<TSModuleTable> NewTSModuleTable(uint32_t length);
551     JSHandle<TSFunctionType> NewTSFunctionType(uint32_t length);
552     JSHandle<TSArrayType> NewTSArrayType();
553     JSHandle<TSIteratorInstanceType> NewTSIteratorInstanceType();
554     JSHandle<TSNamespaceType> NewTSNamespaceType();
555 
556     // ----------------------------------- new string ----------------------------------------
557     JSHandle<EcmaString> NewFromASCII(std::string_view data);
558     JSHandle<EcmaString> NewFromUtf8(std::string_view data);
559     JSHandle<EcmaString> NewFromUtf16(std::u16string_view data);
560 
561     JSHandle<EcmaString> NewFromStdString(const std::string &data);
562 
563     JSHandle<EcmaString> NewFromUtf8(const uint8_t *utf8Data, uint32_t utf8Len);
564 
565     JSHandle<EcmaString> NewFromUtf16(const uint16_t *utf16Data, uint32_t utf16Len);
566     JSHandle<EcmaString> NewFromUtf16Compress(const uint16_t *utf16Data, uint32_t utf16Len);
567     JSHandle<EcmaString> NewFromUtf16NotCompress(const uint16_t *utf16Data, uint32_t utf16Len);
568 
569     JSHandle<EcmaString> NewFromUtf8Literal(const uint8_t *utf8Data, uint32_t utf8Len);
570     JSHandle<EcmaString> NewFromUtf8LiteralCompress(const uint8_t *utf8Data, uint32_t utf8Len);
571     JSHandle<EcmaString> NewCompressedUtf8(const uint8_t *utf8Data, uint32_t utf8Len);
572 
573     JSHandle<EcmaString> NewFromUtf16Literal(const uint16_t *utf16Data, uint32_t utf16Len);
574     JSHandle<EcmaString> NewFromUtf16LiteralCompress(const uint16_t *utf16Data, uint32_t utf16Len);
575     JSHandle<EcmaString> NewFromUtf16LiteralNotCompress(const uint16_t *utf16Data, uint32_t utf16Len);
576 
577     inline EcmaString *AllocLineStringObject(size_t size);
578     inline EcmaString *AllocOldSpaceLineStringObject(size_t size);
579     inline EcmaString *AllocNonMovableLineStringObject(size_t size);
580     inline EcmaString *AllocSlicedStringObject(MemSpaceType type);
581     inline EcmaString *AllocConstantStringObject(MemSpaceType type);
582     inline EcmaString *AllocTreeStringObject();
583 
584     JSHandle<EcmaString> ConcatFromString(const JSHandle<EcmaString> &firstString,
585                                           const JSHandle<EcmaString> &secondString);
586 
587     // used for creating Function
588     JSHandle<JSObject> NewJSObject(const JSHandle<JSHClass> &jshclass);
589 
590     // used for creating jshclass in Builtins, Function, Class_Linker
591     JSHandle<JSHClass> NewEcmaHClass(uint32_t size, JSType type, const JSHandle<JSTaggedValue> &prototype);
592     JSHandle<JSHClass> NewEcmaHClass(uint32_t size, uint32_t inlinedProps, JSType type,
593                                      const JSHandle<JSTaggedValue> &prototype);
594 
595     // used for creating jshclass in Builtins, Function, Class_Linker
596     JSHandle<JSHClass> NewEcmaHClass(uint32_t size, JSType type,
597                                      uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS);
598 
599     // It is used to provide iterators for non ECMA standard jsapi containers.
600     JSHandle<JSAPIPlainArray> NewJSAPIPlainArray(uint32_t capacity);
601     JSHandle<JSAPIPlainArrayIterator> NewJSAPIPlainArrayIterator(const JSHandle<JSAPIPlainArray> &plainarray,
602                                                                  IterationKind kind);
603     JSHandle<JSAPIArrayList> NewJSAPIArrayList(uint32_t capacity);
604 
605     JSHandle<JSAPILightWeightMapIterator> NewJSAPILightWeightMapIterator(const JSHandle<JSAPILightWeightMap> &obj,
606                                                                          IterationKind kind);
607     JSHandle<JSAPILightWeightSetIterator> NewJSAPILightWeightSetIterator(const JSHandle<JSAPILightWeightSet> &obj,
608                                                                          IterationKind kind);
609     JSHandle<TaggedArray> CopyQueue(const JSHandle<TaggedArray> &old, uint32_t newLength,
610                                     uint32_t front, uint32_t tail);
611     JSHandle<JSAPIQueueIterator> NewJSAPIQueueIterator(const JSHandle<JSAPIQueue> &queue);
612     JSHandle<TaggedArray> CopyDeque(const JSHandle<TaggedArray> &old, uint32_t newLength, uint32_t oldLength,
613                                     uint32_t first, uint32_t last);
614     JSHandle<JSAPIDequeIterator> NewJSAPIDequeIterator(const JSHandle<JSAPIDeque> &deque);
615     JSHandle<JSAPIArrayListIterator> NewJSAPIArrayListIterator(const JSHandle<JSAPIArrayList> &arrayList);
616     JSHandle<JSAPIList> NewJSAPIList();
617     JSHandle<JSAPILinkedList> NewJSAPILinkedList();
618     JSHandle<JSAPILinkedListIterator> NewJSAPILinkedListIterator(const JSHandle<JSAPILinkedList> &linkedList);
619     JSHandle<JSAPIListIterator> NewJSAPIListIterator(const JSHandle<JSAPIList> &list);
620     JSHandle<JSAPITreeMapIterator> NewJSAPITreeMapIterator(const JSHandle<JSAPITreeMap> &map, IterationKind kind);
621     JSHandle<JSAPITreeSetIterator> NewJSAPITreeSetIterator(const JSHandle<JSAPITreeSet> &set, IterationKind kind);
622     JSHandle<JSAPIStackIterator> NewJSAPIStackIterator(const JSHandle<JSAPIStack> &stack);
623     JSHandle<JSAPIVector> NewJSAPIVector(uint32_t capacity);
624     JSHandle<JSAPIVectorIterator> NewJSAPIVectorIterator(const JSHandle<JSAPIVector> &vector);
625     JSHandle<JSAPIHashMapIterator> NewJSAPIHashMapIterator(const JSHandle<JSAPIHashMap> &hashMap, IterationKind kind);
626     JSHandle<JSAPIHashSetIterator> NewJSAPIHashSetIterator(const JSHandle<JSAPIHashSet> &hashSet, IterationKind kind);
627     JSHandle<TaggedHashArray> NewTaggedHashArray(uint32_t length);
628     JSHandle<LinkedNode> NewLinkedNode(int hash, const JSHandle<JSTaggedValue> &key,
629                                        const JSHandle<JSTaggedValue> &value,
630                                        const JSHandle<LinkedNode> &next);
631     JSHandle<RBTreeNode> NewTreeNode(int hash, const JSHandle<JSTaggedValue> &key,
632                                      const JSHandle<JSTaggedValue> &value);
633     // --------------------------------------module--------------------------------------------
634     JSHandle<ModuleNamespace> NewModuleNamespace();
635     JSHandle<ImportEntry> NewImportEntry();
636     JSHandle<ImportEntry> NewImportEntry(const JSHandle<JSTaggedValue> &moduleRequest,
637                                          const JSHandle<JSTaggedValue> &importName,
638                                          const JSHandle<JSTaggedValue> &localName);
639     JSHandle<LocalExportEntry> NewLocalExportEntry();
640     JSHandle<LocalExportEntry> NewLocalExportEntry(
641         const JSHandle<JSTaggedValue> &exportName, const JSHandle<JSTaggedValue> &localName,
642         const uint32_t index = 0);
643     JSHandle<IndirectExportEntry> NewIndirectExportEntry();
644     JSHandle<IndirectExportEntry> NewIndirectExportEntry(const JSHandle<JSTaggedValue> &exportName,
645                                                          const JSHandle<JSTaggedValue> &moduleRequest,
646                                                          const JSHandle<JSTaggedValue> &importName);
647     JSHandle<StarExportEntry> NewStarExportEntry();
648     JSHandle<StarExportEntry> NewStarExportEntry(const JSHandle<JSTaggedValue> &moduleRequest);
649     JSHandle<SourceTextModule> NewSourceTextModule();
650     JSHandle<ResolvedBinding> NewResolvedBindingRecord();
651     JSHandle<ResolvedBinding> NewResolvedBindingRecord(const JSHandle<SourceTextModule> &module,
652                                                        const JSHandle<JSTaggedValue> &bindingName);
653     JSHandle<ResolvedIndexBinding> NewResolvedIndexBindingRecord();
654     JSHandle<ResolvedIndexBinding> NewResolvedIndexBindingRecord(const JSHandle<SourceTextModule> &module,
655                                                                            int32_t index);
656 
657     JSHandle<CellRecord> NewCellRecord();
658     JSHandle<JSFunction> NewJSAsyncGeneratorFunction(const JSHandle<Method> &method);
659     // --------------------------------------require--------------------------------------------
660     JSHandle<CjsModule> NewCjsModule();
661     JSHandle<CjsExports> NewCjsExports();
662     JSHandle<CjsRequire> NewCjsRequire();
663 
664     JSHandle<JSHClass> CreateIteratorResultInstanceClass(const JSHandle<GlobalEnv> &env);
665 
666     // --------------------------------------old space object--------------------------------------------
667     JSHandle<JSObject> NewOldSpaceJSObject(const JSHandle<JSHClass> &jshclass);
668     TaggedObject *NewOldSpaceObject(const JSHandle<JSHClass> &hclass);
669     JSHandle<TaggedArray> NewOldSpaceTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole());
670 
671     // ---------------------------------New objects used internally--------------------------------------
672     JSHandle<JSArray> NewJSStableArrayWithElements(const JSHandle<TaggedArray> &elements);
673 
674     // ---------------------------------------Used by AOT------------------------------------------------
675     JSHandle<AOTLiteralInfo> NewAOTLiteralInfo(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole());
676     JSHandle<VTable> NewVTable(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole());
677     JSHandle<JSHClass> NewEcmaHClass(JSHClass *hclass, uint32_t size, JSType type,
678                                      uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS);
679 
680     // napi interface to create object with initial inline properties
681     JSHandle<JSTaggedValue> CreateJSObjectWithProperties(size_t propertyCount, const Local<JSValueRef> *keys,
682                                                          const PropertyDescriptor *attributes);
683     JSHandle<JSTaggedValue> CreateJSObjectWithNamedProperties(size_t propertyCount, const char **keys,
684                                                               const Local<JSValueRef> *values);
685 
686     // Fill the given free memory range with special zam value.
687     void FillFreeMemoryRange(uintptr_t start, uintptr_t end);
688 
689 private:
690     friend class GlobalEnv;
691     friend class GlobalEnvConstants;
692     friend class EcmaString;
693     friend class SnapshotProcessor;
694     friend class TSManager;
695     friend class SingleCharTable;
696     void InitObjectFields(const TaggedObject *object);
697 
698     JSThread *thread_ {nullptr};
699     bool isTriggerGc_ {false};
700     bool triggerSemiGC_ {false};
701 
702     EcmaVM *vm_ {nullptr};
703     Heap *heap_ {nullptr};
704 
705     static constexpr uint32_t LENGTH_THRESHOLD = 50;
706     static constexpr int MAX_LITERAL_HCLASS_CACHE_SIZE = 63;
707 
708     NO_COPY_SEMANTIC(ObjectFactory);
709     NO_MOVE_SEMANTIC(ObjectFactory);
710 
711     void NewObjectHook() const;
712 
713     // used for creating jshclass in GlobalEnv, EcmaVM
714     JSHandle<JSHClass> NewEcmaHClassClass(JSHClass *hclass, uint32_t size, JSType type);
715 
716     JSHandle<JSHClass> NewEcmaReadOnlyHClass(JSHClass *hclass, uint32_t size, JSType type,
717                                              uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS);
718     JSHandle<JSHClass> InitClassClass();
719 
720     // used to create nonmovable js_object
721     JSHandle<JSObject> NewNonMovableJSObject(const JSHandle<JSHClass> &jshclass);
722 
723     // used to create nonmovable utf8 string at global constants
724     JSHandle<EcmaString> NewFromASCIINonMovable(std::string_view data);
725 
726     // used for creating Function
727     JSHandle<JSFunction> NewJSFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &hclass);
728     JSHandle<JSHClass> CreateObjectClass(const JSHandle<TaggedArray> &keys, const JSHandle<TaggedArray> &values);
729     JSHandle<JSHClass> CreateObjectClass(const JSHandle<TaggedArray> &properties, size_t length);
730     JSHandle<JSHClass> CreateFunctionClass(FunctionKind kind, uint32_t size, JSType type,
731                                            const JSHandle<JSTaggedValue> &prototype);
732     JSHandle<JSHClass> CreateBoundFunctionClass();
733     JSHandle<JSHClass> CreateDefaultClassPrototypeHClass(JSHClass *hclass);
734     JSHandle<JSHClass> CreateDefaultClassConstructorHClass(JSHClass *hclass);
735 
736     // used for creating ref.prototype in buildins, such as Number.prototype
737     JSHandle<JSPrimitiveRef> NewJSPrimitiveRef(const JSHandle<JSHClass> &hclass,
738                                                const JSHandle<JSTaggedValue> &object);
739 
740     JSHandle<EcmaString> GetStringFromStringTable(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress) const;
741     JSHandle<EcmaString> GetStringFromStringTableNonMovable(const uint8_t *utf8Data, uint32_t utf8Len) const;
742     // For MUtf-8 string data
743     EcmaString* PUBLIC_API GetRawStringFromStringTable(StringData sd, MemSpaceType type = MemSpaceType::SEMI_SPACE,
744                                                        bool isConstantString = false, uint32_t idOffset = 0) const;
745 
746     JSHandle<EcmaString> GetStringFromStringTable(const uint16_t *utf16Data, uint32_t utf16Len,
747                                                   bool canBeCompress) const;
748 
749     JSHandle<EcmaString> GetStringFromStringTable(EcmaString *string) const;
750 
751     JSHandle<EcmaString> GetStringFromStringTable(const JSHandle<EcmaString> &firstString,
752                                                   const JSHandle<EcmaString> &secondString);
753 
754     JSHandle<TaggedArray> NewEmptyArray();  // only used for EcmaVM.
755     JSHandle<MutantTaggedArray> NewEmptyMutantArray();
756 
757     JSHandle<JSHClass> CreateJSArguments(const JSHandle<GlobalEnv> &env);
758     JSHandle<JSHClass> CreateJSArrayInstanceClass(JSHandle<JSTaggedValue> proto,
759                                                   uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS);
760     JSHandle<JSHClass> CreateJSRegExpInstanceClass(JSHandle<JSTaggedValue> proto);
761 
762     inline TaggedObject *AllocObjectWithSpaceType(size_t size, JSHClass *cls, MemSpaceType type);
763     JSHandle<TaggedArray> NewTaggedArrayWithoutInit(uint32_t length, MemSpaceType spaceType);
764 
765     // For object with many properties, directly create new HClass instead of searching on transitions
766     JSHandle<JSTaggedValue> CreateLargeJSObjectWithProperties(size_t propertyCount,
767                                                               const Local<JSValueRef> *keys,
768                                                               const PropertyDescriptor *descs);
769     JSHandle<JSTaggedValue> CreateLargeJSObjectWithNamedProperties(size_t propertyCount, const char **keys,
770                                                                    const Local<JSValueRef> *values);
771     // For object with numerous properties, directly create it in dictionary mode
772     JSHandle<JSTaggedValue> CreateDictionaryJSObjectWithProperties(size_t propertyCount,
773                                                                    const Local<JSValueRef> *keys,
774                                                                    const PropertyDescriptor *descs);
775     JSHandle<JSTaggedValue> CreateDictionaryJSObjectWithNamedProperties(size_t propertyCount, const char **keys,
776                                                                         const Local<JSValueRef> *values);
777 
778     JSHandle<MutantTaggedArray> NewMutantTaggedArrayWithoutInit(uint32_t length, MemSpaceType spaceType);
779 
780     // For sharedobject
781     JSHandle<JSFunction> NewSFunction(const JSHandle<Method> &methodHandle,
782                                       const JSHandle<JSTaggedValue> &homeObject);
783     JSHandle<JSFunction> NewSFunctionByHClass(const void *func, const JSHandle<JSHClass> &hclass,
784                                               FunctionKind kind);
785     JSHandle<JSFunction> NewSFunctionByHClass(const JSHandle<Method> &methodHandle,
786                                               const JSHandle<JSHClass> &hclass);
787     JSHandle<JSHClass> CreateSFunctionClassWithoutProto(uint32_t size, JSType type,
788                                                         const JSHandle<JSTaggedValue> &prototype);
789     friend class Builtins;    // create builtins object need hclass
790     friend class JSFunction;  // create prototype_or_hclass need hclass
791     friend class JSHClass;    // HC transition need hclass
792     friend class EcmaVM;      // hold the factory instance
793     friend class JsVerificationTest;
794     friend class PandaFileTranslator;
795     friend class LiteralDataExtractor;
796     friend class RuntimeStubs;
797     friend class ClassInfoExtractor;
798     friend class ModuleDataExtractor;
799     friend class ModuleDataAccessor;
800     friend class ConstantPool;
801     friend class EcmaContext;
802     friend class kungfu::TSHClassGenerator;
803 };
804 
805 class ClassLinkerFactory {
806 private:
807     friend class GlobalEnv;  // root class in class_linker need hclass
808     friend class EcmaVM;     // root class in class_linker need hclass
809 };
810 }  // namespace panda::ecmascript
811 #endif  // ECMASCRIPT_OBJECT_FACTORY_H
812