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