• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/accessor_data.h"
19 #include "ecmascript/builtins/builtins.h"
20 #include "ecmascript/builtins/builtins_global.h"
21 #include "ecmascript/ecma_string-inl.h"
22 #include "ecmascript/ecma_vm.h"
23 #include "ecmascript/free_object.h"
24 #include "ecmascript/global_env.h"
25 #include "ecmascript/global_env_constants-inl.h"
26 #include "ecmascript/ic/ic_handler.h"
27 #include "ecmascript/ic/property_box.h"
28 #include "ecmascript/ic/proto_change_details.h"
29 #include "ecmascript/jobs/micro_job_queue.h"
30 #include "ecmascript/jobs/pending_job.h"
31 #include "ecmascript/js_api/js_api_arraylist_iterator.h"
32 #include "ecmascript/js_api/js_api_deque_iterator.h"
33 #include "ecmascript/js_api/js_api_lightweightmap_iterator.h"
34 #include "ecmascript/js_api/js_api_lightweightset_iterator.h"
35 #include "ecmascript/js_api/js_api_linked_list_iterator.h"
36 #include "ecmascript/js_api/js_api_list_iterator.h"
37 #include "ecmascript/js_api/js_api_plain_array_iterator.h"
38 #include "ecmascript/js_api/js_api_queue_iterator.h"
39 #include "ecmascript/js_api/js_api_stack_iterator.h"
40 #include "ecmascript/js_api/js_api_hashmap_iterator.h"
41 #include "ecmascript/js_api/js_api_hashset_iterator.h"
42 #include "ecmascript/js_api/js_api_tree_map_iterator.h"
43 #include "ecmascript/js_api/js_api_tree_set_iterator.h"
44 #include "ecmascript/js_api/js_api_vector_iterator.h"
45 #include "ecmascript/js_async_generator_object.h"
46 #include "ecmascript/jspandafile/class_info_extractor.h"
47 #include "ecmascript/jspandafile/class_literal.h"
48 #include "ecmascript/jspandafile/program_object.h"
49 #include "ecmascript/js_arguments.h"
50 #include "ecmascript/js_array.h"
51 #include "ecmascript/js_array_iterator.h"
52 #include "ecmascript/js_arraybuffer.h"
53 #include "ecmascript/js_finalization_registry.h"
54 #include "ecmascript/js_for_in_iterator.h"
55 #include "ecmascript/js_generator_object.h"
56 #include "ecmascript/js_hclass.h"
57 #include "ecmascript/js_iterator.h"
58 #include "ecmascript/js_map.h"
59 #include "ecmascript/js_map_iterator.h"
60 #include "ecmascript/js_primitive_ref.h"
61 #include "ecmascript/js_promise.h"
62 #include "ecmascript/js_proxy.h"
63 #include "ecmascript/js_realm.h"
64 #include "ecmascript/js_regexp.h"
65 #include "ecmascript/js_regexp_iterator.h"
66 #include "ecmascript/js_set.h"
67 #include "ecmascript/js_set_iterator.h"
68 #include "ecmascript/js_symbol.h"
69 #include "ecmascript/js_tagged_value.h"
70 #include "ecmascript/js_thread.h"
71 #include "ecmascript/marker_cell.h"
72 #include "ecmascript/method.h"
73 #include "ecmascript/module/js_module_source_text.h"
74 #include "ecmascript/object_factory.h"
75 #include "ecmascript/subtyping_operator.h"
76 #include "ecmascript/tagged_node.h"
77 #include "ecmascript/ts_types/ts_type.h"
78 
79 namespace panda::ecmascript {
Init(JSThread * thread,JSHClass * hClass)80 void GlobalEnvConstants::Init(JSThread *thread, JSHClass *hClass)
81 {
82     InitRootsClass(thread, hClass);
83     InitGlobalConstant(thread);
84     InitGlobalCaches();
85 }
86 
InitRootsClass(JSThread * thread,JSHClass * hClass)87 void GlobalEnvConstants::InitRootsClass(JSThread *thread, JSHClass *hClass)
88 {
89     // Global constants are readonly.
90     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
91 
92     SetConstant(ConstantIndex::HCLASS_CLASS_INDEX, JSTaggedValue(hClass));
93     // To reverse the order, the hclass of string needs to load default supers
94     SetConstant(ConstantIndex::ARRAY_CLASS_INDEX,
95                 factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::TAGGED_ARRAY));
96     SetConstant(ConstantIndex::DEFAULT_SUPERS_INDEX,
97                 WeakVector::Create(thread, SubtypingOperator::DEFAULT_SUPERS_CAPACITY, MemSpaceType::NON_MOVABLE));
98     SetConstant(ConstantIndex::FREE_OBJECT_WITH_NONE_FIELD_CLASS_INDEX,
99         factory->NewEcmaReadOnlyHClass(hClass, FreeObject::NEXT_OFFSET, JSType::FREE_OBJECT_WITH_NONE_FIELD));
100     SetConstant(ConstantIndex::FREE_OBJECT_WITH_ONE_FIELD_CLASS_INDEX,
101         factory->NewEcmaReadOnlyHClass(hClass, FreeObject::SIZE_OFFSET, JSType::FREE_OBJECT_WITH_ONE_FIELD));
102     SetConstant(ConstantIndex::FREE_OBJECT_WITH_TWO_FIELD_CLASS_INDEX,
103                 factory->NewEcmaReadOnlyHClass(hClass, FreeObject::SIZE, JSType::FREE_OBJECT_WITH_TWO_FIELD));
104     SetConstant(ConstantIndex::LINE_STRING_CLASS_INDEX, factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::LINE_STRING));
105     SetConstant(ConstantIndex::SLICED_STRING_CLASS_INDEX,
106         factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::SLICED_STRING));
107     SetConstant(ConstantIndex::CONSTANT_STRING_CLASS_INDEX,
108         factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::CONSTANT_STRING));
109     SetConstant(ConstantIndex::TREE_STRING_CLASS_INDEX, factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::TREE_STRING));
110     SetConstant(ConstantIndex::BYTE_ARRAY_CLASS_INDEX,
111                 factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::BYTE_ARRAY));
112     SetConstant(ConstantIndex::CONSTANT_POOL_CLASS_INDEX,
113                 factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::CONSTANT_POOL));
114     SetConstant(ConstantIndex::PROFILE_TYPE_INFO_CLASS_INDEX,
115                 factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::PROFILE_TYPE_INFO));
116     SetConstant(ConstantIndex::AOT_LITERAL_INFO_CLASS_INDEX,
117                 factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::AOT_LITERAL_INFO));
118     SetConstant(ConstantIndex::VTABLE_CLASS_INDEX,
119                 factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::VTABLE));
120     SetConstant(ConstantIndex::COW_MUTANT_TAGGED_ARRAY_CLASS_INDEX,
121                 factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::COW_MUTANT_TAGGED_ARRAY));
122     SetConstant(ConstantIndex::MUTANT_TAGGED_ARRAY_CLASS_INDEX,
123                 factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::MUTANT_TAGGED_ARRAY));
124     InitGlobalConstantSpecial(thread);
125     SetConstant(ConstantIndex::DICTIONARY_CLASS_INDEX,
126                 factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::TAGGED_DICTIONARY));
127     SetConstant(ConstantIndex::COW_ARRAY_CLASS_INDEX,
128                 factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::COW_TAGGED_ARRAY));
129     SetConstant(ConstantIndex::BIGINT_CLASS_INDEX,
130                 factory->NewEcmaReadOnlyHClass(hClass, BigInt::SIZE, JSType::BIGINT));
131     SetConstant(ConstantIndex::JS_NATIVE_POINTER_CLASS_INDEX,
132                 factory->NewEcmaReadOnlyHClass(hClass, JSNativePointer::SIZE, JSType::JS_NATIVE_POINTER));
133     SetConstant(ConstantIndex::ENV_CLASS_INDEX,
134                 factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::LEXICAL_ENV));
135     SetConstant(ConstantIndex::SYMBOL_CLASS_INDEX,
136                 factory->NewEcmaReadOnlyHClass(hClass, JSSymbol::SIZE, JSType::SYMBOL));
137     SetConstant(ConstantIndex::ACCESSOR_DATA_CLASS_INDEX,
138                 factory->NewEcmaReadOnlyHClass(hClass, AccessorData::SIZE, JSType::ACCESSOR_DATA));
139     SetConstant(ConstantIndex::INTERNAL_ACCESSOR_CLASS_INDEX,
140                 factory->NewEcmaReadOnlyHClass(hClass, AccessorData::SIZE, JSType::INTERNAL_ACCESSOR));
141     SetConstant(ConstantIndex::JS_PROXY_ORDINARY_CLASS_INDEX,
142                 factory->NewEcmaHClass(hClass, JSProxy::SIZE, JSType::JS_PROXY));
143     SetConstant(ConstantIndex::COMPLETION_RECORD_CLASS_INDEX,
144                 factory->NewEcmaReadOnlyHClass(hClass, CompletionRecord::SIZE, JSType::COMPLETION_RECORD));
145     SetConstant(ConstantIndex::GENERATOR_CONTEST_INDEX,
146                 factory->NewEcmaReadOnlyHClass(hClass, GeneratorContext::SIZE, JSType::JS_GENERATOR_CONTEXT));
147     SetConstant(ConstantIndex::ASYNC_GENERATOR_REQUEST_CLASS_INDEX,
148                 factory->NewEcmaReadOnlyHClass(hClass, AsyncGeneratorRequest::SIZE,
149                                                JSType::ASYNC_GENERATOR_REQUEST));
150     SetConstant(ConstantIndex::ASYNC_ITERATOR_RECORD_CLASS_INDEX,
151                 factory->NewEcmaReadOnlyHClass(hClass, AsyncIteratorRecord::SIZE, JSType::ASYNC_ITERATOR_RECORD));
152     SetConstant(ConstantIndex::CAPABILITY_RECORD_CLASS_INDEX,
153                 factory->NewEcmaReadOnlyHClass(hClass, PromiseCapability::SIZE, JSType::PROMISE_CAPABILITY));
154     SetConstant(ConstantIndex::REACTIONS_RECORD_CLASS_INDEX,
155                 factory->NewEcmaReadOnlyHClass(hClass, PromiseReaction::SIZE, JSType::PROMISE_REACTIONS));
156     SetConstant(ConstantIndex::PROMISE_ITERATOR_RECORD_CLASS_INDEX,
157                 factory->NewEcmaReadOnlyHClass(hClass, PromiseIteratorRecord::SIZE,
158                                                JSType::PROMISE_ITERATOR_RECORD));
159     SetConstant(ConstantIndex::PROMISE_RECORD_CLASS_INDEX,
160                 factory->NewEcmaReadOnlyHClass(hClass, PromiseRecord::SIZE, JSType::PROMISE_RECORD));
161     SetConstant(ConstantIndex::PROMISE_RESOLVING_FUNCTIONS_CLASS_INDEX,
162                 factory->NewEcmaReadOnlyHClass(hClass, ResolvingFunctionsRecord::SIZE,
163                                                JSType::RESOLVING_FUNCTIONS_RECORD));
164     SetConstant(ConstantIndex::MICRO_JOB_QUEUE_CLASS_INDEX,
165                 factory->NewEcmaReadOnlyHClass(hClass, job::MicroJobQueue::SIZE, JSType::MICRO_JOB_QUEUE));
166     SetConstant(ConstantIndex::PENDING_JOB_CLASS_INDEX,
167                 factory->NewEcmaReadOnlyHClass(hClass, job::PendingJob::SIZE, JSType::PENDING_JOB));
168     SetConstant(ConstantIndex::PROTO_CHANGE_MARKER_CLASS_INDEX,
169                 factory->NewEcmaReadOnlyHClass(hClass, ProtoChangeMarker::SIZE, JSType::PROTO_CHANGE_MARKER));
170     SetConstant(ConstantIndex::PROTO_CHANGE_DETAILS_CLASS_INDEX,
171                 factory->NewEcmaReadOnlyHClass(hClass, ProtoChangeDetails::SIZE, JSType::PROTOTYPE_INFO));
172     SetConstant(ConstantIndex::MARKER_CELL_CLASS_INDEX,
173                 factory->NewEcmaReadOnlyHClass(hClass, MarkerCell::SIZE, JSType::MARKER_CELL));
174     SetConstant(ConstantIndex::TRACK_INFO_CLASS_INDEX,
175                 factory->NewEcmaReadOnlyHClass(hClass, TrackInfo::SIZE, JSType::TRACK_INFO));
176     SetConstant(ConstantIndex::PROTOTYPE_HANDLER_CLASS_INDEX,
177                 factory->NewEcmaReadOnlyHClass(hClass, PrototypeHandler::SIZE, JSType::PROTOTYPE_HANDLER));
178     SetConstant(ConstantIndex::TRANSITION_HANDLER_CLASS_INDEX,
179                 factory->NewEcmaReadOnlyHClass(hClass, TransitionHandler::SIZE, JSType::TRANSITION_HANDLER));
180     SetConstant(ConstantIndex::TRANS_WITH_PROTO_HANDLER_CLASS_INDEX,
181                 factory->NewEcmaReadOnlyHClass(hClass, TransWithProtoHandler::SIZE, JSType::TRANS_WITH_PROTO_HANDLER));
182     SetConstant(ConstantIndex::STORE_TS_HANDLER_CLASS_INDEX,
183                 factory->NewEcmaReadOnlyHClass(hClass, StoreTSHandler::SIZE, JSType::STORE_TS_HANDLER));
184     SetConstant(ConstantIndex::PROPERTY_BOX_CLASS_INDEX,
185                 factory->NewEcmaReadOnlyHClass(hClass, PropertyBox::SIZE, JSType::PROPERTY_BOX));
186     SetConstant(ConstantIndex::PROGRAM_CLASS_INDEX,
187                 factory->NewEcmaReadOnlyHClass(hClass, Program::SIZE, JSType::PROGRAM));
188     SetConstant(
189         ConstantIndex::IMPORT_ENTRY_CLASS_INDEX,
190         factory->NewEcmaReadOnlyHClass(hClass, ImportEntry::SIZE, JSType::IMPORTENTRY_RECORD));
191     SetConstant(
192         ConstantIndex::LOCAL_EXPORT_ENTRY_CLASS_INDEX,
193         factory->NewEcmaReadOnlyHClass(hClass, LocalExportEntry::SIZE, JSType::LOCAL_EXPORTENTRY_RECORD));
194     SetConstant(
195         ConstantIndex::INDIRECT_EXPORT_ENTRY_CLASS_INDEX,
196         factory->NewEcmaReadOnlyHClass(hClass, IndirectExportEntry::SIZE,
197                                        JSType::INDIRECT_EXPORTENTRY_RECORD));
198     SetConstant(
199         ConstantIndex::STAR_EXPORT_ENTRY_CLASS_INDEX,
200         factory->NewEcmaReadOnlyHClass(hClass, StarExportEntry::SIZE, JSType::STAR_EXPORTENTRY_RECORD));
201     SetConstant(
202         ConstantIndex::SOURCE_TEXT_MODULE_CLASS_INDEX,
203         factory->NewEcmaReadOnlyHClass(hClass, SourceTextModule::SIZE, JSType::SOURCE_TEXT_MODULE_RECORD));
204     SetConstant(
205         ConstantIndex::RESOLVED_BINDING_CLASS_INDEX,
206         factory->NewEcmaReadOnlyHClass(hClass, ResolvedBinding::SIZE, JSType::RESOLVEDBINDING_RECORD));
207     SetConstant(
208         ConstantIndex::RESOLVED_INDEX_BINDING_CLASS_INDEX,
209         factory->NewEcmaReadOnlyHClass(hClass, ResolvedIndexBinding::SIZE, JSType::RESOLVEDINDEXBINDING_RECORD));
210 
211     JSHClass *jsProxyCallableClass = *factory->NewEcmaHClass(hClass, JSProxy::SIZE, JSType::JS_PROXY);
212 
213     jsProxyCallableClass->SetCallable(true);
214     SetConstant(ConstantIndex::JS_PROXY_CALLABLE_CLASS_INDEX, JSTaggedValue(jsProxyCallableClass));
215 
216     JSHClass *jsProxyConstructClass = *factory->NewEcmaHClass(hClass, JSProxy::SIZE, JSType::JS_PROXY);
217 
218     jsProxyConstructClass->SetCallable(true);
219     jsProxyConstructClass->SetConstructor(true);
220     SetConstant(ConstantIndex::JS_PROXY_CONSTRUCT_CLASS_INDEX, JSTaggedValue(jsProxyConstructClass));
221 
222     SetConstant(ConstantIndex::JS_REALM_CLASS_INDEX,
223                 factory->NewEcmaHClass(hClass, JSRealm::SIZE, JSType::JS_REALM));
224     SetConstant(ConstantIndex::MACHINE_CODE_CLASS_INDEX,
225                 factory->NewEcmaReadOnlyHClass(hClass, 0, JSType::MACHINE_CODE_OBJECT));
226     SetConstant(ConstantIndex::CLASS_INFO_EXTRACTOR_HCLASS_INDEX,
227                 factory->NewEcmaReadOnlyHClass(hClass, ClassInfoExtractor::SIZE,
228                                                JSType::CLASS_INFO_EXTRACTOR));
229     SetConstant(ConstantIndex::TS_OBJECT_TYPE_CLASS_INDEX,
230                 factory->NewEcmaReadOnlyHClass(hClass, TSObjectType::SIZE, JSType::TS_OBJECT_TYPE));
231     SetConstant(ConstantIndex::TS_CLASS_TYPE_CLASS_INDEX,
232                 factory->NewEcmaReadOnlyHClass(hClass, TSClassType::SIZE, JSType::TS_CLASS_TYPE));
233     SetConstant(ConstantIndex::TS_UNION_TYPE_CLASS_INDEX,
234                 factory->NewEcmaReadOnlyHClass(hClass, TSUnionType::SIZE, JSType::TS_UNION_TYPE));
235     SetConstant(ConstantIndex::TS_INTERFACE_TYPE_CLASS_INDEX,
236                 factory->NewEcmaReadOnlyHClass(hClass, TSInterfaceType::SIZE, JSType::TS_INTERFACE_TYPE));
237     SetConstant(ConstantIndex::TS_CLASS_INSTANCE_TYPE_CLASS_INDEX,
238                 factory->NewEcmaReadOnlyHClass(hClass, TSClassInstanceType::SIZE,
239                                                JSType::TS_CLASS_INSTANCE_TYPE));
240     SetConstant(ConstantIndex::TS_FUNCTION_TYPE_CLASS_INDEX,
241                 factory->NewEcmaReadOnlyHClass(hClass, TSFunctionType::SIZE, JSType::TS_FUNCTION_TYPE));
242     SetConstant(ConstantIndex::TS_ARRAY_TYPE_CLASS_INDEX,
243                 factory->NewEcmaReadOnlyHClass(hClass, TSArrayType::SIZE, JSType::TS_ARRAY_TYPE));
244     SetConstant(ConstantIndex::TS_ITERATOR_INSTANCE_TYPE_CLASS_INDEX,
245                 factory->NewEcmaReadOnlyHClass(hClass, TSIteratorInstanceType::SIZE,
246                 JSType::TS_ITERATOR_INSTANCE_TYPE));
247     SetConstant(ConstantIndex::TS_NAMESPACE_TYPE_CLASS_INDEX,
248                 factory->NewEcmaReadOnlyHClass(hClass, TSNamespaceType::SIZE, JSType::TS_NAMESPACE_TYPE));
249     SetConstant(ConstantIndex::JS_REGEXP_ITERATOR_CLASS_INDEX,
250                 factory->NewEcmaHClass(hClass, JSRegExpIterator::SIZE, JSType::JS_REG_EXP_ITERATOR));
251     SetConstant(ConstantIndex::JS_SET_ITERATOR_CLASS_INDEX,
252                 factory->NewEcmaHClass(hClass, JSSetIterator::SIZE, JSType::JS_SET_ITERATOR, 0)); // 0: no inlined props
253     SetConstant(ConstantIndex::JS_MAP_ITERATOR_CLASS_INDEX,
254                 factory->NewEcmaHClass(hClass, JSMapIterator::SIZE, JSType::JS_MAP_ITERATOR, 0)); // 0: no inlined props
255     SetConstant(ConstantIndex::JS_ARRAY_ITERATOR_CLASS_INDEX,
256                 factory->NewEcmaHClass(hClass, JSArrayIterator::SIZE, JSType::JS_ARRAY_ITERATOR, 0));
257     SetConstant(
258         ConstantIndex::JS_API_ARRAYLIST_ITERATOR_CLASS_INDEX,
259         factory->NewEcmaHClass(hClass, JSAPIArrayListIterator::SIZE, JSType::JS_API_ARRAYLIST_ITERATOR));
260     SetConstant(ConstantIndex::JS_API_DEQUE_ITERATOR_CLASS_INDEX,
261                 factory->NewEcmaHClass(hClass, JSAPIDequeIterator::SIZE, JSType::JS_API_DEQUE_ITERATOR));
262     SetConstant(ConstantIndex::JS_API_LIGHTWEIGHTMAP_ITERATOR_CLASS_INDEX,
263                 factory->NewEcmaHClass(hClass, JSAPILightWeightMapIterator::SIZE,
264                 JSType::JS_API_LIGHT_WEIGHT_MAP_ITERATOR));
265     SetConstant(ConstantIndex::JS_API_LIGHTWEIGHTSET_ITERATOR_CLASS_INDEX,
266                 factory->NewEcmaHClass(hClass, JSAPILightWeightSetIterator::SIZE,
267                 JSType::JS_API_LIGHT_WEIGHT_SET_ITERATOR));
268     SetConstant(
269         ConstantIndex::JS_API_LINKED_LIST_ITERATOR_CLASS_INDEX,
270         factory->NewEcmaHClass(hClass, JSAPILinkedListIterator::SIZE, JSType::JS_API_LINKED_LIST_ITERATOR));
271     SetConstant(ConstantIndex::JS_API_LIST_ITERATOR_CLASS_INDEX,
272                 factory->NewEcmaHClass(hClass, JSAPIListIterator::SIZE, JSType::JS_API_LIST_ITERATOR));
273     SetConstant(
274         ConstantIndex::JS_API_PLAIN_ARRAY_ITERATOR_CLASS_INDEX,
275         factory->NewEcmaHClass(hClass, JSAPIPlainArrayIterator::SIZE, JSType::JS_API_PLAIN_ARRAY_ITERATOR));
276     SetConstant(ConstantIndex::JS_API_QUEUE_ITERATOR_CLASS_INDEX,
277                 factory->NewEcmaHClass(hClass, JSAPIQueueIterator::SIZE, JSType::JS_API_QUEUE_ITERATOR));
278     SetConstant(ConstantIndex::JS_API_STACK_ITERATOR_CLASS_INDEX,
279                 factory->NewEcmaHClass(hClass, JSAPIStackIterator::SIZE, JSType::JS_API_STACK_ITERATOR));
280     SetConstant(ConstantIndex::JS_API_VECTOR_ITERATOR_CLASS_INDEX,
281                 factory->NewEcmaHClass(hClass, JSAPIVectorIterator::SIZE, JSType::JS_API_VECTOR_ITERATOR));
282     SetConstant(ConstantIndex::JS_API_HASH_MAP_ITERATOR_CLASS_INDEX,
283                 factory->NewEcmaHClass(hClass, JSAPIHashMapIterator::SIZE, JSType::JS_API_HASHMAP_ITERATOR));
284     SetConstant(ConstantIndex::JS_API_HASH_SET_ITERATOR_CLASS_INDEX,
285                 factory->NewEcmaHClass(hClass, JSAPIHashSetIterator::SIZE, JSType::JS_API_HASHSET_ITERATOR));
286     SetConstant(ConstantIndex::JS_API_TREE_MAP_ITERATOR_CLASS_INDEX,
287                 factory->NewEcmaHClass(hClass, JSAPITreeMapIterator::SIZE, JSType::JS_API_TREEMAP_ITERATOR));
288     SetConstant(ConstantIndex::JS_API_TREE_SET_ITERATOR_CLASS_INDEX,
289                 factory->NewEcmaHClass(hClass, JSAPITreeSetIterator::SIZE, JSType::JS_API_TREESET_ITERATOR));
290     SetConstant(ConstantIndex::LINKED_NODE_CLASS_INDEX,
291                 factory->NewEcmaHClass(hClass, LinkedNode::SIZE, JSType::LINKED_NODE));
292     SetConstant(ConstantIndex::RB_TREENODE_CLASS_INDEX,
293                 factory->NewEcmaHClass(hClass, RBTreeNode::SIZE, JSType::RB_TREENODE));
294     SetConstant(ConstantIndex::CELL_RECORD_CLASS_INDEX,
295                 factory->NewEcmaReadOnlyHClass(hClass, CellRecord::SIZE, JSType::CELL_RECORD));
296     SetConstant(ConstantIndex::OBJECT_HCLASS_INDEX, factory->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT));
297     SetConstant(ConstantIndex::METHOD_CLASS_INDEX,
298                 factory->NewEcmaHClass(hClass, Method::SIZE, JSType::METHOD));
299     SetConstant(ConstantIndex::CLASS_LITERAL_HCLASS_INDEX,
300                 factory->NewEcmaHClass(hClass, ClassLiteral::SIZE, JSType::CLASS_LITERAL));
301 }
302 
InitGlobalConstantSpecial(JSThread * thread)303 void GlobalEnvConstants::InitGlobalConstantSpecial(JSThread *thread)
304 {
305     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
306     // SPECIAL INIT
307     SetConstant(ConstantIndex::UNDEFINED_INDEX, JSTaggedValue::Undefined());
308     SetConstant(ConstantIndex::NULL_INDEX, JSTaggedValue::Null());
309     SetConstant(ConstantIndex::TRUE_INDEX, JSTaggedValue::True());
310     SetConstant(ConstantIndex::FALSE_INDEX, JSTaggedValue::False());
311     auto vm = thread->GetEcmaVM();
312     SetConstant(ConstantIndex::EMPTY_STRING_OBJECT_INDEX, JSTaggedValue(EcmaStringAccessor::CreateEmptyString(vm)));
313     SetConstant(ConstantIndex::EMPTY_ARRAY_OBJECT_INDEX, factory->NewEmptyArray());
314     SetConstant(ConstantIndex::EMPTY_MUTANT_ARRAY_OBJECT_INDEX, factory->NewEmptyMutantArray());
315     SetConstant(ConstantIndex::EMPTY_LAYOUT_INFO_OBJECT_INDEX, factory->CreateLayoutInfo(0));
316     SetConstant(ConstantIndex::EMPTY_TAGGED_QUEUE_OBJECT_INDEX, factory->NewTaggedQueue(0));
317 }
318 
319 // NOLINTNEXTLINE(readability-function-size)
InitGlobalConstant(JSThread * thread)320 void GlobalEnvConstants::InitGlobalConstant(JSThread *thread)
321 {
322     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
323     /* non ECMA standard jsapi containers iterators, init to Undefined first */
324     InitJSAPIContainers();
325 
326 #define INIT_GLOBAL_ENV_CONSTANT_STRING(Name, Index, Token) \
327     SetConstant(ConstantIndex::Index, factory->NewFromASCIINonMovable(Token));
328 
329     GLOBAL_ENV_CONSTANT_STRING(INIT_GLOBAL_ENV_CONSTANT_STRING)
330 #undef INIT_GLOBAL_ENV_CONSTANT_STRING
331 
332     auto accessor = factory->NewInternalAccessor(reinterpret_cast<void *>(JSFunction::PrototypeSetter),
333                                                  reinterpret_cast<void *>(JSFunction::PrototypeGetter));
334     SetConstant(ConstantIndex::FUNCTION_PROTOTYPE_ACCESSOR, accessor);
335     accessor = factory->NewInternalAccessor(nullptr, reinterpret_cast<void *>(JSFunction::NameGetter));
336     SetConstant(ConstantIndex::FUNCTION_NAME_ACCESSOR, accessor);
337     accessor = factory->NewInternalAccessor(nullptr, reinterpret_cast<void *>(JSFunction::LengthGetter));
338     SetConstant(ConstantIndex::FUNCTION_LENGTH_ACCESSOR, accessor);
339     accessor = factory->NewInternalAccessor(reinterpret_cast<void *>(JSArray::LengthSetter),
340                                             reinterpret_cast<void *>(JSArray::LengthGetter));
341     SetConstant(ConstantIndex::ARRAY_LENGTH_ACCESSOR, accessor);
342     SetConstant(ConstantIndex::CLASS_PROTOTYPE_HCLASS_INDEX,
343                 factory->CreateDefaultClassPrototypeHClass(JSHClass::Cast(GetHClassClass().GetTaggedObject())));
344     SetConstant(ConstantIndex::CLASS_CONSTRUCTOR_HCLASS_INDEX,
345         factory->CreateDefaultClassConstructorHClass(JSHClass::Cast(GetHClassClass().GetTaggedObject())));
346 }
347 
InitGlobalCaches()348 void GlobalEnvConstants::InitGlobalCaches()
349 {
350     SetConstant(ConstantIndex::CACHED_JSCOLLATOR_LOCALES_INDEX, JSTaggedValue::Undefined());
351 }
352 
SetCachedLocales(JSTaggedValue value)353 void GlobalEnvConstants::SetCachedLocales(JSTaggedValue value)
354 {
355     JSTaggedValue cached = GetCachedJSCollatorLocales();
356     if (cached.IsUndefined()) {
357         SetConstant(ConstantIndex::CACHED_JSCOLLATOR_LOCALES_INDEX, value);
358     }
359 }
360 
InitJSAPIContainers()361 void GlobalEnvConstants::InitJSAPIContainers()
362 {
363     for (size_t i = GetJSAPIContainersBegin(); i <= GetJSAPIContainersEnd(); i++) {
364         SetConstant(static_cast<ConstantIndex>(i), JSTaggedValue::Undefined());
365     }
366 }
367 
InitSpecialForSnapshot()368 void GlobalEnvConstants::InitSpecialForSnapshot()
369 {
370     SetConstant(ConstantIndex::UNDEFINED_INDEX, JSTaggedValue::Undefined());
371     SetConstant(ConstantIndex::NULL_INDEX, JSTaggedValue::Null());
372     InitJSAPIContainers();
373 }
374 
InitElementKindHClass(const JSThread * thread,JSHandle<JSHClass> originHClass)375 void GlobalEnvConstants::InitElementKindHClass(const JSThread *thread, JSHandle<JSHClass> originHClass)
376 {
377     auto map = thread->GetArrayHClassIndexMap();
378     for (auto iter : map) {
379         JSHandle<JSHClass> hclass = originHClass;
380         if (iter.first != ElementsKind::GENERIC) {
381             hclass = JSHClass::Clone(thread, originHClass);
382             hclass->SetElementsKind(iter.first);
383         }
384         SetConstant(iter.second, hclass);
385     }
386 }
387 }  // namespace panda::ecmascript
388