• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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/containers/containers_private.h"
17 
18 #include "containers_arraylist.h"
19 #include "containers_deque.h"
20 #include "containers_hashmap.h"
21 #include "containers_hashset.h"
22 #include "containers_lightweightmap.h"
23 #include "containers_lightweightset.h"
24 #include "containers_linked_list.h"
25 #include "containers_list.h"
26 #include "containers_plainarray.h"
27 #include "containers_queue.h"
28 #include "containers_stack.h"
29 #include "containers_treemap.h"
30 #include "containers_treeset.h"
31 #include "containers_vector.h"
32 #include "ecmascript/global_env_constants.h"
33 #include "ecmascript/global_env.h"
34 #include "ecmascript/js_api/js_api_arraylist_iterator.h"
35 #include "ecmascript/js_api/js_api_arraylist.h"
36 #include "ecmascript/js_api/js_api_deque_iterator.h"
37 #include "ecmascript/js_api/js_api_deque.h"
38 #include "ecmascript/js_api/js_api_hashmap_iterator.h"
39 #include "ecmascript/js_api/js_api_hashmap.h"
40 #include "ecmascript/js_api/js_api_hashset_iterator.h"
41 #include "ecmascript/js_api/js_api_hashset.h"
42 #include "ecmascript/js_api/js_api_lightweightmap_iterator.h"
43 #include "ecmascript/js_api/js_api_lightweightmap.h"
44 #include "ecmascript/js_api/js_api_lightweightset_iterator.h"
45 #include "ecmascript/js_api/js_api_lightweightset.h"
46 #include "ecmascript/js_api/js_api_linked_list_iterator.h"
47 #include "ecmascript/js_api/js_api_linked_list.h"
48 #include "ecmascript/js_api/js_api_list_iterator.h"
49 #include "ecmascript/js_api/js_api_list.h"
50 #include "ecmascript/js_api/js_api_plain_array_iterator.h"
51 #include "ecmascript/js_api/js_api_plain_array.h"
52 #include "ecmascript/js_api/js_api_queue_iterator.h"
53 #include "ecmascript/js_api/js_api_queue.h"
54 #include "ecmascript/js_api/js_api_stack_iterator.h"
55 #include "ecmascript/js_api/js_api_stack.h"
56 #include "ecmascript/js_api/js_api_tree_map_iterator.h"
57 #include "ecmascript/js_api/js_api_tree_map.h"
58 #include "ecmascript/js_api/js_api_tree_set_iterator.h"
59 #include "ecmascript/js_api/js_api_tree_set.h"
60 #include "ecmascript/js_api/js_api_vector_iterator.h"
61 #include "ecmascript/js_api/js_api_vector.h"
62 #include "ecmascript/js_function.h"
63 #include "ecmascript/js_iterator.h"
64 #include "ecmascript/object_fast_operator-inl.h"
65 
66 namespace panda::ecmascript::containers {
Load(EcmaRuntimeCallInfo * msg)67 JSTaggedValue ContainersPrivate::Load(EcmaRuntimeCallInfo *msg)
68 {
69     ASSERT(msg != nullptr);
70     JSThread *thread = msg->GetThread();
71     [[maybe_unused]] EcmaHandleScope handleScope(thread);
72     JSHandle<JSTaggedValue> argv = GetCallArg(msg, 0);
73     JSHandle<JSObject> thisValue(GetThis(msg));
74 
75     uint32_t tag = 0;
76     if (!JSTaggedValue::ToElementIndex(argv.GetTaggedValue(), &tag) || tag >= ContainerTag::END) {
77         THROW_TYPE_ERROR_AND_RETURN(thread, "Incorrect input parameters", JSTaggedValue::Exception());
78     }
79 
80     // Lazy set an undefinedIteratorResult to global constants
81     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
82     JSHandle<JSTaggedValue> undefinedHandle = globalConst->GetHandledUndefined();
83     JSHandle<JSObject> undefinedIteratorResult = JSIterator::CreateIterResultObject(thread, undefinedHandle, true);
84     globalConst->SetConstant(ConstantIndex::UNDEFINED_INTERATOR_RESULT_INDEX, undefinedIteratorResult.GetTaggedValue());
85 
86     JSTaggedValue res = JSTaggedValue::Undefined();
87     switch (tag) {
88         case ContainerTag::ArrayList: {
89             res = InitializeContainer(thread, thisValue, InitializeArrayList, "ArrayListConstructor");
90             break;
91         }
92         case ContainerTag::Deque: {
93             res = InitializeContainer(thread, thisValue, InitializeDeque, "DequeConstructor");
94             break;
95         }
96         case ContainerTag::LightWeightMap: {
97             res = InitializeContainer(thread, thisValue, InitializeLightWeightMap, "LightWeightMapConstructor");
98             break;
99         }
100         case ContainerTag::LightWeightSet: {
101             res = InitializeContainer(thread, thisValue, InitializeLightWeightSet, "LightWeightSetConstructor");
102             break;
103         }
104         case ContainerTag::PlainArray: {
105             res = InitializeContainer(thread, thisValue, InitializePlainArray, "PlainArrayConstructor");
106             break;
107         }
108         case ContainerTag::Queue: {
109             res = InitializeContainer(thread, thisValue, InitializeQueue, "QueueConstructor");
110             break;
111         }
112         case ContainerTag::Stack: {
113             res = InitializeContainer(thread, thisValue, InitializeStack, "StackConstructor");
114             break;
115         }
116         case ContainerTag::TreeMap: {
117             res = InitializeContainer(thread, thisValue, InitializeTreeMap, "TreeMapConstructor");
118             break;
119         }
120         case ContainerTag::TreeSet: {
121             res = InitializeContainer(thread, thisValue, InitializeTreeSet, "TreeSetConstructor");
122             break;
123         }
124         case ContainerTag::Vector: {
125             res = InitializeContainer(thread, thisValue, InitializeVector, "VectorConstructor");
126             break;
127         }
128         case ContainerTag::List: {
129             res = InitializeContainer(thread, thisValue, InitializeList, "ListConstructor");
130             break;
131         }
132         case ContainerTag::LinkedList: {
133             res = InitializeContainer(thread, thisValue, InitializeLinkedList, "LinkedListConstructor");
134             break;
135         }
136         case ContainerTag::HashMap: {
137             res = InitializeContainer(thread, thisValue, InitializeHashMap, "HashMapConstructor");
138             break;
139         }
140         case ContainerTag::HashSet: {
141             res = InitializeContainer(thread, thisValue, InitializeHashSet, "HashSetConstructor");
142             break;
143         }
144         case ContainerTag::END:
145             break;
146         default:
147             LOG_ECMA(FATAL) << "this branch is unreachable";
148             UNREACHABLE();
149     }
150 
151     return res;
152 }
153 
InitializeContainer(JSThread * thread,const JSHandle<JSObject> & obj,InitializeFunction func,const char * name)154 JSTaggedValue ContainersPrivate::InitializeContainer(JSThread *thread, const JSHandle<JSObject> &obj,
155                                                      InitializeFunction func, const char *name)
156 {
157     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
158     JSHandle<JSTaggedValue> key(factory->NewFromASCII(name));
159     JSTaggedValue value =
160         ObjectFastOperator::GetPropertyByName<true>(thread, obj.GetTaggedValue(), key.GetTaggedValue());
161     if (!value.IsUndefined()) {
162         return value;
163     }
164     JSHandle<JSTaggedValue> map = func(thread);
165     SetFrozenConstructor(thread, obj, name, map);
166     return map.GetTaggedValue();
167 }
168 
NewContainerConstructor(JSThread * thread,const JSHandle<JSObject> & prototype,EcmaEntrypoint ctorFunc,const char * name,int length)169 JSHandle<JSFunction> ContainersPrivate::NewContainerConstructor(JSThread *thread, const JSHandle<JSObject> &prototype,
170                                                                 EcmaEntrypoint ctorFunc, const char *name, int length)
171 {
172     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
173     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
174     JSHandle<JSFunction> ctor =
175         factory->NewJSFunction(env, reinterpret_cast<void *>(ctorFunc), FunctionKind::BUILTIN_CONSTRUCTOR);
176 
177     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
178     JSFunction::SetFunctionLength(thread, ctor, JSTaggedValue(length));
179     JSHandle<JSTaggedValue> nameString(factory->NewFromASCII(name));
180     JSFunction::SetFunctionName(thread, JSHandle<JSFunctionBase>(ctor), nameString,
181                                 globalConst->GetHandledUndefined());
182     JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
183     PropertyDescriptor descriptor1(thread, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true);
184     JSObject::DefineOwnProperty(thread, prototype, constructorKey, descriptor1);
185 
186     /* set "prototype" in constructor */
187     ctor->SetFunctionPrototype(thread, prototype.GetTaggedValue());
188 
189     return ctor;
190 }
191 
SetFrozenFunction(JSThread * thread,const JSHandle<JSObject> & obj,const char * key,EcmaEntrypoint func,int length,kungfu::BuiltinsStubCSigns::ID builtinId)192 void ContainersPrivate::SetFrozenFunction(JSThread *thread, const JSHandle<JSObject> &obj, const char *key,
193                                           EcmaEntrypoint func, int length, kungfu::BuiltinsStubCSigns::ID builtinId)
194 {
195     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
196     JSHandle<JSTaggedValue> keyString(factory->NewFromASCII(key));
197     JSHandle<JSFunction> function = NewFunction(thread, keyString, func, length, builtinId);
198     PropertyDescriptor descriptor(thread, JSHandle<JSTaggedValue>(function), false, false, false);
199     JSObject::DefineOwnProperty(thread, obj, keyString, descriptor);
200 }
201 
SetFrozenConstructor(JSThread * thread,const JSHandle<JSObject> & obj,const char * keyChar,JSHandle<JSTaggedValue> & value)202 void ContainersPrivate::SetFrozenConstructor(JSThread *thread, const JSHandle<JSObject> &obj, const char *keyChar,
203                                              JSHandle<JSTaggedValue> &value)
204 {
205     JSObject::PreventExtensions(thread, JSHandle<JSObject>::Cast(value));
206     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
207     JSHandle<JSTaggedValue> key(factory->NewFromASCII(keyChar));
208     PropertyDescriptor descriptor(thread, value, false, false, false);
209     JSObject::DefineOwnProperty(thread, obj, key, descriptor);
210 }
211 
NewFunction(JSThread * thread,const JSHandle<JSTaggedValue> & key,EcmaEntrypoint func,int length,kungfu::BuiltinsStubCSigns::ID builtinId)212 JSHandle<JSFunction> ContainersPrivate::NewFunction(JSThread *thread, const JSHandle<JSTaggedValue> &key,
213                                                     EcmaEntrypoint func, int length,
214                                                     kungfu::BuiltinsStubCSigns::ID builtinId)
215 {
216     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
217     JSHandle<JSFunction> function =
218         factory->NewJSFunction(thread->GetEcmaVM()->GetGlobalEnv(), reinterpret_cast<void *>(func),
219                                FunctionKind::NORMAL_FUNCTION, builtinId);
220     JSFunction::SetFunctionLength(thread, function, JSTaggedValue(length));
221     JSHandle<JSFunctionBase> baseFunction(function);
222     JSFunction::SetFunctionName(thread, baseFunction, key, thread->GlobalConstants()->GetHandledUndefined());
223     return function;
224 }
225 
CreateGetter(JSThread * thread,EcmaEntrypoint func,const char * name,int length)226 JSHandle<JSTaggedValue> ContainersPrivate::CreateGetter(JSThread *thread, EcmaEntrypoint func, const char *name,
227                                                         int length)
228 {
229     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
230     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
231     JSHandle<JSFunction> function = factory->NewJSFunction(env, reinterpret_cast<void *>(func));
232     JSFunction::SetFunctionLength(thread, function, JSTaggedValue(length));
233     JSHandle<JSTaggedValue> funcName(factory->NewFromASCII(name));
234     JSHandle<JSTaggedValue> prefix = thread->GlobalConstants()->GetHandledGetString();
235     JSFunction::SetFunctionName(thread, JSHandle<JSFunctionBase>(function), funcName, prefix);
236     return JSHandle<JSTaggedValue>(function);
237 }
238 
SetGetter(JSThread * thread,const JSHandle<JSObject> & obj,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & getter)239 void ContainersPrivate::SetGetter(JSThread *thread, const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &key,
240                                   const JSHandle<JSTaggedValue> &getter)
241 {
242     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
243     JSHandle<AccessorData> accessor = factory->NewAccessorData();
244     accessor->SetGetter(thread, getter);
245     PropertyAttributes attr = PropertyAttributes::DefaultAccessor(false, false, false);
246     JSObject::AddAccessor(thread, JSHandle<JSTaggedValue>::Cast(obj), key, accessor, attr);
247 }
248 
SetFunctionAtSymbol(JSThread * thread,const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & obj,const JSHandle<JSTaggedValue> & symbol,const char * name,EcmaEntrypoint func,int length)249 void ContainersPrivate::SetFunctionAtSymbol(JSThread *thread, const JSHandle<GlobalEnv> &env,
250                                             const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &symbol,
251                                             const char *name, EcmaEntrypoint func, int length)
252 {
253     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
254     JSHandle<JSFunction> function = factory->NewJSFunction(env, reinterpret_cast<void *>(func));
255     JSFunction::SetFunctionLength(thread, function, JSTaggedValue(length));
256     JSHandle<JSTaggedValue> nameString(factory->NewFromASCII(name));
257     JSHandle<JSFunctionBase> baseFunction(function);
258     JSFunction::SetFunctionName(thread, baseFunction, nameString, thread->GlobalConstants()->GetHandledUndefined());
259     PropertyDescriptor descriptor(thread, JSHandle<JSTaggedValue>::Cast(function), false, false, false);
260     JSObject::DefineOwnProperty(thread, obj, symbol, descriptor);
261 }
262 
SetStringTagSymbol(JSThread * thread,const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & obj,const char * key)263 void ContainersPrivate::SetStringTagSymbol(JSThread *thread, const JSHandle<GlobalEnv> &env,
264                                            const JSHandle<JSObject> &obj, const char *key)
265 {
266     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
267     JSHandle<JSTaggedValue> tag(factory->NewFromASCII(key));
268     JSHandle<JSTaggedValue> symbol = env->GetToStringTagSymbol();
269     PropertyDescriptor desc(thread, tag, false, false, false);
270     JSObject::DefineOwnProperty(thread, obj, symbol, desc);
271 }
272 
InitializeArrayList(JSThread * thread)273 JSHandle<JSTaggedValue> ContainersPrivate::InitializeArrayList(JSThread *thread)
274 {
275     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
276     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
277     // ArrayList.prototype
278     JSHandle<JSObject> prototype = factory->NewEmptyJSObject();
279     JSHandle<JSTaggedValue> arrayListFuncPrototypeValue(prototype);
280     // ArrayList.prototype_or_hclass
281     JSHandle<JSHClass> arrayListInstanceClass =
282         factory->NewEcmaHClass(JSAPIArrayList::SIZE, JSType::JS_API_ARRAY_LIST, arrayListFuncPrototypeValue);
283     // ArrayList() = new Function()
284     JSHandle<JSTaggedValue> arrayListFunction(NewContainerConstructor(
285         thread, prototype, ContainersArrayList::ArrayListConstructor, "ArrayList", FuncLength::ZERO));
286     JSHandle<JSFunction>::Cast(arrayListFunction)->SetFunctionPrototype(thread,
287                                                                         arrayListInstanceClass.GetTaggedValue());
288 
289     // "constructor" property on the prototype
290     JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
291     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(prototype), constructorKey, arrayListFunction);
292 
293     // ArrayList.prototype
294     SetFrozenFunction(thread, prototype, "add", ContainersArrayList::Add, FuncLength::ONE);
295     SetFrozenFunction(thread, prototype, "insert", ContainersArrayList::Insert, FuncLength::TWO);
296     SetFrozenFunction(thread, prototype, "clear", ContainersArrayList::Clear, FuncLength::ZERO);
297     SetFrozenFunction(thread, prototype, "clone", ContainersArrayList::Clone, FuncLength::ZERO);
298     SetFrozenFunction(thread, prototype, "has", ContainersArrayList::Has, FuncLength::ONE);
299     SetFrozenFunction(thread, prototype, "getCapacity", ContainersArrayList::GetCapacity, FuncLength::ZERO);
300     SetFrozenFunction(thread, prototype, "increaseCapacityTo",
301                       ContainersArrayList::IncreaseCapacityTo, FuncLength::ONE);
302     SetFrozenFunction(thread, prototype, "trimToCurrentLength",
303                       ContainersArrayList::TrimToCurrentLength, FuncLength::ZERO);
304     SetFrozenFunction(thread, prototype, "getIndexOf", ContainersArrayList::GetIndexOf, FuncLength::ONE);
305     SetFrozenFunction(thread, prototype, "isEmpty", ContainersArrayList::IsEmpty, FuncLength::ZERO);
306     SetFrozenFunction(thread, prototype, "getLastIndexOf", ContainersArrayList::GetLastIndexOf, FuncLength::ONE);
307     SetFrozenFunction(thread, prototype, "removeByIndex", ContainersArrayList::RemoveByIndex, FuncLength::ONE);
308     SetFrozenFunction(thread, prototype, "remove", ContainersArrayList::Remove, FuncLength::ONE);
309     SetFrozenFunction(thread, prototype, "removeByRange", ContainersArrayList::RemoveByRange, FuncLength::TWO);
310     SetFrozenFunction(thread, prototype, "replaceAllElements", ContainersArrayList::ReplaceAllElements,
311         FuncLength::TWO, BUILTINS_STUB_ID(ArrayListReplaceAllElements));
312     SetFrozenFunction(thread, prototype, "sort", ContainersArrayList::Sort, FuncLength::ONE);
313     SetFrozenFunction(thread, prototype, "subArrayList", ContainersArrayList::SubArrayList, FuncLength::TWO);
314     SetFrozenFunction(thread, prototype, "convertToArray", ContainersArrayList::ConvertToArray, FuncLength::ZERO);
315     SetFrozenFunction(thread, prototype, "forEach", ContainersArrayList::ForEach, FuncLength::TWO,
316         BUILTINS_STUB_ID(ArrayListForEach));
317 
318     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
319     SetStringTagSymbol(thread, env, prototype, "ArrayList");
320 
321     JSHandle<JSTaggedValue> lengthGetter = CreateGetter(thread, ContainersArrayList::GetSize, "length",
322                                                         FuncLength::ZERO);
323     JSHandle<JSTaggedValue> lengthKey(thread, globalConst->GetLengthString());
324     SetGetter(thread, prototype, lengthKey, lengthGetter);
325 
326     SetFunctionAtSymbol(thread, env, prototype, env->GetIteratorSymbol(), "[Symbol.iterator]",
327                         ContainersArrayList::GetIteratorObj, FuncLength::ONE);
328     ContainersPrivate::InitializeArrayListIterator(thread, env, globalConst);
329     globalConst->SetConstant(ConstantIndex::ARRAYLIST_FUNCTION_INDEX, arrayListFunction.GetTaggedValue());
330     return arrayListFunction;
331 }
332 
InitializeArrayListIterator(JSThread * thread,const JSHandle<GlobalEnv> & env,GlobalEnvConstants * globalConst)333 void ContainersPrivate::InitializeArrayListIterator(JSThread *thread, const JSHandle<GlobalEnv> &env,
334                                                     GlobalEnvConstants *globalConst)
335 {
336     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
337     // Iterator.hclass
338     JSHandle<JSHClass> iteratorFuncHClass =
339         factory->NewEcmaHClass(JSObject::SIZE, JSType::JS_ITERATOR, env->GetIteratorPrototype());
340     // ArrayListIterator.prototype
341     JSHandle<JSObject> arrayListIteratorPrototype(factory->NewJSObject(iteratorFuncHClass));
342     // Iterator.prototype.next()
343     SetFrozenFunction(thread, arrayListIteratorPrototype, "next", JSAPIArrayListIterator::Next, FuncLength::ONE);
344     SetStringTagSymbol(thread, env, arrayListIteratorPrototype, "ArrayList Iterator");
345     globalConst->SetConstant(ConstantIndex::ARRAYLIST_ITERATOR_PROTOTYPE_INDEX,
346                              arrayListIteratorPrototype.GetTaggedValue());
347 }
348 
InitializeLightWeightMap(JSThread * thread)349 JSHandle<JSTaggedValue> ContainersPrivate::InitializeLightWeightMap(JSThread *thread)
350 {
351     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
352     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
353     JSHandle<JSObject> funcPrototype = factory->NewEmptyJSObject();
354     JSHandle<JSTaggedValue> mapFuncPrototypeValue(funcPrototype);
355     JSHandle<JSHClass> lightWeightMapInstanceClass =
356         factory->NewEcmaHClass(JSAPILightWeightMap::SIZE, JSType::JS_API_LIGHT_WEIGHT_MAP, mapFuncPrototypeValue);
357     JSHandle<JSTaggedValue> lightWeightMapFunction(NewContainerConstructor(
358         thread, funcPrototype, ContainersLightWeightMap::LightWeightMapConstructor, "LightWeightMap",
359         FuncLength::ZERO));
360     JSHandle<JSFunction>::Cast(lightWeightMapFunction)->
361         SetFunctionPrototype(thread, lightWeightMapInstanceClass.GetTaggedValue());
362 
363     // "constructor" property on the prototype
364     JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
365     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(funcPrototype), constructorKey, lightWeightMapFunction);
366 
367     // LightWeightMap.prototype.add()
368     SetFrozenFunction(thread, funcPrototype, "hasAll", ContainersLightWeightMap::HasAll, FuncLength::ONE);
369     SetFrozenFunction(thread, funcPrototype, "hasKey", ContainersLightWeightMap::HasKey, FuncLength::ONE);
370     SetFrozenFunction(thread, funcPrototype, "hasValue", ContainersLightWeightMap::HasValue, FuncLength::ONE);
371     SetFrozenFunction(thread, funcPrototype, "increaseCapacityTo", ContainersLightWeightMap::IncreaseCapacityTo,
372                       FuncLength::ONE);
373     SetFrozenFunction(thread, funcPrototype, "entries", ContainersLightWeightMap::Entries, FuncLength::ONE);
374     SetFrozenFunction(thread, funcPrototype, "get", ContainersLightWeightMap::Get, FuncLength::ONE);
375     SetFrozenFunction(thread, funcPrototype, "getIndexOfKey", ContainersLightWeightMap::GetIndexOfKey, FuncLength::ONE);
376     SetFrozenFunction(thread, funcPrototype, "getIndexOfValue", ContainersLightWeightMap::GetIndexOfValue,
377                       FuncLength::ONE);
378     SetFrozenFunction(thread, funcPrototype, "isEmpty", ContainersLightWeightMap::IsEmpty, FuncLength::ONE);
379     SetFrozenFunction(thread, funcPrototype, "getKeyAt", ContainersLightWeightMap::GetKeyAt, FuncLength::ONE);
380     SetFrozenFunction(thread, funcPrototype, "keys", ContainersLightWeightMap::Keys, FuncLength::ONE);
381     SetFrozenFunction(thread, funcPrototype, "setAll", ContainersLightWeightMap::SetAll, FuncLength::ONE);
382     SetFrozenFunction(thread, funcPrototype, "set", ContainersLightWeightMap::Set, FuncLength::ONE);
383     SetFrozenFunction(thread, funcPrototype, "remove", ContainersLightWeightMap::Remove, FuncLength::ONE);
384     SetFrozenFunction(thread, funcPrototype, "removeAt", ContainersLightWeightMap::RemoveAt, FuncLength::ONE);
385     SetFrozenFunction(thread, funcPrototype, "clear", ContainersLightWeightMap::Clear, FuncLength::ONE);
386     SetFrozenFunction(thread, funcPrototype, "setValueAt", ContainersLightWeightMap::SetValueAt, FuncLength::ONE);
387     SetFrozenFunction(thread, funcPrototype, "forEach", ContainersLightWeightMap::ForEach, FuncLength::ONE,
388                       BUILTINS_STUB_ID(LightWeightMapForEach));
389     SetFrozenFunction(thread, funcPrototype, "toString", ContainersLightWeightMap::ToString, FuncLength::ONE);
390     SetFrozenFunction(thread, funcPrototype, "getValueAt", ContainersLightWeightMap::GetValueAt, FuncLength::ONE);
391     SetFrozenFunction(thread, funcPrototype, "values", ContainersLightWeightMap::Values, FuncLength::ONE);
392 
393     JSHandle<JSTaggedValue> lengthGetter = CreateGetter(thread, ContainersLightWeightMap::Length, "length",
394                                                         FuncLength::ZERO);
395     JSHandle<JSTaggedValue> lengthKey(factory->NewFromASCII("length"));
396     SetGetter(thread, funcPrototype, lengthKey, lengthGetter);
397 
398     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
399     SetFunctionAtSymbol(thread, env, funcPrototype, env->GetIteratorSymbol(), "[Symbol.iterator]",
400                         ContainersLightWeightMap::Entries, FuncLength::ONE);
401 
402     ContainersPrivate::InitializeLightWeightMapIterator(thread);
403     return lightWeightMapFunction;
404 }
405 
InitializeLightWeightMapIterator(JSThread * thread)406 void ContainersPrivate::InitializeLightWeightMapIterator(JSThread *thread)
407 {
408     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
409     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
410     JSHandle<JSHClass> iteratorClass = factory->NewEcmaHClass(JSObject::SIZE, JSType::JS_ITERATOR,
411         env->GetIteratorPrototype());
412     JSHandle<JSObject> lightWeightMapIteratorPrototype(factory->NewJSObject(iteratorClass));
413     SetFrozenFunction(thread, lightWeightMapIteratorPrototype, "next", JSAPILightWeightMapIterator::Next,
414                       FuncLength::ONE);
415     SetStringTagSymbol(thread, env, lightWeightMapIteratorPrototype, "LightWeightMap Iterator");
416     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
417     globalConst->SetConstant(ConstantIndex::LIGHTWEIGHTMAP_ITERATOR_PROTOTYPE_INDEX,
418                              lightWeightMapIteratorPrototype.GetTaggedValue());
419 }
420 
InitializeLightWeightSet(JSThread * thread)421 JSHandle<JSTaggedValue> ContainersPrivate::InitializeLightWeightSet(JSThread *thread)
422 {
423     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
424     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
425     // LightWeightSet.prototype
426     JSHandle<JSObject> funcPrototype = factory->NewEmptyJSObject();
427     JSHandle<JSTaggedValue> funcPrototypeValue(funcPrototype);
428     // LightWeightSet.prototype_or_hclass
429     JSHandle<JSHClass> lightweightSetInstanceClass =
430         factory->NewEcmaHClass(JSAPILightWeightSet::SIZE, JSType::JS_API_LIGHT_WEIGHT_SET, funcPrototypeValue);
431     JSHandle<JSTaggedValue> lightweightSetFunction(
432         NewContainerConstructor(thread, funcPrototype, ContainersLightWeightSet::LightWeightSetConstructor,
433                                 "LightWeightSet", FuncLength::ZERO));
434     JSHandle<JSFunction>::Cast(lightweightSetFunction)->SetFunctionPrototype(thread, lightweightSetInstanceClass.
435         GetTaggedValue());
436     // "constructor" property on the prototype
437     JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
438     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(funcPrototype), constructorKey, lightweightSetFunction);
439     SetFrozenFunction(thread, funcPrototype, "add", ContainersLightWeightSet::Add, FuncLength::ONE);
440     SetFrozenFunction(thread, funcPrototype, "addAll", ContainersLightWeightSet::AddAll, FuncLength::ONE);
441     SetFrozenFunction(thread, funcPrototype, "isEmpty", ContainersLightWeightSet::IsEmpty, FuncLength::ONE);
442     SetFrozenFunction(thread, funcPrototype, "getValueAt", ContainersLightWeightSet::GetValueAt, FuncLength::ONE);
443     SetFrozenFunction(thread, funcPrototype, "hasAll", ContainersLightWeightSet::HasAll, FuncLength::ONE);
444     SetFrozenFunction(thread, funcPrototype, "has", ContainersLightWeightSet::Has, FuncLength::ONE);
445     SetFrozenFunction(thread, funcPrototype, "equal", ContainersLightWeightSet::Equal, FuncLength::ONE);
446     SetFrozenFunction(thread, funcPrototype, "increaseCapacityTo",
447                       ContainersLightWeightSet::IncreaseCapacityTo, FuncLength::ONE);
448     SetFrozenFunction(thread, funcPrototype, "forEach", ContainersLightWeightSet::ForEach, FuncLength::ONE,
449                       BUILTINS_STUB_ID(LightWeightSetForEach));
450     SetFrozenFunction(thread, funcPrototype, "getIndexOf", ContainersLightWeightSet::GetIndexOf, FuncLength::ONE);
451     SetFrozenFunction(thread, funcPrototype, "remove", ContainersLightWeightSet::Remove, FuncLength::ZERO);
452     SetFrozenFunction(thread, funcPrototype, "removeAt", ContainersLightWeightSet::RemoveAt, FuncLength::ZERO);
453     SetFrozenFunction(thread, funcPrototype, "clear", ContainersLightWeightSet::Clear, FuncLength::ONE);
454     SetFrozenFunction(thread, funcPrototype, "toString", ContainersLightWeightSet::ToString, FuncLength::ZERO);
455     SetFrozenFunction(thread, funcPrototype, "toArray", ContainersLightWeightSet::ToArray, FuncLength::ONE);
456     SetFrozenFunction(thread, funcPrototype, "values", ContainersLightWeightSet::Values, FuncLength::ONE);
457     SetFrozenFunction(thread, funcPrototype, "entries", ContainersLightWeightSet::Entries, FuncLength::ZERO);
458     JSHandle<JSTaggedValue> lengthGetter =
459         CreateGetter(thread, ContainersLightWeightSet::GetSize, "length", FuncLength::ZERO);
460 
461     JSHandle<JSTaggedValue> lengthKey(factory->NewFromASCII("length"));
462     SetGetter(thread, funcPrototype, lengthKey, lengthGetter);
463     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
464     SetFunctionAtSymbol(thread, env, funcPrototype, env->GetIteratorSymbol(), "[Symbol.iterator]",
465                         ContainersLightWeightSet::GetIteratorObj, FuncLength::ONE);
466 
467     InitializeLightWeightSetIterator(thread);
468     return lightweightSetFunction;
469 }
470 
InitializeLightWeightSetIterator(JSThread * thread)471 void ContainersPrivate::InitializeLightWeightSetIterator(JSThread *thread)
472 {
473     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
474     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
475     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
476     JSHandle<JSHClass> iteratorClass =
477         JSHandle<JSHClass>(thread, globalConst->GetHandledJSAPIIteratorFuncHClass().GetObject<JSHClass>());
478     JSHandle<JSObject> lightWeightSetIteratorPrototype(factory->NewJSObject(iteratorClass));
479     SetFrozenFunction(
480         thread, lightWeightSetIteratorPrototype, "next", JSAPILightWeightSetIterator::Next, FuncLength::ONE);
481     SetStringTagSymbol(thread, env, lightWeightSetIteratorPrototype, "LightWeightSet Iterator");
482     globalConst->SetConstant(ConstantIndex::LIGHTWEIGHTSET_ITERATOR_PROTOTYPE_INDEX,
483                              lightWeightSetIteratorPrototype.GetTaggedValue());
484 }
485 
InitializeTreeMap(JSThread * thread)486 JSHandle<JSTaggedValue> ContainersPrivate::InitializeTreeMap(JSThread *thread)
487 {
488     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
489     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
490     // TreeMap.prototype
491     JSHandle<JSObject> mapFuncPrototype = factory->NewEmptyJSObject();
492     JSHandle<JSTaggedValue> mapFuncPrototypeValue(mapFuncPrototype);
493     // TreeMap.prototype_or_hclass
494     JSHandle<JSHClass> mapInstanceClass =
495         factory->NewEcmaHClass(JSAPITreeMap::SIZE, JSType::JS_API_TREE_MAP, mapFuncPrototypeValue);
496     // TreeMap() = new Function()
497     JSHandle<JSTaggedValue> mapFunction(NewContainerConstructor(
498         thread, mapFuncPrototype, ContainersTreeMap::TreeMapConstructor, "TreeMap", FuncLength::ZERO));
499     JSHandle<JSFunction>::Cast(mapFunction)->SetFunctionPrototype(thread, mapInstanceClass.GetTaggedValue());
500 
501     // "constructor" property on the prototype
502     JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
503     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(mapFuncPrototype), constructorKey, mapFunction);
504 
505     // TreeMap.prototype
506     SetFrozenFunction(thread, mapFuncPrototype, "set", ContainersTreeMap::Set, FuncLength::TWO);
507     SetFrozenFunction(thread, mapFuncPrototype, "get", ContainersTreeMap::Get, FuncLength::ONE);
508     SetFrozenFunction(thread, mapFuncPrototype, "remove", ContainersTreeMap::Remove, FuncLength::ONE);
509     SetFrozenFunction(thread, mapFuncPrototype, "hasKey", ContainersTreeMap::HasKey, FuncLength::ONE);
510     SetFrozenFunction(thread, mapFuncPrototype, "hasValue", ContainersTreeMap::HasValue, FuncLength::ONE);
511     SetFrozenFunction(thread, mapFuncPrototype, "getFirstKey", ContainersTreeMap::GetFirstKey, FuncLength::ZERO);
512     SetFrozenFunction(thread, mapFuncPrototype, "getLastKey", ContainersTreeMap::GetLastKey, FuncLength::ZERO);
513     SetFrozenFunction(thread, mapFuncPrototype, "setAll", ContainersTreeMap::SetAll, FuncLength::ONE);
514     SetFrozenFunction(thread, mapFuncPrototype, "clear", ContainersTreeMap::Clear, FuncLength::ZERO);
515     SetFrozenFunction(thread, mapFuncPrototype, "getLowerKey", ContainersTreeMap::GetLowerKey, FuncLength::ONE);
516     SetFrozenFunction(thread, mapFuncPrototype, "getHigherKey", ContainersTreeMap::GetHigherKey, FuncLength::ONE);
517     SetFrozenFunction(thread, mapFuncPrototype, "keys", ContainersTreeMap::Keys, FuncLength::ZERO);
518     SetFrozenFunction(thread, mapFuncPrototype, "values", ContainersTreeMap::Values, FuncLength::ZERO);
519     SetFrozenFunction(thread, mapFuncPrototype, "replace", ContainersTreeMap::Replace, FuncLength::TWO);
520     SetFrozenFunction(thread, mapFuncPrototype, "forEach", ContainersTreeMap::ForEach, FuncLength::ONE);
521     SetFrozenFunction(thread, mapFuncPrototype, "entries", ContainersTreeMap::Entries, FuncLength::ZERO);
522     SetFrozenFunction(thread, mapFuncPrototype, "isEmpty", ContainersTreeMap::IsEmpty, FuncLength::ZERO);
523 
524     // @@ToStringTag
525     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
526     SetStringTagSymbol(thread, env, mapFuncPrototype, "TreeMap");
527     // %TreeMapPrototype% [ @@iterator ]
528     JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
529     JSHandle<JSTaggedValue> entries = globalConst->GetHandledEntriesString();
530     JSHandle<JSTaggedValue> entriesFunc =
531         JSObject::GetMethod(thread, JSHandle<JSTaggedValue>::Cast(mapFuncPrototype), entries);
532     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
533     PropertyDescriptor descriptor(thread, entriesFunc, false, false, false);
534     JSObject::DefineOwnProperty(thread, mapFuncPrototype, iteratorSymbol, descriptor);
535     // length
536     JSHandle<JSTaggedValue> lengthGetter =
537         CreateGetter(thread, ContainersTreeMap::GetLength, "length", FuncLength::ZERO);
538     JSHandle<JSTaggedValue> lengthKey(thread, globalConst->GetLengthString());
539     SetGetter(thread, mapFuncPrototype, lengthKey, lengthGetter);
540 
541     InitializeTreeMapIterator(thread);
542     return mapFunction;
543 }
544 
InitializeTreeMapIterator(JSThread * thread)545 void ContainersPrivate::InitializeTreeMapIterator(JSThread *thread)
546 {
547     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
548     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
549     // Iterator.hclass
550     JSHandle<JSHClass> iteratorClass =
551         factory->NewEcmaHClass(JSObject::SIZE, JSType::JS_ITERATOR, env->GetIteratorPrototype());
552 
553     // TreeMapIterator.prototype
554     JSHandle<JSObject> mapIteratorPrototype(factory->NewJSObject(iteratorClass));
555     // TreeIterator.prototype.next()
556     SetFrozenFunction(thread, mapIteratorPrototype, "next", JSAPITreeMapIterator::Next, FuncLength::ZERO);
557     SetStringTagSymbol(thread, env, mapIteratorPrototype, "TreeMap Iterator");
558     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
559     globalConst->SetConstant(ConstantIndex::TREEMAP_ITERATOR_PROTOTYPE_INDEX, mapIteratorPrototype.GetTaggedValue());
560 }
561 
InitializeTreeSet(JSThread * thread)562 JSHandle<JSTaggedValue> ContainersPrivate::InitializeTreeSet(JSThread *thread)
563 {
564     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
565     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
566     // TreeSet.prototype
567     JSHandle<JSObject> setFuncPrototype = factory->NewEmptyJSObject();
568     JSHandle<JSTaggedValue> setFuncPrototypeValue(setFuncPrototype);
569     // TreeSet.prototype_or_hclass
570     JSHandle<JSHClass> setInstanceClass =
571         factory->NewEcmaHClass(JSAPITreeSet::SIZE, JSType::JS_API_TREE_SET, setFuncPrototypeValue);
572     // TreeSet() = new Function()
573     JSHandle<JSTaggedValue> setFunction(NewContainerConstructor(
574         thread, setFuncPrototype, ContainersTreeSet::TreeSetConstructor, "TreeSet", FuncLength::ZERO));
575     JSHandle<JSFunction>::Cast(setFunction)->SetFunctionPrototype(thread, setInstanceClass.GetTaggedValue());
576 
577     // "constructor" property on the prototype
578     JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
579     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(setFuncPrototype), constructorKey, setFunction);
580 
581     // TreeSet.prototype
582     SetFrozenFunction(thread, setFuncPrototype, "add", ContainersTreeSet::Add, FuncLength::TWO);
583     SetFrozenFunction(thread, setFuncPrototype, "remove", ContainersTreeSet::Remove, FuncLength::ONE);
584     SetFrozenFunction(thread, setFuncPrototype, "has", ContainersTreeSet::Has, FuncLength::ONE);
585     SetFrozenFunction(thread, setFuncPrototype, "getFirstValue", ContainersTreeSet::GetFirstValue, FuncLength::ZERO);
586     SetFrozenFunction(thread, setFuncPrototype, "getLastValue", ContainersTreeSet::GetLastValue, FuncLength::ZERO);
587     SetFrozenFunction(thread, setFuncPrototype, "clear", ContainersTreeSet::Clear, FuncLength::ZERO);
588     SetFrozenFunction(thread, setFuncPrototype, "getLowerValue", ContainersTreeSet::GetLowerValue, FuncLength::ONE);
589     SetFrozenFunction(thread, setFuncPrototype, "getHigherValue", ContainersTreeSet::GetHigherValue, FuncLength::ONE);
590     SetFrozenFunction(thread, setFuncPrototype, "popFirst", ContainersTreeSet::PopFirst, FuncLength::ZERO);
591     SetFrozenFunction(thread, setFuncPrototype, "popLast", ContainersTreeSet::PopLast, FuncLength::ZERO);
592     SetFrozenFunction(thread, setFuncPrototype, "isEmpty", ContainersTreeSet::IsEmpty, FuncLength::TWO);
593     SetFrozenFunction(thread, setFuncPrototype, "values", ContainersTreeSet::Values, FuncLength::ZERO);
594     SetFrozenFunction(thread, setFuncPrototype, "forEach", ContainersTreeSet::ForEach, FuncLength::ONE);
595     SetFrozenFunction(thread, setFuncPrototype, "entries", ContainersTreeSet::Entries, FuncLength::ZERO);
596 
597     // @@ToStringTag
598     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
599     SetStringTagSymbol(thread, env, setFuncPrototype, "TreeSet");
600     // %TreeSetPrototype% [ @@iterator ]
601     JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
602     JSHandle<JSTaggedValue> values(thread, globalConst->GetValuesString());
603     JSHandle<JSTaggedValue> valuesFunc =
604         JSObject::GetMethod(thread, JSHandle<JSTaggedValue>::Cast(setFuncPrototype), values);
605     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
606     PropertyDescriptor descriptor(thread, valuesFunc, false, false, false);
607     JSObject::DefineOwnProperty(thread, setFuncPrototype, iteratorSymbol, descriptor);
608     // length
609     JSHandle<JSTaggedValue> lengthGetter =
610         CreateGetter(thread, ContainersTreeSet::GetLength, "length", FuncLength::ZERO);
611     JSHandle<JSTaggedValue> lengthKey(thread, globalConst->GetLengthString());
612     SetGetter(thread, setFuncPrototype, lengthKey, lengthGetter);
613 
614     InitializeTreeSetIterator(thread);
615     return setFunction;
616 }
617 
InitializeTreeSetIterator(JSThread * thread)618 void ContainersPrivate::InitializeTreeSetIterator(JSThread *thread)
619 {
620     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
621     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
622     // Iterator.hclass
623     JSHandle<JSHClass> iteratorClass =
624         factory->NewEcmaHClass(JSObject::SIZE, JSType::JS_ITERATOR, env->GetIteratorPrototype());
625 
626     // TreeSetIterator.prototype
627     JSHandle<JSObject> setIteratorPrototype(factory->NewJSObject(iteratorClass));
628     // TreeSetIterator.prototype.next()
629     SetFrozenFunction(thread, setIteratorPrototype, "next", JSAPITreeSetIterator::Next, FuncLength::ZERO);
630     SetStringTagSymbol(thread, env, setIteratorPrototype, "TreeSet Iterator");
631     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
632     globalConst->SetConstant(ConstantIndex::TREESET_ITERATOR_PROTOTYPE_INDEX, setIteratorPrototype.GetTaggedValue());
633 }
634 
InitializePlainArray(JSThread * thread)635 JSHandle<JSTaggedValue> ContainersPrivate::InitializePlainArray(JSThread *thread)
636 {
637     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
638     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
639     // PlainArray.prototype
640     JSHandle<JSObject> plainArrayFuncPrototype = factory->NewEmptyJSObject();
641     JSHandle<JSTaggedValue> plainArrayFuncPrototypeValue(plainArrayFuncPrototype);
642     // PlainArray.prototype_or_hclass
643     JSHandle<JSHClass> plainArrayInstanceClass =
644         factory->NewEcmaHClass(JSAPIPlainArray::SIZE, JSType::JS_API_PLAIN_ARRAY, plainArrayFuncPrototypeValue);
645     JSHandle<JSTaggedValue> plainArrayFunction(
646         NewContainerConstructor(thread, plainArrayFuncPrototype, ContainersPlainArray::PlainArrayConstructor,
647                                 "PlainArray", FuncLength::ZERO));
648     JSHandle<JSFunction>::Cast(plainArrayFunction)->SetFunctionPrototype(thread,
649                                                                          plainArrayInstanceClass.GetTaggedValue());
650 
651     // "constructor" property on the prototype
652     JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
653     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(plainArrayFuncPrototype), constructorKey,
654                           plainArrayFunction);
655     // PlainArray.prototype.add()
656     SetFrozenFunction(thread, plainArrayFuncPrototype, "add", ContainersPlainArray::Add, FuncLength::ONE);
657     SetFrozenFunction(thread, plainArrayFuncPrototype, "clear", ContainersPlainArray::Clear, FuncLength::ONE);
658     SetFrozenFunction(thread, plainArrayFuncPrototype, "clone", ContainersPlainArray::Clone, FuncLength::ONE);
659     SetFrozenFunction(thread, plainArrayFuncPrototype, "has", ContainersPlainArray::Has, FuncLength::ONE);
660     SetFrozenFunction(thread, plainArrayFuncPrototype, "get", ContainersPlainArray::Get, FuncLength::ONE);
661     SetFrozenFunction(thread, plainArrayFuncPrototype, "forEach", ContainersPlainArray::ForEach, FuncLength::ONE,
662                       BUILTINS_STUB_ID(PlainArrayForEach));
663     SetFrozenFunction(thread, plainArrayFuncPrototype, "toString", ContainersPlainArray::ToString,
664                       FuncLength::ZERO);
665     SetFrozenFunction(thread, plainArrayFuncPrototype, "getIndexOfKey", ContainersPlainArray::GetIndexOfKey,
666                       FuncLength::ZERO);
667     SetFrozenFunction(thread, plainArrayFuncPrototype, "getIndexOfValue", ContainersPlainArray::GetIndexOfValue,
668                       FuncLength::ZERO);
669     SetFrozenFunction(thread, plainArrayFuncPrototype, "isEmpty", ContainersPlainArray::IsEmpty, FuncLength::ZERO);
670     SetFrozenFunction(thread, plainArrayFuncPrototype, "getKeyAt",
671                       ContainersPlainArray::GetKeyAt, FuncLength::ZERO);
672     SetFrozenFunction(thread, plainArrayFuncPrototype, "remove", ContainersPlainArray::Remove, FuncLength::ZERO);
673     SetFrozenFunction(thread, plainArrayFuncPrototype, "removeAt", ContainersPlainArray::RemoveAt,
674                       FuncLength::ZERO);
675     SetFrozenFunction(thread, plainArrayFuncPrototype, "removeRangeFrom", ContainersPlainArray::RemoveRangeFrom,
676                       FuncLength::ZERO);
677     SetFrozenFunction(thread, plainArrayFuncPrototype, "setValueAt", ContainersPlainArray::SetValueAt,
678                       FuncLength::ZERO);
679     SetFrozenFunction(thread, plainArrayFuncPrototype, "getValueAt", ContainersPlainArray::GetValueAt,
680                       FuncLength::ZERO);
681 
682     JSHandle<JSTaggedValue> lengthGetter = CreateGetter(thread, ContainersPlainArray::GetSize, "length",
683                                                         FuncLength::ZERO);
684     JSHandle<JSTaggedValue> lengthKey = globalConst->GetHandledLengthString();
685     SetGetter(thread, plainArrayFuncPrototype, lengthKey, lengthGetter);
686 
687     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
688     SetFunctionAtSymbol(thread, env, plainArrayFuncPrototype, env->GetIteratorSymbol(), "[Symbol.iterator]",
689                         ContainersPlainArray::GetIteratorObj, FuncLength::ONE);
690     InitializePlainArrayIterator(thread);
691     globalConst->SetConstant(ConstantIndex::PLAIN_ARRAY_FUNCTION_INDEX, plainArrayFunction.GetTaggedValue());
692     return plainArrayFunction;
693 }
694 
InitializePlainArrayIterator(JSThread * thread)695 void ContainersPrivate::InitializePlainArrayIterator(JSThread *thread)
696 {
697     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
698     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
699     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
700     JSHandle<JSHClass> iteratorClass =
701         JSHandle<JSHClass>(thread, globalConst->GetHandledJSAPIIteratorFuncHClass().GetObject<JSHClass>());
702     JSHandle<JSObject> plainarrayIteratorPrototype(factory->NewJSObject(iteratorClass));
703     SetFrozenFunction(thread, plainarrayIteratorPrototype, "next", JSAPIPlainArrayIterator::Next, FuncLength::ONE);
704     SetStringTagSymbol(thread, env, plainarrayIteratorPrototype, "PlainArray Iterator");
705     globalConst->SetConstant(ConstantIndex::PLAIN_ARRAY_ITERATOR_PROTOTYPE_INDEX,
706                              plainarrayIteratorPrototype.GetTaggedValue());
707 }
708 
InitializeStack(JSThread * thread)709 JSHandle<JSTaggedValue> ContainersPrivate::InitializeStack(JSThread *thread)
710 {
711     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
712     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
713     // Stack.prototype
714     JSHandle<JSObject> stackFuncPrototype = factory->NewEmptyJSObject();
715     JSHandle<JSTaggedValue> stackFuncPrototypeValue(stackFuncPrototype);
716     // Stack.prototype_or_hclass
717     JSHandle<JSHClass> stackInstanceClass =
718         factory->NewEcmaHClass(JSAPIStack::SIZE, JSType::JS_API_STACK, stackFuncPrototypeValue);
719     // Stack() = new Function()
720     JSHandle<JSTaggedValue> stackFunction(NewContainerConstructor(
721         thread, stackFuncPrototype, ContainersStack::StackConstructor, "Stack", FuncLength::ZERO));
722     JSHandle<JSFunction>::Cast(stackFunction)->SetFunctionPrototype(thread, stackInstanceClass.GetTaggedValue());
723 
724     // "constructor" property on the prototype
725     JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
726     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(stackFuncPrototype), constructorKey, stackFunction);
727 
728     // Stack.prototype.push()
729     SetFrozenFunction(thread, stackFuncPrototype, "push", ContainersStack::Push, FuncLength::ONE);
730     // Stack.prototype.empty()
731     SetFrozenFunction(thread, stackFuncPrototype, "isEmpty", ContainersStack::IsEmpty, FuncLength::ONE);
732     // Stack.prototype.peek()
733     SetFrozenFunction(thread, stackFuncPrototype, "peek", ContainersStack::Peek, FuncLength::ONE);
734     // Stack.prototype.pop()
735     SetFrozenFunction(thread, stackFuncPrototype, "pop", ContainersStack::Pop, FuncLength::ONE);
736     // Stack.prototype.search()
737     SetFrozenFunction(thread, stackFuncPrototype, "locate", ContainersStack::Locate, FuncLength::ONE);
738     // Stack.prototype.forEach()
739     SetFrozenFunction(thread, stackFuncPrototype, "forEach", ContainersStack::ForEach, FuncLength::ONE,
740                       BUILTINS_STUB_ID(StackForEach));
741     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
742     SetStringTagSymbol(thread, env, stackFuncPrototype, "Stack");
743 
744     JSHandle<JSTaggedValue> lengthGetter = CreateGetter(thread, ContainersStack::GetLength, "length", FuncLength::ZERO);
745     JSHandle<JSTaggedValue> lengthKey = globalConst->GetHandledLengthString();
746     SetGetter(thread, stackFuncPrototype, lengthKey, lengthGetter);
747 
748     SetFunctionAtSymbol(thread, env, stackFuncPrototype, env->GetIteratorSymbol(), "[Symbol.iterator]",
749                         ContainersStack::Iterator, FuncLength::ONE);
750 
751     ContainersPrivate::InitializeStackIterator(thread, globalConst);
752     return stackFunction;
753 }
754 
InitializeStackIterator(JSThread * thread,GlobalEnvConstants * globalConst)755 void ContainersPrivate::InitializeStackIterator(JSThread *thread, GlobalEnvConstants *globalConst)
756 {
757     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
758     JSHandle<JSHClass> iteratorFuncHClass = JSHandle<JSHClass>(thread, globalConst->
759                         GetHandledJSAPIIteratorFuncHClass().GetObject<JSHClass>());
760     // StackIterator.prototype
761     JSHandle<JSObject> stackIteratorPrototype(factory->NewJSObject(iteratorFuncHClass));
762     // Iterator.prototype.next()
763     SetFrozenFunction(thread, stackIteratorPrototype, "next", JSAPIStackIterator::Next, FuncLength::ONE);
764     globalConst->SetConstant(ConstantIndex::STACK_ITERATOR_PROTOTYPE_INDEX, stackIteratorPrototype.GetTaggedValue());
765 }
766 
InitializeVector(JSThread * thread)767 JSHandle<JSTaggedValue> ContainersPrivate::InitializeVector(JSThread *thread)
768 {
769     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
770     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
771     // Vector.prototype
772     JSHandle<JSObject> prototype = factory->NewEmptyJSObject();
773     JSHandle<JSTaggedValue> vectorFuncPrototypeValue(prototype);
774     // Vector.prototype_or_hclass
775     JSHandle<JSHClass> vectorInstanceClass =
776         factory->NewEcmaHClass(JSAPIVector::SIZE, JSType::JS_API_VECTOR, vectorFuncPrototypeValue);
777     // Vector() = new Function()
778     JSHandle<JSTaggedValue> vectorFunction(NewContainerConstructor(
779         thread, prototype, ContainersVector::VectorConstructor, "Vector", FuncLength::ZERO));
780     JSHandle<JSFunction>::Cast(vectorFunction)->SetFunctionPrototype(thread, vectorInstanceClass.GetTaggedValue());
781 
782     // "constructor" property on the prototype
783     JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
784     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(prototype), constructorKey, vectorFunction);
785 
786     // Vector.prototype
787     SetFrozenFunction(thread, prototype, "add", ContainersVector::Add, FuncLength::ONE);
788     SetFrozenFunction(thread, prototype, "insert", ContainersVector::Insert, FuncLength::TWO);
789     SetFrozenFunction(thread, prototype, "setLength", ContainersVector::SetLength, FuncLength::ONE);
790     SetFrozenFunction(thread, prototype, "getCapacity", ContainersVector::GetCapacity, FuncLength::ZERO);
791     SetFrozenFunction(thread, prototype, "increaseCapacityTo", ContainersVector::IncreaseCapacityTo, FuncLength::ONE);
792     SetFrozenFunction(thread, prototype, "get", ContainersVector::Get, FuncLength::ONE);
793     SetFrozenFunction(thread, prototype, "getIndexOf", ContainersVector::GetIndexOf, FuncLength::ONE);
794     SetFrozenFunction(thread, prototype, "getIndexFrom", ContainersVector::GetIndexFrom, FuncLength::TWO);
795     SetFrozenFunction(thread, prototype, "isEmpty", ContainersVector::IsEmpty, FuncLength::ZERO);
796     SetFrozenFunction(thread, prototype, "getLastElement", ContainersVector::GetLastElement, FuncLength::ZERO);
797     SetFrozenFunction(thread, prototype, "getLastIndexOf", ContainersVector::GetLastIndexOf, FuncLength::ONE);
798     SetFrozenFunction(thread, prototype, "getLastIndexFrom", ContainersVector::GetLastIndexFrom, FuncLength::TWO);
799     SetFrozenFunction(thread, prototype, "remove", ContainersVector::Remove, FuncLength::ONE);
800     SetFrozenFunction(thread, prototype, "removeByIndex", ContainersVector::RemoveByIndex, FuncLength::ONE);
801     SetFrozenFunction(thread, prototype, "removeByRange", ContainersVector::RemoveByRange, FuncLength::TWO);
802     SetFrozenFunction(thread, prototype, "set", ContainersVector::Set, FuncLength::TWO);
803     SetFrozenFunction(thread, prototype, "subVector", ContainersVector::SubVector, FuncLength::TWO);
804     SetFrozenFunction(thread, prototype, "toString", ContainersVector::ToString, FuncLength::ZERO);
805     SetFrozenFunction(thread, prototype, "forEach", ContainersVector::ForEach, FuncLength::TWO,
806                       BUILTINS_STUB_ID(VectorForEach));
807     SetFrozenFunction(thread, prototype, "replaceAllElements", ContainersVector::ReplaceAllElements, FuncLength::TWO,
808                       BUILTINS_STUB_ID(VectorReplaceAllElements));
809     SetFrozenFunction(thread, prototype, "has", ContainersVector::Has, FuncLength::ONE);
810     SetFrozenFunction(thread, prototype, "sort", ContainersVector::Sort, FuncLength::ZERO);
811     SetFrozenFunction(thread, prototype, "clear", ContainersVector::Clear, FuncLength::ZERO);
812     SetFrozenFunction(thread, prototype, "clone", ContainersVector::Clone, FuncLength::ZERO);
813     SetFrozenFunction(thread, prototype, "copyToArray", ContainersVector::CopyToArray, FuncLength::ONE);
814     SetFrozenFunction(thread, prototype, "convertToArray", ContainersVector::ConvertToArray, FuncLength::ZERO);
815     SetFrozenFunction(thread, prototype, "getFirstElement", ContainersVector::GetFirstElement, FuncLength::ZERO);
816     SetFrozenFunction(thread, prototype, "trimToCurrentLength",
817                       ContainersVector::TrimToCurrentLength, FuncLength::ZERO);
818 
819     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
820     SetStringTagSymbol(thread, env, prototype, "Vector");
821 
822     JSHandle<JSTaggedValue> lengthGetter = CreateGetter(thread, ContainersVector::GetSize, "length", FuncLength::ZERO);
823     JSHandle<JSTaggedValue> lengthKey(thread, globalConst->GetLengthString());
824     SetGetter(thread, prototype, lengthKey, lengthGetter);
825 
826     SetFunctionAtSymbol(thread, env, prototype, env->GetIteratorSymbol(), "[Symbol.iterator]",
827                         ContainersVector::GetIteratorObj, FuncLength::ONE);
828 
829     ContainersPrivate::InitializeVectorIterator(thread, env, globalConst);
830     globalConst->SetConstant(ConstantIndex::VECTOR_FUNCTION_INDEX, vectorFunction.GetTaggedValue());
831     return vectorFunction;
832 }
833 
InitializeVectorIterator(JSThread * thread,const JSHandle<GlobalEnv> & env,GlobalEnvConstants * globalConst)834 void ContainersPrivate::InitializeVectorIterator(JSThread *thread, const JSHandle<GlobalEnv> &env,
835                                                  GlobalEnvConstants *globalConst)
836 {
837     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
838     JSHandle<JSHClass> iteratorFuncHClass = JSHandle<JSHClass>(thread, globalConst->
839                         GetHandledJSAPIIteratorFuncHClass().GetObject<JSHClass>());
840     // VectorIterator.prototype
841     JSHandle<JSObject> vectorIteratorPrototype(factory->NewJSObject(iteratorFuncHClass));
842     // Iterator.prototype.next()
843     SetFrozenFunction(thread, vectorIteratorPrototype, "next", JSAPIVectorIterator::Next, FuncLength::ONE);
844     SetStringTagSymbol(thread, env, vectorIteratorPrototype, "Vector Iterator");
845     globalConst->SetConstant(ConstantIndex::VECTOR_ITERATOR_PROTOTYPE_INDEX, vectorIteratorPrototype.GetTaggedValue());
846 }
847 
InitializeQueue(JSThread * thread)848 JSHandle<JSTaggedValue> ContainersPrivate::InitializeQueue(JSThread *thread)
849 {
850     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
851     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
852     // Queue.prototype
853     JSHandle<JSObject> queueFuncPrototype = factory->NewEmptyJSObject();
854     JSHandle<JSTaggedValue> queueFuncPrototypeValue(queueFuncPrototype);
855     // Queue.prototype_or_hclass
856     JSHandle<JSHClass> queueInstanceClass =
857         factory->NewEcmaHClass(JSAPIQueue::SIZE, JSType::JS_API_QUEUE, queueFuncPrototypeValue);
858     // Queue() = new Function()
859     JSHandle<JSTaggedValue> queueFunction(NewContainerConstructor(
860         thread, queueFuncPrototype, ContainersQueue::QueueConstructor, "Queue", FuncLength::ZERO));
861     JSHandle<JSFunction>::Cast(queueFunction)->SetFunctionPrototype(thread, queueInstanceClass.GetTaggedValue());
862 
863     // "constructor" property on the prototype
864     JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
865     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(queueFuncPrototype), constructorKey, queueFunction);
866 
867     // Queue.prototype.add()
868     SetFrozenFunction(thread, queueFuncPrototype, "add", ContainersQueue::Add, FuncLength::ONE);
869     SetFrozenFunction(thread, queueFuncPrototype, "getFirst", ContainersQueue::GetFirst, FuncLength::ZERO);
870     SetFrozenFunction(thread, queueFuncPrototype, "pop", ContainersQueue::Pop, FuncLength::ZERO);
871     SetFrozenFunction(thread, queueFuncPrototype, "forEach", ContainersQueue::ForEach, FuncLength::TWO,
872                       BUILTINS_STUB_ID(QueueForEach));
873 
874     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
875     SetStringTagSymbol(thread, env, queueFuncPrototype, "Queue");
876 
877     JSHandle<JSTaggedValue> lengthGetter = CreateGetter(thread, ContainersQueue::GetSize, "length", FuncLength::ZERO);
878     JSHandle<JSTaggedValue> lengthKey(thread, globalConst->GetLengthString());
879     SetGetter(thread, queueFuncPrototype, lengthKey, lengthGetter);
880 
881     SetFunctionAtSymbol(thread, env, queueFuncPrototype, env->GetIteratorSymbol(), "[Symbol.iterator]",
882                         ContainersQueue::GetIteratorObj, FuncLength::ONE);
883 
884     ContainersPrivate::InitializeQueueIterator(thread, env, globalConst);
885     return queueFunction;
886 }
887 
InitializeQueueIterator(JSThread * thread,const JSHandle<GlobalEnv> & env,GlobalEnvConstants * globalConst)888 void ContainersPrivate::InitializeQueueIterator(JSThread *thread, const JSHandle<GlobalEnv> &env,
889                                                 GlobalEnvConstants *globalConst)
890 {
891     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
892     JSHandle<JSHClass> iteratorFuncHClass =
893         factory->NewEcmaHClass(JSObject::SIZE, JSType::JS_ITERATOR, env->GetIteratorPrototype());
894     // QueueIterator.prototype
895     JSHandle<JSObject> queueIteratorPrototype(factory->NewJSObject(iteratorFuncHClass));
896     // Iterator.prototype.next()
897     SetFrozenFunction(thread, queueIteratorPrototype, "next", JSAPIQueueIterator::Next, FuncLength::ONE);
898     SetStringTagSymbol(thread, env, queueIteratorPrototype, "Queue Iterator");
899     globalConst->SetConstant(ConstantIndex::QUEUE_ITERATOR_PROTOTYPE_INDEX, queueIteratorPrototype.GetTaggedValue());
900 }
901 
InitializeDeque(JSThread * thread)902 JSHandle<JSTaggedValue> ContainersPrivate::InitializeDeque(JSThread *thread)
903 {
904     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
905     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
906     // Deque.prototype
907     JSHandle<JSObject> dequeFuncPrototype = factory->NewEmptyJSObject();
908     JSHandle<JSTaggedValue> dequeFuncPrototypeValue(dequeFuncPrototype);
909     // Deque.prototype_or_hclass
910     JSHandle<JSHClass> dequeInstanceClass =
911         factory->NewEcmaHClass(JSAPIDeque::SIZE, JSType::JS_API_DEQUE, dequeFuncPrototypeValue);
912     // Deque() = new Function()
913     JSHandle<JSTaggedValue> dequeFunction(NewContainerConstructor(
914         thread, dequeFuncPrototype, ContainersDeque::DequeConstructor, "Deque", FuncLength::ZERO));
915     JSHandle<JSFunction>::Cast(dequeFunction)->SetFunctionPrototype(thread, dequeInstanceClass.GetTaggedValue());
916 
917     // "constructor" property on the prototype
918     JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
919     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(dequeFuncPrototype), constructorKey, dequeFunction);
920 
921     SetFrozenFunction(thread, dequeFuncPrototype, "insertFront", ContainersDeque::InsertFront, FuncLength::ONE);
922     SetFrozenFunction(thread, dequeFuncPrototype, "insertEnd", ContainersDeque::InsertEnd, FuncLength::ONE);
923     SetFrozenFunction(thread, dequeFuncPrototype, "getFirst", ContainersDeque::GetFirst, FuncLength::ZERO);
924     SetFrozenFunction(thread, dequeFuncPrototype, "getLast", ContainersDeque::GetLast, FuncLength::ZERO);
925     SetFrozenFunction(thread, dequeFuncPrototype, "has", ContainersDeque::Has, FuncLength::ONE);
926     SetFrozenFunction(thread, dequeFuncPrototype, "popFirst", ContainersDeque::PopFirst, FuncLength::ZERO);
927     SetFrozenFunction(thread, dequeFuncPrototype, "popLast", ContainersDeque::PopLast, FuncLength::ZERO);
928     SetFrozenFunction(thread, dequeFuncPrototype, "forEach", ContainersDeque::ForEach, FuncLength::TWO,
929                       BUILTINS_STUB_ID(DequeForEach));
930 
931     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
932     SetStringTagSymbol(thread, env, dequeFuncPrototype, "Deque");
933 
934     JSHandle<JSTaggedValue> lengthGetter = CreateGetter(thread, ContainersDeque::GetSize, "length", FuncLength::ZERO);
935     JSHandle<JSTaggedValue> lengthKey = globalConst->GetHandledLengthString();
936     SetGetter(thread, dequeFuncPrototype, lengthKey, lengthGetter);
937 
938     SetFunctionAtSymbol(thread, env, dequeFuncPrototype, env->GetIteratorSymbol(), "[Symbol.iterator]",
939                         ContainersDeque::GetIteratorObj, FuncLength::ONE);
940 
941     ContainersPrivate::InitializeDequeIterator(thread, env, globalConst);
942 
943     return dequeFunction;
944 }
945 
InitializeDequeIterator(JSThread * thread,const JSHandle<GlobalEnv> & env,GlobalEnvConstants * globalConst)946 void ContainersPrivate::InitializeDequeIterator(JSThread *thread, const JSHandle<GlobalEnv> &env,
947                                                 GlobalEnvConstants *globalConst)
948 {
949     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
950     JSHandle<JSHClass> iteratorFuncHClass = JSHandle<JSHClass>(thread, globalConst->
951                         GetHandledJSAPIIteratorFuncHClass().GetObject<JSHClass>());
952     JSHandle<JSObject> dequeIteratorPrototype(factory->NewJSObject(iteratorFuncHClass));
953     SetFrozenFunction(thread, dequeIteratorPrototype, "next", JSAPIDequeIterator::Next, FuncLength::ONE);
954     SetStringTagSymbol(thread, env, dequeIteratorPrototype, "Deque Iterator");
955     globalConst->SetConstant(ConstantIndex::DEQUE_ITERATOR_PROTOTYPE_INDEX, dequeIteratorPrototype.GetTaggedValue());
956 }
957 
InitializeList(JSThread * thread)958 JSHandle<JSTaggedValue> ContainersPrivate::InitializeList(JSThread *thread)
959 {
960     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
961     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
962     JSHandle<JSObject> listFuncPrototype = factory->NewEmptyJSObject();
963     JSHandle<JSTaggedValue> listFuncPrototypeValue(listFuncPrototype);
964     JSHandle<JSHClass> listInstanceClass =
965         factory->NewEcmaHClass(JSAPIList::SIZE, JSType::JS_API_LIST, listFuncPrototypeValue);
966     JSHandle<JSTaggedValue> listFunction(NewContainerConstructor(
967         thread, listFuncPrototype, ContainersList::ListConstructor, "List", FuncLength::ZERO));
968     JSHandle<JSFunction>::Cast(listFunction)->SetFunctionPrototype(thread, listInstanceClass.GetTaggedValue());
969 
970     JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
971     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(listFuncPrototype), constructorKey, listFunction);
972 
973     SetFrozenFunction(thread, listFuncPrototype, "add", ContainersList::Add, FuncLength::ONE);
974     SetFrozenFunction(thread, listFuncPrototype, "getFirst", ContainersList::GetFirst, FuncLength::ONE);
975     SetFrozenFunction(thread, listFuncPrototype, "getLast", ContainersList::GetLast, FuncLength::ONE);
976     SetFrozenFunction(thread, listFuncPrototype, "insert", ContainersList::Insert, FuncLength::ONE);
977     SetFrozenFunction(thread, listFuncPrototype, "clear", ContainersList::Clear, FuncLength::ONE);
978     SetFrozenFunction(thread, listFuncPrototype, "removeByIndex", ContainersList::RemoveByIndex, FuncLength::ONE);
979     SetFrozenFunction(thread, listFuncPrototype, "remove", ContainersList::Remove, FuncLength::ONE);
980     SetFrozenFunction(thread, listFuncPrototype, "has", ContainersList::Has, FuncLength::ONE);
981     SetFrozenFunction(thread, listFuncPrototype, "isEmpty", ContainersList::IsEmpty, FuncLength::ONE);
982     SetFrozenFunction(thread, listFuncPrototype, "get", ContainersList::Get, FuncLength::ONE);
983     SetFrozenFunction(thread, listFuncPrototype, "getIndexOf", ContainersList::GetIndexOf, FuncLength::ONE);
984     SetFrozenFunction(thread, listFuncPrototype, "getLastIndexOf", ContainersList::GetLastIndexOf, FuncLength::ONE);
985     SetFrozenFunction(thread, listFuncPrototype, "set", ContainersList::Set, FuncLength::ONE);
986     SetFrozenFunction(thread, listFuncPrototype, "forEach", ContainersList::ForEach, FuncLength::ONE,
987                       BUILTINS_STUB_ID(ListForEach));
988     SetFrozenFunction(thread, listFuncPrototype, "replaceAllElements", ContainersList::ReplaceAllElements,
989                       FuncLength::ONE);
990     SetFrozenFunction(thread, listFuncPrototype, "equal", ContainersList::Equal, FuncLength::ONE);
991     SetFrozenFunction(thread, listFuncPrototype, "sort", ContainersList::Sort, FuncLength::ONE);
992     SetFrozenFunction(thread, listFuncPrototype, "convertToArray", ContainersList::ConvertToArray, FuncLength::ONE);
993     SetFrozenFunction(thread, listFuncPrototype, "getSubList", ContainersList::GetSubList, FuncLength::ONE);
994 
995     JSHandle<JSTaggedValue> lengthGetter = CreateGetter(thread, ContainersList::Length, "length", FuncLength::ZERO);
996     JSHandle<JSTaggedValue> lengthKey(factory->NewFromASCII("length"));
997     SetGetter(thread, listFuncPrototype, lengthKey, lengthGetter);
998 
999     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1000     SetFunctionAtSymbol(thread, env, listFuncPrototype, env->GetIteratorSymbol(), "[Symbol.iterator]",
1001                         ContainersList::GetIteratorObj, FuncLength::ONE);
1002 
1003     InitializeListIterator(thread, env);
1004     globalConst->SetConstant(ConstantIndex::LIST_FUNCTION_INDEX, listFunction.GetTaggedValue());
1005     return listFunction;
1006 }
1007 
InitializeLinkedList(JSThread * thread)1008 JSHandle<JSTaggedValue> ContainersPrivate::InitializeLinkedList(JSThread *thread)
1009 {
1010     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
1011     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1012     JSHandle<JSObject> linkedListFuncPrototype = factory->NewEmptyJSObject();
1013     JSHandle<JSTaggedValue> linkedListFuncPrototypeValue(linkedListFuncPrototype);
1014     JSHandle<JSHClass> linkedListInstanceClass =
1015         factory->NewEcmaHClass(JSAPILinkedList::SIZE, JSType::JS_API_LINKED_LIST, linkedListFuncPrototypeValue);
1016     JSHandle<JSTaggedValue> linkedListFunction(NewContainerConstructor(
1017         thread, linkedListFuncPrototype, ContainersLinkedList::LinkedListConstructor, "LinkedList", FuncLength::ZERO));
1018     JSHandle<JSFunction>::Cast(linkedListFunction)->SetFunctionPrototype(thread,
1019                                                                          linkedListInstanceClass.GetTaggedValue());
1020 
1021     JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
1022     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(linkedListFuncPrototype), constructorKey, linkedListFunction);
1023 
1024     SetFrozenFunction(thread, linkedListFuncPrototype, "add", ContainersLinkedList::Add, FuncLength::ONE);
1025     SetFrozenFunction(thread, linkedListFuncPrototype, "insert", ContainersLinkedList::Insert, FuncLength::ONE);
1026     SetFrozenFunction(thread, linkedListFuncPrototype, "clear", ContainersLinkedList::Clear, FuncLength::ONE);
1027     SetFrozenFunction(thread, linkedListFuncPrototype, "clone", ContainersLinkedList::Clone, FuncLength::ONE);
1028     SetFrozenFunction(thread, linkedListFuncPrototype, "removeFirst", ContainersLinkedList::RemoveFirst,
1029                       FuncLength::ONE);
1030     SetFrozenFunction(thread, linkedListFuncPrototype, "removeLast", ContainersLinkedList::RemoveLast, FuncLength::ONE);
1031     SetFrozenFunction(thread, linkedListFuncPrototype, "removeFirstFound", ContainersLinkedList::RemoveFirstFound,
1032                       FuncLength::ONE);
1033     SetFrozenFunction(thread, linkedListFuncPrototype, "removeByIndex", ContainersLinkedList::RemoveByIndex,
1034                       FuncLength::ONE);
1035     SetFrozenFunction(thread, linkedListFuncPrototype, "remove", ContainersLinkedList::Remove, FuncLength::ONE);
1036     SetFrozenFunction(thread, linkedListFuncPrototype, "removeLastFound", ContainersLinkedList::RemoveLastFound,
1037                       FuncLength::ONE);
1038     SetFrozenFunction(thread, linkedListFuncPrototype, "has", ContainersLinkedList::Has, FuncLength::ONE);
1039     SetFrozenFunction(thread, linkedListFuncPrototype, "get", ContainersLinkedList::Get, FuncLength::ONE);
1040     SetFrozenFunction(thread, linkedListFuncPrototype, "addFirst", ContainersLinkedList::AddFirst, FuncLength::ONE);
1041     SetFrozenFunction(thread, linkedListFuncPrototype, "getFirst", ContainersLinkedList::GetFirst, FuncLength::ONE);
1042     SetFrozenFunction(thread, linkedListFuncPrototype, "getLast", ContainersLinkedList::GetLast, FuncLength::ONE);
1043     SetFrozenFunction(thread, linkedListFuncPrototype, "getIndexOf", ContainersLinkedList::GetIndexOf, FuncLength::ONE);
1044     SetFrozenFunction(thread, linkedListFuncPrototype, "getLastIndexOf", ContainersLinkedList::GetLastIndexOf,
1045                       FuncLength::ONE);
1046     SetFrozenFunction(thread, linkedListFuncPrototype, "convertToArray", ContainersLinkedList::ConvertToArray,
1047                       FuncLength::ONE);
1048     SetFrozenFunction(thread, linkedListFuncPrototype, "set", ContainersLinkedList::Set, FuncLength::ONE);
1049     SetFrozenFunction(thread, linkedListFuncPrototype, "forEach", ContainersLinkedList::ForEach, FuncLength::ONE,
1050                       BUILTINS_STUB_ID(LinkedListForEach));
1051 
1052     JSHandle<JSTaggedValue> lengthGetter = CreateGetter(thread, ContainersLinkedList::Length, "length",
1053                                                         FuncLength::ZERO);
1054     JSHandle<JSTaggedValue> lengthKey(factory->NewFromASCII("length"));
1055     SetGetter(thread, linkedListFuncPrototype, lengthKey, lengthGetter);
1056 
1057     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1058     SetFunctionAtSymbol(thread, env, linkedListFuncPrototype, env->GetIteratorSymbol(), "[Symbol.iterator]",
1059                         ContainersLinkedList::GetIteratorObj, FuncLength::ONE);
1060 
1061     InitializeLinkedListIterator(thread, env);
1062     globalConst->SetConstant(ConstantIndex::LINKED_LIST_FUNCTION_INDEX, linkedListFunction.GetTaggedValue());
1063     return linkedListFunction;
1064 }
1065 
InitializeLinkedListIterator(JSThread * thread,const JSHandle<GlobalEnv> & env)1066 void ContainersPrivate::InitializeLinkedListIterator(JSThread *thread, const JSHandle<GlobalEnv> &env)
1067 {
1068     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
1069     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1070     JSHandle<JSHClass> iteratorClass =
1071         JSHandle<JSHClass>(thread, globalConst->GetHandledJSAPIIteratorFuncHClass().GetObject<JSHClass>());
1072     JSHandle<JSObject> setIteratorPrototype(factory->NewJSObject(iteratorClass));
1073     SetFrozenFunction(thread, setIteratorPrototype, "next", JSAPILinkedListIterator::Next, FuncLength::ONE);
1074     SetStringTagSymbol(thread, env, setIteratorPrototype, "linkedlist Iterator");
1075     globalConst->SetConstant(ConstantIndex::LINKED_LIST_ITERATOR_PROTOTYPE_INDEX,
1076         setIteratorPrototype.GetTaggedValue());
1077 }
1078 
InitializeListIterator(JSThread * thread,const JSHandle<GlobalEnv> & env)1079 void ContainersPrivate::InitializeListIterator(JSThread *thread, const JSHandle<GlobalEnv> &env)
1080 {
1081     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
1082     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1083     JSHandle<JSHClass> iteratorClass =
1084         JSHandle<JSHClass>(thread, globalConst->GetHandledJSAPIIteratorFuncHClass().GetObject<JSHClass>());
1085     JSHandle<JSObject> setIteratorPrototype(factory->NewJSObject(iteratorClass));
1086     SetFrozenFunction(thread, setIteratorPrototype, "next", JSAPIListIterator::Next, FuncLength::ONE);
1087     SetStringTagSymbol(thread, env, setIteratorPrototype, "list Iterator");
1088     globalConst->SetConstant(ConstantIndex::LIST_ITERATOR_PROTOTYPE_INDEX, setIteratorPrototype.GetTaggedValue());
1089 }
1090 
InitializeHashMap(JSThread * thread)1091 JSHandle<JSTaggedValue> ContainersPrivate::InitializeHashMap(JSThread *thread)
1092 {
1093     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
1094     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1095     // HashMap.prototype
1096     JSHandle<JSObject> hashMapFuncPrototype = factory->NewEmptyJSObject();
1097     JSHandle<JSTaggedValue> hashMapFuncPrototypeValue(hashMapFuncPrototype);
1098     // HashMap.prototype_or_hclass
1099     JSHandle<JSHClass> hashMapInstanceClass =
1100         factory->NewEcmaHClass(JSAPIHashMap::SIZE, JSType::JS_API_HASH_MAP, hashMapFuncPrototypeValue);
1101 
1102     JSHandle<JSTaggedValue> hashMapFunction(NewContainerConstructor(
1103         thread, hashMapFuncPrototype, ContainersHashMap::HashMapConstructor, "HashMap", FuncLength::ZERO));
1104     JSHandle<JSFunction>::Cast(hashMapFunction)->SetFunctionPrototype(thread, hashMapInstanceClass.GetTaggedValue());
1105 
1106     // "constructor" property on the prototype
1107     JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
1108     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(hashMapFuncPrototype), constructorKey, hashMapFunction);
1109     // HashMap.prototype.set()
1110     SetFrozenFunction(thread, hashMapFuncPrototype, "set", ContainersHashMap::Set, FuncLength::TWO);
1111      // HashMap.prototype.setall()
1112     SetFrozenFunction(thread, hashMapFuncPrototype, "setAll", ContainersHashMap::SetAll, FuncLength::ONE);
1113     // HashMap.prototype.isEmpty()
1114     SetFrozenFunction(thread, hashMapFuncPrototype, "isEmpty", ContainersHashMap::IsEmpty, FuncLength::ZERO);
1115     // HashMap.prototype.remove()
1116     SetFrozenFunction(thread, hashMapFuncPrototype, "remove", ContainersHashMap::Remove, FuncLength::ONE);
1117      // HashMap.prototype.clear()
1118     SetFrozenFunction(thread, hashMapFuncPrototype, "clear", ContainersHashMap::Clear, FuncLength::ZERO);
1119     // HashMap.prototype.get()
1120     SetFrozenFunction(thread, hashMapFuncPrototype, "get", ContainersHashMap::Get, FuncLength::ONE);
1121     // HashMap.prototype.forEach()
1122     SetFrozenFunction(thread, hashMapFuncPrototype, "forEach", ContainersHashMap::ForEach, FuncLength::TWO,
1123                       BUILTINS_STUB_ID(HashMapForEach));
1124     // HashMap.prototype.hasKey()
1125     SetFrozenFunction(thread, hashMapFuncPrototype, "hasKey", ContainersHashMap::HasKey, FuncLength::ONE);
1126      // HashMap.prototype.hasValue()
1127     SetFrozenFunction(thread, hashMapFuncPrototype, "hasValue", ContainersHashMap::HasValue, FuncLength::ONE);
1128      // HashMap.prototype.replace()
1129     SetFrozenFunction(thread, hashMapFuncPrototype, "replace", ContainersHashMap::Replace, FuncLength::TWO);
1130     // HashMap.prototype.keys()
1131     SetFrozenFunction(thread, hashMapFuncPrototype, "keys", ContainersHashMap::Keys, FuncLength::ZERO);
1132     // HashMap.prototype.Values()
1133     SetFrozenFunction(thread, hashMapFuncPrototype, "values", ContainersHashMap::Values, FuncLength::ZERO);
1134     // HashMap.prototype.keys()
1135     SetFrozenFunction(thread, hashMapFuncPrototype, "entries", ContainersHashMap::Entries, FuncLength::ZERO);
1136     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1137     // @@ToStringTag
1138     SetStringTagSymbol(thread, env, hashMapFuncPrototype, "HashMap");
1139     // %HashMapPrototype% [ @@iterator ]
1140     JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
1141     JSHandle<JSTaggedValue> entries(factory->NewFromASCII("entries"));
1142     JSHandle<JSTaggedValue> entriesFunc =
1143         JSObject::GetMethod(thread, JSHandle<JSTaggedValue>::Cast(hashMapFuncPrototype), entries);
1144     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
1145     PropertyDescriptor descriptor(thread, entriesFunc, false, false, false);
1146     JSObject::DefineOwnProperty(thread, hashMapFuncPrototype, iteratorSymbol, descriptor);
1147 
1148     JSHandle<JSTaggedValue> lengthGetter =
1149         CreateGetter(thread, ContainersHashMap::GetLength, "length", FuncLength::ZERO);
1150     JSHandle<JSTaggedValue> lengthKey(thread, globalConst->GetLengthString());
1151     SetGetter(thread, hashMapFuncPrototype, lengthKey, lengthGetter);
1152     InitializeHashMapIterator(thread);
1153     return hashMapFunction;
1154 }
1155 
InitializeHashMapIterator(JSThread * thread)1156 void ContainersPrivate::InitializeHashMapIterator(JSThread *thread)
1157 {
1158     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1159     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
1160     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1161     JSHandle<JSHClass> iteratorFuncHClass =
1162         JSHandle<JSHClass>::Cast(globalConst->GetHandledJSAPIIteratorFuncHClass());
1163     // HashMapIterator.prototype
1164     JSHandle<JSObject> hashMapIteratorPrototype(factory->NewJSObject(iteratorFuncHClass));
1165     // HashMapIterator.prototype.next()
1166     SetFrozenFunction(thread, hashMapIteratorPrototype, "next", JSAPIHashMapIterator::Next, FuncLength::ZERO);
1167     SetStringTagSymbol(thread, env, hashMapIteratorPrototype, "HashMap Iterator");
1168 
1169     globalConst->SetConstant(ConstantIndex::HASHMAP_ITERATOR_PROTOTYPE_INDEX,
1170                              hashMapIteratorPrototype.GetTaggedValue());
1171 }
1172 
InitializeHashSet(JSThread * thread)1173 JSHandle<JSTaggedValue> ContainersPrivate::InitializeHashSet(JSThread *thread)
1174 {
1175     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
1176     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1177     // HashSet.prototype
1178     JSHandle<JSObject> hashSetFuncPrototype = factory->NewEmptyJSObject();
1179     JSHandle<JSTaggedValue> hashSetFuncPrototypeValue(hashSetFuncPrototype);
1180     // HashSet.prototype_or_hclass
1181     JSHandle<JSHClass> hashSetInstanceClass =
1182         factory->NewEcmaHClass(JSAPIHashSet::SIZE, JSType::JS_API_HASH_SET, hashSetFuncPrototypeValue);
1183 
1184     JSHandle<JSTaggedValue> hashSetFunction(NewContainerConstructor(
1185         thread, hashSetFuncPrototype, ContainersHashSet::HashSetConstructor, "HashSet", FuncLength::ZERO));
1186     JSHandle<JSFunction>::Cast(hashSetFunction)->SetFunctionPrototype(thread, hashSetInstanceClass.GetTaggedValue());
1187 
1188     // "constructor" property on the prototype
1189     JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
1190     JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(hashSetFuncPrototype), constructorKey, hashSetFunction);
1191 
1192     SetFrozenFunction(thread, hashSetFuncPrototype, "isEmpty", ContainersHashSet::IsEmpty, FuncLength::ZERO);
1193     SetFrozenFunction(thread, hashSetFuncPrototype, "has", ContainersHashSet::Has, FuncLength::ONE);
1194     SetFrozenFunction(thread, hashSetFuncPrototype, "add", ContainersHashSet::Add, FuncLength::ONE);
1195     SetFrozenFunction(thread, hashSetFuncPrototype, "has", ContainersHashSet::Has, FuncLength::ONE);
1196     SetFrozenFunction(thread, hashSetFuncPrototype, "remove", ContainersHashSet::Remove, FuncLength::ONE);
1197     SetFrozenFunction(thread, hashSetFuncPrototype, "clear", ContainersHashSet::Clear, FuncLength::ZERO);
1198     SetFrozenFunction(thread, hashSetFuncPrototype, "values", ContainersHashSet::Values, FuncLength::ZERO);
1199     SetFrozenFunction(thread, hashSetFuncPrototype, "entries", ContainersHashSet::Entries, FuncLength::ZERO);
1200     SetFrozenFunction(thread, hashSetFuncPrototype, "forEach", ContainersHashSet::ForEach, FuncLength::TWO,
1201                       BUILTINS_STUB_ID(HashSetForEach));
1202 
1203     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1204     // @@ToStringTag
1205     SetStringTagSymbol(thread, env, hashSetFuncPrototype, "HashSet");
1206     // %HashSetPrototype% [ @@iterator ]
1207     JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
1208     JSHandle<JSTaggedValue> values(thread, globalConst->GetValuesString());
1209     JSHandle<JSTaggedValue> valuesFunc =
1210         JSObject::GetMethod(thread, JSHandle<JSTaggedValue>::Cast(hashSetFuncPrototype), values);
1211     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
1212     PropertyDescriptor descriptor(thread, valuesFunc, false, false, false);
1213     JSObject::DefineOwnProperty(thread, hashSetFuncPrototype, iteratorSymbol, descriptor);
1214 
1215     JSHandle<JSTaggedValue> lengthGetter =
1216         CreateGetter(thread, ContainersHashSet::GetLength, "length", FuncLength::ZERO);
1217     JSHandle<JSTaggedValue> lengthKey(thread, globalConst->GetLengthString());
1218     SetGetter(thread, hashSetFuncPrototype, lengthKey, lengthGetter);
1219     InitializeHashSetIterator(thread);
1220     return hashSetFunction;
1221 }
1222 
InitializeHashSetIterator(JSThread * thread)1223 void ContainersPrivate::InitializeHashSetIterator(JSThread *thread)
1224 {
1225     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1226     auto globalConst = const_cast<GlobalEnvConstants *>(thread->GlobalConstants());
1227     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1228     JSHandle<JSHClass> iteratorFuncHClass =
1229         JSHandle<JSHClass>::Cast(globalConst->GetHandledJSAPIIteratorFuncHClass());
1230 
1231     // HashSetIterator.prototype
1232     JSHandle<JSObject> hashSetIteratorPrototype(factory->NewJSObject(iteratorFuncHClass));
1233     // HashSetIterator.prototype.next()
1234     SetFrozenFunction(thread, hashSetIteratorPrototype, "next", JSAPIHashSetIterator::Next, FuncLength::ZERO);
1235     SetStringTagSymbol(thread, env, hashSetIteratorPrototype, "HashSet Iterator");
1236     globalConst->SetConstant(ConstantIndex::HASHSET_ITERATOR_PROTOTYPE_INDEX,
1237                              hashSetIteratorPrototype.GetTaggedValue());
1238 }
1239 }  // namespace panda::ecmascript::containers
1240