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