• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "ecmascript/jspandafile/program_object.h"
17 #include "ecmascript/layout_info-inl.h"
18 #include "ecmascript/mem/heap-inl.h"
19 #include "ecmascript/symbol_table.h"
20 #include "ecmascript/jspandafile/program_object.h"
21 
22 // class Object;
23 namespace panda::ecmascript {
NewSObjectHook() const24 void ObjectFactory::NewSObjectHook() const
25 {
26     CHECK_NO_HEAP_ALLOC;
27 #ifndef NDEBUG
28     static std::atomic<uint32_t> count = 0;
29     static uint32_t frequency = vm_->GetJSOptions().GetForceSharedGCFrequency();
30     static constexpr uint32_t CONCURRENT_MARK_FREQUENCY_FACTOR = 2;
31     if (frequency == 0 || !vm_->GetJSOptions().EnableForceGC() || !vm_->IsInitialized() ||
32         !thread_->IsAllContextsInitialized()) {
33         return;
34     }
35     if (count++ % frequency == 0) {
36         if (count % (CONCURRENT_MARK_FREQUENCY_FACTOR * frequency) == 0) {
37             sHeap_->CollectGarbage<TriggerGCType::SHARED_GC, GCReason::OTHER>(thread_);
38         } else if (sHeap_->CheckCanTriggerConcurrentMarking(thread_)) {
39             sHeap_->TriggerConcurrentMarking<TriggerGCType::SHARED_GC, GCReason::OTHER>(thread_);
40         }
41         if (!ecmascript::AnFileDataManager::GetInstance()->IsEnable()) {
42             sHeap_->WaitGCFinished(thread_);
43             sHeap_->CollectGarbage<TriggerGCType::SHARED_FULL_GC, GCReason::OTHER>(thread_);
44         }
45     }
46 #endif
47 }
48 
CreateSFunctionClass(uint32_t size,JSType type,const JSHandle<JSTaggedValue> & prototype,bool isAccessor,bool setProto)49 JSHandle<JSHClass> ObjectFactory::CreateSFunctionClass(uint32_t size, JSType type,
50                                                        const JSHandle<JSTaggedValue> &prototype,
51                                                        bool isAccessor, bool setProto)
52 {
53     const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
54     uint32_t fieldOrder = 0;
55     ASSERT(JSFunction::LENGTH_INLINE_PROPERTY_INDEX == fieldOrder);
56     PropertyAttributes attributes = PropertyAttributes::Default(false, false, false);
57     attributes.SetIsAccessor(isAccessor);
58     attributes.SetIsInlinedProps(true);
59     attributes.SetRepresentation(Representation::TAGGED);
60     JSHandle<LayoutInfo> layoutInfoHandle = CreateSLayoutInfo(JSFunction::LENGTH_OF_INLINE_PROPERTIES);
61     {
62         attributes.SetOffset(fieldOrder);
63         layoutInfoHandle->AddKey(thread_, fieldOrder, globalConst->GetLengthString(), attributes);
64         fieldOrder++;
65     }
66 
67     ASSERT(JSFunction::NAME_INLINE_PROPERTY_INDEX == fieldOrder);
68     {
69         attributes.SetOffset(fieldOrder);
70         layoutInfoHandle->AddKey(thread_, fieldOrder,
71                                  globalConst->GetHandledNameString().GetTaggedValue(), attributes);
72         fieldOrder++;
73     }
74     if (setProto) {
75         ASSERT(JSFunction::PROTOTYPE_INLINE_PROPERTY_INDEX == fieldOrder);
76         {
77             attributes.SetOffset(fieldOrder);
78             layoutInfoHandle->AddKey(thread_, fieldOrder,
79                                      globalConst->GetPrototypeString(), attributes);
80             fieldOrder++;
81         }
82     }
83     JSHandle<JSHClass> functionClass = NewSEcmaHClass(size, fieldOrder, type, prototype,
84         JSHandle<JSTaggedValue>(layoutInfoHandle));
85     functionClass->SetCallable(true);
86     return functionClass;
87 }
88 
NewSEcmaHClass(uint32_t size,JSType type,uint32_t inlinedProps)89 JSHandle<JSHClass> ObjectFactory::NewSEcmaHClass(uint32_t size, JSType type, uint32_t inlinedProps)
90 {
91     return NewSEcmaHClass(JSHClass::Cast(thread_->GlobalConstants()->GetHClassClass().GetTaggedObject()),
92                           size, type, inlinedProps);
93 }
94 
NewSEcmaHClass(JSHClass * hclass,uint32_t size,JSType type,uint32_t inlinedProps)95 JSHandle<JSHClass> ObjectFactory::NewSEcmaHClass(JSHClass *hclass, uint32_t size, JSType type, uint32_t inlinedProps)
96 {
97     NewSObjectHook();
98     uint32_t classSize = JSHClass::SIZE;
99     auto *newClass = static_cast<JSHClass *>(sHeap_->AllocateNonMovableOrHugeObject(thread_, hclass, classSize));
100     newClass->Initialize(thread_, size, type, inlinedProps, thread_->GlobalConstants()->GetHandledEmptySLayoutInfo());
101     return JSHandle<JSHClass>(thread_, newClass);
102 }
103 
104 // This function don't UpdateProtoClass
NewSEcmaHClass(uint32_t size,uint32_t inlinedProps,JSType type,const JSHandle<JSTaggedValue> & prototype,const JSHandle<JSTaggedValue> & layout)105 JSHandle<JSHClass> ObjectFactory::NewSEcmaHClass(uint32_t size, uint32_t inlinedProps, JSType type,
106     const JSHandle<JSTaggedValue> &prototype, const JSHandle<JSTaggedValue> &layout)
107 {
108     NewSObjectHook();
109     uint32_t classSize = JSHClass::SIZE;
110     auto *newClass = static_cast<JSHClass *>(sHeap_->AllocateNonMovableOrHugeObject(
111         thread_, JSHClass::Cast(thread_->GlobalConstants()->GetHClassClass().GetTaggedObject()), classSize));
112     newClass->Initialize(thread_, size, type, inlinedProps, layout);
113     JSHandle<JSHClass> hclass(thread_, newClass);
114     if (prototype->IsJSObject()) {
115         prototype->GetTaggedObject()->GetClass()->SetIsPrototype(true);
116     }
117     hclass->SetProto(thread_, prototype.GetTaggedValue());
118     hclass->SetNumberOfProps(inlinedProps);
119     hclass->SetExtensible(false);
120     return hclass;
121 }
122 
NewSEcmaHClassDictMode(uint32_t size,uint32_t inlinedProps,JSType type,const JSHandle<JSTaggedValue> & prototype)123 JSHandle<JSHClass> ObjectFactory::NewSEcmaHClassDictMode(uint32_t size, uint32_t inlinedProps, JSType type,
124                                                          const JSHandle<JSTaggedValue> &prototype)
125 {
126     NewSObjectHook();
127     uint32_t classSize = JSHClass::SIZE;
128     auto *newClass = static_cast<JSHClass *>(sHeap_->AllocateNonMovableOrHugeObject(thread_,
129         JSHClass::Cast(thread_->GlobalConstants()->GetHClassClass().GetTaggedObject()), classSize));
130     newClass->Initialize(thread_, size, type, inlinedProps, thread_->GlobalConstants()->GetHandledEmptySLayoutInfo());
131     JSHandle<JSHClass> hclass(thread_, newClass);
132     if (prototype->IsJSObject()) {
133         prototype->GetTaggedObject()->GetClass()->SetIsPrototype(true);
134     }
135     hclass->SetProto(thread_, prototype.GetTaggedValue());
136     hclass->SetNumberOfProps(0);
137     hclass->SetIsDictionaryMode(true);
138     hclass->SetExtensible(false);
139     return hclass;
140 }
141 
NewSEcmaHClassClass(JSHClass * hclass,uint32_t size,JSType type)142 JSHandle<JSHClass> ObjectFactory::NewSEcmaHClassClass(JSHClass *hclass, uint32_t size, JSType type)
143 {
144     NewSObjectHook();
145     uint32_t classSize = JSHClass::SIZE;
146     auto *newClass = static_cast<JSHClass *>(sHeap_->AllocateClassClass(thread_, hclass, classSize));
147     newClass->Initialize(thread_, size, type, 0, thread_->GlobalConstants()->GetHandledEmptySLayoutInfo());
148     return JSHandle<JSHClass>(thread_, newClass);
149 }
150 
NewSEcmaReadOnlyHClass(JSHClass * hclass,uint32_t size,JSType type,uint32_t inlinedProps)151 JSHandle<JSHClass> ObjectFactory::NewSEcmaReadOnlyHClass(JSHClass *hclass, uint32_t size, JSType type,
152                                                          uint32_t inlinedProps)
153 {
154     NewSObjectHook();
155     uint32_t classSize = JSHClass::SIZE;
156     auto *newClass = static_cast<JSHClass *>(sHeap_->AllocateReadOnlyOrHugeObject(thread_, hclass, classSize));
157     ASSERT(newClass != nullptr);
158     newClass->Initialize(thread_, size, type, inlinedProps, thread_->GlobalConstants()->GetHandledEmptySLayoutInfo());
159     return JSHandle<JSHClass>(thread_, newClass);
160 }
161 
InitSClassClass()162 JSHandle<JSHClass> ObjectFactory::InitSClassClass()
163 {
164     JSHandle<JSHClass> hClassHandle = NewSEcmaHClassClass(nullptr, JSHClass::SIZE, JSType::HCLASS);
165     JSHClass *hclass = reinterpret_cast<JSHClass *>(hClassHandle.GetTaggedValue().GetTaggedObject());
166     hclass->SetClass(thread_, hclass);
167     return hClassHandle;
168 }
169 
NewSAccessorData()170 JSHandle<AccessorData> ObjectFactory::NewSAccessorData()
171 {
172     NewSObjectHook();
173     TaggedObject *header = sHeap_->AllocateOldOrHugeObject(
174         thread_, JSHClass::Cast(thread_->GlobalConstants()->GetAccessorDataClass().GetTaggedObject()));
175     JSHandle<AccessorData> acc(thread_, AccessorData::Cast(header));
176     acc->SetGetter(thread_, JSTaggedValue::Undefined());
177     acc->SetSetter(thread_, JSTaggedValue::Undefined());
178     return acc;
179 }
180 
NewSMethod(const JSPandaFile * jsPandaFile,MethodLiteral * methodLiteral,JSHandle<ConstantPool> constpool,uint32_t entryIndex,bool needSetAotFlag,bool * canFastCall)181 JSHandle<Method> ObjectFactory::NewSMethod(const JSPandaFile *jsPandaFile, MethodLiteral *methodLiteral,
182                                            JSHandle<ConstantPool> constpool, uint32_t entryIndex,
183                                            bool needSetAotFlag, bool *canFastCall)
184 {
185     JSHandle<Method> method;
186     if (jsPandaFile->IsNewVersion()) {
187         method = Method::Create(thread_, jsPandaFile, methodLiteral);
188     } else {
189         method = NewSMethod(methodLiteral);
190         method->SetConstantPool(thread_, constpool);
191     }
192     if (needSetAotFlag) {
193         auto aotFileManager = thread_->GetEcmaVM()->GetAOTFileManager();
194         aotFileManager->SetAOTFuncEntry(jsPandaFile, nullptr, *method, entryIndex, canFastCall);
195     } else {
196         method->InitInterpreterStatusForCompiledMethod(thread_);
197     }
198     return method;
199 }
200 
NewSMethod(const MethodLiteral * methodLiteral,MemSpaceType spaceType)201 JSHandle<Method> ObjectFactory::NewSMethod(const MethodLiteral *methodLiteral, MemSpaceType spaceType)
202 {
203     ASSERT(spaceType == SHARED_NON_MOVABLE || spaceType == SHARED_OLD_SPACE);
204     NewSObjectHook();
205     TaggedObject *header = nullptr;
206     if (spaceType == SHARED_NON_MOVABLE) {
207         header = sHeap_->AllocateNonMovableOrHugeObject(thread_,
208             JSHClass::Cast(thread_->GlobalConstants()->GetMethodClass().GetTaggedObject()));
209     } else {
210         header = sHeap_->AllocateOldOrHugeObject(thread_,
211             JSHClass::Cast(thread_->GlobalConstants()->GetMethodClass().GetTaggedObject()));
212     }
213     JSHandle<Method> method(thread_, header);
214     InitializeMethod(methodLiteral, method);
215     return method;
216 }
217 
NewSMethodForNativeFunction(const void * func,FunctionKind kind,kungfu::BuiltinsStubCSigns::ID builtinId,MemSpaceType spaceType)218 JSHandle<Method> ObjectFactory::NewSMethodForNativeFunction(const void *func, FunctionKind kind,
219                                                             kungfu::BuiltinsStubCSigns::ID builtinId,
220                                                             MemSpaceType spaceType)
221 {
222     uint32_t numArgs = 2;  // function object and this
223     auto method = NewSMethod(nullptr, spaceType);
224     method->SetNativePointer(const_cast<void *>(func));
225     method->SetNativeBit(true);
226     if (builtinId != kungfu::BuiltinsStubCSigns::INVALID) {
227         bool isFast = kungfu::BuiltinsStubCSigns::IsFastBuiltin(builtinId);
228         method->SetFastBuiltinBit(isFast);
229         method->SetBuiltinId(static_cast<uint8_t>(builtinId));
230     }
231     method->SetNumArgsWithCallField(numArgs);
232     method->SetFunctionKind(kind);
233     return method;
234 }
235 
NewSFunctionByHClass(const JSHandle<Method> & method,const JSHandle<JSHClass> & hclass)236 JSHandle<JSFunction> ObjectFactory::NewSFunctionByHClass(const JSHandle<Method> &method,
237                                                          const JSHandle<JSHClass> &hclass)
238 {
239     JSHandle<JSFunction> function(NewSharedOldSpaceJSObject(hclass));
240     hclass->SetCallable(true);
241     JSFunction::InitializeSFunction(thread_, function, method->GetFunctionKind());
242     function->SetMethod(thread_, method);
243     function->SetTaskConcurrentFuncFlag(0); // 0 : default value
244     if (method->IsAotWithCallField()) {
245         thread_->GetEcmaVM()->GetAOTFileManager()->
246             SetAOTFuncEntry(method->GetJSPandaFile(), *function, *method);
247     } else {
248         SetCodeEntryToFunctionFromMethod(function, method);
249     }
250     return function;
251 }
252 
253 // new function with name/length accessor
NewSFunctionWithAccessor(const void * func,const JSHandle<JSHClass> & hclass,FunctionKind kind,kungfu::BuiltinsStubCSigns::ID builtinId,MemSpaceType spaceType)254 JSHandle<JSFunction> ObjectFactory::NewSFunctionWithAccessor(const void *func, const JSHandle<JSHClass> &hclass,
255     FunctionKind kind, kungfu::BuiltinsStubCSigns::ID builtinId, MemSpaceType spaceType)
256 {
257     ASSERT(spaceType == SHARED_NON_MOVABLE || spaceType == SHARED_OLD_SPACE);
258     JSHandle<Method> method = NewSMethodForNativeFunction(func, kind, builtinId, spaceType);
259     return NewSFunctionByHClass(method, hclass);
260 }
261 
262 // new function without name/length accessor
NewSFunctionByHClass(const void * func,const JSHandle<JSHClass> & hclass,FunctionKind kind,kungfu::BuiltinsStubCSigns::ID builtinId,MemSpaceType spaceType)263 JSHandle<JSFunction> ObjectFactory::NewSFunctionByHClass(const void *func, const JSHandle<JSHClass> &hclass,
264     FunctionKind kind, kungfu::BuiltinsStubCSigns::ID builtinId, MemSpaceType spaceType)
265 {
266     ASSERT(spaceType == SHARED_NON_MOVABLE || spaceType == SHARED_OLD_SPACE);
267     JSHandle<Method> method = NewSMethodForNativeFunction(func, kind, builtinId, spaceType);
268     JSHandle<JSFunction> function(NewSharedOldSpaceJSObject(hclass));
269     hclass->SetCallable(true);
270     JSFunction::InitializeWithDefaultValue(thread_, function);
271     function->SetMethod(thread_, method);
272     return function;
273 }
274 
NewSharedOldSpaceObject(const JSHandle<JSHClass> & hclass)275 TaggedObject *ObjectFactory::NewSharedOldSpaceObject(const JSHandle<JSHClass> &hclass)
276 {
277     NewSObjectHook();
278     TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_, *hclass);
279     if (header == nullptr) {
280         LOG_ECMA(FATAL) << "ObjectFactory::NewSharedOldSpaceObject:header is nullptr";
281     }
282     uint32_t inobjPropCount = hclass->GetInlinedProperties();
283     if (inobjPropCount > 0) {
284         InitializeExtraProperties(hclass, header, inobjPropCount);
285     }
286     return header;
287 }
288 
NewSharedOldSpaceJSObjectWithInit(const JSHandle<JSHClass> & jshclass)289 JSHandle<JSObject> ObjectFactory::NewSharedOldSpaceJSObjectWithInit(const JSHandle<JSHClass> &jshclass)
290 {
291     auto obj = NewSharedOldSpaceJSObject(jshclass);
292     InitializeJSObject(obj, jshclass);
293     return obj;
294 }
295 
NewSharedOldSpaceJSObject(const JSHandle<JSHClass> & jshclass)296 JSHandle<JSObject> ObjectFactory::NewSharedOldSpaceJSObject(const JSHandle<JSHClass> &jshclass)
297 {
298     JSHandle<JSObject> obj(thread_, JSObject::Cast(NewSharedOldSpaceObject(jshclass)));
299     JSHandle<TaggedArray> emptyArray = SharedEmptyArray();
300     obj->InitializeHash();
301     obj->SetElements(thread_, emptyArray);
302     obj->SetProperties(thread_, emptyArray);
303     return obj;
304 }
305 
CreateSObjectWithProperties(std::vector<PropertyDescriptor> & descs)306 JSHandle<JSTaggedValue> ObjectFactory::CreateSObjectWithProperties(std::vector<PropertyDescriptor> &descs)
307 {
308     JSHandle<JSHClass> hclass = JSHClass::CreateSHClass(thread_, descs);
309     JSHandle<JSObject> object = NewSharedOldSpaceJSObject(hclass);
310     JSObject::SetSProperties(thread_, object, descs);
311     JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
312     JSHandle<JSTaggedValue> objFuncProto = env->GetSObjectFunctionPrototype();
313     hclass->SetPrototype(thread_, objFuncProto);
314     hclass->SetExtensible(false);
315     return JSHandle<JSTaggedValue>(object);
316 }
317 
SharedEmptyArray() const318 JSHandle<TaggedArray> ObjectFactory::SharedEmptyArray() const
319 {
320     return JSHandle<TaggedArray>(thread_->GlobalConstants()->GetHandledEmptyArray());
321 }
322 
CopySArray(const JSHandle<TaggedArray> & old,uint32_t oldLength,uint32_t newLength,JSTaggedValue initVal,ElementsKind kind)323 JSHandle<TaggedArray> ObjectFactory::CopySArray(const JSHandle<TaggedArray> &old, uint32_t oldLength,
324                                                 uint32_t newLength, JSTaggedValue initVal, ElementsKind kind)
325 {
326     if (newLength == 0) {
327         return SharedEmptyArray();
328     }
329     if (newLength > oldLength) {
330         return ExtendSArray(old, newLength, initVal, kind);
331     }
332 
333     NewObjectHook();
334     // Shared-array does not support Mutantarray yet.
335     ASSERT(!old->GetClass()->IsMutantTaggedArray());
336 
337     size_t size = TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), newLength);
338     JSHClass *arrayClass = JSHClass::Cast(thread_->GlobalConstants()->GetArrayClass().GetTaggedObject());
339     TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_, arrayClass, size);
340     JSHandle<TaggedArray> newArray(thread_, header);
341     newArray->SetLength(newLength);
342     newArray->SetExtraLength(old->GetExtraLength());
343 
344     for (uint32_t i = 0; i < newLength; i++) {
345         newArray->Set(thread_, i, old->Get(i));
346     }
347 
348     return newArray;
349 }
350 
ExtendSArray(const JSHandle<TaggedArray> & old,uint32_t length,JSTaggedValue initVal,ElementsKind kind)351 JSHandle<TaggedArray> ObjectFactory::ExtendSArray(const JSHandle<TaggedArray> &old, uint32_t length,
352                                                   JSTaggedValue initVal, [[maybe_unused]] ElementsKind kind)
353 {
354     ASSERT(length > old->GetLength());
355     NewObjectHook();
356     size_t size = TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), length);
357     JSHClass *arrayClass = nullptr;
358     // Shared-array does not support Mutantarray yet.
359     ASSERT(!old->GetClass()->IsMutantTaggedArray());
360     arrayClass = JSHClass::Cast(thread_->GlobalConstants()->GetArrayClass().GetTaggedObject());
361 
362     TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_, arrayClass, size);
363     JSHandle<TaggedArray> newArray(thread_, header);
364     newArray->SetLength(length);
365     newArray->SetExtraLength(old->GetExtraLength());
366 
367     uint32_t oldLength = old->GetLength();
368     for (uint32_t i = 0; i < oldLength; i++) {
369         newArray->Set(thread_, i, old->Get(i));
370     }
371     for (uint32_t i = oldLength; i < length; i++) {
372         newArray->Set(thread_, i, initVal);
373     }
374     return newArray;
375 }
376 
NewSTaggedArrayWithoutInit(uint32_t length,MemSpaceType spaceType)377 JSHandle<TaggedArray> ObjectFactory::NewSTaggedArrayWithoutInit(uint32_t length, MemSpaceType spaceType)
378 {
379     NewSObjectHook();
380     size_t size = TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), length);
381     TaggedObject *header;
382     auto arrayClass = JSHClass::Cast(thread_->GlobalConstants()->GetArrayClass().GetTaggedObject());
383     switch (spaceType) {
384         case MemSpaceType::SHARED_OLD_SPACE:
385             header = sHeap_->AllocateOldOrHugeObject(thread_, arrayClass, size);
386             break;
387         case MemSpaceType::SHARED_READ_ONLY_SPACE:
388             header = sHeap_->AllocateReadOnlyOrHugeObject(thread_, arrayClass, size);
389             break;
390         default:
391             LOG_ECMA(FATAL) << "this branch is unreachable";
392             UNREACHABLE();
393     }
394     JSHandle<TaggedArray> array(thread_, header);
395     array->SetLength(length);
396     return array;
397 }
398 
CreateSLayoutInfo(uint32_t properties)399 JSHandle<LayoutInfo> ObjectFactory::CreateSLayoutInfo(uint32_t properties)
400 {
401     uint32_t arrayLength = LayoutInfo::ComputeArrayLength(properties);
402     JSHandle<LayoutInfo> layoutInfoHandle = JSHandle<LayoutInfo>::Cast(NewSTaggedArrayWithoutInit(arrayLength));
403     layoutInfoHandle->Initialize(thread_);
404     return layoutInfoHandle;
405 }
406 
NewSEmptyLayoutInfo()407 JSHandle<LayoutInfo> ObjectFactory::NewSEmptyLayoutInfo()
408 {
409     JSHandle<LayoutInfo> layoutInfoHandle = JSHandle<LayoutInfo>::Cast(
410         NewSTaggedArrayWithoutInit(0, MemSpaceType::SHARED_READ_ONLY_SPACE));
411     layoutInfoHandle->Initialize(thread_);
412     return layoutInfoHandle;
413 }
414 
NewJSSArray()415 JSHandle<JSSharedArray> ObjectFactory::NewJSSArray()
416 {
417     JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
418     JSHandle<JSFunction> function(env->GetSharedArrayFunction());
419     return JSHandle<JSSharedArray>(NewJSObjectByConstructor(function));
420 }
421 
NewSJsonFixedArray(size_t start,size_t length,const std::vector<JSHandle<JSTaggedValue>> & vec)422 JSHandle<TaggedArray> ObjectFactory::NewSJsonFixedArray(size_t start, size_t length,
423                                                         const std::vector<JSHandle<JSTaggedValue>> &vec)
424 {
425     if (length == 0) {
426         return SharedEmptyArray();
427     }
428 
429     JSHandle<TaggedArray> array = NewTaggedArrayWithoutInit(length, MemSpaceType::SHARED_OLD_SPACE);
430     array->SetExtraLength(0);
431     for (size_t i = 0; i < length; i++) {
432         array->Set(thread_, i, vec[start + i]);
433     }
434     return array;
435 }
436 
CopyAndReSortSLayoutInfo(const JSHandle<LayoutInfo> & old,int end,int capacity)437 JSHandle<LayoutInfo> ObjectFactory::CopyAndReSortSLayoutInfo(const JSHandle<LayoutInfo> &old, int end, int capacity)
438 {
439     ASSERT(capacity >= end);
440     JSHandle<LayoutInfo> newArr = CreateSLayoutInfo(capacity);
441     Span<struct Properties> sp(old->GetProperties(), end);
442     for (int i = 0; i < end; i++) {
443         newArr->AddKey(thread_, i, sp[i].key_, PropertyAttributes(sp[i].attr_));
444     }
445     return newArr;
446 }
447 
NewSDictionaryArray(uint32_t length)448 JSHandle<TaggedArray> ObjectFactory::NewSDictionaryArray(uint32_t length)
449 {
450     NewSObjectHook();
451     ASSERT(length > 0);
452     size_t size = TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), length);
453     auto header = sHeap_->AllocateOldOrHugeObject(
454         thread_, JSHClass::Cast(thread_->GlobalConstants()->GetDictionaryClass().GetTaggedObject()), size);
455     JSHandle<TaggedArray> array(thread_, header);
456     array->InitializeWithSpecialValue(JSTaggedValue::Undefined(), length);
457     return array;
458 }
459 
NewSEmptyProfileTypeInfoCell()460 JSHandle<ProfileTypeInfoCell> ObjectFactory::NewSEmptyProfileTypeInfoCell()
461 {
462     NewSObjectHook();
463     auto header = sHeap_->AllocateReadOnlyOrHugeObject(thread_,
464         JSHClass::Cast(thread_->GlobalConstants()->GetProfileTypeInfoCell0Class().GetTaggedObject()));
465     JSHandle<ProfileTypeInfoCell> profileTypeInfoCell(thread_, header);
466     profileTypeInfoCell->SetValue(thread_, JSTaggedValue::Undefined());
467     profileTypeInfoCell->SetMachineCode(thread_, JSTaggedValue::Hole());
468     profileTypeInfoCell->SetHandle(thread_, JSTaggedValue::Undefined());
469     profileTypeInfoCell->SetExtraInfoMap(thread_, JSTaggedValue::Undefined());
470     return profileTypeInfoCell;
471 }
472 
NewSFunctionTemplate(const JSHandle<Method> & method,const JSHandle<JSTaggedValue> & module,int32_t length)473 JSHandle<FunctionTemplate> ObjectFactory::NewSFunctionTemplate(
474     const JSHandle<Method> &method, const JSHandle<JSTaggedValue> &module, int32_t length)
475 {
476     NewSObjectHook();
477     auto globalConstants = thread_->GlobalConstants();
478     auto header = sHeap_->AllocateOldOrHugeObject(thread_,
479         JSHClass::Cast(globalConstants->GetFunctionTemplateClass().GetTaggedObject()));
480     JSHandle<FunctionTemplate> functionTemplate(thread_, header);
481     functionTemplate->SetMethod(thread_, method);
482     functionTemplate->SetModule(thread_, module);
483     functionTemplate->SetRawProfileTypeInfo(thread_, globalConstants->GetEmptyProfileTypeInfoCell(), SKIP_BARRIER);
484     functionTemplate->SetLength(length);
485     return functionTemplate;
486 }
487 
NewSEmptyArray()488 JSHandle<TaggedArray> ObjectFactory::NewSEmptyArray()
489 {
490     NewSObjectHook();
491     auto header = sHeap_->AllocateReadOnlyOrHugeObject(thread_,
492         JSHClass::Cast(thread_->GlobalConstants()->GetArrayClass().GetTaggedObject()), TaggedArray::SIZE);
493     JSHandle<TaggedArray> array(thread_, header);
494     array->SetLength(0);
495     array->SetExtraLength(0);
496     return array;
497 }
498 
NewSEmptyMutantArray()499 JSHandle<MutantTaggedArray> ObjectFactory::NewSEmptyMutantArray()
500 {
501     NewSObjectHook();
502     auto header = sHeap_->AllocateReadOnlyOrHugeObject(thread_,
503         JSHClass::Cast(thread_->GlobalConstants()->GetMutantTaggedArrayClass().GetTaggedObject()), TaggedArray::SIZE);
504     JSHandle<MutantTaggedArray> array(thread_, header);
505     array->SetLength(0);
506     array->SetExtraLength(0);
507     return array;
508 }
509 
NewSJSNativePointer(void * externalPointer,const NativePointerCallback & callBack,void * data,bool nonMovable,size_t nativeBindingsize,NativeFlag flag)510 JSHandle<JSNativePointer> ObjectFactory::NewSJSNativePointer(void *externalPointer,
511                                                              const NativePointerCallback &callBack,
512                                                              void *data,
513                                                              bool nonMovable,
514                                                              size_t nativeBindingsize,
515                                                              NativeFlag flag)
516 {
517     NewSObjectHook();
518     TaggedObject *header;
519     auto jsNativePointerClass =
520         JSHClass::Cast(thread_->GlobalConstants()->GetSJSNativePointerClass().GetTaggedObject());
521     jsNativePointerClass->SetIsJSShared(true);
522     if (nonMovable) {
523         header = sHeap_->AllocateNonMovableOrHugeObject(thread_, jsNativePointerClass);
524     } else {
525         header = sHeap_->AllocateOldOrHugeObject(thread_, jsNativePointerClass);
526     }
527     JSHandle<JSNativePointer> obj(thread_, header);
528     obj->SetExternalPointer(externalPointer);
529     obj->SetDeleter(callBack);
530     obj->SetData(data);
531     obj->SetBindingSize(nativeBindingsize);
532     obj->SetNativeFlag(flag);
533 
534     if (callBack != nullptr) {
535         sHeap_->IncNativeSizeAfterLastGC(nativeBindingsize);
536         vm_->PushToSharedNativePointerList(static_cast<JSNativePointer *>(header));
537         // In some cases, the size of JS/TS object is too small and the native binding size is too large.
538         // Check and try trigger concurrent mark here.
539         size_t nativeSizeAfterLastGC = sHeap_->GetNativeSizeAfterLastGC();
540         if (nativeSizeAfterLastGC > sHeap_->GetNativeSizeTriggerSharedGC()) {
541             sHeap_->CollectGarbage<TriggerGCType::SHARED_GC, GCReason::ALLOCATION_FAILED>(thread_);
542         } else if (sHeap_->CheckCanTriggerConcurrentMarking(thread_) &&
543             nativeSizeAfterLastGC > sHeap_->GetNativeSizeTriggerSharedCM()) {
544             sHeap_->TriggerConcurrentMarking<TriggerGCType::SHARED_GC, GCReason::ALLOCATION_LIMIT>(thread_);
545         }
546     }
547     return obj;
548 }
549 
NewSReadOnlyJSNativePointer(void * externalPointer)550 JSHandle<JSNativePointer> ObjectFactory::NewSReadOnlyJSNativePointer(void* externalPointer)
551 {
552     NewSObjectHook();
553     auto jsNativePointerClass =
554         JSHClass::Cast(thread_->GlobalConstants()->GetSJSNativePointerClass().GetTaggedObject());
555     jsNativePointerClass->SetIsJSShared(true);
556     TaggedObject* header = sHeap_->AllocateReadOnlyOrHugeObject(thread_, jsNativePointerClass);
557     JSHandle<JSNativePointer> obj(thread_, header);
558     obj->SetExternalPointer(externalPointer);
559     obj->SetDeleter(nullptr);
560     obj->SetData(nullptr);
561     obj->SetBindingSize(0);
562     obj->SetNativeFlag(NativeFlag::NO_DIV);
563     return obj;
564 }
565 
NewSInternalAccessor(void * setter,void * getter)566 JSHandle<AccessorData> ObjectFactory::NewSInternalAccessor(void *setter, void *getter)
567 {
568     NewSObjectHook();
569     TaggedObject *header = sHeap_->AllocateReadOnlyOrHugeObject(thread_,
570         JSHClass::Cast(thread_->GlobalConstants()->GetInternalAccessorClass().GetTaggedObject()));
571     JSHandle<InternalAccessor> obj(thread_, InternalAccessor::Cast(header));
572 
573     obj->SetSetter(reinterpret_cast<InternalAccessor::InternalSetFunc>(setter));
574     obj->SetGetter(reinterpret_cast<InternalAccessor::InternalGetFunc>(getter));
575     return JSHandle<AccessorData>::Cast(obj);
576 }
577 
NewSConstantPool(uint32_t capacity)578 JSHandle<ConstantPool> ObjectFactory::NewSConstantPool(uint32_t capacity)
579 {
580     NewSObjectHook();
581     size_t size = ConstantPool::ComputeSize(capacity);
582     auto header = sHeap_->AllocateOldOrHugeObject(
583         thread_, JSHClass::Cast(sHeap_->GetGlobalConst()->GetConstantPoolClass().GetTaggedObject()), size);
584     JSHandle<ConstantPool> array(thread_, header);
585     array->InitializeWithSpecialValue(thread_, JSTaggedValue::Hole(), capacity);
586     return array;
587 }
588 
NewSCOWTaggedArray(uint32_t length,JSTaggedValue initVal)589 JSHandle<COWTaggedArray> ObjectFactory::NewSCOWTaggedArray(uint32_t length, JSTaggedValue initVal)
590 {
591     NewSObjectHook();
592     ASSERT(length > 0);
593 
594     size_t size = TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), length);
595     auto header = sHeap_->AllocateNonMovableOrHugeObject(
596         thread_, JSHClass::Cast(sHeap_->GetGlobalConst()->GetCOWArrayClass().GetTaggedObject()), size);
597     JSHandle<COWTaggedArray> cowArray(thread_, header);
598     cowArray->InitializeWithSpecialValue(initVal, length);
599     return cowArray;
600 }
601 
NewSClassLiteral()602 JSHandle<ClassLiteral> ObjectFactory::NewSClassLiteral()
603 {
604     NewSObjectHook();
605     TaggedObject *header = sHeap_->AllocateOldOrHugeObject(
606         thread_, JSHClass::Cast(sHeap_->GetGlobalConst()->GetClassLiteralClass().GetTaggedObject()));
607     JSHandle<TaggedArray> emptyArray = EmptyArray();
608 
609     JSHandle<ClassLiteral> classLiteral(thread_, header);
610     classLiteral->SetArray(thread_, emptyArray);
611     classLiteral->SetIsAOTUsed(false);
612 
613     return classLiteral;
614 }
615 
NewSClassInfoExtractor(JSHandle<JSTaggedValue> method)616 JSHandle<ClassInfoExtractor> ObjectFactory::NewSClassInfoExtractor(
617     JSHandle<JSTaggedValue> method)
618 {
619     NewSObjectHook();
620     TaggedObject *header = sHeap_->AllocateOldOrHugeObject(
621         thread_, JSHClass::Cast(sHeap_->GetGlobalConst()->GetClassInfoExtractorHClass().GetTaggedObject()));
622     JSHandle<ClassInfoExtractor> obj(thread_, header);
623     obj->ClearBitField();
624     obj->SetConstructorMethod(thread_, method.GetTaggedValue());
625     JSHandle<TaggedArray> emptyArray = EmptyArray();
626     obj->SetNonStaticKeys(thread_, emptyArray, SKIP_BARRIER);
627     obj->SetNonStaticProperties(thread_, emptyArray, SKIP_BARRIER);
628     obj->SetNonStaticElements(thread_, emptyArray, SKIP_BARRIER);
629     obj->SetStaticKeys(thread_, emptyArray, SKIP_BARRIER);
630     obj->SetStaticProperties(thread_, emptyArray, SKIP_BARRIER);
631     obj->SetStaticElements(thread_, emptyArray, SKIP_BARRIER);
632     return obj;
633 }
634 
NewSOldSpaceTaggedArray(uint32_t length,JSTaggedValue initVal)635 JSHandle<TaggedArray> ObjectFactory::NewSOldSpaceTaggedArray(uint32_t length, JSTaggedValue initVal)
636 {
637     return NewSTaggedArray(length, initVal, MemSpaceType::SHARED_OLD_SPACE);
638 }
639 
NewSTaggedArray(uint32_t length,JSTaggedValue initVal,MemSpaceType spaceType)640 JSHandle<TaggedArray> ObjectFactory::NewSTaggedArray(uint32_t length, JSTaggedValue initVal, MemSpaceType spaceType)
641 {
642     NewSObjectHook();
643     if (length == 0) {
644         return EmptyArray();
645     }
646 
647     size_t size = TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), length);
648     TaggedObject *header = nullptr;
649     JSHClass *arrayClass = JSHClass::Cast(thread_->GlobalConstants()->GetArrayClass().GetTaggedObject());
650     switch (spaceType) {
651         case MemSpaceType::SHARED_OLD_SPACE:
652             header = sHeap_->AllocateOldOrHugeObject(thread_, arrayClass, size);
653             break;
654         case MemSpaceType::SHARED_NON_MOVABLE:
655             header = sHeap_->AllocateNonMovableOrHugeObject(thread_, arrayClass, size);
656             break;
657         default:
658             LOG_ECMA(FATAL) << "this branch is unreachable";
659             UNREACHABLE();
660     }
661 
662     JSHandle<TaggedArray> array(thread_, header);
663     array->InitializeWithSpecialValue(initVal, length);
664     return array;
665 }
666 
NewSWellKnownSymbol(const JSHandle<JSTaggedValue> & name)667 JSHandle<JSSymbol> ObjectFactory::NewSWellKnownSymbol(const JSHandle<JSTaggedValue> &name)
668 {
669     NewSObjectHook();
670     TaggedObject *header = sHeap_->AllocateNonMovableOrHugeObject(
671         thread_, JSHClass::Cast(thread_->GlobalConstants()->GetSymbolClass().GetTaggedObject()));
672     JSHandle<JSSymbol> obj(thread_, JSSymbol::Cast(header));
673     obj->SetFlags(0);
674     obj->SetWellKnownSymbol();
675     obj->SetDescription(thread_, name);
676     obj->SetHashField(SymbolTable::Hash(name.GetTaggedValue()));
677     return obj;
678 }
679 
NewSPublicSymbol(const JSHandle<JSTaggedValue> & name)680 JSHandle<JSSymbol> ObjectFactory::NewSPublicSymbol(const JSHandle<JSTaggedValue> &name)
681 {
682     NewObjectHook();
683     TaggedObject *header = sHeap_->AllocateNonMovableOrHugeObject(
684         thread_, JSHClass::Cast(thread_->GlobalConstants()->GetSymbolClass().GetTaggedObject()));
685     JSHandle<JSSymbol> obj(thread_, JSSymbol::Cast(header));
686     obj->SetFlags(0);
687     obj->SetDescription(thread_, name);
688     obj->SetHashField(SymbolTable::Hash(name.GetTaggedValue()));
689     return obj;
690 }
691 
NewSEmptySymbol()692 JSHandle<JSSymbol> ObjectFactory::NewSEmptySymbol()
693 {
694     NewObjectHook();
695     TaggedObject *header = sHeap_->AllocateNonMovableOrHugeObject(
696         thread_, JSHClass::Cast(thread_->GlobalConstants()->GetSymbolClass().GetTaggedObject()));
697     JSHandle<JSSymbol> obj(thread_, JSSymbol::Cast(header));
698     obj->SetDescription(thread_, JSTaggedValue::Undefined());
699     obj->SetFlags(0);
700     obj->SetHashField(0);
701     return obj;
702 }
703 
NewSWellKnownSymbolWithChar(std::string_view description)704 JSHandle<JSSymbol> ObjectFactory::NewSWellKnownSymbolWithChar(std::string_view description)
705 {
706     JSHandle<EcmaString> string = NewFromUtf8(description);
707     return NewSWellKnownSymbol(JSHandle<JSTaggedValue>(string));
708 }
709 
NewSPublicSymbolWithChar(std::string_view description)710 JSHandle<JSSymbol> ObjectFactory::NewSPublicSymbolWithChar(std::string_view description)
711 {
712     JSHandle<EcmaString> string = NewFromUtf8(description);
713     return NewSPublicSymbol(JSHandle<JSTaggedValue>(string));
714 }
715 
NewSSourceTextModule()716 JSHandle<SourceTextModule> ObjectFactory::NewSSourceTextModule()
717 {
718     NewObjectHook();
719     TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
720         JSHClass::Cast(thread_->GlobalConstants()->GetSourceTextModuleClass().GetTaggedObject()));
721     JSHandle<SourceTextModule> obj(thread_, header);
722     JSTaggedValue undefinedValue = thread_->GlobalConstants()->GetUndefined();
723     obj->SetEnvironment(thread_, undefinedValue);
724     obj->SetNamespace(thread_, undefinedValue);
725     obj->SetRequestedModules(thread_, undefinedValue);
726     obj->SetImportEntries(thread_, undefinedValue);
727     obj->SetLocalExportEntries(thread_, undefinedValue);
728     obj->SetIndirectExportEntries(thread_, undefinedValue);
729     obj->SetStarExportEntries(thread_, undefinedValue);
730     obj->SetNameDictionary(thread_, undefinedValue);
731     // [[CycleRoot]]: For a module not in a cycle, this would be the module itself.
732     obj->SetCycleRoot(thread_, obj);
733     obj->SetTopLevelCapability(thread_, undefinedValue);
734     obj->SetAsyncParentModules(thread_, undefinedValue);
735     obj->SetHasTLA(false);
736     obj->SetAsyncEvaluatingOrdinal(SourceTextModule::NOT_ASYNC_EVALUATED);
737     obj->SetPendingAsyncDependencies(SourceTextModule::UNDEFINED_INDEX);
738     obj->SetDFSIndex(SourceTextModule::UNDEFINED_INDEX);
739     obj->SetDFSAncestorIndex(SourceTextModule::UNDEFINED_INDEX);
740     obj->SetEvaluationError(SourceTextModule::UNDEFINED_INDEX);
741     obj->SetStatus(ModuleStatus::UNINSTANTIATED);
742     obj->SetTypes(ModuleTypes::UNKNOWN);
743     obj->SetIsNewBcVersion(false);
744     obj->SetRegisterCounts(UINT16_MAX);
745     obj->SetLazyImportStatus(ToUintPtr(nullptr));
746     obj->SetEcmaModuleFilename(ToUintPtr(nullptr));
747     obj->SetEcmaModuleRecordName(ToUintPtr(nullptr));
748     obj->SetSharedType(SharedTypes::UNSENDABLE_MODULE);
749     obj->SetSendableEnv(thread_, undefinedValue);
750     return obj;
751 }
752 
NewSModuleNamespace()753 JSHandle<ModuleNamespace> ObjectFactory::NewSModuleNamespace()
754 {
755     NewObjectHook();
756     JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
757     JSHandle<JSHClass> hclass = JSHandle<JSHClass>::Cast(env->GetSharedModuleNamespaceClass());
758     JSHandle<JSObject> obj = NewSharedOldSpaceJSObject(hclass);
759 
760     JSHandle<ModuleNamespace> moduleNamespace = JSHandle<ModuleNamespace>::Cast(obj);
761     moduleNamespace->SetModule(thread_, JSTaggedValue::Undefined());
762     moduleNamespace->SetExports(thread_, JSTaggedValue::Undefined());
763     moduleNamespace->SetDeregisterProcession(thread_, JSTaggedValue::Undefined());
764     return moduleNamespace;
765 }
766 
NewSImportEntry(const JSHandle<JSTaggedValue> & moduleRequest,const JSHandle<JSTaggedValue> & importName,const JSHandle<JSTaggedValue> & localName)767 JSHandle<ImportEntry> ObjectFactory::NewSImportEntry(const JSHandle<JSTaggedValue> &moduleRequest,
768                                                      const JSHandle<JSTaggedValue> &importName,
769                                                      const JSHandle<JSTaggedValue> &localName)
770 {
771     NewObjectHook();
772     TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
773         JSHClass::Cast(thread_->GlobalConstants()->GetImportEntryClass().GetTaggedObject()));
774     JSHandle<ImportEntry> obj(thread_, header);
775     obj->SetModuleRequest(thread_, moduleRequest);
776     obj->SetImportName(thread_, importName);
777     obj->SetLocalName(thread_, localName);
778     return obj;
779 }
780 
NewSLocalExportEntry(const JSHandle<JSTaggedValue> & exportName,const JSHandle<JSTaggedValue> & localName,const uint32_t index)781 JSHandle<LocalExportEntry> ObjectFactory::NewSLocalExportEntry(const JSHandle<JSTaggedValue> &exportName,
782     const JSHandle<JSTaggedValue> &localName, const uint32_t index)
783 {
784     NewObjectHook();
785     TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
786         JSHClass::Cast(thread_->GlobalConstants()->GetLocalExportEntryClass().GetTaggedObject()));
787     JSHandle<LocalExportEntry> obj(thread_, header);
788     obj->SetExportName(thread_, exportName);
789     obj->SetLocalName(thread_, localName);
790     obj->SetLocalIndex(index);
791     return obj;
792 }
793 
NewSIndirectExportEntry(const JSHandle<JSTaggedValue> & exportName,const JSHandle<JSTaggedValue> & moduleRequest,const JSHandle<JSTaggedValue> & importName)794 JSHandle<IndirectExportEntry> ObjectFactory::NewSIndirectExportEntry(const JSHandle<JSTaggedValue> &exportName,
795                                                                      const JSHandle<JSTaggedValue> &moduleRequest,
796                                                                      const JSHandle<JSTaggedValue> &importName)
797 {
798     NewObjectHook();
799     TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
800         JSHClass::Cast(thread_->GlobalConstants()->GetIndirectExportEntryClass().GetTaggedObject()));
801     JSHandle<IndirectExportEntry> obj(thread_, header);
802     obj->SetExportName(thread_, exportName);
803     obj->SetModuleRequest(thread_, moduleRequest);
804     obj->SetImportName(thread_, importName);
805     return obj;
806 }
807 
NewSStarExportEntry(const JSHandle<JSTaggedValue> & moduleRequest)808 JSHandle<StarExportEntry> ObjectFactory::NewSStarExportEntry(const JSHandle<JSTaggedValue> &moduleRequest)
809 {
810     NewObjectHook();
811     TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
812         JSHClass::Cast(thread_->GlobalConstants()->GetStarExportEntryClass().GetTaggedObject()));
813     JSHandle<StarExportEntry> obj(thread_, header);
814     obj->SetModuleRequest(thread_, moduleRequest);
815     return obj;
816 }
817 
NewSResolvedIndexBindingRecord()818 JSHandle<ResolvedIndexBinding> ObjectFactory::NewSResolvedIndexBindingRecord()
819 {
820     JSHandle<JSTaggedValue> undefinedValue = thread_->GlobalConstants()->GetHandledUndefined();
821     JSHandle<SourceTextModule> ecmaModule(undefinedValue);
822     int32_t index = 0;
823     return NewSResolvedIndexBindingRecord(ecmaModule, index);
824 }
825 
NewSResolvedIndexBindingRecord(const JSHandle<SourceTextModule> & module,int32_t index)826 JSHandle<ResolvedIndexBinding> ObjectFactory::NewSResolvedIndexBindingRecord(const JSHandle<SourceTextModule> &module,
827                                                                              int32_t index)
828 {
829     NewObjectHook();
830     TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
831         JSHClass::Cast(thread_->GlobalConstants()->GetResolvedIndexBindingClass().GetTaggedObject()));
832     JSHandle<ResolvedIndexBinding> obj(thread_, header);
833     obj->SetModule(thread_, module);
834     obj->SetIndex(index);
835     return obj;
836 }
837 
NewSResolvedBindingRecord()838 JSHandle<ResolvedBinding> ObjectFactory::NewSResolvedBindingRecord()
839 {
840     JSHandle<JSTaggedValue> undefinedValue = thread_->GlobalConstants()->GetHandledUndefined();
841     JSHandle<SourceTextModule> ecmaModule(undefinedValue);
842     JSHandle<JSTaggedValue> bindingName(undefinedValue);
843     return NewSResolvedBindingRecord(ecmaModule, bindingName);
844 }
845 
NewSResolvedBindingRecord(const JSHandle<SourceTextModule> & module,const JSHandle<JSTaggedValue> & bindingName)846 JSHandle<ResolvedBinding> ObjectFactory::NewSResolvedBindingRecord(const JSHandle<SourceTextModule> &module,
847                                                                    const JSHandle<JSTaggedValue> &bindingName)
848 {
849     NewObjectHook();
850     TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
851         JSHClass::Cast(thread_->GlobalConstants()->GetResolvedBindingClass().GetTaggedObject()));
852     JSHandle<ResolvedBinding> obj(thread_, header);
853     obj->SetModule(thread_, module);
854     obj->SetBindingName(thread_, bindingName);
855     return obj;
856 }
857 
NewSResolvedRecordIndexBindingRecord()858 JSHandle<ResolvedRecordIndexBinding> ObjectFactory::NewSResolvedRecordIndexBindingRecord()
859 {
860     JSHandle<JSTaggedValue> undefinedValue = thread_->GlobalConstants()->GetHandledUndefined();
861     JSHandle<EcmaString> ecmaModule(undefinedValue);
862     JSHandle<EcmaString> fileName(undefinedValue);
863     int32_t index = 0;
864     return NewSResolvedRecordIndexBindingRecord(ecmaModule, fileName, index);
865 }
866 
NewSResolvedRecordIndexBindingRecord(const JSHandle<EcmaString> & moduleRecord,const JSHandle<EcmaString> & abcFileName,int32_t index)867 JSHandle<ResolvedRecordIndexBinding> ObjectFactory::NewSResolvedRecordIndexBindingRecord(
868     const JSHandle<EcmaString> &moduleRecord, const JSHandle<EcmaString> &abcFileName, int32_t index)
869 {
870     NewObjectHook();
871     TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
872         JSHClass::Cast(thread_->GlobalConstants()->GetResolvedRecordIndexBindingClass().GetTaggedObject()));
873     JSHandle<ResolvedRecordIndexBinding> obj(thread_, header);
874     obj->SetModuleRecord(thread_, moduleRecord);
875     obj->SetAbcFileName(thread_, abcFileName);
876     obj->SetIndex(index);
877     return obj;
878 }
879 
NewSResolvedRecordBindingRecord()880 JSHandle<ResolvedRecordBinding> ObjectFactory::NewSResolvedRecordBindingRecord()
881 {
882     JSHandle<JSTaggedValue> undefinedValue = thread_->GlobalConstants()->GetHandledUndefined();
883     JSHandle<EcmaString> ecmaModule(undefinedValue);
884     JSHandle<JSTaggedValue> bindingName(undefinedValue);
885     return NewSResolvedRecordBindingRecord(ecmaModule, bindingName);
886 }
887 
NewSResolvedRecordBindingRecord(const JSHandle<EcmaString> & moduleRecord,const JSHandle<JSTaggedValue> & bindingName)888 JSHandle<ResolvedRecordBinding> ObjectFactory::NewSResolvedRecordBindingRecord(
889     const JSHandle<EcmaString> &moduleRecord, const JSHandle<JSTaggedValue> &bindingName)
890 {
891     NewObjectHook();
892     TaggedObject *header = sHeap_->AllocateOldOrHugeObject(thread_,
893         JSHClass::Cast(thread_->GlobalConstants()->GetResolvedRecordBindingClass().GetTaggedObject()));
894     JSHandle<ResolvedRecordBinding> obj(thread_, header);
895     obj->SetModuleRecord(thread_, moduleRecord);
896     obj->SetBindingName(thread_, bindingName);
897     return obj;
898 }
899 
NewSAOTLiteralInfo(uint32_t length,JSTaggedValue initVal)900 JSHandle<AOTLiteralInfo> ObjectFactory::NewSAOTLiteralInfo(uint32_t length, JSTaggedValue initVal)
901 {
902     NewObjectHook();
903     size_t size = AOTLiteralInfo::ComputeSize(length);
904     auto header = sHeap_->AllocateOldOrHugeObject(thread_,
905         JSHClass::Cast(sHeap_->GetGlobalConst()->GetAOTLiteralInfoClass().GetTaggedObject()), size);
906 
907     JSHandle<AOTLiteralInfo> aotLiteralInfo(thread_, header);
908     aotLiteralInfo->InitializeWithSpecialValue(initVal, length);
909     return aotLiteralInfo;
910 }
911 
NewSendableEnv(int numSlots)912 JSHandle<SendableEnv> ObjectFactory::NewSendableEnv(int numSlots)
913 {
914     NewObjectHook();
915     size_t size = SendableEnv::ComputeSize(numSlots);
916     auto header = sHeap_->AllocateOldOrHugeObject(thread_,
917         JSHClass::Cast(sHeap_->GetGlobalConst()->GetSendableEnvClass().GetTaggedObject()), size);
918     JSHandle<SendableEnv> array(thread_, header);
919     array->InitializeWithSpecialValue(JSTaggedValue::Hole(), numSlots + SendableEnv::SENDABLE_RESERVED_ENV_LENGTH);
920     return array;
921 }
922 
NewJSSendableFunction(const JSHandle<Method> & methodHandle)923 JSHandle<JSFunction> ObjectFactory::NewJSSendableFunction(const JSHandle<Method> &methodHandle)
924 {
925     JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
926     FunctionKind kind = methodHandle->GetFunctionKind();
927     JSHandle<JSHClass> hclass;
928     switch (kind) {
929         case FunctionKind::ASYNC_FUNCTION: {
930             hclass = JSHandle<JSHClass>::Cast(env->GetSAsyncFunctionClass());
931             break;
932         }
933         case FunctionKind::BASE_CONSTRUCTOR: {
934             hclass = JSHandle<JSHClass>::Cast(env->GetSFunctionClassWithProto());
935             break;
936         }
937         default:
938             LOG_ECMA(FATAL) << "this branch is unreachable";
939             UNREACHABLE();
940     }
941 
942     JSHandle<JSFunction> func = NewSFunctionByHClass(methodHandle, hclass);
943     ASSERT_NO_ABRUPT_COMPLETION(thread_);
944     return func;
945 }
946 }  // namespace panda::ecmascript
947