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
55 namespace panda::ecmascript {
Init(JSThread * thread)56 void 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
CopySharedConstantsFrom(const GlobalEnvConstants * src)76 void 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
InitSharedStrings(ObjectFactory * factory)91 void 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
InitSharedRootsClasses(ObjectFactory * factory)99 void 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, StoreAOTHandler::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
InitSharedMiscellanious(JSThread * thread,ObjectFactory * factory)246 void 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 // Constant Symbols
269 SetConstant(ConstantIndex::PROTO_TRANS_ROOT_HCLASS_SYMBOL_INDEX, factory->NewSConstantPrivateSymbol());
270 // Emptys
271 auto vm = thread->GetEcmaVM();
272 SetConstant(ConstantIndex::EMPTY_STRING_OBJECT_INDEX, JSTaggedValue(EcmaStringAccessor::CreateEmptyString(vm)));
273 SetConstant(ConstantIndex::SINGLE_CHAR_TABLE_INDEX, SingleCharTable::CreateSingleCharTable(thread));
274 SetConstant(ConstantIndex::EMPTY_ARRAY_OBJECT_INDEX, factory->NewSEmptyArray());
275 SetConstant(ConstantIndex::EMPTY_MUTANT_ARRAY_OBJECT_INDEX, factory->NewSEmptyMutantArray());
276 SetConstant(ConstantIndex::EMPTY_SLAYOUT_INFO_OBJECT_INDEX, factory->NewSEmptyLayoutInfo());
277 SetConstant(ConstantIndex::UINT64_MAX_BIGINT_INDEX, BigInt::CreateUint64MaxBigInt(thread));
278 SetConstant(ConstantIndex::INT64_MAX_BIGINT_INDEX, BigInt::CreateInt64MaxBigInt(thread));
279 SetConstant(ConstantIndex::EMPTY_PROFILE_TYPE_INFO_CELL_INDEX, factory->NewSEmptyProfileTypeInfoCell());
280 // Native function method
281 SetConstant(ConstantIndex::NORMAL_FUNCTION_METHOD_INDEX,
282 factory->NewSEmptyNativeFunctionMethod(FunctionKind::NORMAL_FUNCTION));
283 SetConstant(ConstantIndex::GETTER_FUNCTION_METHOD_INDEX,
284 factory->NewSEmptyNativeFunctionMethod(FunctionKind::GETTER_FUNCTION));
285 SetConstant(ConstantIndex::SETTER_FUNCTION_METHOD_INDEX,
286 factory->NewSEmptyNativeFunctionMethod(FunctionKind::SETTER_FUNCTION));
287 SetConstant(ConstantIndex::ARROW_FUNCTION_METHOD_INDEX,
288 factory->NewSEmptyNativeFunctionMethod(FunctionKind::ARROW_FUNCTION));
289 SetConstant(ConstantIndex::ASYNC_ARROW_FUNCTION_METHOD_INDEX,
290 factory->NewSEmptyNativeFunctionMethod(FunctionKind::ASYNC_ARROW_FUNCTION));
291 SetConstant(ConstantIndex::CONCURRENT_FUNCTION_METHOD_INDEX,
292 factory->NewSEmptyNativeFunctionMethod(FunctionKind::CONCURRENT_FUNCTION));
293 SetConstant(ConstantIndex::ASYNC_FUNCTION_METHOD_INDEX,
294 factory->NewSEmptyNativeFunctionMethod(FunctionKind::ASYNC_FUNCTION));
295 SetConstant(ConstantIndex::BASE_CONSTRUCTOR_METHOD_INDEX,
296 factory->NewSEmptyNativeFunctionMethod(FunctionKind::BASE_CONSTRUCTOR));
297 SetConstant(ConstantIndex::CLASS_CONSTRUCTOR_METHOD_INDEX,
298 factory->NewSEmptyNativeFunctionMethod(FunctionKind::CLASS_CONSTRUCTOR));
299 SetConstant(ConstantIndex::BUILTIN_PROXY_CONSTRUCTOR_METHOD_INDEX,
300 factory->NewSEmptyNativeFunctionMethod(FunctionKind::BUILTIN_PROXY_CONSTRUCTOR));
301 SetConstant(ConstantIndex::BUILTIN_CONSTRUCTOR_METHOD_INDEX,
302 factory->NewSEmptyNativeFunctionMethod(FunctionKind::BUILTIN_CONSTRUCTOR));
303 SetConstant(ConstantIndex::DERIVED_CONSTRUCTOR_METHOD_INDEX,
304 factory->NewSEmptyNativeFunctionMethod(FunctionKind::DERIVED_CONSTRUCTOR));
305 SetConstant(ConstantIndex::GENERATOR_FUNCTION_METHOD_INDEX,
306 factory->NewSEmptyNativeFunctionMethod(FunctionKind::GENERATOR_FUNCTION));
307 SetConstant(ConstantIndex::NONE_FUNCTION_METHOD_INDEX,
308 factory->NewSEmptyNativeFunctionMethod(FunctionKind::NONE_FUNCTION));
309 SetConstant(ConstantIndex::ASYNC_GENERATOR_FUNCTION_METHOD_INDEX,
310 factory->NewSEmptyNativeFunctionMethod(FunctionKind::ASYNC_GENERATOR_FUNCTION));
311 }
312
InitRootsClassesPartOne(JSHClass * hClass,ObjectFactory * factory)313 void GlobalEnvConstants::InitRootsClassesPartOne(JSHClass *hClass, ObjectFactory *factory)
314 {
315 SetConstant(ConstantIndex::JS_API_LINKED_LIST_ITERATOR_CLASS_INDEX,
316 factory->NewEcmaHClass(hClass, JSAPILinkedListIterator::SIZE, JSType::JS_API_LINKED_LIST_ITERATOR));
317 SetConstant(ConstantIndex::JS_API_LIST_ITERATOR_CLASS_INDEX,
318 factory->NewEcmaHClass(hClass, JSAPIListIterator::SIZE, JSType::JS_API_LIST_ITERATOR));
319 SetConstant(ConstantIndex::JS_API_PLAIN_ARRAY_ITERATOR_CLASS_INDEX,
320 factory->NewEcmaHClass(hClass, JSAPIPlainArrayIterator::SIZE, JSType::JS_API_PLAIN_ARRAY_ITERATOR));
321 SetConstant(ConstantIndex::JS_API_QUEUE_ITERATOR_CLASS_INDEX,
322 factory->NewEcmaHClass(hClass, JSAPIQueueIterator::SIZE, JSType::JS_API_QUEUE_ITERATOR));
323 SetConstant(ConstantIndex::JS_API_STACK_ITERATOR_CLASS_INDEX,
324 factory->NewEcmaHClass(hClass, JSAPIStackIterator::SIZE, JSType::JS_API_STACK_ITERATOR));
325 SetConstant(ConstantIndex::JS_API_VECTOR_ITERATOR_CLASS_INDEX,
326 factory->NewEcmaHClass(hClass, JSAPIVectorIterator::SIZE, JSType::JS_API_VECTOR_ITERATOR));
327 SetConstant(ConstantIndex::JS_API_HASH_MAP_ITERATOR_CLASS_INDEX,
328 factory->NewEcmaHClass(hClass, JSAPIHashMapIterator::SIZE, JSType::JS_API_HASHMAP_ITERATOR));
329 SetConstant(ConstantIndex::JS_API_HASH_SET_ITERATOR_CLASS_INDEX,
330 factory->NewEcmaHClass(hClass, JSAPIHashSetIterator::SIZE, JSType::JS_API_HASHSET_ITERATOR));
331 SetConstant(ConstantIndex::JS_API_TREE_MAP_ITERATOR_CLASS_INDEX,
332 factory->NewEcmaHClass(hClass, JSAPITreeMapIterator::SIZE, JSType::JS_API_TREEMAP_ITERATOR));
333 SetConstant(ConstantIndex::JS_API_TREE_SET_ITERATOR_CLASS_INDEX,
334 factory->NewEcmaHClass(hClass, JSAPITreeSetIterator::SIZE, JSType::JS_API_TREESET_ITERATOR));
335 SetConstant(ConstantIndex::OBJECT_HCLASS_INDEX, factory->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT));
336 SetConstant(ConstantIndex::CLASS_PROTOTYPE_HCLASS_INDEX,
337 factory->CreateDefaultClassPrototypeHClass(hClass));
338 SetConstant(ConstantIndex::CLASS_CONSTRUCTOR_HCLASS_INDEX,
339 factory->CreateDefaultClassConstructorHClass(hClass));
340 SetConstant(ConstantIndex::JS_PROXY_ORDINARY_CLASS_INDEX,
341 factory->NewEcmaHClass(hClass, JSProxy::SIZE, JSType::JS_PROXY));
342 // napi_wrap need set NativePointer to object, so only mark the shared bit in shared JSNativePointer hclass.
343 SetConstant(ConstantIndex::JS_NATIVE_POINTER_CLASS_INDEX,
344 factory->NewEcmaReadOnlyHClass(hClass, JSNativePointer::SIZE, JSType::JS_NATIVE_POINTER));
345 }
346
InitRootsClassesPartTwo(JSHClass * hClass,ObjectFactory * factory)347 void GlobalEnvConstants::InitRootsClassesPartTwo(JSHClass *hClass, ObjectFactory *factory)
348 {
349 SetConstant(ConstantIndex::JS_REALM_CLASS_INDEX,
350 factory->NewEcmaHClass(hClass, JSRealm::SIZE, JSType::JS_REALM));
351 SetConstant(ConstantIndex::JS_REGEXP_ITERATOR_CLASS_INDEX,
352 factory->NewEcmaHClass(hClass, JSRegExpIterator::SIZE, JSType::JS_REG_EXP_ITERATOR));
353 SetConstant(ConstantIndex::JS_SET_ITERATOR_CLASS_INDEX,
354 factory->NewEcmaHClass(hClass, JSSetIterator::SIZE, JSType::JS_SET_ITERATOR, 0)); // 0: no inlined props
355 SetConstant(ConstantIndex::JS_SHARED_SET_ITERATOR_CLASS_INDEX,
356 factory->NewEcmaHClass(hClass, JSSharedSetIterator::SIZE,
357 JSType::JS_SHARED_SET_ITERATOR, 0)); // 0: no inlined props
358 SetConstant(ConstantIndex::JS_MAP_ITERATOR_CLASS_INDEX,
359 factory->NewEcmaHClass(hClass, JSMapIterator::SIZE, JSType::JS_MAP_ITERATOR, 0)); // 0: no inlined props
360 SetConstant(ConstantIndex::JS_SHARED_MAP_ITERATOR_CLASS_INDEX,
361 factory->NewEcmaHClass(hClass, JSSharedMapIterator::SIZE,
362 JSType::JS_SHARED_MAP_ITERATOR, 0)); // 0: no inlined props
363 SetConstant(ConstantIndex::JS_ARRAY_ITERATOR_CLASS_INDEX,
364 factory->NewEcmaHClass(hClass, JSArrayIterator::SIZE, JSType::JS_ARRAY_ITERATOR, 0));
365 SetConstant(ConstantIndex::JS_SHARED_ARRAY_ITERATOR_CLASS_INDEX,
366 factory->NewEcmaHClass(hClass, JSSharedArrayIterator::SIZE, JSType::JS_SHARED_ARRAY_ITERATOR, 0));
367 SetConstant(ConstantIndex::JS_API_ARRAYLIST_ITERATOR_CLASS_INDEX,
368 factory->NewEcmaHClass(hClass, JSAPIArrayListIterator::SIZE, JSType::JS_API_ARRAYLIST_ITERATOR));
369 SetConstant(ConstantIndex::JS_API_DEQUE_ITERATOR_CLASS_INDEX,
370 factory->NewEcmaHClass(hClass, JSAPIDequeIterator::SIZE, JSType::JS_API_DEQUE_ITERATOR));
371 SetConstant(ConstantIndex::JS_API_LIGHTWEIGHTMAP_ITERATOR_CLASS_INDEX,
372 factory->NewEcmaHClass(hClass, JSAPILightWeightMapIterator::SIZE,
373 JSType::JS_API_LIGHT_WEIGHT_MAP_ITERATOR));
374 SetConstant(ConstantIndex::JS_API_LIGHTWEIGHTSET_ITERATOR_CLASS_INDEX,
375 factory->NewEcmaHClass(hClass, JSAPILightWeightSetIterator::SIZE,
376 JSType::JS_API_LIGHT_WEIGHT_SET_ITERATOR));
377 SetConstant(ConstantIndex::JS_API_BITVECTOR_ITERATOR_CLASS_INDEX,
378 factory->NewEcmaHClass(hClass, JSAPIBitVectorIterator::SIZE, JSType::JS_API_BITVECTOR_ITERATOR));
379 }
380
InitRootsClasses(ObjectFactory * factory)381 void GlobalEnvConstants::InitRootsClasses(ObjectFactory *factory)
382 {
383 JSHClass *hClass = JSHClass::Cast(GetHClassClass().GetTaggedObject());
384
385 InitRootsClassesPartOne(hClass, factory);
386 InitRootsClassesPartTwo(hClass, factory);
387 // JS_PROXY_CALLABLE_CLASS_INDEX
388 JSHClass *jsProxyCallableClass = *factory->NewEcmaHClass(hClass, JSProxy::SIZE, JSType::JS_PROXY);
389 jsProxyCallableClass->SetCallable(true);
390 SetConstant(ConstantIndex::JS_PROXY_CALLABLE_CLASS_INDEX, JSTaggedValue(jsProxyCallableClass));
391 // JS_PROXY_CONSTRUCT_CLASS_INDEX
392 JSHClass *jsProxyConstructClass = *factory->NewEcmaHClass(hClass, JSProxy::SIZE, JSType::JS_PROXY);
393 jsProxyConstructClass->SetCallable(true);
394 jsProxyConstructClass->SetConstructor(true);
395 SetConstant(ConstantIndex::JS_PROXY_CONSTRUCT_CLASS_INDEX, JSTaggedValue(jsProxyConstructClass));
396 }
397
InitMiscellanious(JSThread * thread,ObjectFactory * factory)398 void GlobalEnvConstants::InitMiscellanious(JSThread *thread, ObjectFactory *factory)
399 {
400 SetConstant(ConstantIndex::EMPTY_LAYOUT_INFO_OBJECT_INDEX, factory->CreateLayoutInfo(0));
401 SetConstant(ConstantIndex::EMPTY_TAGGED_QUEUE_OBJECT_INDEX, factory->NewTaggedQueue(0));
402 SetConstant(ConstantIndex::DEFAULT_SUPERS_INDEX,
403 WeakVector::Create(thread, VTable::DEFAULT_SUPERS_CAPACITY, MemSpaceType::NON_MOVABLE));
404
405 // non ECMA standard jsapi containers iterators, init to Undefined first
406 InitJSAPIContainers();
407
408 InitGlobalCaches();
409 }
410
InitGlobalCaches()411 void GlobalEnvConstants::InitGlobalCaches()
412 {
413 SetConstant(ConstantIndex::CACHED_JSCOLLATOR_LOCALES_INDEX, JSTaggedValue::Undefined());
414 }
415
SetCachedLocales(JSTaggedValue value)416 void GlobalEnvConstants::SetCachedLocales(JSTaggedValue value)
417 {
418 JSTaggedValue cached = GetCachedJSCollatorLocales();
419 if (cached.IsUndefined()) {
420 SetConstant(ConstantIndex::CACHED_JSCOLLATOR_LOCALES_INDEX, value);
421 }
422 }
423
InitJSAPIContainers()424 void GlobalEnvConstants::InitJSAPIContainers()
425 {
426 for (size_t i = GetJSAPIContainersBegin(); i <= GetJSAPIContainersEnd(); i++) {
427 SetConstant(static_cast<ConstantIndex>(i), JSTaggedValue::Undefined());
428 }
429 }
430
InitSpecialForSnapshot()431 void GlobalEnvConstants::InitSpecialForSnapshot()
432 {
433 SetConstant(ConstantIndex::UNDEFINED_INDEX, JSTaggedValue::Undefined());
434 SetConstant(ConstantIndex::NULL_INDEX, JSTaggedValue::Null());
435 InitJSAPIContainers();
436 }
437
InitElementKindHClass(const JSThread * thread,JSHandle<JSHClass> originHClass)438 void GlobalEnvConstants::InitElementKindHClass(const JSThread *thread, JSHandle<JSHClass> originHClass)
439 {
440 {
441 JSHandle<JSHClass> hclass;
442 #define INIT_ARRAY_HCLASS_INDEX_ARRAYS(name) \
443 hclass = JSHClass::CloneWithElementsKind(thread, originHClass, ElementsKind::name, false); \
444 SetConstant(ConstantIndex::ELEMENT_##name##_HCLASS_INDEX, hclass);
445
446 ELEMENTS_KIND_INIT_HCLASS_LIST(INIT_ARRAY_HCLASS_INDEX_ARRAYS)
447 #undef INIT_ARRAY_HCLASS_INDEX_ARRAYS
448 }
449 SetConstant(ConstantIndex::ELEMENT_HOLE_TAGGED_HCLASS_INDEX, originHClass);
450 {
451 JSHandle<JSHClass> hclass;
452 #define INIT_ARRAY_HCLASS_INDEX_ARRAYS(name) \
453 hclass = JSHClass::CloneWithElementsKind(thread, originHClass, ElementsKind::name, true); \
454 SetConstant(ConstantIndex::ELEMENT_##name##_PROTO_HCLASS_INDEX, hclass);
455
456 ELEMENTS_KIND_INIT_HCLASS_LIST(INIT_ARRAY_HCLASS_INDEX_ARRAYS)
457 #undef INIT_ARRAY_HCLASS_INDEX_ARRAYS
458 }
459 }
460 } // namespace panda::ecmascript
461