1/*
2 * Copyright (c) 2021-2022 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/global_env_constants.h"
17
18#include "ecmascript/ecma_string-inl.h"
19#include "ecmascript/free_object.h"
20#include "ecmascript/ic/ic_handler.h"
21#include "ecmascript/ic/proto_change_details.h"
22#include "ecmascript/jobs/pending_job.h"
23#include "ecmascript/js_api/js_api_arraylist_iterator.h"
24#include "ecmascript/js_api/js_api_bitvector_iterator.h"
25#include "ecmascript/js_api/js_api_deque_iterator.h"
26#include "ecmascript/js_api/js_api_lightweightmap_iterator.h"
27#include "ecmascript/js_api/js_api_lightweightset_iterator.h"
28#include "ecmascript/js_api/js_api_linked_list_iterator.h"
29#include "ecmascript/js_api/js_api_list_iterator.h"
30#include "ecmascript/js_api/js_api_plain_array_iterator.h"
31#include "ecmascript/js_api/js_api_queue_iterator.h"
32#include "ecmascript/js_api/js_api_stack_iterator.h"
33#include "ecmascript/js_api/js_api_hashmap_iterator.h"
34#include "ecmascript/js_api/js_api_hashset_iterator.h"
35#include "ecmascript/js_api/js_api_tree_map_iterator.h"
36#include "ecmascript/js_api/js_api_tree_set_iterator.h"
37#include "ecmascript/js_api/js_api_vector_iterator.h"
38#include "ecmascript/js_async_generator_object.h"
39#include "ecmascript/jspandafile/program_object.h"
40#include "ecmascript/js_array_iterator.h"
41#include "ecmascript/js_finalization_registry.h"
42#include "ecmascript/js_map_iterator.h"
43#include "ecmascript/js_promise.h"
44#include "ecmascript/js_realm.h"
45#include "ecmascript/js_regexp_iterator.h"
46#include "ecmascript/js_set_iterator.h"
47#include "ecmascript/marker_cell.h"
48#include "ecmascript/runtime.h"
49#include "ecmascript/shared_objects/js_shared_array.h"
50#include "ecmascript/shared_objects/js_shared_array_iterator.h"
51#include "ecmascript/shared_objects/js_shared_map_iterator.h"
52#include "ecmascript/shared_objects/js_shared_set_iterator.h"
53#include "ecmascript/vtable.h"
54
55namespace panda::ecmascript {
56void GlobalEnvConstants::Init(JSThread *thread)
57{
58    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
59    if (Runtime::GetInstance()->SharedConstInited()) {
60        auto *globalConst = Runtime::GetInstance()->GetGlobalEnvConstants();
61        // 1. Copy shareds.
62        for (size_t i = static_cast<size_t>(ConstantIndex::SHARED_BEGIN);
63            i <= static_cast<size_t>(ConstantIndex::SHARED_END); i++) {
64            constants_[i] = globalConst->constants_[i];
65        }
66    } else {
67        InitSharedRootsClasses(factory);
68        InitSharedMiscellanious(thread, factory);
69        InitSharedStrings(factory);
70    }
71    // 2. Init non-shareds.
72    InitMiscellanious(thread, factory);
73    InitRootsClasses(factory);
74}
75
76void GlobalEnvConstants::CopySharedConstantsFrom(const GlobalEnvConstants *src)
77{
78    for (size_t i = 0; i < static_cast<size_t>(ConstantIndex::SHARED_BEGIN); i++) {
79        constants_[i] = JSTaggedValue::Hole();
80    }
81    for (size_t i = static_cast<size_t>(ConstantIndex::SHARED_BEGIN);
82        i <= static_cast<size_t>(ConstantIndex::SHARED_END); i++) {
83        constants_[i] = src->constants_[i];
84    }
85    for (size_t i = static_cast<size_t>(ConstantIndex::SHARED_END) + 1;
86        i < static_cast<size_t>(ConstantIndex::CONSTANT_COUNT); i++) {
87        constants_[i] = JSTaggedValue::Hole();
88    }
89}
90
91void GlobalEnvConstants::InitSharedStrings(ObjectFactory *factory)
92{
93    #define INIT_GLOBAL_ENV_CONSTANT_STRING(Name, Index, Token) \
94        SetConstant(ConstantIndex::Index, factory->NewFromASCIIReadOnly(Token));
95        SHARED_GLOBAL_ENV_CONSTANT_STRING(INIT_GLOBAL_ENV_CONSTANT_STRING)
96    #undef INIT_GLOBAL_ENV_CONSTANT_STRING
97}
98
99void GlobalEnvConstants::InitSharedRootsClasses(ObjectFactory *factory)
100{
101    // Global constants are readonly.
102    JSHClass *hClass = *factory->InitSClassClass();
103
104    SetConstant(ConstantIndex::HCLASS_CLASS_INDEX, JSTaggedValue(hClass));
105    // To reverse the order, the hclass of string needs to load default supers
106    SetConstant(ConstantIndex::ARRAY_CLASS_INDEX,
107        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::TAGGED_ARRAY));
108    SetConstant(ConstantIndex::FREE_OBJECT_WITH_NONE_FIELD_CLASS_INDEX,
109                factory->NewSEcmaReadOnlyHClass(hClass, FreeObject::NEXT_OFFSET, JSType::FREE_OBJECT_WITH_NONE_FIELD));
110    SetConstant(ConstantIndex::FREE_OBJECT_WITH_ONE_FIELD_CLASS_INDEX,
111        factory->NewSEcmaReadOnlyHClass(hClass, FreeObject::SIZE_OFFSET, JSType::FREE_OBJECT_WITH_ONE_FIELD));
112    SetConstant(ConstantIndex::FREE_OBJECT_WITH_TWO_FIELD_CLASS_INDEX,
113        factory->NewSEcmaReadOnlyHClass(hClass, FreeObject::SIZE, JSType::FREE_OBJECT_WITH_TWO_FIELD));
114    SetConstant(ConstantIndex::LINE_STRING_CLASS_INDEX,
115        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::LINE_STRING));
116    SetConstant(ConstantIndex::SLICED_STRING_CLASS_INDEX,
117        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::SLICED_STRING));
118    SetConstant(ConstantIndex::CONSTANT_STRING_CLASS_INDEX,
119        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::CONSTANT_STRING));
120    SetConstant(ConstantIndex::TREE_STRING_CLASS_INDEX,
121        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::TREE_STRING));
122    SetConstant(ConstantIndex::BYTE_ARRAY_CLASS_INDEX,
123        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::BYTE_ARRAY));
124    SetConstant(ConstantIndex::CONSTANT_POOL_CLASS_INDEX,
125        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::CONSTANT_POOL));
126    SetConstant(ConstantIndex::PROFILE_TYPE_INFO_CLASS_INDEX,
127        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::PROFILE_TYPE_INFO));
128    SetConstant(ConstantIndex::AOT_LITERAL_INFO_CLASS_INDEX,
129        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::AOT_LITERAL_INFO));
130    SetConstant(ConstantIndex::EXTRA_PROFILE_TYPE_INFO_CLASS_INDEX,
131        factory->NewSEcmaReadOnlyHClass(hClass, ExtraProfileTypeInfo::SIZE, JSType::EXTRA_PROFILE_TYPE_INFO));
132    SetConstant(ConstantIndex::PROFILE_TYPE_INFO_CELL_0_CLASS_INDEX,
133        factory->NewSEcmaReadOnlyHClass(hClass, ProfileTypeInfoCell::SIZE, JSType::PROFILE_TYPE_INFO_CELL_0));
134    SetConstant(ConstantIndex::PROFILE_TYPE_INFO_CELL_1_CLASS_INDEX,
135        factory->NewSEcmaReadOnlyHClass(hClass, ProfileTypeInfoCell::SIZE, JSType::PROFILE_TYPE_INFO_CELL_1));
136    SetConstant(ConstantIndex::PROFILE_TYPE_INFO_CELL_N_CLASS_INDEX,
137        factory->NewSEcmaReadOnlyHClass(hClass, ProfileTypeInfoCell::SIZE, JSType::PROFILE_TYPE_INFO_CELL_N));
138    SetConstant(ConstantIndex::FUNCTION_TEMPLATE_CLASS_INDEX,
139        factory->NewSEcmaReadOnlyHClass(hClass, FunctionTemplate::SIZE, JSType::FUNCTION_TEMPLATE));
140    SetConstant(ConstantIndex::VTABLE_CLASS_INDEX,
141        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::VTABLE));
142    SetConstant(ConstantIndex::COW_MUTANT_TAGGED_ARRAY_CLASS_INDEX,
143        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::COW_MUTANT_TAGGED_ARRAY));
144    SetConstant(ConstantIndex::MUTANT_TAGGED_ARRAY_CLASS_INDEX,
145        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::MUTANT_TAGGED_ARRAY));
146    SetConstant(ConstantIndex::DICTIONARY_CLASS_INDEX,
147        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::TAGGED_DICTIONARY));
148    SetConstant(ConstantIndex::COW_ARRAY_CLASS_INDEX,
149        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::COW_TAGGED_ARRAY));
150    SetConstant(ConstantIndex::BIGINT_CLASS_INDEX,
151        factory->NewSEcmaReadOnlyHClass(hClass, BigInt::SIZE, JSType::BIGINT));
152    SetConstant(ConstantIndex::SENDABLE_JS_NATIVE_POINTER_CLASS_INDEX,
153        factory->NewSEcmaReadOnlyHClass(hClass, JSNativePointer::SIZE, JSType::JS_NATIVE_POINTER));
154    SetConstant(ConstantIndex::ENV_CLASS_INDEX,
155        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::LEXICAL_ENV));
156    SetConstant(ConstantIndex::SYMBOL_CLASS_INDEX,
157        factory->NewSEcmaReadOnlyHClass(hClass, JSSymbol::SIZE, JSType::SYMBOL));
158    SetConstant(ConstantIndex::ACCESSOR_DATA_CLASS_INDEX,
159        factory->NewSEcmaReadOnlyHClass(hClass, AccessorData::SIZE, JSType::ACCESSOR_DATA));
160    SetConstant(ConstantIndex::INTERNAL_ACCESSOR_CLASS_INDEX,
161        factory->NewSEcmaReadOnlyHClass(hClass, AccessorData::SIZE, JSType::INTERNAL_ACCESSOR));
162    SetConstant(ConstantIndex::COMPLETION_RECORD_CLASS_INDEX,
163        factory->NewSEcmaReadOnlyHClass(hClass, CompletionRecord::SIZE, JSType::COMPLETION_RECORD));
164    SetConstant(ConstantIndex::GENERATOR_CONTEST_INDEX,
165        factory->NewSEcmaReadOnlyHClass(hClass, GeneratorContext::SIZE, JSType::JS_GENERATOR_CONTEXT));
166    SetConstant(ConstantIndex::ASYNC_GENERATOR_REQUEST_CLASS_INDEX,
167        factory->NewSEcmaReadOnlyHClass(hClass, AsyncGeneratorRequest::SIZE,
168                                        JSType::ASYNC_GENERATOR_REQUEST));
169    SetConstant(ConstantIndex::ASYNC_ITERATOR_RECORD_CLASS_INDEX,
170        factory->NewSEcmaReadOnlyHClass(hClass, AsyncIteratorRecord::SIZE, JSType::ASYNC_ITERATOR_RECORD));
171    SetConstant(ConstantIndex::CAPABILITY_RECORD_CLASS_INDEX,
172        factory->NewSEcmaReadOnlyHClass(hClass, PromiseCapability::SIZE, JSType::PROMISE_CAPABILITY));
173    SetConstant(ConstantIndex::REACTIONS_RECORD_CLASS_INDEX,
174        factory->NewSEcmaReadOnlyHClass(hClass, PromiseReaction::SIZE, JSType::PROMISE_REACTIONS));
175    SetConstant(ConstantIndex::PROMISE_ITERATOR_RECORD_CLASS_INDEX,
176        factory->NewSEcmaReadOnlyHClass(hClass, PromiseIteratorRecord::SIZE,
177                                        JSType::PROMISE_ITERATOR_RECORD));
178    SetConstant(ConstantIndex::PROMISE_RECORD_CLASS_INDEX,
179        factory->NewSEcmaReadOnlyHClass(hClass, PromiseRecord::SIZE, JSType::PROMISE_RECORD));
180    SetConstant(ConstantIndex::PROMISE_RESOLVING_FUNCTIONS_CLASS_INDEX,
181        factory->NewSEcmaReadOnlyHClass(hClass, ResolvingFunctionsRecord::SIZE,
182                                        JSType::RESOLVING_FUNCTIONS_RECORD));
183    SetConstant(ConstantIndex::MICRO_JOB_QUEUE_CLASS_INDEX,
184        factory->NewSEcmaReadOnlyHClass(hClass, job::MicroJobQueue::SIZE, JSType::MICRO_JOB_QUEUE));
185    SetConstant(ConstantIndex::PENDING_JOB_CLASS_INDEX,
186        factory->NewSEcmaReadOnlyHClass(hClass, job::PendingJob::SIZE, JSType::PENDING_JOB));
187    SetConstant(ConstantIndex::PROTO_CHANGE_MARKER_CLASS_INDEX,
188        factory->NewSEcmaReadOnlyHClass(hClass, ProtoChangeMarker::SIZE, JSType::PROTO_CHANGE_MARKER));
189    SetConstant(ConstantIndex::PROTO_CHANGE_DETAILS_CLASS_INDEX,
190        factory->NewSEcmaReadOnlyHClass(hClass, ProtoChangeDetails::SIZE, JSType::PROTOTYPE_INFO));
191    SetConstant(ConstantIndex::MARKER_CELL_CLASS_INDEX,
192        factory->NewSEcmaReadOnlyHClass(hClass, MarkerCell::SIZE, JSType::MARKER_CELL));
193    SetConstant(ConstantIndex::TRACK_INFO_CLASS_INDEX,
194        factory->NewSEcmaReadOnlyHClass(hClass, TrackInfo::SIZE, JSType::TRACK_INFO));
195    SetConstant(ConstantIndex::PROTOTYPE_HANDLER_CLASS_INDEX,
196        factory->NewSEcmaReadOnlyHClass(hClass, PrototypeHandler::SIZE, JSType::PROTOTYPE_HANDLER));
197    SetConstant(ConstantIndex::TRANSITION_HANDLER_CLASS_INDEX,
198        factory->NewSEcmaReadOnlyHClass(hClass, TransitionHandler::SIZE, JSType::TRANSITION_HANDLER));
199    SetConstant(ConstantIndex::TRANS_WITH_PROTO_HANDLER_CLASS_INDEX,
200        factory->NewSEcmaReadOnlyHClass(hClass, TransWithProtoHandler::SIZE, JSType::TRANS_WITH_PROTO_HANDLER));
201    SetConstant(ConstantIndex::STORE_TS_HANDLER_CLASS_INDEX,
202        factory->NewSEcmaReadOnlyHClass(hClass, StoreTSHandler::SIZE, JSType::STORE_TS_HANDLER));
203    SetConstant(ConstantIndex::PROPERTY_BOX_CLASS_INDEX,
204        factory->NewSEcmaReadOnlyHClass(hClass, PropertyBox::SIZE, JSType::PROPERTY_BOX));
205    SetConstant(ConstantIndex::PROGRAM_CLASS_INDEX,
206        factory->NewSEcmaReadOnlyHClass(hClass, Program::SIZE, JSType::PROGRAM));
207    SetConstant(ConstantIndex::IMPORT_ENTRY_CLASS_INDEX,
208        factory->NewSEcmaReadOnlyHClass(hClass, ImportEntry::SIZE, JSType::IMPORTENTRY_RECORD));
209    SetConstant(ConstantIndex::LOCAL_EXPORT_ENTRY_CLASS_INDEX,
210        factory->NewSEcmaReadOnlyHClass(hClass, LocalExportEntry::SIZE, JSType::LOCAL_EXPORTENTRY_RECORD));
211    SetConstant(ConstantIndex::INDIRECT_EXPORT_ENTRY_CLASS_INDEX,
212        factory->NewSEcmaReadOnlyHClass(hClass, IndirectExportEntry::SIZE,
213                                        JSType::INDIRECT_EXPORTENTRY_RECORD));
214    SetConstant(ConstantIndex::STAR_EXPORT_ENTRY_CLASS_INDEX,
215        factory->NewSEcmaReadOnlyHClass(hClass, StarExportEntry::SIZE, JSType::STAR_EXPORTENTRY_RECORD));
216    SetConstant(ConstantIndex::SOURCE_TEXT_MODULE_CLASS_INDEX,
217        factory->NewSEcmaReadOnlyHClass(hClass, SourceTextModule::SIZE, JSType::SOURCE_TEXT_MODULE_RECORD));
218    SetConstant(ConstantIndex::RESOLVED_BINDING_CLASS_INDEX,
219        factory->NewSEcmaReadOnlyHClass(hClass, ResolvedBinding::SIZE, JSType::RESOLVEDBINDING_RECORD));
220    SetConstant(ConstantIndex::RESOLVED_INDEX_BINDING_CLASS_INDEX,
221        factory->NewSEcmaReadOnlyHClass(hClass, ResolvedIndexBinding::SIZE, JSType::RESOLVEDINDEXBINDING_RECORD));
222    SetConstant(ConstantIndex::MACHINE_CODE_CLASS_INDEX,
223        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::MACHINE_CODE_OBJECT));
224    SetConstant(ConstantIndex::CLASS_INFO_EXTRACTOR_HCLASS_INDEX,
225        factory->NewSEcmaReadOnlyHClass(hClass, ClassInfoExtractor::SIZE,
226                                        JSType::CLASS_INFO_EXTRACTOR));
227    SetConstant(ConstantIndex::CELL_RECORD_CLASS_INDEX,
228        factory->NewSEcmaReadOnlyHClass(hClass, CellRecord::SIZE, JSType::CELL_RECORD));
229    SetConstant(ConstantIndex::METHOD_CLASS_INDEX,
230        factory->NewSEcmaReadOnlyHClass(hClass, Method::SIZE, JSType::METHOD));
231    SetConstant(ConstantIndex::LINKED_NODE_CLASS_INDEX,
232        factory->NewSEcmaReadOnlyHClass(hClass, LinkedNode::SIZE, JSType::LINKED_NODE));
233    SetConstant(ConstantIndex::RB_TREENODE_CLASS_INDEX,
234        factory->NewSEcmaReadOnlyHClass(hClass, RBTreeNode::SIZE, JSType::RB_TREENODE));
235    SetConstant(ConstantIndex::CLASS_LITERAL_HCLASS_INDEX,
236        factory->NewSEcmaReadOnlyHClass(hClass, ClassLiteral::SIZE, JSType::CLASS_LITERAL));
237    SetConstant(ConstantIndex::RESOLVED_RECORD_INEDX_BINDING_CLASS_INDEX,
238        factory->NewSEcmaReadOnlyHClass(hClass, ResolvedRecordIndexBinding::SIZE,
239        JSType::RESOLVEDRECORDINDEXBINDING_RECORD));
240    SetConstant(ConstantIndex::RESOLVED_RECORD_BINDING_CLASS_INDEX,
241        factory->NewSEcmaReadOnlyHClass(hClass, ResolvedRecordBinding::SIZE, JSType::RESOLVEDRECORDBINDING_RECORD));
242    SetConstant(ConstantIndex::SENDABLE_ENV_CLASS_INDEX,
243        factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::SENDABLE_ENV));
244}
245
246void GlobalEnvConstants::InitSharedMiscellanious(JSThread *thread, ObjectFactory *factory)
247{
248    // Accessors
249    auto accessor = factory->NewSInternalAccessor(reinterpret_cast<void *>(JSFunction::PrototypeSetter),
250                                                  reinterpret_cast<void *>(JSFunction::PrototypeGetter));
251    SetConstant(ConstantIndex::FUNCTION_PROTOTYPE_ACCESSOR, accessor);
252    accessor = factory->NewSInternalAccessor(nullptr, reinterpret_cast<void *>(JSFunction::NameGetter));
253    SetConstant(ConstantIndex::FUNCTION_NAME_ACCESSOR, accessor);
254    accessor = factory->NewSInternalAccessor(nullptr, reinterpret_cast<void *>(JSFunction::LengthGetter));
255    SetConstant(ConstantIndex::FUNCTION_LENGTH_ACCESSOR, accessor);
256    accessor = factory->NewSInternalAccessor(reinterpret_cast<void *>(JSArray::LengthSetter),
257                                             reinterpret_cast<void *>(JSArray::LengthGetter));
258    SetConstant(ConstantIndex::ARRAY_LENGTH_ACCESSOR, accessor);
259    accessor = factory->NewSInternalAccessor(reinterpret_cast<void *>(JSSharedArray::DummyLengthSetter),
260                                             reinterpret_cast<void *>(JSSharedArray::LengthGetter));
261    SetConstant(ConstantIndex::SHARED_ARRAY_LENGTH_ACCESSOR, accessor);
262    // Specials
263    SetConstant(ConstantIndex::UNDEFINED_INDEX, JSTaggedValue::Undefined());
264    SetConstant(ConstantIndex::NULL_INDEX, JSTaggedValue::Null());
265    SetConstant(ConstantIndex::HOLE_INDEX, JSTaggedValue::Hole());
266    SetConstant(ConstantIndex::TRUE_INDEX, JSTaggedValue::True());
267    SetConstant(ConstantIndex::FALSE_INDEX, JSTaggedValue::False());
268    // Emptys
269    auto vm = thread->GetEcmaVM();
270    SetConstant(ConstantIndex::EMPTY_STRING_OBJECT_INDEX, JSTaggedValue(EcmaStringAccessor::CreateEmptyString(vm)));
271    SetConstant(ConstantIndex::SINGLE_CHAR_TABLE_INDEX, SingleCharTable::CreateSingleCharTable(thread));
272    SetConstant(ConstantIndex::EMPTY_ARRAY_OBJECT_INDEX, factory->NewSEmptyArray());
273    SetConstant(ConstantIndex::EMPTY_MUTANT_ARRAY_OBJECT_INDEX, factory->NewSEmptyMutantArray());
274    SetConstant(ConstantIndex::EMPTY_SLAYOUT_INFO_OBJECT_INDEX, factory->NewSEmptyLayoutInfo());
275    SetConstant(ConstantIndex::UINT64_MAX_BIGINT_INDEX, BigInt::CreateUint64MaxBigInt(thread));
276    SetConstant(ConstantIndex::INT64_MAX_BIGINT_INDEX, BigInt::CreateInt64MaxBigInt(thread));
277    SetConstant(ConstantIndex::EMPTY_PROFILE_TYPE_INFO_CELL_INDEX, factory->NewSEmptyProfileTypeInfoCell());
278}
279
280void GlobalEnvConstants::InitRootsClassesPartOne(JSHClass *hClass, ObjectFactory *factory)
281{
282    SetConstant(ConstantIndex::JS_API_LINKED_LIST_ITERATOR_CLASS_INDEX,
283                factory->NewEcmaHClass(hClass, JSAPILinkedListIterator::SIZE, JSType::JS_API_LINKED_LIST_ITERATOR));
284    SetConstant(ConstantIndex::JS_API_LIST_ITERATOR_CLASS_INDEX,
285                factory->NewEcmaHClass(hClass, JSAPIListIterator::SIZE, JSType::JS_API_LIST_ITERATOR));
286    SetConstant(ConstantIndex::JS_API_PLAIN_ARRAY_ITERATOR_CLASS_INDEX,
287                factory->NewEcmaHClass(hClass, JSAPIPlainArrayIterator::SIZE, JSType::JS_API_PLAIN_ARRAY_ITERATOR));
288    SetConstant(ConstantIndex::JS_API_QUEUE_ITERATOR_CLASS_INDEX,
289                factory->NewEcmaHClass(hClass, JSAPIQueueIterator::SIZE, JSType::JS_API_QUEUE_ITERATOR));
290    SetConstant(ConstantIndex::JS_API_STACK_ITERATOR_CLASS_INDEX,
291                factory->NewEcmaHClass(hClass, JSAPIStackIterator::SIZE, JSType::JS_API_STACK_ITERATOR));
292    SetConstant(ConstantIndex::JS_API_VECTOR_ITERATOR_CLASS_INDEX,
293                factory->NewEcmaHClass(hClass, JSAPIVectorIterator::SIZE, JSType::JS_API_VECTOR_ITERATOR));
294    SetConstant(ConstantIndex::JS_API_HASH_MAP_ITERATOR_CLASS_INDEX,
295                factory->NewEcmaHClass(hClass, JSAPIHashMapIterator::SIZE, JSType::JS_API_HASHMAP_ITERATOR));
296    SetConstant(ConstantIndex::JS_API_HASH_SET_ITERATOR_CLASS_INDEX,
297                factory->NewEcmaHClass(hClass, JSAPIHashSetIterator::SIZE, JSType::JS_API_HASHSET_ITERATOR));
298    SetConstant(ConstantIndex::JS_API_TREE_MAP_ITERATOR_CLASS_INDEX,
299                factory->NewEcmaHClass(hClass, JSAPITreeMapIterator::SIZE, JSType::JS_API_TREEMAP_ITERATOR));
300    SetConstant(ConstantIndex::JS_API_TREE_SET_ITERATOR_CLASS_INDEX,
301                factory->NewEcmaHClass(hClass, JSAPITreeSetIterator::SIZE, JSType::JS_API_TREESET_ITERATOR));
302    SetConstant(ConstantIndex::OBJECT_HCLASS_INDEX, factory->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT));
303    SetConstant(ConstantIndex::CLASS_PROTOTYPE_HCLASS_INDEX,
304                factory->CreateDefaultClassPrototypeHClass(hClass));
305    SetConstant(ConstantIndex::CLASS_CONSTRUCTOR_HCLASS_INDEX,
306                factory->CreateDefaultClassConstructorHClass(hClass));
307    SetConstant(ConstantIndex::JS_PROXY_ORDINARY_CLASS_INDEX,
308                factory->NewEcmaHClass(hClass, JSProxy::SIZE, JSType::JS_PROXY));
309    // napi_wrap need set NativePointer to object, so only mark the shared bit in shared JSNativePointer hclass.
310    SetConstant(ConstantIndex::JS_NATIVE_POINTER_CLASS_INDEX,
311        factory->NewEcmaReadOnlyHClass(hClass, JSNativePointer::SIZE, JSType::JS_NATIVE_POINTER));
312}
313
314void GlobalEnvConstants::InitRootsClassesPartTwo(JSHClass *hClass, ObjectFactory *factory)
315{
316    SetConstant(ConstantIndex::JS_REALM_CLASS_INDEX,
317                factory->NewEcmaHClass(hClass, JSRealm::SIZE, JSType::JS_REALM));
318    SetConstant(ConstantIndex::JS_REGEXP_ITERATOR_CLASS_INDEX,
319                factory->NewEcmaHClass(hClass, JSRegExpIterator::SIZE, JSType::JS_REG_EXP_ITERATOR));
320    SetConstant(ConstantIndex::JS_SET_ITERATOR_CLASS_INDEX,
321                factory->NewEcmaHClass(hClass, JSSetIterator::SIZE, JSType::JS_SET_ITERATOR, 0)); // 0: no inlined props
322    SetConstant(ConstantIndex::JS_SHARED_SET_ITERATOR_CLASS_INDEX,
323                factory->NewEcmaHClass(hClass, JSSharedSetIterator::SIZE,
324                JSType::JS_SHARED_SET_ITERATOR, 0)); // 0: no inlined props
325    SetConstant(ConstantIndex::JS_MAP_ITERATOR_CLASS_INDEX,
326                factory->NewEcmaHClass(hClass, JSMapIterator::SIZE, JSType::JS_MAP_ITERATOR, 0)); // 0: no inlined props
327    SetConstant(ConstantIndex::JS_SHARED_MAP_ITERATOR_CLASS_INDEX,
328                factory->NewEcmaHClass(hClass, JSSharedMapIterator::SIZE,
329                JSType::JS_SHARED_MAP_ITERATOR, 0)); // 0: no inlined props
330    SetConstant(ConstantIndex::JS_ARRAY_ITERATOR_CLASS_INDEX,
331                factory->NewEcmaHClass(hClass, JSArrayIterator::SIZE, JSType::JS_ARRAY_ITERATOR, 0));
332    SetConstant(ConstantIndex::JS_SHARED_ARRAY_ITERATOR_CLASS_INDEX,
333                factory->NewEcmaHClass(hClass, JSSharedArrayIterator::SIZE, JSType::JS_SHARED_ARRAY_ITERATOR, 0));
334    SetConstant(ConstantIndex::JS_API_ARRAYLIST_ITERATOR_CLASS_INDEX,
335                factory->NewEcmaHClass(hClass, JSAPIArrayListIterator::SIZE, JSType::JS_API_ARRAYLIST_ITERATOR));
336    SetConstant(ConstantIndex::JS_API_DEQUE_ITERATOR_CLASS_INDEX,
337                factory->NewEcmaHClass(hClass, JSAPIDequeIterator::SIZE, JSType::JS_API_DEQUE_ITERATOR));
338    SetConstant(ConstantIndex::JS_API_LIGHTWEIGHTMAP_ITERATOR_CLASS_INDEX,
339                factory->NewEcmaHClass(hClass, JSAPILightWeightMapIterator::SIZE,
340                JSType::JS_API_LIGHT_WEIGHT_MAP_ITERATOR));
341    SetConstant(ConstantIndex::JS_API_LIGHTWEIGHTSET_ITERATOR_CLASS_INDEX,
342                factory->NewEcmaHClass(hClass, JSAPILightWeightSetIterator::SIZE,
343                JSType::JS_API_LIGHT_WEIGHT_SET_ITERATOR));
344    SetConstant(ConstantIndex::JS_API_BITVECTOR_ITERATOR_CLASS_INDEX,
345                factory->NewEcmaHClass(hClass, JSAPIBitVectorIterator::SIZE, JSType::JS_API_BITVECTOR_ITERATOR));
346}
347
348void GlobalEnvConstants::InitRootsClasses(ObjectFactory *factory)
349{
350    JSHClass *hClass = JSHClass::Cast(GetHClassClass().GetTaggedObject());
351
352    InitRootsClassesPartOne(hClass, factory);
353    InitRootsClassesPartTwo(hClass, factory);
354    // JS_PROXY_CALLABLE_CLASS_INDEX
355    JSHClass *jsProxyCallableClass = *factory->NewEcmaHClass(hClass, JSProxy::SIZE, JSType::JS_PROXY);
356    jsProxyCallableClass->SetCallable(true);
357    SetConstant(ConstantIndex::JS_PROXY_CALLABLE_CLASS_INDEX, JSTaggedValue(jsProxyCallableClass));
358    // JS_PROXY_CONSTRUCT_CLASS_INDEX
359    JSHClass *jsProxyConstructClass = *factory->NewEcmaHClass(hClass, JSProxy::SIZE, JSType::JS_PROXY);
360    jsProxyConstructClass->SetCallable(true);
361    jsProxyConstructClass->SetConstructor(true);
362    SetConstant(ConstantIndex::JS_PROXY_CONSTRUCT_CLASS_INDEX, JSTaggedValue(jsProxyConstructClass));
363}
364
365void GlobalEnvConstants::InitMiscellanious(JSThread *thread, ObjectFactory *factory)
366{
367    SetConstant(ConstantIndex::EMPTY_LAYOUT_INFO_OBJECT_INDEX, factory->CreateLayoutInfo(0));
368    SetConstant(ConstantIndex::EMPTY_TAGGED_QUEUE_OBJECT_INDEX, factory->NewTaggedQueue(0));
369    SetConstant(ConstantIndex::DEFAULT_SUPERS_INDEX,
370                WeakVector::Create(thread, VTable::DEFAULT_SUPERS_CAPACITY, MemSpaceType::NON_MOVABLE));
371
372    // non ECMA standard jsapi containers iterators, init to Undefined first
373    InitJSAPIContainers();
374
375    InitGlobalCaches();
376}
377
378void GlobalEnvConstants::InitGlobalCaches()
379{
380    SetConstant(ConstantIndex::CACHED_JSCOLLATOR_LOCALES_INDEX, JSTaggedValue::Undefined());
381}
382
383void GlobalEnvConstants::SetCachedLocales(JSTaggedValue value)
384{
385    JSTaggedValue cached = GetCachedJSCollatorLocales();
386    if (cached.IsUndefined()) {
387        SetConstant(ConstantIndex::CACHED_JSCOLLATOR_LOCALES_INDEX, value);
388    }
389}
390
391void GlobalEnvConstants::InitJSAPIContainers()
392{
393    for (size_t i = GetJSAPIContainersBegin(); i <= GetJSAPIContainersEnd(); i++) {
394        SetConstant(static_cast<ConstantIndex>(i), JSTaggedValue::Undefined());
395    }
396}
397
398void GlobalEnvConstants::InitSpecialForSnapshot()
399{
400    SetConstant(ConstantIndex::UNDEFINED_INDEX, JSTaggedValue::Undefined());
401    SetConstant(ConstantIndex::NULL_INDEX, JSTaggedValue::Null());
402    InitJSAPIContainers();
403}
404
405void GlobalEnvConstants::InitElementKindHClass(const JSThread *thread, JSHandle<JSHClass> originHClass)
406{
407    auto map = thread->GetArrayHClassIndexMap();
408    for (auto iter : map) {
409        JSHandle<JSHClass> hclassWithProto = JSHClass::CloneWithElementsKind(thread, originHClass, iter.first, true);
410        if (iter.first != ElementsKind::GENERIC) {
411            JSHandle<JSHClass> hclass = JSHClass::CloneWithElementsKind(thread, originHClass, iter.first, false);
412            SetConstant(iter.second.first, hclass);
413        } else {
414            SetConstant(iter.second.first, originHClass);
415        }
416        SetConstant(iter.second.second, hclassWithProto);
417    }
418}
419}  // namespace panda::ecmascript
420